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 static android.provider.ContactsContract.CommonDataKinds;
20 
21 import android.content.ContentResolver;
22 import android.content.ContentUris;
23 import android.content.ContentValues;
24 import android.database.Cursor;
25 import android.net.Uri;
26 import android.provider.ContactsContract;
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 
queryById(ContentResolver resolver, long dataId, String[] projection)35     public static String[] queryById(ContentResolver resolver, long dataId, String[] projection) {
36         Uri uri = ContentUris.withAppendedId(URI, dataId);
37         Cursor cursor = resolver.query(uri, projection, null, null, null);
38         return CommonDatabaseUtils.singleRecordToArray(cursor);
39     }
40 
insertName(ContentResolver resolver, long rawContactId, String name)41     public static void insertName(ContentResolver resolver, long rawContactId, String name) {
42         ContentValues values = new ContentValues();
43         values.put(ContactsContract.Data.MIMETYPE,
44                 CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
45         values.put(CommonDataKinds.StructuredName.DISPLAY_NAME, name);
46         insertData(resolver, rawContactId, values);
47     }
48 
insertPhoneNumber(ContentResolver resolver, long rawContactId, String number)49     public static long insertPhoneNumber(ContentResolver resolver, long rawContactId,
50             String number) {
51         ContentValues values = new ContentValues();
52         values.put(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
53         values.put(CommonDataKinds.Phone.NUMBER, number);
54         return DataUtil.insertData(resolver, rawContactId, values);
55     }
56 
insertEmail(ContentResolver resolver, long rawContactId, String email)57     public static long insertEmail(ContentResolver resolver, long rawContactId, String email) {
58         ContentValues values = new ContentValues();
59         values.put(ContactsContract.Data.MIMETYPE, CommonDataKinds.Email.CONTENT_ITEM_TYPE);
60         values.put(CommonDataKinds.Email.ADDRESS, email);
61         return DataUtil.insertData(resolver, rawContactId, values);
62     }
63 
insertAutoGeneratedName(ContentResolver resolver, long rawContactId)64     public static void insertAutoGeneratedName(ContentResolver resolver, long rawContactId) {
65         insertName(resolver, rawContactId, "test raw contact " + rawContactId);
66     }
67 
insertData(ContentResolver resolver, long rawContactId, ContentValues values)68     public static long insertData(ContentResolver resolver, long rawContactId,
69             ContentValues values) {
70         // copy
71         ContentValues newValues = new ContentValues(values);
72         newValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
73 
74         Uri uri = resolver.insert(URI, newValues);
75         return ContentUris.parseId(uri);
76     }
77 
delete(ContentResolver resolver, long dataId)78     public static void delete(ContentResolver resolver, long dataId) {
79         Uri uri = ContentUris.withAppendedId(URI, dataId);
80         resolver.delete(uri, null, null);
81     }
82 
update(ContentResolver resolver, long dataId, ContentValues values)83     public static void update(ContentResolver resolver, long dataId, ContentValues values) {
84         Uri uri = ContentUris.withAppendedId(URI, dataId);
85         resolver.update(uri, values, null, null);
86     }
87 }
88