org.whispersystems.signalservice.api.util.PhoneNumberFormatter Java Examples

The following examples show how to use org.whispersystems.signalservice.api.util.PhoneNumberFormatter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NumberViewState.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public String getCountryDisplayName() {
  if (selectedCountryName != null) {
    return selectedCountryName;
  }

  PhoneNumberUtil util = PhoneNumberUtil.getInstance();

  if (isValid()) {
    String actualCountry = getActualCountry(util, getE164Number());

    if (actualCountry != null) {
      return actualCountry;
    }
  }

  String regionCode = util.getRegionCodeForCountryCode(countryCode);
  return PhoneNumberFormatter.getRegionDisplayName(regionCode);
}
 
Example #2
Source File: PhoneNumberFormatterTest.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
  public void testFormatNumberE164() throws Exception, InvalidNumberException {
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "(020) 7946 0018")).isEqualTo(NUMBER_UK);
//    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "044 20 7946 0018")).isEqualTo(NUMBER_UK);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "+442079460018")).isEqualTo(NUMBER_UK);

    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_CH, "+41 44 668 18 00")).isEqualTo(NUMBER_CH);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_CH, "+41 (044) 6681800")).isEqualTo(NUMBER_CH);

    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049 030 123456")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049 (0)30123456")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049((0)30)123456")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "+49 (0) 30  1 2  3 45 6 ")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "030 123456")).isEqualTo(NUMBER_DE);

    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0171123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0171/123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "+490171/123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "00490171/123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049171/123456")).isEqualTo(NUMBER_MOBILE_DE);
  }
 
Example #3
Source File: PhoneNumberFormatterTest.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
public void testFormatNumberE164() throws Exception, InvalidNumberException {
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "(020) 7946 0018")).isEqualTo(NUMBER_UK);
//    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "044 20 7946 0018")).isEqualTo(NUMBER_UK);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "+442079460018")).isEqualTo(NUMBER_UK);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_UK, "+4402079460018")).isEqualTo(NUMBER_UK);

    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_CH, "+41 44 668 18 00")).isEqualTo(NUMBER_CH);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_CH, "+41 (044) 6681800")).isEqualTo(NUMBER_CH);

    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049 030 123456")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049 (0)30123456")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049((0)30)123456")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "+49 (0) 30  1 2  3 45 6 ")).isEqualTo(NUMBER_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "030 123456")).isEqualTo(NUMBER_DE);

    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0171123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0171/123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "+490171/123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "00490171/123456")).isEqualTo(NUMBER_MOBILE_DE);
    assertThat(PhoneNumberFormatter.formatE164(COUNTRY_CODE_DE, "0049171/123456")).isEqualTo(NUMBER_MOBILE_DE);
  }
 
Example #4
Source File: PhoneNumberFormatterTest.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
public void testIsValidNumber() throws Exception {
  assertTrue(PhoneNumberFormatter.isValidNumber("+6831234", "683"));
  assertTrue(PhoneNumberFormatter.isValidNumber("+35851234", "358"));
  assertTrue(PhoneNumberFormatter.isValidNumber("+358512345", "358"));

  assertTrue(PhoneNumberFormatter.isValidNumber("+5521912345678", "55"));
  assertTrue(PhoneNumberFormatter.isValidNumber("+552112345678", "55"));
  assertTrue(PhoneNumberFormatter.isValidNumber("+16105880522", "1"));

  assertFalse(PhoneNumberFormatter.isValidNumber("+014085041212", "0"));
  assertFalse(PhoneNumberFormatter.isValidNumber("+014085041212", "1"));
  assertFalse(PhoneNumberFormatter.isValidNumber("+5512345678", "55"));
  assertFalse(PhoneNumberFormatter.isValidNumber("+161058805220", "1"));
  assertFalse(PhoneNumberFormatter.isValidNumber("+1610588052", "1"));
  assertFalse(PhoneNumberFormatter.isValidNumber("+15880522", "1"));

  assertTrue(PhoneNumberFormatter.isValidNumber("+971812345678901", "971"));
  assertFalse(PhoneNumberFormatter.isValidNumber("+9718123456789012", "971"));
}
 
Example #5
Source File: NumberViewState.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Finds actual name of region from a valid number. So for example +1 might map to US or Canada or other territories.
 */
private static @Nullable String getActualCountry(@NonNull PhoneNumberUtil util, @NonNull String e164Number) {
  try {
    Phonenumber.PhoneNumber phoneNumber = getPhoneNumber(util, e164Number);
    String                  regionCode  = util.getRegionCodeForNumber(phoneNumber);

    if (regionCode != null) {
      return PhoneNumberFormatter.getRegionDisplayName(regionCode);
    }

  } catch (NumberParseException e) {
    return null;
  }
  return null;
}
 
Example #6
Source File: CountryListLoader.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ArrayList<Map<String, String>> loadInBackground() {
  Set<String> regions                    = PhoneNumberUtil.getInstance().getSupportedRegions();
  ArrayList<Map<String, String>> results = new ArrayList<>(regions.size());

  for (String region : regions) {
    Map<String, String> data = new HashMap<>(2);
    data.put("country_name", PhoneNumberFormatter.getRegionDisplayName(region));
    data.put("country_code", "+" +PhoneNumberUtil.getInstance().getCountryCodeForRegion(region));
    results.add(data);
  }

  Collections.sort(results, new RegionComparator());

  return results;
}
 
Example #7
Source File: PhoneNumberFormatterTest.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void testFormatRemoteNumberE164() throws Exception, InvalidNumberException {
  assertThat(PhoneNumberFormatter.formatNumber(LOCAL_NUMBER_US, NUMBER_UK)).isEqualTo(LOCAL_NUMBER_US);
  assertThat(PhoneNumberFormatter.formatNumber(LOCAL_NUMBER_US, LOCAL_NUMBER_US)).isEqualTo(LOCAL_NUMBER_US);

  assertThat(PhoneNumberFormatter.formatNumber(NUMBER_UK, NUMBER_UK)).isEqualTo(NUMBER_UK);
  assertThat(PhoneNumberFormatter.formatNumber(NUMBER_CH, NUMBER_CH)).isEqualTo(NUMBER_CH);
  assertThat(PhoneNumberFormatter.formatNumber(NUMBER_DE, NUMBER_DE)).isEqualTo(NUMBER_DE);
  assertThat(PhoneNumberFormatter.formatNumber(NUMBER_MOBILE_DE, NUMBER_DE)).isEqualTo(NUMBER_MOBILE_DE);

  assertThat(PhoneNumberFormatter.formatNumber("+4402079460018", LOCAL_NUMBER_US)).isEqualTo(NUMBER_UK);
}
 
Example #8
Source File: PhoneNumberFormatterTest.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFormatNumberEmail() throws Exception {
  try {
    PhoneNumberFormatter.formatNumber("[email protected]", LOCAL_NUMBER_US);
    throw new AssertionFailedError("should have thrown on email");
  } catch (InvalidNumberException ine) {
    // success
  }
}
 
Example #9
Source File: PhoneNumberFormatterTest.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public void testFormatNumberEmail() throws Exception {
  try {
    PhoneNumberFormatter.formatNumber("[email protected]", LOCAL_NUMBER_US);
    throw new AssertionFailedError("should have thrown on email");
  } catch (InvalidNumberException ine) {
    // success
  }
}
 
Example #10
Source File: Util.java    From signal-cli with GNU General Public License v3.0 4 votes vote down vote up
public static String canonicalizeNumber(String number, String localNumber) throws InvalidNumberException {
    return PhoneNumberFormatter.formatNumber(number, localNumber);
}
 
Example #11
Source File: Main.java    From signal-cli with GNU General Public License v3.0 4 votes vote down vote up
private static Namespace parseArgs(String[] args) {
    ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
            .build()
            .defaultHelp(true)
            .description("Commandline interface for Signal.")
            .version(BaseConfig.PROJECT_NAME + " " + BaseConfig.PROJECT_VERSION);

    parser.addArgument("-v", "--version")
            .help("Show package version.")
            .action(Arguments.version());
    parser.addArgument("--config")
            .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");

    MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
    mut.addArgument("-u", "--username")
            .help("Specify your phone number, that will be used for verification.");
    mut.addArgument("--dbus")
            .help("Make request via user dbus.")
            .action(Arguments.storeTrue());
    mut.addArgument("--dbus-system")
            .help("Make request via system dbus.")
            .action(Arguments.storeTrue());

    Subparsers subparsers = parser.addSubparsers()
            .title("subcommands")
            .dest("command")
            .description("valid subcommands")
            .help("additional help");

    final Map<String, Command> commands = Commands.getCommands();
    for (Map.Entry<String, Command> entry : commands.entrySet()) {
        Subparser subparser = subparsers.addParser(entry.getKey());
        entry.getValue().attachToSubparser(subparser);
    }

    Namespace ns;
    try {
        ns = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
        parser.handleError(e);
        return null;
    }

    if ("link".equals(ns.getString("command"))) {
        if (ns.getString("username") != null) {
            parser.printUsage();
            System.err.println("You cannot specify a username (phone number) when linking");
            System.exit(2);
        }
    } else if (!ns.getBoolean("dbus") && !ns.getBoolean("dbus_system")) {
        if (ns.getString("username") == null) {
            parser.printUsage();
            System.err.println("You need to specify a username (phone number)");
            System.exit(2);
        }
        if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"), null)) {
            System.err.println("Invalid username (phone number), make sure you include the country code.");
            System.exit(2);
        }
    }
    if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
        System.err.println("You cannot specify recipients by phone number and groups at the same time");
        System.exit(2);
    }
    return ns;
}
 
Example #12
Source File: PhoneNumberFormatterTest.java    From libsignal-service-java with GNU General Public License v3.0 4 votes vote down vote up
public void testFormatNumber() throws Exception, InvalidNumberException {
  assertThat(PhoneNumberFormatter.formatNumber("(555) 555-5555", LOCAL_NUMBER_US)).isEqualTo(LOCAL_NUMBER_US);
  assertThat(PhoneNumberFormatter.formatNumber("555-5555", LOCAL_NUMBER_US)).isEqualTo(LOCAL_NUMBER_US);
  assertThat(PhoneNumberFormatter.formatNumber("(123) 555-5555", LOCAL_NUMBER_US)).isNotEqualTo(LOCAL_NUMBER_US);
}
 
Example #13
Source File: AccountData.java    From signald with GNU General Public License v3.0 4 votes vote down vote up
public void validate() throws InvalidStorageFileException {
    if (!PhoneNumberFormatter.isValidNumber(this.username, null)) {
        throw new InvalidStorageFileException("phone number " + this.username + " is not valid");
    }
}
 
Example #14
Source File: Manager.java    From signald with GNU General Public License v3.0 4 votes vote down vote up
private String canonicalizeNumber(String number) throws InvalidNumberException {
    String localNumber = accountData.username;
    return PhoneNumberFormatter.formatNumber(number, localNumber);
}
 
Example #15
Source File: PhoneNumberFormatterTest.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testFormatRemoteNumberE164() throws Exception, InvalidNumberException {
  assertThat(PhoneNumberFormatter.formatNumber("+4402079460018", LOCAL_NUMBER_US)).isEqualTo(NUMBER_UK);
}
 
Example #16
Source File: PhoneNumberFormatterTest.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testFormatNumber() throws Exception, InvalidNumberException {
  assertThat(PhoneNumberFormatter.formatNumber("(555) 555-5555", LOCAL_NUMBER_US)).isEqualTo(LOCAL_NUMBER_US);
  assertThat(PhoneNumberFormatter.formatNumber("555-5555", LOCAL_NUMBER_US)).isEqualTo(LOCAL_NUMBER_US);
  assertThat(PhoneNumberFormatter.formatNumber("(123) 555-5555", LOCAL_NUMBER_US)).isNotEqualTo(LOCAL_NUMBER_US);
}
 
Example #17
Source File: NumberViewState.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private static String getConfiguredE164Number(int countryCode, long number) {
  return PhoneNumberFormatter.formatE164(String.valueOf(countryCode), String.valueOf(number));
}
 
Example #18
Source File: NumberViewState.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
public boolean isValid() {
  return PhoneNumberFormatter.isValidNumber(getE164Number(), Integer.toString(getCountryCode()));
}