Skip to content

Commit

Permalink
Only use the NATIONAL format for the US and UK.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Nov 5, 2020
1 parent 872f935 commit cfdef7b
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.whispersystems.libsignal.util.Pair;
import org.whispersystems.libsignal.util.guava.Optional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -36,6 +37,11 @@ public class PhoneNumberFormatter {
add("AC");
}};

private static final Set<Integer> NATIONAL_FORMAT_COUNTRY_CODES = new HashSet<>(Arrays.asList(
1, // US
44 // UK
));

private static final Pattern US_NO_AREACODE = Pattern.compile("^(\\d{7})$");
private static final Pattern BR_NO_AREACODE = Pattern.compile("^(9?\\d{8})$");

Expand Down Expand Up @@ -89,7 +95,10 @@ public class PhoneNumberFormatter {
try {
Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(e164, localCountryCode);

if (localNumber.isPresent() && localNumber.get().countryCode == parsedNumber.getCountryCode()) {
if (localNumber.isPresent() &&
localNumber.get().countryCode == parsedNumber.getCountryCode() &&
NATIONAL_FORMAT_COUNTRY_CODES.contains(localNumber.get().getCountryCode()))
{
return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
} else {
return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
Expand Down

0 comments on commit cfdef7b

Please sign in to comment.