• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.provider.cts.contacts;
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 
27 import java.util.List;
28 
29 /**
30  * Convenience methods for operating on the RawContacts table.
31  */
32 public class RawContactUtil {
33 
34     private static final Uri URI = ContactsContract.RawContacts.CONTENT_URI;
35 
update(ContentResolver resolver, long rawContactId, ContentValues values)36     public static int update(ContentResolver resolver, long rawContactId,
37             ContentValues values) {
38         Uri uri = ContentUris.withAppendedId(URI, rawContactId);
39         return resolver.update(uri, values, null, null);
40     }
41 
42     /**
43      * Updates the account associated with a raw contact in the Contacts Provider.
44      *
45      * @param resolver The {@link ContentResolver} to use for the update.
46      * @param rawContactId The ID of the raw contact to update.
47      * @param accountUpdateTo The new {@link Account} to associate with the raw contact.
48      * @return The number of rows updated.
49      */
updateRawContactAccount(ContentResolver resolver, long rawContactId, Account accountUpdateTo)50     public static int updateRawContactAccount(ContentResolver resolver, long rawContactId,
51             Account accountUpdateTo) {
52         ContentValues values = new ContentValues();
53         if (accountUpdateTo == null) {
54             // Using null account: put the null account name and account type to the content values.
55             values.put(ContactsContract.RawContacts.ACCOUNT_NAME, (String) null);
56             values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, (String) null);
57         } else {
58             values.put(ContactsContract.RawContacts.ACCOUNT_NAME, accountUpdateTo.name);
59             values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, accountUpdateTo.type);
60         }
61         return update(resolver, rawContactId, values);
62     }
63 
createRawContactWithName(ContentResolver resolver, Account account, String name)64     public static long createRawContactWithName(ContentResolver resolver, Account account,
65             String name) {
66         Long rawContactId = insertRawContactIgnoringNullAccount(resolver, account);
67         DataUtil.insertName(resolver, rawContactId, name);
68         return rawContactId;
69     }
70 
createRawContactWithAutoGeneratedName(ContentResolver resolver, Account account)71     public static long createRawContactWithAutoGeneratedName(ContentResolver resolver,
72             Account account) {
73         Long rawContactId = insertRawContactIgnoringNullAccount(resolver, account);
74         DataUtil.insertAutoGeneratedName(resolver, rawContactId);
75         return rawContactId;
76     }
77 
78     /**
79      * Inserts a raw contact into the Contacts Provider, ignoring the provided account if it is
80      * null.
81      *
82      * @param resolver The {@link ContentResolver} to use for the insertion.
83      * @param account  The {@link Account} to associate with the raw contact, or null to insert
84      *                 without an account.
85      * @return The ID of the newly inserted raw contact.
86      */
insertRawContactIgnoringNullAccount(ContentResolver resolver, Account account)87     public static long insertRawContactIgnoringNullAccount(ContentResolver resolver,
88             Account account) {
89         ContentValues values = new ContentValues();
90         if (account != null) {
91             values.put(ContactsContract.RawContacts.ACCOUNT_NAME, account.name);
92             values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type);
93         }
94         Uri uri = resolver.insert(URI, values);
95         return ContentUris.parseId(uri);
96     }
97 
98     /**
99      * Inserts a raw contact into the Contacts Provider, using null values for the account if the
100      * provided account is null.
101      *
102      * @param resolver The {@link ContentResolver} to use for the insertion.
103      * @param account The {@link Account} to associate with the raw contact, or null to insert
104      *                with null account values.
105      * @return The ID of the newly inserted raw contact.
106      */
insertRawContactUsingNullAccount(ContentResolver resolver, Account account)107     public static long insertRawContactUsingNullAccount(ContentResolver resolver, Account account) {
108         ContentValues values = new ContentValues();
109         if (account == null) {
110             // Using null account: put the null account name and account type to the content values.
111             values.put(ContactsContract.RawContacts.ACCOUNT_NAME, (String) null);
112             values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, (String) null);
113         } else {
114             values.put(ContactsContract.RawContacts.ACCOUNT_NAME, account.name);
115             values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type);
116         }
117         Uri uri = resolver.insert(URI, values);
118         return ContentUris.parseId(uri);
119     }
120 
queryByRawContactId(ContentResolver resolver, long rawContactId, String[] projection)121     public static String[] queryByRawContactId(ContentResolver resolver,
122             long rawContactId, String[] projection) {
123         Uri uri = ContentUris.withAppendedId(URI, rawContactId);
124         Cursor cursor = resolver.query(uri, projection, null, null, null);
125         return CommonDatabaseUtils.singleRecordToArray(cursor);
126     }
127 
128     /**
129      * Returns a list of raw contact records.
130      *
131      * @return A list of records.  Where each record is represented as an array of strings.
132      */
queryByContactId(ContentResolver resolver, long contactId, String[] projection)133     public static List<String[]> queryByContactId(ContentResolver resolver, long contactId,
134             String[] projection) {
135         Uri uri = ContentUris.withAppendedId(URI, contactId);
136         Cursor cursor = resolver.query(uri, projection, null, null, null);
137         return CommonDatabaseUtils.multiRecordToArray(cursor);
138     }
139 
delete(ContentResolver resolver, long rawContactId, boolean isSyncAdapter)140     public static void delete(ContentResolver resolver, long rawContactId,
141             boolean isSyncAdapter) {
142         Uri uri = ContentUris.withAppendedId(URI, rawContactId)
143                 .buildUpon()
144                 .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, isSyncAdapter + "")
145                 .build();
146         resolver.delete(uri, null, null);
147     }
148 
queryContactIdByRawContactId(ContentResolver resolver, long rawContactid)149     public static long queryContactIdByRawContactId(ContentResolver resolver, long rawContactid) {
150         String[] projection = new String[]{
151                 ContactsContract.RawContacts.CONTACT_ID
152         };
153         String[] result = RawContactUtil.queryByRawContactId(resolver, rawContactid,
154                 projection);
155         if (result == null) {
156             return CommonDatabaseUtils.NOT_FOUND;
157         }
158         return Long.parseLong(result[0]);
159     }
160 
rawContactExistsById(ContentResolver resolver, long rawContactid)161     public static boolean rawContactExistsById(ContentResolver resolver, long rawContactid) {
162         long contactId = queryContactIdByRawContactId(resolver, rawContactid);
163         return contactId != CommonDatabaseUtils.NOT_FOUND;
164     }
165 }
166