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