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 createRawContactWithName(ContentResolver resolver, Account account, String name)42 public static long createRawContactWithName(ContentResolver resolver, Account account, 43 String name) { 44 Long rawContactId = insertRawContact(resolver, account); 45 DataUtil.insertName(resolver, rawContactId, name); 46 return rawContactId; 47 } 48 createRawContactWithAutoGeneratedName(ContentResolver resolver, Account account)49 public static long createRawContactWithAutoGeneratedName(ContentResolver resolver, 50 Account account) { 51 Long rawContactId = insertRawContact(resolver, account); 52 DataUtil.insertAutoGeneratedName(resolver, rawContactId); 53 return rawContactId; 54 } 55 insertRawContact(ContentResolver resolver, Account account)56 public static long insertRawContact(ContentResolver resolver, Account account) { 57 ContentValues values = new ContentValues(); 58 values.put(ContactsContract.RawContacts.ACCOUNT_NAME, account.name); 59 values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type); 60 Uri uri = resolver.insert(URI, values); 61 return ContentUris.parseId(uri); 62 } 63 queryByRawContactId(ContentResolver resolver, long rawContactId, String[] projection)64 public static String[] queryByRawContactId(ContentResolver resolver, 65 long rawContactId, String[] projection) { 66 Uri uri = ContentUris.withAppendedId(URI, rawContactId); 67 Cursor cursor = resolver.query(uri, projection, null, null, null); 68 return CommonDatabaseUtils.singleRecordToArray(cursor); 69 } 70 71 /** 72 * Returns a list of raw contact records. 73 * 74 * @return A list of records. Where each record is represented as an array of strings. 75 */ queryByContactId(ContentResolver resolver, long contactId, String[] projection)76 public static List<String[]> queryByContactId(ContentResolver resolver, long contactId, 77 String[] projection) { 78 Uri uri = ContentUris.withAppendedId(URI, contactId); 79 Cursor cursor = resolver.query(uri, projection, null, null, null); 80 return CommonDatabaseUtils.multiRecordToArray(cursor); 81 } 82 delete(ContentResolver resolver, long rawContactId, boolean isSyncAdapter)83 public static void delete(ContentResolver resolver, long rawContactId, 84 boolean isSyncAdapter) { 85 Uri uri = ContentUris.withAppendedId(URI, rawContactId) 86 .buildUpon() 87 .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, isSyncAdapter + "") 88 .build(); 89 resolver.delete(uri, null, null); 90 } 91 queryContactIdByRawContactId(ContentResolver resolver, long rawContactid)92 public static long queryContactIdByRawContactId(ContentResolver resolver, long rawContactid) { 93 String[] projection = new String[]{ 94 ContactsContract.RawContacts.CONTACT_ID 95 }; 96 String[] result = RawContactUtil.queryByRawContactId(resolver, rawContactid, 97 projection); 98 if (result == null) { 99 return CommonDatabaseUtils.NOT_FOUND; 100 } 101 return Long.parseLong(result[0]); 102 } 103 rawContactExistsById(ContentResolver resolver, long rawContactid)104 public static boolean rawContactExistsById(ContentResolver resolver, long rawContactid) { 105 long contactId = queryContactIdByRawContactId(resolver, rawContactid); 106 return contactId != CommonDatabaseUtils.NOT_FOUND; 107 } 108 } 109