Home | History | Annotate | Download | only in impl
      1 /*
      2  * Copyright (C) 2017 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.dialer.phonenumbergeoutil.impl;
     18 
     19 import android.content.Context;
     20 import android.text.TextUtils;
     21 import com.android.dialer.common.LogUtil;
     22 import com.android.dialer.compat.CompatUtils;
     23 import com.android.dialer.phonenumbergeoutil.PhoneNumberGeoUtil;
     24 import com.google.i18n.phonenumbers.NumberParseException;
     25 import com.google.i18n.phonenumbers.PhoneNumberUtil;
     26 import com.google.i18n.phonenumbers.Phonenumber;
     27 import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
     28 import java.util.Locale;
     29 import javax.inject.Inject;
     30 
     31 /** Implementation of {@link PhoneNumberGeoUtil}. */
     32 public class PhoneNumberGeoUtilImpl implements PhoneNumberGeoUtil {
     33 
     34   @Inject
     35   public PhoneNumberGeoUtilImpl() {}
     36 
     37   @Override
     38   public String getGeoDescription(Context context, String number, String countryIso) {
     39     LogUtil.v("PhoneNumberGeoUtilImpl.getGeoDescription", "" + LogUtil.sanitizePii(number));
     40 
     41     if (TextUtils.isEmpty(number)) {
     42       return null;
     43     }
     44 
     45     PhoneNumberUtil util = PhoneNumberUtil.getInstance();
     46     PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
     47 
     48     Locale locale = CompatUtils.getLocale(context);
     49     Phonenumber.PhoneNumber pn = null;
     50     try {
     51       LogUtil.v(
     52           "PhoneNumberGeoUtilImpl.getGeoDescription",
     53           "parsing '" + LogUtil.sanitizePii(number) + "' for countryIso '" + countryIso + "'...");
     54       pn = util.parse(number, countryIso);
     55       LogUtil.v(
     56           "PhoneNumberGeoUtilImpl.getGeoDescription",
     57           "- parsed number: " + LogUtil.sanitizePii(pn));
     58     } catch (NumberParseException e) {
     59       LogUtil.e(
     60           "PhoneNumberGeoUtilImpl.getGeoDescription",
     61           "getGeoDescription: NumberParseException for incoming number '"
     62               + LogUtil.sanitizePii(number)
     63               + "'");
     64     }
     65 
     66     if (pn != null) {
     67       String description = geocoder.getDescriptionForNumber(pn, locale);
     68       LogUtil.v(
     69           "PhoneNumberGeoUtilImpl.getGeoDescription", "- got description: '" + description + "'");
     70       return description;
     71     }
     72 
     73     return null;
     74   }
     75 }
     76