import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader;
+import java.util.List;
import java.util.Locale;
/**
/**
* Returns the customary display name in the given language for the given territory the phone
- * number is from.
+ * number is from. If it could be from many territories, nothing is returned.
*/
private String getCountryNameForNumber(PhoneNumber number, Locale language) {
- String regionCode = phoneUtil.getRegionCodeForNumber(number);
- return getRegionDisplayName(regionCode, language);
+ List<String> regionCodes =
+ phoneUtil.getRegionCodesForCountryCode(number.getCountryCode());
+ if (regionCodes.size() == 1) {
+ return getRegionDisplayName(regionCodes.get(0), language);
+ } else {
+ String regionWhereNumberIsValid = "ZZ";
+ for (String regionCode : regionCodes) {
+ if (phoneUtil.isValidNumberForRegion(number, regionCode)) {
+ if (!regionWhereNumberIsValid.equals("ZZ")) {
+ // If we can't assign the phone number as definitely belonging to only one territory,
+ // then we return nothing.
+ return "";
+ }
+ regionWhereNumberIsValid = regionCode;
+ }
+ }
+ return getRegionDisplayName(regionWhereNumberIsValid, language);
+ }
}
/**
new PhoneNumber().setCountryCode(1).setNationalNumber(6174240000L);
private static final PhoneNumber US_INVALID_NUMBER =
new PhoneNumber().setCountryCode(1).setNationalNumber(123456789L);
+ private static final PhoneNumber NANPA_TOLL_FREE =
+ new PhoneNumber().setCountryCode(1).setNationalNumber(8002431234L);
private static final PhoneNumber BS_NUMBER1 =
new PhoneNumber().setCountryCode(1).setNationalNumber(2423651234L);
private static final PhoneNumber AU_NUMBER =
geocoder.getDescriptionForNumber(US_NUMBER4, new Locale("en", "US")));
}
+ public void testGetDescriptionForNumberBelongingToMultipleCountriesIsEmpty() {
+ // Test that nothing is returned when the number passed in is valid but not
+ // covered by the geocoding data file and belongs to multiple countries
+ assertEquals("",
+ geocoder.getDescriptionForNumber(NANPA_TOLL_FREE, new Locale("en", "US")));
+ }
+
public void testGetDescriptionForNumber_en_US() {
assertEquals("CA",
geocoder.getDescriptionForNumber(US_NUMBER1, new Locale("en", "US")));
<!-- Cocos Islands -->
<!-- Country calling code shared with Australia. -->
<!-- This country is used to test ShortNumberInfo, so at least the country calling code must be
- recognised by the library. -->
+ recognised by the library. However, we don't want all numbers to be considered valid for
+ it, so we set at least a general description that allows our test geocoding numbers to be
+ valid only for Australia. -->
<territory id="CC" countryCode="61">
+ <generalDesc>
+ <nationalNumberPattern>5\d{3,14}</nationalNumberPattern>
+ <possibleNumberPattern>\d{2,14}</possibleNumberPattern>
+ </generalDesc>
</territory>
<!-- China -->
<!-- Christmas Islands -->
<!-- Country calling code shared with Australia. -->
<!-- This country is used to test ShortNumberInfo, so at least the country calling code must be
- recognised by the library. -->
+ recognised by the library. However, we don't want all numbers to be considered valid for
+ it, so we set at least a general description that allows our test geocoding numbers to be
+ valid only for Australia. -->
<territory id="CX" countryCode="61">
+ <generalDesc>
+ <nationalNumberPattern>5\d{3,14}</nationalNumberPattern>
+ <possibleNumberPattern>\d{2,14}</possibleNumberPattern>
+ </generalDesc>
</territory>
<!-- Germany -->