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