• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.mms.service;
18 
19 import android.telephony.TelephonyManager;
20 import android.text.TextUtils;
21 import android.util.Log;
22 import com.android.i18n.phonenumbers.NumberParseException;
23 import com.android.i18n.phonenumbers.PhoneNumberUtil;
24 import com.android.i18n.phonenumbers.Phonenumber;
25 
26 import java.util.Locale;
27 
28 /**
29  * Utility to handle phone numbers.
30  */
31 public class PhoneUtils {
32 
33     /**
34      * Get a canonical national format phone number. If parsing fails, just return the
35      * original number.
36      *
37      * @param telephonyManager TelephonyManager instance for the subscription for which the method
38      * is called
39      * @param phoneText The input phone number text
40      * @return The formatted number or the original phone number if failed to parse
41      */
getNationalNumber(TelephonyManager telephonyManager, String phoneText)42     public static String getNationalNumber(TelephonyManager telephonyManager, String phoneText) {
43         final String country = getSimOrDefaultLocaleCountry(telephonyManager);
44         final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
45         final Phonenumber.PhoneNumber parsed = getParsedNumber(phoneNumberUtil, phoneText, country);
46         if (parsed == null) {
47             return phoneText;
48         }
49         return phoneNumberUtil
50                 .format(parsed, PhoneNumberUtil.PhoneNumberFormat.NATIONAL)
51                 .replaceAll("\\D", "");
52     }
53 
54     // Parse the input number into internal format
getParsedNumber(PhoneNumberUtil phoneNumberUtil, String phoneText, String country)55     private static Phonenumber.PhoneNumber getParsedNumber(PhoneNumberUtil phoneNumberUtil,
56             String phoneText, String country) {
57         try {
58             final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneText, country);
59             if (phoneNumberUtil.isValidNumber(phoneNumber)) {
60                 return phoneNumber;
61             } else {
62                 LogUtil.e("getParsedNumber: not a valid phone number"
63                         + " for country " + country);
64                 return null;
65             }
66         } catch (final NumberParseException e) {
67             LogUtil.e("getParsedNumber: Not able to parse phone number", e);
68             return null;
69         }
70     }
71 
72     // Get the country/region either from the SIM ID or from locale
getSimOrDefaultLocaleCountry(TelephonyManager telephonyManager)73     private static String getSimOrDefaultLocaleCountry(TelephonyManager telephonyManager) {
74         String country = getSimCountry(telephonyManager);
75         if (TextUtils.isEmpty(country)) {
76             country = Locale.getDefault().getCountry();
77         }
78 
79         return country;
80     }
81 
82     // Get country/region from SIM ID
getSimCountry(TelephonyManager telephonyManager)83     private static String getSimCountry(TelephonyManager telephonyManager) {
84         final String country = telephonyManager.getSimCountryIso();
85         if (TextUtils.isEmpty(country)) {
86             return null;
87         }
88         return country.toUpperCase();
89     }
90 }
91