Added new method to format the phone number when neccessary.
Change-Id: I90fb38c1d36dc1ccf7eaa3b4cc34cd29f5151493
This commit is contained in:
@@ -1374,13 +1374,57 @@ public class PhoneNumberUtils
|
||||
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
|
||||
String result = null;
|
||||
try {
|
||||
PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso);
|
||||
PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso);
|
||||
result = util.formatInOriginalFormat(pn, defaultCountryIso);
|
||||
} catch (NumberParseException e) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the phone number only if the given number hasn't been formatted.
|
||||
* <p>
|
||||
* The number which has only dailable character is treated as not being
|
||||
* formatted.
|
||||
*
|
||||
* @param phoneNumber
|
||||
* the number to be formatted.
|
||||
* @param phoneNumberE164
|
||||
* the E164 format number whose country code is used if the given
|
||||
* 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.
|
||||
* @return the formatted number if the given number has been formatted,
|
||||
* otherwise, return the given number.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public static String formatNumber(
|
||||
String phoneNumber, String phoneNumberE164, String defaultCountryIso) {
|
||||
int len = phoneNumber.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (!isDialable(phoneNumber.charAt(i))) {
|
||||
return phoneNumber;
|
||||
}
|
||||
}
|
||||
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
|
||||
// Get the country code from phoneNumberE164
|
||||
if (phoneNumberE164 != null && phoneNumberE164.length() >= 2
|
||||
&& phoneNumberE164.charAt(0) == '+') {
|
||||
try {
|
||||
PhoneNumber pn = util.parse(phoneNumberE164, defaultCountryIso);
|
||||
String regionCode = util.getRegionCodeForNumber(pn);
|
||||
if (!TextUtils.isEmpty(regionCode)) {
|
||||
defaultCountryIso = regionCode;
|
||||
}
|
||||
} catch (NumberParseException e) {
|
||||
}
|
||||
}
|
||||
String result = formatNumber(phoneNumber, defaultCountryIso);
|
||||
return result != null ? result : phoneNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a phone number by removing the characters other than digits. If
|
||||
* the given number has keypad letters, the letters will be converted to
|
||||
|
||||
@@ -20,10 +20,6 @@ import android.test.AndroidTestCase;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.content.Context;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class PhoneNumberUtilsTest extends AndroidTestCase {
|
||||
|
||||
@@ -527,4 +523,16 @@ public class PhoneNumberUtilsTest extends AndroidTestCase {
|
||||
assertEquals("+16502910000", PhoneNumberUtils.normalizeNumber("+1 650 2910000"));
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
public void testFormatDailabeNumber() {
|
||||
// Using the phoneNumberE164's country code
|
||||
assertEquals("(650) 291-0000",
|
||||
PhoneNumberUtils.formatNumber("6502910000", "+16502910000", "CN"));
|
||||
// The phoneNumberE164 is null
|
||||
assertEquals("(650) 291-0000", PhoneNumberUtils.formatNumber("6502910000", null, "US"));
|
||||
// The given number has a country code.
|
||||
assertEquals("+1 650-291-0000", PhoneNumberUtils.formatNumber("+16502910000", null, "CN"));
|
||||
// The given number was formatted.
|
||||
assertEquals("650-291-0000", PhoneNumberUtils.formatNumber("650-291-0000", null, "US"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user