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.content.ContentResolver; 20 import android.content.ContentUris; 21 import android.content.ContentValues; 22 import android.net.Uri; 23 import android.provider.ContactsContract; 24 import android.provider.ContactsContract.CommonDataKinds.StructuredName; 25 import android.provider.ContactsContract.Data; 26 import android.test.mock.MockContentResolver; 27 28 /** 29 * Convenience methods for operating on the Data table. 30 */ 31 public class DataUtil { 32 33 private static final Uri URI = ContactsContract.Data.CONTENT_URI; 34 delete(ContentResolver resolver, long dataId)35 public static void delete(ContentResolver resolver, long dataId) { 36 Uri uri = ContentUris.withAppendedId(URI, dataId); 37 resolver.delete(uri, null, null); 38 } 39 update(ContentResolver resolver, long dataId, ContentValues values)40 public static void update(ContentResolver resolver, long dataId, ContentValues values) { 41 Uri uri = ContentUris.withAppendedId(URI, dataId); 42 resolver.update(uri, values, null, null); 43 } 44 insertStructuredName(ContentResolver resolver, long rawContactId, ContentValues values)45 public static Uri insertStructuredName(ContentResolver resolver, long rawContactId, 46 ContentValues values) { 47 values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); 48 values.put(ContactsContract.Data.MIMETYPE, 49 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); 50 Uri resultUri = resolver.insert(ContactsContract.Data.CONTENT_URI, values); 51 return resultUri; 52 } 53 insertStructuredName(ContentResolver resolver, long rawContactId, String givenName, String familyName)54 public static Uri insertStructuredName(ContentResolver resolver, long rawContactId, 55 String givenName, String familyName) { 56 return insertStructuredName(resolver, rawContactId, givenName, familyName, 57 /* phonetic given =*/ null); 58 } 59 insertStructuredName( ContentResolver resolver, long rawContactId, String givenName, String familyName, String phoneticFamily)60 public static Uri insertStructuredName( 61 ContentResolver resolver, long rawContactId, String givenName, String familyName, 62 String phoneticFamily) { 63 return insertStructuredName(resolver, rawContactId, givenName, familyName, phoneticFamily, 64 /* isSuperPrimary = true */ false); 65 } 66 insertStructuredName( ContentResolver resolver, long rawContactId, String givenName, String familyName, String phoneticFamily, boolean isSuperPrimary)67 public static Uri insertStructuredName( 68 ContentResolver resolver, long rawContactId, String givenName, String familyName, 69 String phoneticFamily, boolean isSuperPrimary) { 70 ContentValues values = new ContentValues(); 71 StringBuilder sb = new StringBuilder(); 72 if (givenName != null) { 73 sb.append(givenName); 74 } 75 if (givenName != null && familyName != null) { 76 sb.append(" "); 77 } 78 if (familyName != null) { 79 sb.append(familyName); 80 } 81 if (sb.length() == 0 && phoneticFamily != null) { 82 sb.append(phoneticFamily); 83 } 84 values.put(StructuredName.DISPLAY_NAME, sb.toString()); 85 values.put(StructuredName.GIVEN_NAME, givenName); 86 values.put(StructuredName.FAMILY_NAME, familyName); 87 if (phoneticFamily != null) { 88 // When creating phonetic names, be careful to use PHONETIC_FAMILY_NAME instead of 89 // PHONETIC_GIVEN_NAME, to work around b/19612393. 90 values.put(StructuredName.PHONETIC_FAMILY_NAME, phoneticFamily); 91 } 92 if (isSuperPrimary) { 93 values.put(Data.IS_PRIMARY, 1); 94 values.put(Data.IS_SUPER_PRIMARY, 1); 95 } 96 97 return insertStructuredName(resolver, rawContactId, values); 98 } 99 insertPhoneticName(ContentResolver resolver, long rawContactId, String phoneticFamilyName)100 public static Uri insertPhoneticName(ContentResolver resolver, long rawContactId, 101 String phoneticFamilyName) { 102 ContentValues values = new ContentValues(); 103 // When creating phonetic names, be careful to use PHONETIC_FAMILY_NAME instead of 104 // PHONETIC_GIVEN_NAME, to work around b/19612393. 105 values.put(StructuredName.PHONETIC_FAMILY_NAME, phoneticFamilyName); 106 return insertStructuredName(resolver, rawContactId, values); 107 } 108 }