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
38      * @param subId The SIM ID associated with this number
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, int subId, String phoneText)42     public static String getNationalNumber(TelephonyManager telephonyManager, int subId,
43             String phoneText) {
44         final String country = getSimOrDefaultLocaleCountry(telephonyManager, subId);
45         final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
46         final Phonenumber.PhoneNumber parsed = getParsedNumber(phoneNumberUtil, phoneText, country);
47         if (parsed == null) {
48             return phoneText;
49         }
50         return phoneNumberUtil
51                 .format(parsed, PhoneNumberUtil.PhoneNumberFormat.NATIONAL)
52                 .replaceAll("\\D", "");
53     }
54 
55     // Parse the input number into internal format
getParsedNumber(PhoneNumberUtil phoneNumberUtil, String phoneText, String country)56     private static Phonenumber.PhoneNumber getParsedNumber(PhoneNumberUtil phoneNumberUtil,
57             String phoneText, String country) {
58         try {
59             final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneText, country);
60             if (phoneNumberUtil.isValidNumber(phoneNumber)) {
61                 return phoneNumber;
62             } else {
63                 LogUtil.e("getParsedNumber: not a valid phone number"
64                         + " for country " + country);
65                 return null;
66             }
67         } catch (final NumberParseException e) {
68             LogUtil.e("getParsedNumber: Not able to parse phone number", e);
69             return null;
70         }
71     }
72 
73     // Get the country/region either from the SIM ID or from locale
getSimOrDefaultLocaleCountry(TelephonyManager telephonyManager, int subId)74     private static String getSimOrDefaultLocaleCountry(TelephonyManager telephonyManager,
75             int subId) {
76         String country = getSimCountry(telephonyManager, subId);
77         if (TextUtils.isEmpty(country)) {
78             country = Locale.getDefault().getCountry();
79         }
80 
81         return country;
82     }
83 
84     // Get country/region from SIM ID
getSimCountry(TelephonyManager telephonyManager, int subId)85     private static String getSimCountry(TelephonyManager telephonyManager, int subId) {
86         final String country = telephonyManager.getSimCountryIso(subId);
87         if (TextUtils.isEmpty(country)) {
88             return null;
89         }
90         return country.toUpperCase();
91     }
92 }
93