Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad079e31 authored by Steve Statia's avatar Steve Statia Committed by Android (Google) Code Review
Browse files

Merge "Add Taiwan country code to be formatted for local calls to remove the...

Merge "Add Taiwan country code to be formatted for local calls to remove the international prefix." into main
parents 6b4ba15d 43bc7843
Loading
Loading
Loading
Loading
+52 −31
Original line number Diff line number Diff line
@@ -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,36 +1655,47 @@ 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)) {
            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 {
                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.
                     * 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)) {
                } 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.
                     * 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))) {
                } 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.
@@ -1685,7 +1704,9 @@ public class PhoneNumberUtils {
                } else {
                    result = util.formatInOriginalFormat(pn, defaultCountryIso);
                }
            }
        } catch (NumberParseException e) {
            if (DBG) log("formatNumber: NumberParseException caught " + e);
        }
        return result;
    }