diff options
author | Shaopeng Jia <shaopengjia@google.com> | 2011-11-18 02:49:50 -0800 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2011-11-18 02:49:50 -0800 |
commit | b534ed595ee4012f9b991dc34cdd4ef661fc3c8b (patch) | |
tree | 11d8eb4bcea4c8059ee0ac716d321f1d1227411b /java/src/com/android/i18n/phonenumbers | |
parent | a405a9aba999cd9b2710cb93e2ae3146e328d4ff (diff) | |
parent | a406b5fc8c99b50812e9e1a97cdaa4cb1be505ee (diff) | |
download | libphonenumber-b534ed595ee4012f9b991dc34cdd4ef661fc3c8b.tar.gz |
am a406b5fc: Cherry pick relavant changes to mr0 to fix bug of losing national prefix in formatInOriginalFormat.
* commit 'a406b5fc8c99b50812e9e1a97cdaa4cb1be505ee':
Cherry pick relavant changes to mr0 to fix bug of losing national prefix in formatInOriginalFormat.
Diffstat (limited to 'java/src/com/android/i18n/phonenumbers')
-rw-r--r-- | java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java index e754828d..7c40d18d 100644 --- a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java @@ -1237,7 +1237,11 @@ public class PhoneNumberUtil { * @return the formatted phone number in its original number format */ public String formatInOriginalFormat(PhoneNumber number, String regionCallingFrom) { - if (number.hasRawInput() && !isValidNumber(number)) { + if (number.hasRawInput() && + (!hasFormattingPatternForNumber(number) || !isValidNumber(number))) { + // We check if we have the formatting pattern because without that, we might format the number + // as a group without national prefix. We also want to check the validity of the number + // because we don't want to risk formatting the number if we don't really understand it. return number.getRawInput(); } if (!number.hasCountryCodeSource()) { @@ -1256,6 +1260,18 @@ public class PhoneNumberUtil { } } + private boolean hasFormattingPatternForNumber(PhoneNumber number) { + String phoneNumberRegion = getRegionCodeForCountryCode(number.getCountryCode()); + PhoneMetadata metadata = getMetadataForRegion(phoneNumberRegion); + if (metadata == null) { + return false; + } + String nationalNumber = getNationalSignificantNumber(number); + NumberFormat formatRule = + chooseFormattingPatternForNumber(metadata.numberFormats(), nationalNumber); + return formatRule != null; + } + /** * Formats a phone number for out-of-country dialing purposes. * @@ -1424,6 +1440,22 @@ public class PhoneNumberUtil { return formattedNationalNumber; } + private NumberFormat chooseFormattingPatternForNumber(List<NumberFormat> availableFormats, + String nationalNumber) { + for (NumberFormat numFormat : availableFormats) { + int size = numFormat.leadingDigitsPatternSize(); + if (size == 0 || regexCache.getPatternForRegex( + // We always use the last leading_digits_pattern, as it is the most detailed. + numFormat.getLeadingDigitsPattern(size - 1)).matcher(nationalNumber).lookingAt()) { + Matcher m = regexCache.getPatternForRegex(numFormat.getPattern()).matcher(nationalNumber); + if (m.matches()) { + return numFormat; + } + } + } + return null; + } + // Simple wrapper of formatAccordingToFormats for the common case of no carrier code. private String formatAccordingToFormats(String nationalNumber, List<NumberFormat> availableFormats, |