blob: a736ee26bcd83682ca4dd59b52b1ae14d9b2093d [file] [log] [blame]
Ye Wen3cf9a942014-10-15 13:35:27 -07001/*
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
17package com.android.mms.service;
18
19import android.telephony.TelephonyManager;
20import android.text.TextUtils;
21import android.util.Log;
22import com.android.i18n.phonenumbers.NumberParseException;
23import com.android.i18n.phonenumbers.PhoneNumberUtil;
24import com.android.i18n.phonenumbers.Phonenumber;
25
26import java.util.Locale;
27
28/**
29 * Utility to handle phone numbers.
30 */
31public 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 Hsu1f4477d2019-01-07 13:31:30 +080040 * @param countryIsoOverride String to override sim country iso.
Ye Wen3cf9a942014-10-15 13:35:27 -070041 * @return The formatted number or the original phone number if failed to parse
42 */
43 public static String getNationalNumber(TelephonyManager telephonyManager, int subId,
Hotice Hsu1f4477d2019-01-07 13:31:30 +080044 String phoneText, String countryIsoOverride) {
45 String country = getSimOrDefaultLocaleCountry(telephonyManager, subId);
46
47 if (!TextUtils.isEmpty(countryIsoOverride)) {
48 country = countryIsoOverride.toUpperCase();
49 }
50
Ye Wen3cf9a942014-10-15 13:35:27 -070051 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 Wen8153aed2015-06-16 13:36:31 -070069 LogUtil.e("getParsedNumber: not a valid phone number"
Ye Wen3cf9a942014-10-15 13:35:27 -070070 + " for country " + country);
71 return null;
72 }
73 } catch (final NumberParseException e) {
Ye Wen8153aed2015-06-16 13:36:31 -070074 LogUtil.e("getParsedNumber: Not able to parse phone number", e);
Ye Wen3cf9a942014-10-15 13:35:27 -070075 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}