1/*
2 * Copyright (C) 2015 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.dialer.database;
18
19import android.database.MatrixCursor;
20import android.provider.ContactsContract.Contacts;
21import android.provider.ContactsContract.Data;
22import android.provider.ContactsContract.CommonDataKinds.Phone;
23import android.text.TextUtils;
24
25import com.android.dialer.database.DialerDatabaseHelper.ContactNumber;
26
27public class DatabaseTestUtils {
28    public static MatrixCursor constructNewNameCursor() {
29        final MatrixCursor cursor = new MatrixCursor(new String[]{
30                DialerDatabaseHelper.SmartDialDbColumns.DISPLAY_NAME_PRIMARY,
31                DialerDatabaseHelper.SmartDialDbColumns.CONTACT_ID});
32        return cursor;
33    }
34
35    public static MatrixCursor constructNewContactCursor() {
36        final MatrixCursor cursor = new MatrixCursor(new String[]{
37                    Phone._ID,                          // 0
38                    Phone.TYPE,                         // 1
39                    Phone.LABEL,                        // 2
40                    Phone.NUMBER,                       // 3
41                    Phone.CONTACT_ID,                   // 4
42                    Phone.LOOKUP_KEY,                   // 5
43                    Phone.DISPLAY_NAME_PRIMARY,         // 6
44                    Phone.PHOTO_ID,                     // 7
45                    Data.LAST_TIME_USED,                // 8
46                    Data.TIMES_USED,                    // 9
47                    Contacts.STARRED,                   // 10
48                    Data.IS_SUPER_PRIMARY,              // 11
49                    Contacts.IN_VISIBLE_GROUP,          // 12
50                    Data.IS_PRIMARY,                    // 13
51                    Data.CARRIER_PRESENCE});            // 14
52        return cursor;
53    }
54
55    public static ContactNumber constructNewContactWithDummyIds(MatrixCursor contactCursor,
56            MatrixCursor nameCursor, String number, int id, String displayName) {
57        return constructNewContact(contactCursor, nameCursor, id, number, id, String.valueOf(id),
58                displayName, 0, 0, 0, 0, 0, 0, 0, 0);
59    }
60
61    public static ContactNumber constructNewContact(MatrixCursor contactCursor,
62            MatrixCursor nameCursor, int id, String number, int contactId, String lookupKey,
63            String displayName, int photoId, int lastTimeUsed, int timesUsed, int starred,
64            int isSuperPrimary, int inVisibleGroup, int isPrimary, int carrierPresence) {
65        if (contactCursor == null || nameCursor == null) {
66            throw new IllegalArgumentException("Provided MatrixCursors cannot be null");
67        }
68
69        if (TextUtils.isEmpty(number)) {
70            // Add a dummy number, otherwise DialerDatabaseHelper simply ignores the entire
71            // row if the number is empty
72            number = "0";
73        }
74
75        contactCursor.addRow(new Object[]{id, "", "", number, contactId, lookupKey, displayName,
76                photoId, lastTimeUsed, timesUsed, starred, isSuperPrimary, inVisibleGroup,
77                isPrimary, carrierPresence});
78        nameCursor.addRow(new Object[]{displayName, contactId});
79
80        return new ContactNumber(contactId, id, displayName, number, lookupKey, 0, 0);
81    }
82}
83