1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.android.dialer.calllog;
16 
17 import android.content.Context;
18 import android.database.Cursor;
19 import android.net.Uri;
20 import android.provider.ContactsContract;
21 import android.provider.ContactsContract.CommonDataKinds.Phone;
22 import android.provider.ContactsContract.Contacts;
23 import android.provider.ContactsContract.DisplayNameSources;
24 import android.provider.ContactsContract.PhoneLookup;
25 import android.telephony.PhoneNumberUtils;
26 import android.text.TextUtils;
27 
28 import com.android.contacts.common.util.Constants;
29 import com.android.contacts.common.util.PhoneNumberHelper;
30 import com.android.contacts.common.util.UriUtils;
31 import com.android.dialer.service.CachedNumberLookupService;
32 import com.android.dialer.service.CachedNumberLookupService.CachedContactInfo;
33 import com.android.dialerbind.ObjectFactory;
34 
35 import org.json.JSONException;
36 import org.json.JSONObject;
37 
38 import java.util.List;
39 
40 /**
41  * Utility class to look up the contact information for a given number.
42  */
43 public class ContactInfoHelper {
44     private final Context mContext;
45     private final String mCurrentCountryIso;
46 
47     private static final CachedNumberLookupService mCachedNumberLookupService =
48             ObjectFactory.newCachedNumberLookupService();
49 
ContactInfoHelper(Context context, String currentCountryIso)50     public ContactInfoHelper(Context context, String currentCountryIso) {
51         mContext = context;
52         mCurrentCountryIso = currentCountryIso;
53     }
54 
55     /**
56      * Returns the contact information for the given number.
57      * <p>
58      * If the number does not match any contact, returns a contact info containing only the number
59      * and the formatted number.
60      * <p>
61      * If an error occurs during the lookup, it returns null.
62      *
63      * @param number the number to look up
64      * @param countryIso the country associated with this number
65      */
lookupNumber(String number, String countryIso)66     public ContactInfo lookupNumber(String number, String countryIso) {
67         final ContactInfo info;
68 
69         // Determine the contact info.
70         if (PhoneNumberHelper.isUriNumber(number)) {
71             // This "number" is really a SIP address.
72             ContactInfo sipInfo = queryContactInfoForSipAddress(number);
73             if (sipInfo == null || sipInfo == ContactInfo.EMPTY) {
74                 // Check whether the "username" part of the SIP address is
75                 // actually the phone number of a contact.
76                 String username = PhoneNumberHelper.getUsernameFromUriNumber(number);
77                 if (PhoneNumberUtils.isGlobalPhoneNumber(username)) {
78                     sipInfo = queryContactInfoForPhoneNumber(username, countryIso);
79                 }
80             }
81             info = sipInfo;
82         } else {
83             // Look for a contact that has the given phone number.
84             ContactInfo phoneInfo = queryContactInfoForPhoneNumber(number, countryIso);
85 
86             if (phoneInfo == null || phoneInfo == ContactInfo.EMPTY) {
87                 // Check whether the phone number has been saved as an "Internet call" number.
88                 phoneInfo = queryContactInfoForSipAddress(number);
89             }
90             info = phoneInfo;
91         }
92 
93         final ContactInfo updatedInfo;
94         if (info == null) {
95             // The lookup failed.
96             updatedInfo = null;
97         } else {
98             // If we did not find a matching contact, generate an empty contact info for the number.
99             if (info == ContactInfo.EMPTY) {
100                 // Did not find a matching contact.
101                 updatedInfo = new ContactInfo();
102                 updatedInfo.number = number;
103                 updatedInfo.formattedNumber = formatPhoneNumber(number, null, countryIso);
104                 updatedInfo.normalizedNumber = PhoneNumberUtils.formatNumberToE164(
105                         number, countryIso);
106                 updatedInfo.lookupUri = createTemporaryContactUri(updatedInfo.formattedNumber);
107             } else {
108                 updatedInfo = info;
109             }
110         }
111         return updatedInfo;
112     }
113 
114     /**
115      * Creates a JSON-encoded lookup uri for a unknown number without an associated contact
116      *
117      * @param number - Unknown phone number
118      * @return JSON-encoded URI that can be used to perform a lookup when clicking on the quick
119      *         contact card.
120      */
createTemporaryContactUri(String number)121     private static Uri createTemporaryContactUri(String number) {
122         try {
123             final JSONObject contactRows = new JSONObject().put(Phone.CONTENT_ITEM_TYPE,
124                     new JSONObject().put(Phone.NUMBER, number).put(Phone.TYPE, Phone.TYPE_CUSTOM));
125 
126             final String jsonString = new JSONObject().put(Contacts.DISPLAY_NAME, number)
127                     .put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.PHONE)
128                     .put(Contacts.CONTENT_ITEM_TYPE, contactRows).toString();
129 
130             return Contacts.CONTENT_LOOKUP_URI
131                     .buildUpon()
132                     .appendPath(Constants.LOOKUP_URI_ENCODED)
133                     .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
134                             String.valueOf(Long.MAX_VALUE))
135                     .encodedFragment(jsonString)
136                     .build();
137         } catch (JSONException e) {
138             return null;
139         }
140     }
141 
142     /**
143      * Looks up a contact using the given URI.
144      * <p>
145      * It returns null if an error occurs, {@link ContactInfo#EMPTY} if no matching contact is
146      * found, or the {@link ContactInfo} for the given contact.
147      * <p>
148      * The {@link ContactInfo#formattedNumber} field is always set to {@code null} in the returned
149      * value.
150      */
lookupContactFromUri(Uri uri)151     private ContactInfo lookupContactFromUri(Uri uri) {
152         final ContactInfo info;
153         Cursor phonesCursor =
154                 mContext.getContentResolver().query(uri, PhoneQuery._PROJECTION, null, null, null);
155 
156         if (phonesCursor != null) {
157             try {
158                 if (phonesCursor.moveToFirst()) {
159                     info = new ContactInfo();
160                     long contactId = phonesCursor.getLong(PhoneQuery.PERSON_ID);
161                     String lookupKey = phonesCursor.getString(PhoneQuery.LOOKUP_KEY);
162                     info.lookupKey = lookupKey;
163                     info.lookupUri = Contacts.getLookupUri(contactId, lookupKey);
164                     info.name = phonesCursor.getString(PhoneQuery.NAME);
165                     info.type = phonesCursor.getInt(PhoneQuery.PHONE_TYPE);
166                     info.label = phonesCursor.getString(PhoneQuery.LABEL);
167                     info.number = phonesCursor.getString(PhoneQuery.MATCHED_NUMBER);
168                     info.normalizedNumber = phonesCursor.getString(PhoneQuery.NORMALIZED_NUMBER);
169                     info.photoId = phonesCursor.getLong(PhoneQuery.PHOTO_ID);
170                     info.photoUri =
171                             UriUtils.parseUriOrNull(phonesCursor.getString(PhoneQuery.PHOTO_URI));
172                     info.formattedNumber = null;
173                 } else {
174                     info = ContactInfo.EMPTY;
175                 }
176             } finally {
177                 phonesCursor.close();
178             }
179         } else {
180             // Failed to fetch the data, ignore this request.
181             info = null;
182         }
183         return info;
184     }
185 
186     /**
187      * Determines the contact information for the given SIP address.
188      * <p>
189      * It returns the contact info if found.
190      * <p>
191      * If no contact corresponds to the given SIP address, returns {@link ContactInfo#EMPTY}.
192      * <p>
193      * If the lookup fails for some other reason, it returns null.
194      */
queryContactInfoForSipAddress(String sipAddress)195     private ContactInfo queryContactInfoForSipAddress(String sipAddress) {
196         final ContactInfo info;
197 
198         // "contactNumber" is a SIP address, so use the PhoneLookup table with the SIP parameter.
199         Uri.Builder uriBuilder = PhoneLookup.CONTENT_FILTER_URI.buildUpon();
200         uriBuilder.appendPath(Uri.encode(sipAddress));
201         uriBuilder.appendQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, "1");
202         return lookupContactFromUri(uriBuilder.build());
203     }
204 
205     /**
206      * Determines the contact information for the given phone number.
207      * <p>
208      * It returns the contact info if found.
209      * <p>
210      * If no contact corresponds to the given phone number, returns {@link ContactInfo#EMPTY}.
211      * <p>
212      * If the lookup fails for some other reason, it returns null.
213      */
queryContactInfoForPhoneNumber(String number, String countryIso)214     private ContactInfo queryContactInfoForPhoneNumber(String number, String countryIso) {
215         String contactNumber = number;
216         if (!TextUtils.isEmpty(countryIso)) {
217             // Normalize the number: this is needed because the PhoneLookup query below does not
218             // accept a country code as an input.
219             String numberE164 = PhoneNumberUtils.formatNumberToE164(number, countryIso);
220             if (!TextUtils.isEmpty(numberE164)) {
221                 // Only use it if the number could be formatted to E164.
222                 contactNumber = numberE164;
223             }
224         }
225 
226         // The "contactNumber" is a regular phone number, so use the PhoneLookup table.
227         Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));
228         ContactInfo info = lookupContactFromUri(uri);
229         if (info != null && info != ContactInfo.EMPTY) {
230             info.formattedNumber = formatPhoneNumber(number, null, countryIso);
231         } else if (mCachedNumberLookupService != null) {
232             CachedContactInfo cacheInfo =
233                     mCachedNumberLookupService.lookupCachedContactFromNumber(mContext, number);
234             if (cacheInfo != null) {
235                 info = cacheInfo.getContactInfo().isBadData ? null : cacheInfo.getContactInfo();
236             } else {
237                 info = null;
238             }
239         }
240         return info;
241     }
242 
243     /**
244      * Format the given phone number
245      *
246      * @param number the number to be formatted.
247      * @param normalizedNumber the normalized number of the given number.
248      * @param countryIso the ISO 3166-1 two letters country code, the country's convention will be
249      *        used to format the number if the normalized phone is null.
250      *
251      * @return the formatted number, or the given number if it was formatted.
252      */
formatPhoneNumber(String number, String normalizedNumber, String countryIso)253     private String formatPhoneNumber(String number, String normalizedNumber, String countryIso) {
254         if (TextUtils.isEmpty(number)) {
255             return "";
256         }
257         // If "number" is really a SIP address, don't try to do any formatting at all.
258         if (PhoneNumberHelper.isUriNumber(number)) {
259             return number;
260         }
261         if (TextUtils.isEmpty(countryIso)) {
262             countryIso = mCurrentCountryIso;
263         }
264         return PhoneNumberUtils.formatNumber(number, normalizedNumber, countryIso);
265     }
266 
267     /**
268      * Parses the given URI to determine the original lookup key of the contact.
269      */
getLookupKeyFromUri(Uri lookupUri)270     public static String getLookupKeyFromUri(Uri lookupUri) {
271         // Would be nice to be able to persist the lookup key somehow to avoid having to parse
272         // the uri entirely just to retrieve the lookup key, but every uri is already parsed
273         // once anyway to check if it is an encoded JSON uri, so this has negligible effect
274         // on performance.
275         if (lookupUri != null && !UriUtils.isEncodedContactUri(lookupUri)) {
276             final List<String> segments = lookupUri.getPathSegments();
277             // This returns the third path segment of the uri, where the lookup key is located.
278             // See {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
279             return (segments.size() < 3) ? null : Uri.encode(segments.get(2));
280         } else {
281             return null;
282         }
283     }
284 
285     /**
286      * Given a contact's sourceType, return true if the contact is a business
287      *
288      * @param sourceType sourceType of the contact. This is usually populated by
289      *        {@link #mCachedNumberLookupService}.
290      */
isBusiness(int sourceType)291     public boolean isBusiness(int sourceType) {
292         return mCachedNumberLookupService != null
293                 && mCachedNumberLookupService.isBusiness(sourceType);
294     }
295 
296     /**
297      * This function looks at a contact's source and determines if the user can
298      * mark caller ids from this source as invalid.
299      *
300      * @param sourceType The source type to be checked
301      * @param objectId The ID of the Contact object.
302      * @return true if contacts from this source can be marked with an invalid caller id
303      */
canReportAsInvalid(int sourceType, String objectId)304     public boolean canReportAsInvalid(int sourceType, String objectId) {
305         return mCachedNumberLookupService != null
306                 && mCachedNumberLookupService.canReportAsInvalid(sourceType, objectId);
307     }
308 
309 
310 }
311