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 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 void update(ContentResolver resolver, long rawContactId, 37 ContentValues values) { 38 Uri uri = ContentUris.withAppendedId(URI, rawContactId); 39 resolver.update(uri, values, null, null); 40 } 41 queryByRawContactId(ContentResolver resolver, long rawContactId, String[] projection)42 public static String[] queryByRawContactId(ContentResolver resolver, 43 long rawContactId, String[] projection) { 44 Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, 45 rawContactId); 46 Cursor cursor = resolver.query(uri, projection, null, null, null); 47 return CommonDatabaseUtils.singleRecordToArray(cursor); 48 } 49 delete(ContentResolver resolver, long rawContactId, boolean isSyncAdapter)50 public static void delete(ContentResolver resolver, long rawContactId, 51 boolean isSyncAdapter) { 52 Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId) 53 .buildUpon() 54 .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, isSyncAdapter + "") 55 .build(); 56 resolver.delete(uri, null, null); 57 } 58 queryContactIdByRawContactId(ContentResolver resolver, long rawContactid)59 public static long queryContactIdByRawContactId(ContentResolver resolver, long rawContactid) { 60 String[] projection = new String[]{ 61 ContactsContract.RawContacts.CONTACT_ID 62 }; 63 String[] result = RawContactUtil.queryByRawContactId(resolver, rawContactid, 64 projection); 65 if (result == null) { 66 return CommonDatabaseUtils.NOT_FOUND; 67 } 68 return Long.parseLong(result[0]); 69 } 70 rawContactExistsById(ContentResolver resolver, long rawContactid)71 public static boolean rawContactExistsById(ContentResolver resolver, long rawContactid) { 72 long contactId = queryContactIdByRawContactId(resolver, rawContactid); 73 return contactId != CommonDatabaseUtils.NOT_FOUND; 74 } 75 createRawContact(ContentResolver resolver, Account account, String... extras)76 public static long createRawContact(ContentResolver resolver, Account account, 77 String... extras) { 78 ContentValues values = new ContentValues(); 79 CommonDatabaseUtils.extrasVarArgsToValues(values, extras); 80 final Uri uri = TestUtil.maybeAddAccountQueryParameters( 81 ContactsContract.RawContacts.CONTENT_URI, account); 82 Uri contactUri = resolver.insert(uri, values); 83 return ContentUris.parseId(contactUri); 84 } 85 createRawContactWithAccountDataSet(ContentResolver resolver, String accountName, String accountType, String dataSet, String... extras)86 public static long createRawContactWithAccountDataSet(ContentResolver resolver, 87 String accountName, String accountType, String dataSet, String... extras) { 88 ContentValues values = new ContentValues(); 89 CommonDatabaseUtils.extrasVarArgsToValues(values, extras); 90 final Uri uri = TestUtil.maybeAddAccountWithDataSetQueryParameters( 91 ContactsContract.RawContacts.CONTENT_URI, accountName, accountType, dataSet); 92 Uri contactUri = resolver.insert(uri, values); 93 return ContentUris.parseId(contactUri); 94 } 95 createRawContactWithName(ContentResolver resolver)96 public static long createRawContactWithName(ContentResolver resolver) { 97 return createRawContactWithName(resolver, null); 98 } 99 createRawContactWithName(ContentResolver resolver, Account account)100 public static long createRawContactWithName(ContentResolver resolver, Account account) { 101 return createRawContactWithName(resolver, "John", "Doe", account); 102 } 103 createRawContactWithName(ContentResolver resolver, String firstName, String lastName)104 public static long createRawContactWithName(ContentResolver resolver, String firstName, 105 String lastName) { 106 return createRawContactWithName(resolver, firstName, lastName, null); 107 } 108 createRawContactWithName(ContentResolver resolver, String firstName, String lastName, Account account)109 public static long createRawContactWithName(ContentResolver resolver, String firstName, 110 String lastName, Account account) { 111 long rawContactId = createRawContact(resolver, account); 112 DataUtil.insertStructuredName(resolver, rawContactId, firstName, lastName); 113 return rawContactId; 114 } 115 createRawContact(ContentResolver resolver)116 public static long createRawContact(ContentResolver resolver) { 117 return createRawContact(resolver, null); 118 } 119 createRawContactWithBackupId(ContentResolver resolver, String backupId, Account account)120 public static long createRawContactWithBackupId(ContentResolver resolver, String backupId, 121 Account account) { 122 ContentValues values = new ContentValues(); 123 values.put(ContactsContract.RawContacts.BACKUP_ID, backupId); 124 final Uri uri = ContactsContract.RawContacts.CONTENT_URI 125 .buildUpon() 126 .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, 127 account.name) 128 .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, 129 account.type) 130 .build(); 131 Uri contactUri = resolver.insert(uri, values); 132 return ContentUris.parseId(contactUri); 133 } 134 } 135