Return optional for telephone number region name for the unknown case to be localized.

This commit is contained in:
Alan Evans
2020-12-09 15:44:45 -04:00
parent ec8793c6fe
commit a8dd81eace
4 changed files with 23 additions and 8 deletions

View File

@@ -29,7 +29,7 @@ public final class CountryListLoader extends AsyncTaskLoader<ArrayList<Map<Strin
for (String region : regions) { for (String region : regions) {
Map<String, String> data = new HashMap<>(2); Map<String, String> data = new HashMap<>(2);
data.put("country_name", PhoneNumberFormatter.getRegionDisplayName(region)); data.put("country_name", PhoneNumberFormatter.getRegionDisplayNameLegacy(region));
data.put("country_code", "+" +PhoneNumberUtil.getInstance().getCountryCodeForRegion(region)); data.put("country_code", "+" +PhoneNumberUtil.getInstance().getCountryCodeForRegion(region));
results.add(data); results.add(data);
} }

View File

@@ -29,7 +29,7 @@ class DeleteAccountRepository {
} }
@NonNull String getRegionDisplayName(@NonNull String region) { @NonNull String getRegionDisplayName(@NonNull String region) {
return PhoneNumberFormatter.getRegionDisplayName(region); return PhoneNumberFormatter.getRegionDisplayName(region).or("");
} }
int getRegionCountryCode(@NonNull String region) { int getRegionCountryCode(@NonNull String region) {
@@ -73,7 +73,7 @@ class DeleteAccountRepository {
} }
private static @NonNull Country getCountryForRegion(@NonNull String region) { private static @NonNull Country getCountryForRegion(@NonNull String region) {
return new Country(PhoneNumberFormatter.getRegionDisplayName(region), return new Country(PhoneNumberFormatter.getRegionDisplayName(region).or(""),
PhoneNumberUtil.getInstance().getCountryCodeForRegion(region), PhoneNumberUtil.getInstance().getCountryCodeForRegion(region),
region); region);
} }

View File

@@ -58,7 +58,7 @@ public final class NumberViewState implements Parcelable {
} }
String regionCode = util.getRegionCodeForCountryCode(countryCode); String regionCode = util.getRegionCodeForCountryCode(countryCode);
return PhoneNumberFormatter.getRegionDisplayName(regionCode); return PhoneNumberFormatter.getRegionDisplayNameLegacy(regionCode);
} }
/** /**
@@ -70,7 +70,7 @@ public final class NumberViewState implements Parcelable {
String regionCode = util.getRegionCodeForNumber(phoneNumber); String regionCode = util.getRegionCodeForNumber(phoneNumber);
if (regionCode != null) { if (regionCode != null) {
return PhoneNumberFormatter.getRegionDisplayName(regionCode); return PhoneNumberFormatter.getRegionDisplayNameLegacy(regionCode);
} }
} catch (NumberParseException e) { } catch (NumberParseException e) {

View File

@@ -12,6 +12,8 @@ import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import org.whispersystems.libsignal.logging.Log; import org.whispersystems.libsignal.logging.Log;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.internal.util.Util;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -113,9 +115,22 @@ public class PhoneNumberFormatter {
} }
} }
public static String getRegionDisplayName(String regionCode) { /**
return (regionCode == null || regionCode.equals("ZZ") || regionCode.equals(PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY)) * @deprecated Use {@link #getRegionDisplayName} as it can be localized when the region is not found.
? "Unknown country" : new Locale("", regionCode).getDisplayCountry(Locale.getDefault()); */
@Deprecated
public static String getRegionDisplayNameLegacy(String regionCode) {
return getRegionDisplayName(regionCode).or("Unknown country");
}
public static Optional<String> getRegionDisplayName(String regionCode) {
if (regionCode != null && !regionCode.equals("ZZ") && !regionCode.equals(PhoneNumberUtil.REGION_CODE_FOR_NON_GEO_ENTITY)) {
String displayCountry = new Locale("", regionCode).getDisplayCountry(Locale.getDefault());
if (!Util.isEmpty(displayCountry)) {
return Optional.of(displayCountry);
}
}
return Optional.absent();
} }
public static String formatE164(String countryCode, String number) { public static String formatE164(String countryCode, String number) {