Home | History | Annotate | Download | only in testutil
      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.accounts.Account;
     20 import android.content.ContentResolver;
     21 import android.content.ContentUris;
     22 import android.content.ContentValues;
     23 import android.database.Cursor;
     24 import android.net.Uri;
     25 import android.provider.ContactsContract;
     26 import android.test.mock.MockContentResolver;
     27 
     28 import java.util.List;
     29 
     30 /**
     31  * Convenience methods for operating on the RawContacts table.
     32  */
     33 public class RawContactUtil {
     34 
     35     private static final Uri URI = ContactsContract.RawContacts.CONTENT_URI;
     36 
     37     public static void update(ContentResolver resolver, long rawContactId,
     38             ContentValues values) {
     39         Uri uri = ContentUris.withAppendedId(URI, rawContactId);
     40         resolver.update(uri, values, null, null);
     41     }
     42 
     43     public static String[] queryByRawContactId(ContentResolver resolver,
     44             long rawContactId, String[] projection) {
     45         Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI,
     46                 rawContactId);
     47         Cursor cursor = resolver.query(uri, projection, null, null, null);
     48         return CommonDatabaseUtils.singleRecordToArray(cursor);
     49     }
     50 
     51     /**
     52      * Returns a list of raw contact records.
     53      *
     54      * @return A list of records.  Where each record is represented as an array of strings.
     55      */
     56     public static List<String[]> queryByContactId(ContentResolver resolver, long contactId,
     57             String[] projection) {
     58         Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contactId);
     59         Cursor cursor = resolver.query(uri, projection, null, null, null);
     60         return CommonDatabaseUtils.multiRecordToArray(cursor);
     61     }
     62 
     63     public static void delete(ContentResolver resolver, long rawContactId,
     64             boolean isSyncAdapter) {
     65         Uri uri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId)
     66                 .buildUpon()
     67                 .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, isSyncAdapter + "")
     68                 .build();
     69         resolver.delete(uri, null, null);
     70     }
     71 
     72     public static long queryContactIdByRawContactId(ContentResolver resolver, long rawContactid) {
     73         String[] projection = new String[]{
     74                 ContactsContract.RawContacts.CONTACT_ID
     75         };
     76         String[] result = RawContactUtil.queryByRawContactId(resolver, rawContactid,
     77                 projection);
     78         if (result == null) {
     79             return CommonDatabaseUtils.NOT_FOUND;
     80         }
     81         return Long.parseLong(result[0]);
     82     }
     83 
     84     public static boolean rawContactExistsById(ContentResolver resolver, long rawContactid) {
     85         long contactId = queryContactIdByRawContactId(resolver, rawContactid);
     86         return contactId != CommonDatabaseUtils.NOT_FOUND;
     87     }
     88 
     89     public static long createRawContact(ContentResolver resolver, Account account,
     90             String... extras) {
     91         ContentValues values = new ContentValues();
     92         CommonDatabaseUtils.extrasVarArgsToValues(values, extras);
     93         final Uri uri = TestUtil.maybeAddAccountQueryParameters(ContactsContract.RawContacts.CONTENT_URI, account);
     94         Uri contactUri = resolver.insert(uri, values);
     95         return ContentUris.parseId(contactUri);
     96     }
     97 
     98     public static long createRawContactWithName(ContentResolver resolver) {
     99         return createRawContactWithName(resolver, null);
    100     }
    101 
    102     public static long createRawContactWithName(ContentResolver resolver, Account account) {
    103         return createRawContactWithName(resolver, "John", "Doe", account);
    104     }
    105 
    106     public static long createRawContactWithName(ContentResolver resolver, String firstName,
    107             String lastName) {
    108         return createRawContactWithName(resolver, firstName, lastName, null);
    109     }
    110 
    111     public static long createRawContactWithName(ContentResolver resolver, String firstName,
    112             String lastName, Account account) {
    113         long rawContactId = createRawContact(resolver, account);
    114         DataUtil.insertStructuredName(resolver, rawContactId, firstName, lastName);
    115         return rawContactId;
    116     }
    117 
    118     public static long createRawContact(ContentResolver resolver) {
    119         return createRawContact(resolver, null);
    120     }
    121 }
    122