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

Commit 7850cdde authored by Libin Tang's avatar Libin Tang Committed by Wink Saville
Browse files

Fix +NANP issue and cleanup plus code conversion.

This patch includes the plus code conversion clean up.
1. change the plus code conversion based on the current and default
   number systems retrieved from MCC.
2. for format such as +NANP, replace the '+' with the current IDP (011).
3. comments changes.
parent 41ceb907
Loading
Loading
Loading
Loading
+101 −82
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.SparseIntArray;

import static com.android.internal.telephony.TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY;
import static com.android.internal.telephony.TelephonyProperties.PROPERTY_IDP_STRING;
import static com.android.internal.telephony.TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY;

import java.util.Locale;
import java.util.regex.Matcher;
@@ -929,7 +931,7 @@ public class PhoneNumberUtils
        "JM", // Jamaica
        "PR", // Puerto Rico
        "MS", // Montserrat
        "NP", // Northern Mariana Islands
        "MP", // Northern Mariana Islands
        "KN", // Saint Kitts and Nevis
        "LC", // Saint Lucia
        "VC", // Saint Vincent and the Grenadines
@@ -962,17 +964,7 @@ public class PhoneNumberUtils
    public static int getFormatTypeForLocale(Locale locale) {
        String country = locale.getCountry();

        // Check for the NANP countries
        int length = NANP_COUNTRIES.length;
        for (int i = 0; i < length; i++) {
            if (NANP_COUNTRIES[i].equals(country)) {
                return FORMAT_NANP;
            }
        }
        if (locale.equals(Locale.JAPAN)) {
            return FORMAT_JAPAN;
        }
        return FORMAT_UNKNOWN;
        return getFormatTypeFromCountryCode(country);
    }

    /**
@@ -1272,6 +1264,11 @@ public class PhoneNumberUtils
     *
     * Otherwise, this function returns the dial string passed in
     *
     * @param dialStr the original dial string
     * @return the converted dial string if the current/default countries belong to NANP,
     * and if there is the "+" in the original dial string. Otherwise, the original dial
     * string returns.
     *
     * This API is for CDMA only
     *
     * @hide TODO: pending API Council approval
@@ -1280,8 +1277,13 @@ public class PhoneNumberUtils
        if (!TextUtils.isEmpty(dialStr)) {
            if (isReallyDialable(dialStr.charAt(0)) &&
                isNonSeparator(dialStr)) {
                String currIso = SystemProperties.get(PROPERTY_OPERATOR_ISO_COUNTRY, "");
                String defaultIso = SystemProperties.get(PROPERTY_ICC_OPERATOR_ISO_COUNTRY, "");
                if (!TextUtils.isEmpty(currIso) && !TextUtils.isEmpty(defaultIso)) {
                    return cdmaCheckAndProcessPlusCodeByNumberFormat(dialStr,
                        getFormatTypeForLocale(Locale.getDefault()));
                            getFormatTypeFromCountryCode(currIso),
                            getFormatTypeFromCountryCode(defaultIso));
                }
            }
        }
        return dialStr;
@@ -1296,26 +1298,35 @@ public class PhoneNumberUtils
     * plus sign, then process the plus sign.
     * Currently, this function supports the plus sign conversion within NANP only.
     * Specifically, it handles the plus sign in the following ways:
     * 1)+NANP or +1NANP,remove +, e.g.
     *   +8475797000 is converted to 8475797000,
     * 1)+1NANP,remove +, e.g.
     *   +18475797000 is converted to 18475797000,
     * 2)+non-NANP Numbers,replace + with the current NANP IDP, e.g,
     * 2)+NANP or +non-NANP Numbers,replace + with the current NANP IDP, e.g,
     *   +8475797000 is converted to 0118475797000,
     *   +11875767800 is converted to 01111875767800
     * 3)+NANP in post dial string(s), e.g.
     *   8475797000;+8475231753 is converted to 8475797000;8475231753
     * 3)+1NANP in post dial string(s), e.g.
     *   8475797000;+18475231753 is converted to 8475797000;18475231753
     *
     * This function returns the original dial string if locale/numbering plan
     * aren't supported.
     *
     * @param dialStr the original dial string
     * @param currFormat the numbering system of the current country that the phone is camped on
     * @param defaultFormat the numbering system of the country that the phone is activated on
     * @return the converted dial string if the current/default countries belong to NANP,
     * and if there is the "+" in the original dial string. Otherwise, the original dial
     * string returns.
     *
     * @hide
     */
    public static String cdmaCheckAndProcessPlusCodeByNumberFormat(String dialStr,int numFormat) {
    public static String
    cdmaCheckAndProcessPlusCodeByNumberFormat(String dialStr,int currFormat,int defaultFormt) {
        String retStr = dialStr;

        // Checks if the plus sign character is in the passed-in dial string
        if (dialStr != null &&
            dialStr.lastIndexOf(PLUS_SIGN_STRING) != -1) {

            // Format the string based on the rules for the country the number is from,
            // and the current country the phone is camped on.
            if ((currFormat == defaultFormt) && (currFormat == FORMAT_NANP)) {
                // Handle case where default and current telephone numbering plans are NANP.
                String postDialStr = null;
                String tempDialStr = dialStr;

@@ -1327,22 +1338,10 @@ public class PhoneNumberUtils
                // applied
                do {
                    String networkDialStr;

                // Format the string based on the rules for the country the number is from
                if (numFormat != FORMAT_NANP) {
                    // TODO: to support NANP international conversion and
                    // other telephone numbering plan
                    // Currently the phone is ever used in non-NANP system
                    // return the original dial string
                    Log.e("checkAndProcessPlusCode:non-NANP not supported", dialStr);
                    return dialStr;
                } else {
                    // For the case that the default and current telephone
                    // numbering plans are NANP
                    networkDialStr = extractNetworkPortion(tempDialStr);
                    // Handles the conversion within NANP
                    networkDialStr = processPlusCodeWithinNanp(networkDialStr);
                }

                    // Concatenates the string that is converted from network portion
                    if (!TextUtils.isEmpty(networkDialStr)) {
                        if (retStr == null) {
@@ -1352,7 +1351,7 @@ public class PhoneNumberUtils
                        }
                    } else {
                        // This should never happen since we checked the if dialStr is null
                    // and if it contains the plus sign in the begining of this function.
                        // and if it contains the plus sign in the beginning of this function.
                        // The plus sign is part of the network portion.
                        Log.e("checkAndProcessPlusCode: null newDialStr", networkDialStr);
                        return dialStr;
@@ -1379,6 +1378,12 @@ public class PhoneNumberUtils
                    }
                    if (DBG) log("checkAndProcessPlusCode,postDialStr=" + postDialStr);
                } while (!TextUtils.isEmpty(postDialStr) && !TextUtils.isEmpty(tempDialStr));
            } else {
                // TODO: Support NANP international conversion and other telephone numbering plans.
                // Currently the phone is never used in non-NANP system, so return the original
                // dial string.
                Log.e("checkAndProcessPlusCode:non-NANP not supported", dialStr);
            }
        }
        return retStr;
     }
@@ -1401,6 +1406,20 @@ public class PhoneNumberUtils
        }
    }

    private static int getFormatTypeFromCountryCode (String country) {
        // Check for the NANP countries
        int length = NANP_COUNTRIES.length;
        for (int i = 0; i < length; i++) {
            if (NANP_COUNTRIES[i].compareToIgnoreCase(country) == 0) {
                return FORMAT_NANP;
            }
        }
        if ("jp".compareToIgnoreCase(country) == 0) {
            return FORMAT_JAPAN;
        }
        return FORMAT_UNKNOWN;
    }

    /**
     * This function checks if the passed in string conforms to the NANP format
     * i.e. NXX-NXX-XXXX, N is any digit 2-9 and X is any digit 0-9
@@ -1446,8 +1465,8 @@ public class PhoneNumberUtils
    /**
     * This function handles the plus code conversion within NANP CDMA network
     * If the number format is
     * 1)+NANP or +1NANP,remove +,
     * 2)+non-NANP Numbers,replace + with the current IDP
     * 1)+1NANP,remove +,
     * 2)other than +1NANP, any + numbers,replace + with the current IDP
     */
    private static String processPlusCodeWithinNanp(String networkDialStr) {
        String retStr = networkDialStr;
@@ -1459,7 +1478,7 @@ public class PhoneNumberUtils
            networkDialStr.charAt(0) == PLUS_SIGN_CHAR &&
            networkDialStr.length() > 1) {
            String newStr = networkDialStr.substring(1);
            if (isNanp(newStr) || isOneNanp(newStr)) {
            if (isOneNanp(newStr)) {
                // Remove the leading plus sign
                retStr = newStr;
             } else {
+23 −20
Original line number Diff line number Diff line
@@ -338,7 +338,7 @@ public class PhoneNumberUtilsTest extends TestCase {

    @SmallTest
    public void testCheckAndProcessPlusCode() {
        assertEquals("8475797000",
        assertEquals("0118475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+8475797000"));
        assertEquals("18475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+18475797000"));
@@ -350,18 +350,18 @@ public class PhoneNumberUtilsTest extends TestCase {
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+11875767800"));
        assertEquals("8475797000,18475231753",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,+18475231753"));
        assertEquals("8475797000,18475231753",
        assertEquals("0118475797000,18475231753",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("+8475797000,+18475231753"));
        assertEquals("8475797000;8475231753",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;+8475231753"));
        assertEquals("8475797000;0118469312345",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;+8469312345"));
        assertEquals("8475797000,0111234567",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,+1234567"));
        assertEquals("847597000;01111875767000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("847597000;+11875767000"));
        assertEquals("8475797000,,8475231753",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,,+8475231753"));
        assertEquals("8475797000;,8475231753",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,+8475231753"));
        assertEquals("8475797000,,0118469312345",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,,+8469312345"));
        assertEquals("8475797000;,0118469312345",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000;,+8469312345"));
        assertEquals("8475797000,;18475231753",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCode("8475797000,;+18475231753"));
        assertEquals("8475797000;,01111875767000",
@@ -391,17 +391,20 @@ public class PhoneNumberUtilsTest extends TestCase {

    @SmallTest
    public void testCheckAndProcessPlusCodeByNumberFormat() {
        assertEquals("+8475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+8475797000",
                PhoneNumberUtils.FORMAT_JAPAN));
        assertEquals("+0384756700",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+0384756700",
                PhoneNumberUtils.FORMAT_JAPAN));
        assertEquals("+1234567",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+1234567",
                PhoneNumberUtils.FORMAT_UNKNOWN));
        assertEquals("+23456700000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+23456700000",
                PhoneNumberUtils.FORMAT_UNKNOWN));
        assertEquals("18475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000",
                PhoneNumberUtils.FORMAT_NANP,PhoneNumberUtils.FORMAT_NANP));
        assertEquals("+18475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000",
                PhoneNumberUtils.FORMAT_NANP,PhoneNumberUtils.FORMAT_JAPAN));
        assertEquals("+18475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000",
                PhoneNumberUtils.FORMAT_NANP,PhoneNumberUtils.FORMAT_UNKNOWN));
        assertEquals("+18475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000",
                PhoneNumberUtils.FORMAT_JAPAN,PhoneNumberUtils.FORMAT_JAPAN));
        assertEquals("+18475797000",
                PhoneNumberUtils.cdmaCheckAndProcessPlusCodeByNumberFormat("+18475797000",
                PhoneNumberUtils.FORMAT_UNKNOWN,PhoneNumberUtils.FORMAT_UNKNOWN));
    }
}