Home | History | Annotate | Download | only in contacts
      1 /*
      2  * Copyright (C) 2011 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.android.providers.contacts;
     18 
     19 import com.android.i18n.phonenumbers.NumberParseException;
     20 import com.android.i18n.phonenumbers.PhoneNumberUtil;
     21 import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
     22 import com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
     23 
     24 import android.content.ContentValues;
     25 import android.content.Context;
     26 import android.provider.CallLog.Calls;
     27 import android.util.Log;
     28 
     29 import java.util.Locale;
     30 
     31 /**
     32  * Default implementation of {@link CallLogInsertionHelper}.
     33  * <p>
     34  * It added the country ISO abbreviation and the geocoded location.
     35  * <p>
     36  * It uses {@link PhoneNumberOfflineGeocoder} to compute the geocoded location of a phone number.
     37  */
     38 /*package*/ class DefaultCallLogInsertionHelper implements CallLogInsertionHelper {
     39     private static DefaultCallLogInsertionHelper sInstance;
     40 
     41     private final CountryMonitor mCountryMonitor;
     42     private PhoneNumberUtil mPhoneNumberUtil;
     43     private PhoneNumberOfflineGeocoder mPhoneNumberOfflineGeocoder;
     44     private final Locale mLocale;
     45 
     46     public static synchronized DefaultCallLogInsertionHelper getInstance(Context context) {
     47         if (sInstance == null) {
     48             sInstance = new DefaultCallLogInsertionHelper(context);
     49         }
     50         return sInstance;
     51     }
     52 
     53     private DefaultCallLogInsertionHelper(Context context) {
     54         mCountryMonitor = new CountryMonitor(context);
     55         mLocale = context.getResources().getConfiguration().locale;
     56     }
     57 
     58     @Override
     59     public void addComputedValues(ContentValues values) {
     60         // Insert the current country code, so we know the country the number belongs to.
     61         String countryIso = getCurrentCountryIso();
     62         values.put(Calls.COUNTRY_ISO, countryIso);
     63         // Insert the geocoded location, so that we do not need to compute it on the fly.
     64         values.put(Calls.GEOCODED_LOCATION,
     65                 getGeocodedLocationFor(values.getAsString(Calls.NUMBER), countryIso));
     66     }
     67 
     68     private String getCurrentCountryIso() {
     69         return mCountryMonitor.getCountryIso();
     70     }
     71 
     72     private synchronized PhoneNumberUtil getPhoneNumberUtil() {
     73         if (mPhoneNumberUtil == null) {
     74             mPhoneNumberUtil = PhoneNumberUtil.getInstance();
     75         }
     76         return mPhoneNumberUtil;
     77     }
     78 
     79     private PhoneNumber parsePhoneNumber(String number, String countryIso) {
     80         try {
     81             return getPhoneNumberUtil().parse(number, countryIso);
     82         } catch (NumberParseException e) {
     83             return null;
     84         }
     85     }
     86 
     87     private synchronized PhoneNumberOfflineGeocoder getPhoneNumberOfflineGeocoder() {
     88         if (mPhoneNumberOfflineGeocoder == null) {
     89             mPhoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder.getInstance();
     90         }
     91         return mPhoneNumberOfflineGeocoder;
     92     }
     93 
     94     @Override
     95     public String getGeocodedLocationFor(String number, String countryIso) {
     96         PhoneNumber structuredPhoneNumber = parsePhoneNumber(number, countryIso);
     97         if (structuredPhoneNumber != null) {
     98             return getPhoneNumberOfflineGeocoder().getDescriptionForNumber(
     99                     structuredPhoneNumber, mLocale);
    100         } else {
    101             return null;
    102         }
    103     }
    104 }
    105