diff --git a/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java b/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java index 1d074b79f..1a93cc1f2 100644 --- a/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java +++ b/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java @@ -135,10 +135,12 @@ public class PhoneNumberOfflineGeocoder { } /** - * Returns a text description for the given language code for the given phone number. The - * description might consist of the name of the country where the phone number is from and/or the - * name of the geographical area the phone number is from. This method assumes the validity of the - * number passed in has already been checked. + * Returns a text description for the given phone number, in the language provided. The + * description might consist of the name of the country where the phone number is from, or the + * name of the geographical area the phone number is from if more detailed information is + * available. + * + *
This method assumes the validity of the number passed in has already been checked. * * @param number a valid phone number for which we want to get a text description * @param languageCode the language code for which the description should be written @@ -156,10 +158,44 @@ public class PhoneNumberOfflineGeocoder { } /** - * Returns a text description for the given language code for the given phone number. The - * description might consist of the name of the country where the phone number is from and/or the - * name of the geographical area the phone number is from. This method explictly checkes the - * validity of the number passed in. + * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but also considers the + * region of the user. If the phone number is from the same region as the user, only a lower-level + * description will be returned, if one exists. Otherwise, the phone number's region will be + * returned, with optionally some more detailed information. + * + *
For example, for a user from the region "US" (United States), we would show "Mountain View, + * CA" for a particular number, omitting the United States from the description. For a user from + * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United + * States" or even just "United States". + * + *
This method assumes the validity of the number passed in has already been checked.
+ *
+ * @param number the phone number for which we want to get a text description
+ * @param languageCode the language code for which the description should be written
+ * @param userRegion the region code for a given user. This region will be omitted from the
+ * description if the phone number comes from this region. It is a two-letter uppercase ISO
+ * country code as defined by ISO 3166-1.
+ * @return a text description for the given language code for the given phone number, or empty
+ * string if the number passed in is invalid
+ */
+ public String getDescriptionForValidNumber(PhoneNumber number, Locale languageCode,
+ String userRegion) {
+ // If the user region matches the number's region, then we just show the lower-level
+ // description, if one exists - if no description exists, we will show the region(country) name
+ // for the number.
+ String regionCode = phoneUtil.getRegionCodeForNumber(number);
+ if (userRegion.equals(regionCode)) {
+ return getDescriptionForValidNumber(number, languageCode);
+ }
+ // Otherwise, we just show the region(country) name for now.
+ return getCountryNameForNumber(number, languageCode);
+ // TODO: Concatenate the lower-level and country-name information in an appropriate
+ // way for each language.
+ }
+
+ /**
+ * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale)} but explicitly checks
+ * the validity of the number passed in.
*
* @param number the phone number for which we want to get a text description
* @param languageCode the language code for which the description should be written
@@ -173,6 +209,26 @@ public class PhoneNumberOfflineGeocoder {
return getDescriptionForValidNumber(number, languageCode);
}
+ /**
+ * As per {@link #getDescriptionForValidNumber(PhoneNumber, Locale, String)} but
+ * explicitly checks the validity of the number passed in.
+ *
+ * @param number the phone number for which we want to get a text description
+ * @param languageCode the language code for which the description should be written
+ * @param userRegion the region code for a given user. This region will be omitted from the
+ * description if the phone number comes from this region. It is a two-letter uppercase ISO
+ * country code as defined by ISO 3166-1.
+ * @return a text description for the given language code for the given phone number, or empty
+ * string if the number passed in is invalid
+ */
+ public String getDescriptionForNumber(PhoneNumber number, Locale languageCode,
+ String userRegion) {
+ if (!phoneUtil.isValidNumber(number)) {
+ return "";
+ }
+ return getDescriptionForValidNumber(number, languageCode, userRegion);
+ }
+
/**
* Returns an area-level text description in the given language for the given phone number.
*
diff --git a/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java b/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
index e8488e47b..96c9d3d79 100644
--- a/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
+++ b/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java
@@ -116,6 +116,26 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase {
geocoder.getDescriptionForNumber(KO_NUMBER3, Locale.KOREAN));
}
+ public void testGetDescriptionForNumberWithUserRegion() {
+ // User in Italy, American number. We should just show United States, in German, and not more
+ // detailed information.
+ assertEquals("Vereinigte Staaten von Amerika",
+ geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "IT"));
+ // Unknown region - should just show country name.
+ assertEquals("Vereinigte Staaten von Amerika",
+ geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "ZZ"));
+ // User in the States, language German, should show detailed data.
+ assertEquals("Kalifornien",
+ geocoder.getDescriptionForNumber(US_NUMBER1, Locale.GERMAN, "US"));
+ // User in the States, language French, no data for French, so we fallback to English detailed
+ // data.
+ assertEquals("CA",
+ geocoder.getDescriptionForNumber(US_NUMBER1, Locale.FRENCH, "US"));
+ // Invalid number - return an empty string.
+ assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH,
+ "US"));
+ }
+
public void testGetDescriptionForInvalidNumber() {
assertEquals("", geocoder.getDescriptionForNumber(KO_INVALID_NUMBER, Locale.ENGLISH));
assertEquals("", geocoder.getDescriptionForNumber(US_INVALID_NUMBER, Locale.ENGLISH));
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
index 80eae06d0..37aabb672 100644
--- a/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
+++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java
@@ -356,7 +356,7 @@ public class AsYouTypeFormatter {
}
return ableToFormat
? prefixBeforeNationalNumber + tempNationalNumber
- : tempNationalNumber;
+ : accruedInput.toString();
} else {
return attemptToChooseFormattingPattern();
}
@@ -428,8 +428,7 @@ public class AsYouTypeFormatter {
// number (excluding national prefix) have been entered.
if (nationalNumber.length() >= MIN_LEADING_DIGITS_LENGTH) {
getAvailableFormats(nationalNumber.substring(0, MIN_LEADING_DIGITS_LENGTH));
- maybeCreateNewTemplate();
- return inputAccruedNationalNumber();
+ return maybeCreateNewTemplate() ? inputAccruedNationalNumber() : accruedInput.toString();
} else {
return prefixBeforeNationalNumber + nationalNumber.toString();
}
@@ -446,7 +445,7 @@ public class AsYouTypeFormatter {
}
return ableToFormat
? prefixBeforeNationalNumber + tempNationalNumber
- : tempNationalNumber;
+ : accruedInput.toString();
} else {
return prefixBeforeNationalNumber.toString();
}
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java
index 62586f13c..e0892baee 100644
--- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java
+++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java
@@ -294,8 +294,8 @@ final class PhoneNumberMatcher implements Iterator This method first parses the number, then invokes
- * {@link #isPossibleNumber(Phonenumber.PhoneNumber)} with the resultant PhoneNumber object.
+ * This method first parses the number, then invokes {@link #isPossibleNumber(PhoneNumber)}
+ * with the resultant PhoneNumber object.
*
* @param number the number that needs to be checked, in the form of a string
* @param regionDialingFrom the region that we are expecting the number to be dialed from.
@@ -2683,15 +2717,14 @@ public class PhoneNumberUtil {
/**
* Takes two phone numbers as strings and compares them for equality. This is a convenience
- * wrapper for {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)}. No
- * default region is known.
+ * wrapper for {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
*
* @param firstNumber first number to compare. Can contain formatting, and can have country
* calling code specified with + at the start.
* @param secondNumber second number to compare. Can contain formatting, and can have country
* calling code specified with + at the start.
* @return NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
- * {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)} for more details.
+ * {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
*/
public MatchType isNumberMatch(String firstNumber, String secondNumber) {
try {
@@ -2723,14 +2756,13 @@ public class PhoneNumberUtil {
/**
* Takes two phone numbers and compares them for equality. This is a convenience wrapper for
- * {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)}. No default region is
- * known.
+ * {@link #isNumberMatch(PhoneNumber, PhoneNumber)}. No default region is known.
*
* @param firstNumber first number to compare in proto buffer format.
* @param secondNumber second number to compare. Can contain formatting, and can have country
* calling code specified with + at the start.
* @return NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
- * {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)} for more details.
+ * {@link #isNumberMatch(PhoneNumber, PhoneNumber)} for more details.
*/
public MatchType isNumberMatch(PhoneNumber firstNumber, String secondNumber) {
// First see if the second number has an implicit country calling code, by attempting to parse
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR
index cbc064bbe..a1d4d34f5 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN
index b0b740658..468f4d25d 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GN differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP
index 9db7e6e3a..17540452d 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JP differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE
index 68de57a00..a87c7e87d 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KE differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL
index 69d172c06..4d334626f 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG
index e8dfd31d4..cb7d0125e 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SG differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR
index a7260aadf..463390582 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SR differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX
index 50b2362e1..5500274e2 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SX differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH
index 8b56a1f64..7800483a7 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH differ
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK
index 849f1c7d3..48c9c31db 100644
Binary files a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK and b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TK differ
diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java
index 64171ac5b..a4f7244cb 100644
--- a/java/libphonenumber/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java
+++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/AsYouTypeFormatterTest.java
@@ -93,6 +93,7 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("+81 90 1234 5678", formatter.inputDigit('8'));
assertEquals("+81 90 12 345 6789", formatter.inputDigit('9'));
assertEquals("+81901234567890", formatter.inputDigit('0'));
+ assertEquals("+819012345678901", formatter.inputDigit('1'));
}
public void testAYTFUS() {
@@ -716,6 +717,15 @@ public class AsYouTypeFormatterTest extends TestCase {
assertEquals("+81 222 12 567", formatter.inputDigit('7'));
assertEquals("+81 222 12 5678", formatter.inputDigit('8'));
+ // 011113
+ formatter.clear();
+ assertEquals("0", formatter.inputDigit('0'));
+ assertEquals("01", formatter.inputDigit('1'));
+ assertEquals("011", formatter.inputDigit('1'));
+ assertEquals("011 1", formatter.inputDigit('1'));
+ assertEquals("011 11", formatter.inputDigit('1'));
+ assertEquals("011113", formatter.inputDigit('3'));
+
// +81 3332 2 5678
formatter.clear();
assertEquals("+", formatter.inputDigit('+'));
diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java
index c23181d80..9ee4bafc0 100644
--- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java
+++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java
@@ -266,6 +266,13 @@ public class PhoneNumberMatcherTest extends TestCase {
findMatchesInContexts(possibleOnlyContexts, false, true);
}
+ public void testPercentageNotSeenAsPhoneNumber() throws Exception {
+ ArrayList