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 
17 package com.android.providers.contacts.testutil;
18 
19 import android.accounts.Account;
20 import android.content.ContentResolver;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.provider.ContactsContract;
26 import android.test.mock.MockContentResolver;
27 import com.android.providers.contacts.AccountWithDataSet;
28 
29 import java.util.List;
30 
31 /**
32  * Convenience methods for operating on the RawContacts table.
33  */
34 public class RawContactUtil {
35 
36     private static final Uri URI = ContactsContract.RawContacts.CONTENT_URI;
37 
update(ContentResolver resolver, long rawContactId, ContentValues values)38     public static void update(ContentResolver resolver, long rawContactId,
39             ContentValues values) {
40         Uri uri = ContentUris.withAppendedId(URI, rawContactId);
41         resolver.update(uri, values, null, null);
42     }
43 
queryByRawContactId(ContentResolver resolver, long rawContactId, String[] projection)44     public static String[] queryByRawContactId(ContentResolver resolver,
45             long rawContactId, String[] projection) {
46         Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI,
47                 rawContactId);
48         Cursor cursor = resolver.query(uri, projection, null, null, null);
49         return CommonDatabaseUtils.singleRecordToArray(cursor);
50     }
51 
52     /**
53      * Returns a list of raw contact records.
54      *
55      * @return A list of records.  Where each record is represented as an array of strings.
56      */
queryByContactId(ContentResolver resolver, long contactId, String[] projection)57     public static List<String[]> queryByContactId(ContentResolver resolver, long contactId,
58             String[] projection) {
59         Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contactId);
60         Cursor cursor = resolver.query(uri, projection, null, null, null);
61         return CommonDatabaseUtils.multiRecordToArray(cursor);
62     }
63 
delete(ContentResolver resolver, long rawContactId, boolean isSyncAdapter)64     public static void delete(ContentResolver resolver, long rawContactId,
65             boolean isSyncAdapter) {
66         Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId)
67                 .buildUpon()
68                 .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, isSyncAdapter + "")
69                 .build();
70         resolver.delete(uri, null, null);
71     }
72 
queryContactIdByRawContactId(ContentResolver resolver, long rawContactid)73     public static long queryContactIdByRawContactId(ContentResolver resolver, long rawContactid) {
74         String[] projection = new String[]{
75                 ContactsContract.RawContacts.CONTACT_ID
76         };
77         String[] result = RawContactUtil.queryByRawContactId(resolver, rawContactid,
78                 projection);
79         if (result == null) {
80             return CommonDatabaseUtils.NOT_FOUND;
81         }
82         return Long.parseLong(result[0]);
83     }
84 
rawContactExistsById(ContentResolver resolver, long rawContactid)85     public static boolean rawContactExistsById(ContentResolver resolver, long rawContactid) {
86         long contactId = queryContactIdByRawContactId(resolver, rawContactid);
87         return contactId != CommonDatabaseUtils.NOT_FOUND;
88     }
89 
createRawContact(ContentResolver resolver, Account account, String... extras)90     public static long createRawContact(ContentResolver resolver, Account account,
91             String... extras) {
92         ContentValues values = new ContentValues();
93         CommonDatabaseUtils.extrasVarArgsToValues(values, extras);
94         final Uri uri = TestUtil.maybeAddAccountQueryParameters(
95                 ContactsContract.RawContacts.CONTENT_URI, account);
96         Uri contactUri = resolver.insert(uri, values);
97         return ContentUris.parseId(contactUri);
98     }
99 
createRawContactWithAccountDataSet(ContentResolver resolver, AccountWithDataSet accountWithDataSet, String... extras)100     public static long createRawContactWithAccountDataSet(ContentResolver resolver,
101             AccountWithDataSet accountWithDataSet, String... extras) {
102         ContentValues values = new ContentValues();
103         CommonDatabaseUtils.extrasVarArgsToValues(values, extras);
104         final Uri uri = TestUtil.maybeAddAccountWithDataSetQueryParameters(
105                 ContactsContract.RawContacts.CONTENT_URI, accountWithDataSet);
106         Uri contactUri = resolver.insert(uri, values);
107         return ContentUris.parseId(contactUri);
108     }
109 
createRawContactWithName(ContentResolver resolver)110     public static long createRawContactWithName(ContentResolver resolver) {
111         return createRawContactWithName(resolver, null);
112     }
113 
createRawContactWithName(ContentResolver resolver, Account account)114     public static long createRawContactWithName(ContentResolver resolver, Account account) {
115         return createRawContactWithName(resolver, "John", "Doe", account);
116     }
117 
createRawContactWithName(ContentResolver resolver, String firstName, String lastName)118     public static long createRawContactWithName(ContentResolver resolver, String firstName,
119             String lastName) {
120         return createRawContactWithName(resolver, firstName, lastName, null);
121     }
122 
createRawContactWithName(ContentResolver resolver, String firstName, String lastName, Account account)123     public static long createRawContactWithName(ContentResolver resolver, String firstName,
124             String lastName, Account account) {
125         long rawContactId = createRawContact(resolver, account);
126         DataUtil.insertStructuredName(resolver, rawContactId, firstName, lastName);
127         return rawContactId;
128     }
129 
createRawContact(ContentResolver resolver)130     public static long createRawContact(ContentResolver resolver) {
131         return createRawContact(resolver, null);
132     }
133 
createRawContactWithBackupId(ContentResolver resolver, String backupId, Account account)134     public static long createRawContactWithBackupId(ContentResolver resolver, String backupId,
135             Account account) {
136         ContentValues values = new ContentValues();
137         values.put(ContactsContract.RawContacts.BACKUP_ID, backupId);
138         final Uri uri = ContactsContract.RawContacts.CONTENT_URI
139                 .buildUpon()
140                 .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME,
141                         account.name)
142                 .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE,
143                         account.type)
144                 .build();
145         Uri contactUri = resolver.insert(uri, values);
146         return ContentUris.parseId(contactUri);
147     }
148 }
149