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