Ye Wen | 3cf9a94 | 2014-10-15 13:35:27 -0700 | [diff] [blame] | 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 |
Hotice Hsu | 1f4477d | 2019-01-07 13:31:30 +0800 | [diff] [blame] | 40 | * @param countryIsoOverride String to override sim country iso. |
Ye Wen | 3cf9a94 | 2014-10-15 13:35:27 -0700 | [diff] [blame] | 41 | * @return The formatted number or the original phone number if failed to parse |
| 42 | */ |
| 43 | public static String getNationalNumber(TelephonyManager telephonyManager, int subId, |
Hotice Hsu | 1f4477d | 2019-01-07 13:31:30 +0800 | [diff] [blame] | 44 | String phoneText, String countryIsoOverride) { |
| 45 | String country = getSimOrDefaultLocaleCountry(telephonyManager, subId); |
| 46 | |
| 47 | if (!TextUtils.isEmpty(countryIsoOverride)) { |
| 48 | country = countryIsoOverride.toUpperCase(); |
| 49 | } |
| 50 | |
Ye Wen | 3cf9a94 | 2014-10-15 13:35:27 -0700 | [diff] [blame] | 51 | final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); |
| 52 | final Phonenumber.PhoneNumber parsed = getParsedNumber(phoneNumberUtil, phoneText, country); |
| 53 | if (parsed == null) { |
| 54 | return phoneText; |
| 55 | } |
| 56 | return phoneNumberUtil |
| 57 | .format(parsed, PhoneNumberUtil.PhoneNumberFormat.NATIONAL) |
| 58 | .replaceAll("\\D", ""); |
| 59 | } |
| 60 | |
| 61 | // Parse the input number into internal format |
| 62 | private static Phonenumber.PhoneNumber getParsedNumber(PhoneNumberUtil phoneNumberUtil, |
| 63 | String phoneText, String country) { |
| 64 | try { |
| 65 | final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneText, country); |
| 66 | if (phoneNumberUtil.isValidNumber(phoneNumber)) { |
| 67 | return phoneNumber; |
| 68 | } else { |
Ye Wen | 8153aed | 2015-06-16 13:36:31 -0700 | [diff] [blame] | 69 | LogUtil.e("getParsedNumber: not a valid phone number" |
Ye Wen | 3cf9a94 | 2014-10-15 13:35:27 -0700 | [diff] [blame] | 70 | + " for country " + country); |
| 71 | return null; |
| 72 | } |
| 73 | } catch (final NumberParseException e) { |
Ye Wen | 8153aed | 2015-06-16 13:36:31 -0700 | [diff] [blame] | 74 | LogUtil.e("getParsedNumber: Not able to parse phone number", e); |
Ye Wen | 3cf9a94 | 2014-10-15 13:35:27 -0700 | [diff] [blame] | 75 | return null; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Get the country/region either from the SIM ID or from locale |
| 80 | private static String getSimOrDefaultLocaleCountry(TelephonyManager telephonyManager, |
| 81 | int subId) { |
| 82 | String country = getSimCountry(telephonyManager, subId); |
| 83 | if (TextUtils.isEmpty(country)) { |
| 84 | country = Locale.getDefault().getCountry(); |
| 85 | } |
| 86 | |
| 87 | return country; |
| 88 | } |
| 89 | |
| 90 | // Get country/region from SIM ID |
| 91 | private static String getSimCountry(TelephonyManager telephonyManager, int subId) { |
| 92 | final String country = telephonyManager.getSimCountryIso(subId); |
| 93 | if (TextUtils.isEmpty(country)) { |
| 94 | return null; |
| 95 | } |
| 96 | return country.toUpperCase(); |
| 97 | } |
| 98 | } |