From 7850cdde66705152b859aafda875833acdda9653 Mon Sep 17 00:00:00 2001 From: Libin Tang Date: Tue, 18 Aug 2009 13:22:47 -0500 Subject: [PATCH] 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. --- .../android/telephony/PhoneNumberUtils.java | 183 ++++++++++-------- .../telephony/PhoneNumberUtilsTest.java | 43 ++-- 2 files changed, 124 insertions(+), 102 deletions(-) diff --git a/telephony/java/android/telephony/PhoneNumberUtils.java b/telephony/java/android/telephony/PhoneNumberUtils.java index 13713e5e0a77a..4368464eea1f4 100644 --- a/telephony/java/android/telephony/PhoneNumberUtils.java +++ b/telephony/java/android/telephony/PhoneNumberUtils.java @@ -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)) { - return cdmaCheckAndProcessPlusCodeByNumberFormat(dialStr, - getFormatTypeForLocale(Locale.getDefault())); + 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, + getFormatTypeFromCountryCode(currIso), + getFormatTypeFromCountryCode(defaultIso)); + } } } return dialStr; @@ -1296,89 +1298,92 @@ 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; - String postDialStr = null; - String tempDialStr = dialStr; - - // Sets the retStr to null since the conversion will be performed below. - retStr = null; - if (DBG) log("checkAndProcessPlusCode,dialStr=" + dialStr); - // This routine is to process the plus sign in the dial string by loop through - // the network portion, post dial portion 1, post dial portion 2... etc. if - // 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 + // Sets the retStr to null since the conversion will be performed below. + retStr = null; + if (DBG) log("checkAndProcessPlusCode,dialStr=" + dialStr); + // This routine is to process the plus sign in the dial string by loop through + // the network portion, post dial portion 1, post dial portion 2... etc. if + // applied + do { + String networkDialStr; 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) { - retStr = networkDialStr; - } else { - retStr = retStr.concat(networkDialStr); - } - } 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. - // The plus sign is part of the network portion. - Log.e("checkAndProcessPlusCode: null newDialStr", networkDialStr); - return dialStr; - } - postDialStr = extractPostDialPortion(tempDialStr); - if (!TextUtils.isEmpty(postDialStr)) { - int dialableIndex = findDialableIndexFromPostDialStr(postDialStr); - // dialableIndex should always be greater than 0 - if (dialableIndex >= 1) { - retStr = appendPwCharBackToOrigDialStr(dialableIndex, - retStr,postDialStr); - // Skips the P/W character, extracts the dialable portion - tempDialStr = postDialStr.substring(dialableIndex); - } else { - // Non-dialable character such as P/W should not be at the end of - // the dial string after P/W processing in CdmaConnection.java - // Set the postDialStr to "" to break out of the loop - if (dialableIndex < 0) { - postDialStr = ""; + // Concatenates the string that is converted from network portion + if (!TextUtils.isEmpty(networkDialStr)) { + if (retStr == null) { + retStr = networkDialStr; + } else { + retStr = retStr.concat(networkDialStr); } - Log.e("wrong postDialStr=", postDialStr); + } else { + // This should never happen since we checked the if dialStr is null + // 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; } - } - if (DBG) log("checkAndProcessPlusCode,postDialStr=" + postDialStr); - } while (!TextUtils.isEmpty(postDialStr) && !TextUtils.isEmpty(tempDialStr)); + postDialStr = extractPostDialPortion(tempDialStr); + if (!TextUtils.isEmpty(postDialStr)) { + int dialableIndex = findDialableIndexFromPostDialStr(postDialStr); + + // dialableIndex should always be greater than 0 + if (dialableIndex >= 1) { + retStr = appendPwCharBackToOrigDialStr(dialableIndex, + retStr,postDialStr); + // Skips the P/W character, extracts the dialable portion + tempDialStr = postDialStr.substring(dialableIndex); + } else { + // Non-dialable character such as P/W should not be at the end of + // the dial string after P/W processing in CdmaConnection.java + // Set the postDialStr to "" to break out of the loop + if (dialableIndex < 0) { + postDialStr = ""; + } + Log.e("wrong postDialStr=", postDialStr); + } + } + 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 { diff --git a/tests/CoreTests/com/android/internal/telephony/PhoneNumberUtilsTest.java b/tests/CoreTests/com/android/internal/telephony/PhoneNumberUtilsTest.java index 466b555e8f888..7426d3316e8d2 100644 --- a/tests/CoreTests/com/android/internal/telephony/PhoneNumberUtilsTest.java +++ b/tests/CoreTests/com/android/internal/telephony/PhoneNumberUtilsTest.java @@ -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)); } }