1/*
2 * Copyright (C) 2013 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
17package com.android.providers.contacts.testutil;
18
19import android.content.ContentProviderOperation;
20import android.content.ContentResolver;
21import android.content.ContentValues;
22import android.content.OperationApplicationException;
23import android.database.Cursor;
24import android.os.RemoteException;
25import android.provider.ContactsContract;
26import android.util.Log;
27
28import java.util.ArrayList;
29import java.util.List;
30
31/**
32 * Common database methods.
33 */
34public class CommonDatabaseUtils {
35    public static final String TAG = CommonDatabaseUtils.class.getSimpleName();
36
37    // primitive value used when record is not found.
38    public static final long NOT_FOUND = -1;
39
40    public static String[] singleRecordToArray(Cursor cursor) {
41        String[] result = null;
42        try {
43            if (cursor.moveToNext()) {
44                result = new String[cursor.getColumnCount()];
45                fillArray(cursor, result);
46            }
47        } finally {
48            closeQuietly(cursor);
49        }
50        return result;
51    }
52
53    public static List<String[]> multiRecordToArray(Cursor cursor) {
54        ArrayList<String[]> result = new ArrayList<String[]>();
55        try {
56            while (cursor.moveToNext()) {
57                String[] record = new String[cursor.getColumnCount()];
58                fillArray(cursor, record);
59                result.add(record);
60            }
61        } finally {
62            closeQuietly(cursor);
63        }
64        return result;
65    }
66
67    private static void fillArray(Cursor cursor, String[] array) {
68        for (int i = 0; i < array.length; i++) {
69            array[i] = cursor.getString(i);
70        }
71    }
72
73    public static void closeQuietly(Cursor cursor) {
74        if (cursor != null) {
75            cursor.close();
76        }
77    }
78
79    public static void extrasVarArgsToValues(ContentValues values, String... extras) {
80        for (int i = 0; i < extras.length; ) {
81            values.put(extras[i], extras[i + 1]);
82            i += 2;
83        }
84    }
85
86    public static void applyBatch(ContentResolver resolver,
87            ArrayList<ContentProviderOperation> operations) {
88        try {
89            resolver.applyBatch(ContactsContract.AUTHORITY, operations);
90        } catch (OperationApplicationException e) {
91            Log.wtf(TAG, "ContentResolver batch operation failed.");
92        } catch (RemoteException e) {
93            Log.wtf(TAG, "Remote exception when performing batch operation.");
94        }
95    }
96
97    /**
98     * Returns an array of values extracted from a {@link ContentValues}, based on the order of
99     * the provided projection.
100     * @param values {@link ContentValues} object containing the values to convert into an array
101     * @param projection array of column names
102     *
103     * @return array of values, in the correct order as defined by the projection
104     */
105    public static Object[] getArrayFromContentValues(ContentValues values, String[] projection) {
106        final Object[] result = new Object[projection.length];
107        for (int i = 0; i < projection.length; i++) {
108            result[i] = values.get(projection[i]);
109        }
110        return result;
111    }
112}
113