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.
diff --git a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
index e754828..7c40d18 100644
--- a/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
+++ b/java/src/com/android/i18n/phonenumbers/PhoneNumberUtil.java
@@ -1237,7 +1237,11 @@
* @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 @@
}
}
+ 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 @@
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,
diff --git a/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
index 480854b..c613977 100644
--- a/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
+++ b/java/test/com/android/i18n/phonenumbers/PhoneNumberUtilTest.java
@@ -716,6 +716,11 @@
PhoneNumber number5 = phoneUtil.parse("+442087654321", RegionCode.GB);
assertEquals("(020) 8765 4321", phoneUtil.formatInOriginalFormat(number5, RegionCode.GB));
+
+ // This number is valid, but we don't have a formatting pattern for it. Fall back to the raw
+ // input.
+ PhoneNumber number8 = phoneUtil.parseAndKeepRawInput("02-4567-8900", RegionCode.KR);
+ assertEquals("02-4567-8900", phoneUtil.formatInOriginalFormat(number8, RegionCode.KR));
}
public void testIsPremiumRate() {