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 phoneticGiven)60     public static Uri insertStructuredName(
61             ContentResolver resolver, long rawContactId, String givenName, String familyName,
62             String phoneticGiven) {
63         return insertStructuredName(resolver, rawContactId, givenName, familyName, phoneticGiven,
64                 /* isSuperPrimary = true */ false);
65     }
66 
insertStructuredName( ContentResolver resolver, long rawContactId, String givenName, String familyName, String phoneticGiven, boolean isSuperPrimary)67     public static Uri insertStructuredName(
68             ContentResolver resolver, long rawContactId, String givenName, String familyName,
69             String phoneticGiven, 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 && phoneticGiven != null) {
82             sb.append(phoneticGiven);
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 (phoneticGiven != null) {
88             values.put(StructuredName.PHONETIC_GIVEN_NAME, phoneticGiven);
89         }
90         if (isSuperPrimary) {
91             values.put(Data.IS_PRIMARY, 1);
92             values.put(Data.IS_SUPER_PRIMARY, 1);
93         }
94 
95         return insertStructuredName(resolver, rawContactId, values);
96     }
97 }