summaryrefslogtreecommitdiffstats
path: root/telephony
diff options
context:
space:
mode:
authorShaopeng Jia <shaopengjia@google.com>2012-02-02 16:52:26 +0100
committerShaopeng Jia <shaopengjia@google.com>2012-02-06 15:46:10 +0100
commit26cd243601fc05c5ed39c2d1e3ab203a8eb97c25 (patch)
tree89cb552adcb51f93eaeadfd4b3e3ff80ed782205 /telephony
parent26daa02bdb0c62f8162ad8d4d771cabce8644b4a (diff)
downloadframeworks_base-26cd243601fc05c5ed39c2d1e3ab203a8eb97c25.zip
frameworks_base-26cd243601fc05c5ed39c2d1e3ab203a8eb97c25.tar.gz
frameworks_base-26cd243601fc05c5ed39c2d1e3ab203a8eb97c25.tar.bz2
Fix the 3-parameter formatNumber to correctly handle IDDs.
Also added more unittests. Bug: 5726454 Change-Id: I7fd96b47e7b0c34b66a2fe698308342e125ec0f3
Diffstat (limited to 'telephony')
-rw-r--r--telephony/java/android/telephony/PhoneNumberUtils.java11
-rw-r--r--telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java17
2 files changed, 25 insertions, 3 deletions
diff --git a/telephony/java/android/telephony/PhoneNumberUtils.java b/telephony/java/android/telephony/PhoneNumberUtils.java
index 5da3d97..24a4876 100644
--- a/telephony/java/android/telephony/PhoneNumberUtils.java
+++ b/telephony/java/android/telephony/PhoneNumberUtils.java
@@ -1438,7 +1438,8 @@ public class PhoneNumberUtils
* phoneNumber doesn't have the country code.
* @param defaultCountryIso
* the ISO 3166-1 two letters country code whose convention will
- * be used if the phoneNumberE164 is null or invalid.
+ * be used if the phoneNumberE164 is null or invalid, or if phoneNumber
+ * contains IDD.
* @return the formatted number if the given number has been formatted,
* otherwise, return the given number.
*
@@ -1457,9 +1458,13 @@ public class PhoneNumberUtils
if (phoneNumberE164 != null && phoneNumberE164.length() >= 2
&& phoneNumberE164.charAt(0) == '+') {
try {
- PhoneNumber pn = util.parse(phoneNumberE164, defaultCountryIso);
+ // The number to be parsed is in E164 format, so the default region used doesn't
+ // matter.
+ PhoneNumber pn = util.parse(phoneNumberE164, "ZZ");
String regionCode = util.getRegionCodeForNumber(pn);
- if (!TextUtils.isEmpty(regionCode)) {
+ if (!TextUtils.isEmpty(regionCode) &&
+ // This makes sure phoneNumber doesn't contain an IDD
+ normalizeNumber(phoneNumber).indexOf(phoneNumberE164.substring(1)) <= 0) {
defaultCountryIso = regionCode;
}
} catch (NumberParseException e) {
diff --git a/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java b/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java
index 5b76272..9d9680d 100644
--- a/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java
+++ b/telephony/tests/telephonytests/src/com/android/internal/telephony/PhoneNumberUtilsTest.java
@@ -512,6 +512,9 @@ public class PhoneNumberUtilsTest extends AndroidTestCase {
public void testFormatNumber() {
assertEquals("(650) 291-0000", PhoneNumberUtils.formatNumber("650 2910000", "US"));
assertEquals("223-4567", PhoneNumberUtils.formatNumber("2234567", "US"));
+ assertEquals("011 86 10 8888 0000",
+ PhoneNumberUtils.formatNumber("011861088880000", "US"));
+ assertEquals("010 8888 0000", PhoneNumberUtils.formatNumber("01088880000", "CN"));
// formatNumber doesn't format alpha numbers, but keep them as they are.
assertEquals("800-GOOG-114", PhoneNumberUtils.formatNumber("800-GOOG-114", "US"));
}
@@ -542,6 +545,16 @@ public class PhoneNumberUtilsTest extends AndroidTestCase {
// Using the phoneNumberE164's country code
assertEquals("(650) 291-0000",
PhoneNumberUtils.formatNumber("6502910000", "+16502910000", "CN"));
+ // Using the default country code for a phone number containing the IDD
+ assertEquals("011 86 10 8888 0000",
+ PhoneNumberUtils.formatNumber("011861088880000", "+861088880000", "US"));
+ assertEquals("00 86 10 8888 0000",
+ PhoneNumberUtils.formatNumber("00861088880000", "+861088880000", "GB"));
+ assertEquals("+86 10 8888 0000",
+ PhoneNumberUtils.formatNumber("+861088880000", "+861088880000", "GB"));
+ // Wrong default country, so no formatting is done
+ assertEquals("011861088880000",
+ PhoneNumberUtils.formatNumber("011861088880000", "+861088880000", "GB"));
// The phoneNumberE164 is null
assertEquals("(650) 291-0000", PhoneNumberUtils.formatNumber("6502910000", null, "US"));
// The given number has a country code.
@@ -553,7 +566,11 @@ public class PhoneNumberUtilsTest extends AndroidTestCase {
// An invalid Polish number should be left as it is. Note Poland doesn't use '0' as a
// national prefix; therefore, the leading '0' makes the number invalid.
assertEquals("0506128687", PhoneNumberUtils.formatNumber("0506128687", null, "PL"));
+ // Wrong default country, so no formatting is done
+ assertEquals("011861088880000",
+ PhoneNumberUtils.formatNumber("011861088880000", "", "GB"));
}
+
@SmallTest
public void testIsEmergencyNumber() {
// There are two parallel sets of tests here: one for the