1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.media.photopicker.util;
18 
19 import android.database.Cursor;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 
24 /**
25  * Provide the utility methods to handle cursor.
26  */
27 public class CursorUtils {
28 
29     /**
30      * Get the string from the {@code cursor} with the {@code columnName}.
31      *
32      * @param cursor the cursor to be parsed
33      * @param columnName the column name of the value
34      * @return the string value from the {@code cursor}, or {@code null} when {@code cursor} doesn't
35      *         contain {@code columnName}
36      */
37     @Nullable
getCursorString(@onNull Cursor cursor, @NonNull String columnName)38     public static String getCursorString(@NonNull Cursor cursor, @NonNull String columnName) {
39         final int index = cursor.getColumnIndex(columnName);
40         return (index != -1) ? cursor.getString(index) : null;
41     }
42 
43     /**
44      * Get the long value from the {@code cursor} with the {@code columnName}.
45      *
46      * @param cursor the cursor to be parsed
47      * @param columnName the column name of the value
48      * @return the long value from the {@code cursor}, or -1 when {@code cursor} doesn't contain
49      *         {@code columnName}
50      */
getCursorLong(@onNull Cursor cursor, @NonNull String columnName)51     public static long getCursorLong(@NonNull Cursor cursor, @NonNull String columnName) {
52         final int index = cursor.getColumnIndex(columnName);
53         if (index == -1) {
54             return -1;
55         }
56 
57         final String value = cursor.getString(index);
58         if (value == null) {
59             return -1;
60         }
61 
62         try {
63             return Long.parseLong(value);
64         } catch (NumberFormatException e) {
65             return -1;
66         }
67     }
68 
69     /**
70      * Get the int value from the {@code cursor} with the {@code columnName}.
71      *
72      * @param cursor the cursor to be parsed
73      * @param columnName the column name of the value
74      * @return the int value from the {@code cursor}, or 0 when {@code cursor} doesn't contain
75      *         {@code columnName}
76      */
getCursorInt(@onNull Cursor cursor, @NonNull String columnName)77     public static int getCursorInt(@NonNull Cursor cursor, @NonNull String columnName) {
78         final int index = cursor.getColumnIndex(columnName);
79         return (index != -1) ? cursor.getInt(index) : 0;
80     }
81 }
82