diff --git a/telephony/java/android/telephony/PhoneNumberUtils.java b/telephony/java/android/telephony/PhoneNumberUtils.java index 0ecafc70b9a47309fa39db9b93741a0839444ec4..019fb7b7affe2a183be7b097a5258912e1ef2d8e 100644 --- a/telephony/java/android/telephony/PhoneNumberUtils.java +++ b/telephony/java/android/telephony/PhoneNumberUtils.java @@ -49,6 +49,7 @@ import com.android.telephony.Rlog; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.Arrays; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -1285,6 +1286,13 @@ public class PhoneNumberUtils { private static final String SINGAPORE_ISO_COUNTRY_CODE = "SG"; + private static final String[] COUNTRY_CODES_TO_FORMAT_NATIONALLY = new String[] { + "KR", // Korea + "JP", // Japan + "SG", // Singapore + "TW", // Taiwan + }; + /** * Breaks the given number down and formats it according to the rules * for the country the number is from. @@ -1647,45 +1655,58 @@ public class PhoneNumberUtils { defaultCountryIso = defaultCountryIso.toUpperCase(Locale.ROOT); } + Rlog.v(LOG_TAG, "formatNumber: defaultCountryIso: " + defaultCountryIso); + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); String result = null; try { PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso); - if (KOREA_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) && - (pn.getCountryCode() == util.getCountryCodeForRegion(KOREA_ISO_COUNTRY_CODE)) && - (pn.getCountryCodeSource() == - PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)) { - /** - * Need to reformat any local Korean phone numbers (when the user is in Korea) with - * country code to corresponding national format which would replace the leading - * +82 with 0. - */ - result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL); - } else if (JAPAN_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) && - pn.getCountryCode() == util.getCountryCodeForRegion(JAPAN_ISO_COUNTRY_CODE) && - (pn.getCountryCodeSource() == - PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)) { - /** - * Need to reformat Japanese phone numbers (when user is in Japan) with the national - * dialing format. - */ - result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL); - } else if (Flags.removeCountryCodeFromLocalSingaporeCalls() && - (SINGAPORE_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) && - pn.getCountryCode() == - util.getCountryCodeForRegion(SINGAPORE_ISO_COUNTRY_CODE) && - (pn.getCountryCodeSource() == - PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN))) { - /* - * Need to reformat Singaporean phone numbers (when the user is in Singapore) - * with the country code (+65) removed to comply with Singaporean regulations. - */ - result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL); + if (Flags.nationalCountryCodeFormattingForLocalCalls()) { + if (Arrays.asList(COUNTRY_CODES_TO_FORMAT_NATIONALLY).contains(defaultCountryIso) + && pn.getCountryCode() == util.getCountryCodeForRegion(defaultCountryIso) + && pn.getCountryCodeSource() + == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN) { + return util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL); + } else { + return util.formatInOriginalFormat(pn, defaultCountryIso); + } } else { - result = util.formatInOriginalFormat(pn, defaultCountryIso); + if (KOREA_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) && ( + pn.getCountryCode() == util.getCountryCodeForRegion(KOREA_ISO_COUNTRY_CODE)) + && (pn.getCountryCodeSource() + == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)) { + /** + * Need to reformat any local Korean phone numbers (when the user is in + * Korea) with country code to corresponding national format which would + * replace the leading +82 with 0. + */ + result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL); + } else if (JAPAN_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) + && pn.getCountryCode() == util.getCountryCodeForRegion( + JAPAN_ISO_COUNTRY_CODE) && (pn.getCountryCodeSource() + == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)) { + /** + * Need to reformat Japanese phone numbers (when user is in Japan) with the + * national dialing format. + */ + result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL); + } else if (Flags.removeCountryCodeFromLocalSingaporeCalls() && ( + SINGAPORE_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) + && pn.getCountryCode() == util.getCountryCodeForRegion( + SINGAPORE_ISO_COUNTRY_CODE) && (pn.getCountryCodeSource() + == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN))) { + /* + * Need to reformat Singaporean phone numbers (when the user is in Singapore) + * with the country code (+65) removed to comply with Singaporean regulations. + */ + result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL); + } else { + result = util.formatInOriginalFormat(pn, defaultCountryIso); + } } } catch (NumberParseException e) { + if (DBG) log("formatNumber: NumberParseException caught " + e); } return result; }