summaryrefslogtreecommitdiff
path: root/java/src/com
diff options
context:
space:
mode:
authorShaopeng Jia <shaopengjia@google.com>2011-11-17 23:58:45 +0100
committerShaopeng Jia <shaopengjia@google.com>2011-11-17 23:58:45 +0100
commita406b5fc8c99b50812e9e1a97cdaa4cb1be505ee (patch)
tree14159381c8a79c446566a1c409be55a55ecb0f5c /java/src/com
parentf3c6df010e7bc8ead25ba577ae09474b70d08adc (diff)
downloadlibphonenumber-a406b5fc8c99b50812e9e1a97cdaa4cb1be505ee.tar.gz
Cherry pick relavant changes to mr0 to fix bug of losing national prefix
in formatInOriginalFormat. Bug: 5520899 Change-Id: I4af2bea37756b7180bcffea86f8cc7fce3a7c767
Diffstat (limited to 'java/src/com')
-rw-r--r--java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java34
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 88f2398f..df8de2ae 100644
--- a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
+++ b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
@@ -1225,7 +1225,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()) {
@@ -1244,6 +1248,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.
*
@@ -1412,6 +1428,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,