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
|
||||
|
||||
Reference in New Issue
Block a user