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