1 /* 2 * Copyright (C) 2009 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.example.android.businesscard; 18 19 import android.content.ContentResolver; 20 import android.content.Intent; 21 import android.database.Cursor; 22 import android.net.Uri; 23 import android.provider.ContactsContract.Contacts; 24 import android.provider.ContactsContract.CommonDataKinds.Phone; 25 26 /** 27 * An implementation of {@link ContactAccessor} that uses current Contacts API. 28 * This class should be used on Eclair or beyond, but would not work on any earlier 29 * release of Android. As a matter of fact, it could not even be loaded. 30 * <p> 31 * This implementation has several advantages: 32 * <ul> 33 * <li>It sees contacts from multiple accounts. 34 * <li>It works with aggregated contacts. So for example, if the contact is the result 35 * of aggregation of two raw contacts from different accounts, it may return the name from 36 * one and the phone number from the other. 37 * <li>It is efficient because it uses the more efficient current API. 38 * <li>Not obvious in this particular example, but it has access to new kinds 39 * of data available exclusively through the new APIs. Exercise for the reader: add support 40 * for nickname (see {@link android.provider.ContactsContract.CommonDataKinds.Nickname}) or 41 * social status updates (see {@link android.provider.ContactsContract.StatusUpdates}). 42 * </ul> 43 */ 44 public class ContactAccessorSdk5 extends ContactAccessor { 45 46 /** 47 * Returns a Pick Contact intent using the Eclair "contacts" URI. 48 */ 49 @Override getPickContactIntent()50 public Intent getPickContactIntent() { 51 return new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 52 } 53 54 /** 55 * Retrieves the contact information. 56 */ 57 @Override loadContact(ContentResolver contentResolver, Uri contactUri)58 public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) { 59 ContactInfo contactInfo = new ContactInfo(); 60 long contactId = -1; 61 62 // Load the display name for the specified person 63 Cursor cursor = contentResolver.query(contactUri, 64 new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null); 65 try { 66 if (cursor.moveToFirst()) { 67 contactId = cursor.getLong(0); 68 contactInfo.setDisplayName(cursor.getString(1)); 69 } 70 } finally { 71 cursor.close(); 72 } 73 74 // Load the phone number (if any). 75 cursor = contentResolver.query(Phone.CONTENT_URI, 76 new String[]{Phone.NUMBER}, 77 Phone.CONTACT_ID + "=" + contactId, null, Phone.IS_SUPER_PRIMARY + " DESC"); 78 try { 79 if (cursor.moveToFirst()) { 80 contactInfo.setPhoneNumber(cursor.getString(0)); 81 } 82 } finally { 83 cursor.close(); 84 } 85 86 return contactInfo; 87 } 88 } 89