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.AsyncQueryHandler;
20 import android.content.ContentResolver;
21 import android.content.Intent;
22 import android.database.Cursor;
23 import android.net.Uri;
24 import android.provider.Contacts.People;
25 import android.provider.Contacts.People.Phones;
26 
27 /**
28  * An implementation of {@link ContactAccessor} that uses legacy Contacts API.
29  * These APIs are deprecated and should not be used unless we are running on a
30  * pre-Eclair SDK.
31  * <p>
32  * There are several reasons why we wouldn't want to use this class on an Eclair device:
33  * <ul>
34  * <li>It would see at most one account, namely the first Google account created on the device.
35  * <li>It would work through a compatibility layer, which would make it inherently less efficient.
36  * <li>Not relevant to this particular example, but it would not have access to new kinds
37  * of data available through current APIs.
38  * </ul>
39  */
40 @SuppressWarnings("deprecation")
41 public class ContactAccessorSdk3_4 extends ContactAccessor {
42 
43     /**
44      * Returns a Pick Contact intent using the pre-Eclair "people" URI.
45      */
46     @Override
getPickContactIntent()47     public Intent getPickContactIntent() {
48         return new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
49     }
50 
51     /**
52      * Retrieves the contact information.
53      */
54     @Override
loadContact(ContentResolver contentResolver, Uri contactUri)55     public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {
56         ContactInfo contactInfo = new ContactInfo();
57         Cursor cursor = contentResolver.query(contactUri,
58                 new String[]{People.DISPLAY_NAME}, null, null, null);
59         try {
60             if (cursor.moveToFirst()) {
61                 contactInfo.setDisplayName(cursor.getString(0));
62             }
63         } finally {
64             cursor.close();
65         }
66 
67         Uri phoneUri = Uri.withAppendedPath(contactUri, Phones.CONTENT_DIRECTORY);
68         cursor = contentResolver.query(phoneUri,
69                 new String[]{Phones.NUMBER}, null, null, Phones.ISPRIMARY + " DESC");
70 
71         try {
72             if (cursor.moveToFirst()) {
73                 contactInfo.setPhoneNumber(cursor.getString(0));
74             }
75         } finally {
76             cursor.close();
77         }
78 
79         return contactInfo;
80     }
81 }
82