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