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 c3247c913..32bc68185 100644 --- a/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java +++ b/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoder.java @@ -22,6 +22,7 @@ import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader; +import java.util.List; import java.util.Locale; /** @@ -60,11 +61,27 @@ public class PhoneNumberOfflineGeocoder { /** * 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 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); + } } /** 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 5e7ed0c3f..ed9bd3b03 100644 --- a/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java +++ b/java/geocoder/test/com/google/i18n/phonenumbers/geocoding/PhoneNumberOfflineGeocoderTest.java @@ -51,6 +51,8 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase { 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 = @@ -84,6 +86,13 @@ public class PhoneNumberOfflineGeocoderTest extends TestCase { 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"))); diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC index 97fa27e94..f715e55a8 100644 Binary files a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC and b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CC differ diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX index ad6bff5c3..b6984d8b6 100644 Binary files a/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX and b/java/libphonenumber/test/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting_CX differ diff --git a/resources/PhoneNumberMetadataForTesting.xml b/resources/PhoneNumberMetadataForTesting.xml index 280373d81..ae670caa5 100644 --- a/resources/PhoneNumberMetadataForTesting.xml +++ b/resources/PhoneNumberMetadataForTesting.xml @@ -223,8 +223,14 @@ + 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. --> + + 5\d{3,14} + \d{2,14} + @@ -249,8 +255,14 @@ + 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. --> + + 5\d{3,14} + \d{2,14} +