Home | History | Annotate | Download | only in emergency
      1 /*
      2  * Copyright (C) 2016 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 package com.android.emergency;
     17 
     18 import android.content.ContentResolver;
     19 import android.content.ContentUris;
     20 import android.content.ContentValues;
     21 import android.database.Cursor;
     22 import android.net.Uri;
     23 import android.provider.ContactsContract;
     24 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
     25 
     26 /**
     27  * Utils to create and delete contacts.
     28  */
     29 public class ContactTestUtils {
     30 
     31     /** Deletes contacts that match the given name and phone number. */
     32     public static boolean deleteContact(ContentResolver contentResolver,
     33                                         String name,
     34                                         String phone) {
     35         Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
     36                 Uri.encode(phone));
     37         Cursor cursor = contentResolver.query(contactUri, null, null, null, null);
     38         try {
     39             if (cursor.moveToFirst()) {
     40                 do {
     41                     String displayName = cursor.getString(
     42                             cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
     43                     if (displayName.equals(name)) {
     44                         String lookupKey = cursor.getString(
     45                                 cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
     46                         Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,
     47                                 lookupKey);
     48                         contentResolver.delete(uri, null, null);
     49                         return true;
     50                     }
     51                 } while (cursor.moveToNext());
     52             }
     53             return false;
     54         } finally {
     55             cursor.close();
     56         }
     57     }
     58 
     59     /**
     60      * Creates a new contact with the given name and phone number. Returns the
     61      * ContactsContract.CommonDataKinds.Phone.CONTENT_URI corresponding to the new contact.
     62      */
     63     public static Uri createContact(ContentResolver contentResolver,
     64                                     String name,
     65                                     String phoneNumber) {
     66         ContentValues values = new ContentValues();
     67         Uri rawContactUri = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI,
     68                 values);
     69         long rawContactId = ContentUris.parseId(rawContactUri);
     70         insertStructuredName(contentResolver, rawContactId, name, values);
     71         return insertPhoneNumber(contentResolver, rawContactId,
     72                 phoneNumber,
     73                 ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
     74     }
     75 
     76     private static void insertStructuredName(ContentResolver contentResolver,
     77                                              long rawContactId,
     78                                              String name,
     79                                              ContentValues values) {
     80         values.put(StructuredName.DISPLAY_NAME, name);
     81         values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
     82         values.put(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
     83         contentResolver.insert(ContactsContract.Data.CONTENT_URI, values);
     84     }
     85 
     86     private static Uri insertPhoneNumber(ContentResolver contentResolver,
     87                                          long rawContactId,
     88                                          String phoneNumber,
     89                                          int type) {
     90         ContentValues values = new ContentValues();
     91         values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
     92         values.put(ContactsContract.Data.MIMETYPE,
     93                 ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
     94         values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber);
     95         values.put(ContactsContract.CommonDataKinds.Phone.TYPE, type);
     96         values.put(ContactsContract.CommonDataKinds.Phone.LABEL, "Mobile");
     97         return contentResolver.insert(ContactsContract.Data.CONTENT_URI, values);
     98     }
     99 
    100     private ContactTestUtils() {
    101         // Prevent instantiation
    102         throw new UnsupportedOperationException();
    103     }
    104 }
    105