Java Code Examples for com.google.i18n.phonenumbers.PhoneNumberUtil#getRegionCodeForNumber()

The following examples show how to use com.google.i18n.phonenumbers.PhoneNumberUtil#getRegionCodeForNumber() . 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: ShortCodeUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isShortCode(@NonNull String localNumber, @NonNull String number) {
  try {
    PhoneNumberUtil         util              = PhoneNumberUtil.getInstance();
    Phonenumber.PhoneNumber localNumberObject = util.parse(localNumber, null);
    String                  localCountryCode  = util.getRegionCodeForNumber(localNumberObject);
    String                  bareNumber        = number.replaceAll("[^0-9+]", "");

    // libphonenumber seems incorrect for Russia and a few other countries with 4 digit short codes.
    if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) {
      return true;
    }

    Phonenumber.PhoneNumber shortCode = util.parse(number, localCountryCode);
    return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(shortCode, localCountryCode);
  } catch (NumberParseException e) {
    Log.w(TAG, e);
    return false;
  }
}
 
Example 2
Source File: ShortCodeUtil.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isShortCode(@NonNull String localNumber, @NonNull String number) {
  try {
    PhoneNumberUtil         util              = PhoneNumberUtil.getInstance();
    Phonenumber.PhoneNumber localNumberObject = util.parse(localNumber, null);
    String                  localCountryCode  = util.getRegionCodeForNumber(localNumberObject);
    String                  bareNumber        = number.replaceAll("[^0-9+]", "");

    // libphonenumber doesn't seem to be correct for Germany and Finland
    if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) {
      return true;
    }

    // libphonenumber seems incorrect for Russia and a few other countries with 4 digit short codes.
    if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) {
      return true;
    }

    Phonenumber.PhoneNumber shortCode = util.parse(number, localCountryCode);
    return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(shortCode, localCountryCode);
  } catch (NumberParseException e) {
    Log.w(TAG, e);
    return false;
  }
}
 
Example 3
Source File: Global.java    From Faceless with GNU General Public License v3.0 6 votes vote down vote up
public static String[] normalizeNumber(String phoneNumber, String defaultRegion) {
	PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
	PhoneNumber input;
	try {
		input = phoneUtil.parse(phoneNumber, defaultRegion.trim().toUpperCase(Locale.US));
		if (phoneUtil.isValidNumber(input)) {
			String parsedRegionCode = phoneUtil.getRegionCodeForNumber(input);
			if (parsedRegionCode != null) {
				return new String[] { phoneUtil.format(input, PhoneNumberUtil.PhoneNumberFormat.E164), parsedRegionCode };
			}
			else {
				return null;
			}
		}
		else {
			return null;
		}
	}
	catch (Exception e) {
		return null;
	}
}
 
Example 4
Source File: PhoneUtils.java    From android-phone-number-with-flags with Apache License 2.0 6 votes vote down vote up
private static String parseNumber(String paramString) {
    if (paramString == null) {
        return null;
    }
    PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance();
    String result;
    try {
        Phonenumber.PhoneNumber localPhoneNumber = numberUtil.parse(paramString, null);
        result = numberUtil.getRegionCodeForNumber(localPhoneNumber);
        if (result == null) {
            return null;
        }
    } catch (NumberParseException localNumberParseException) {
        return null;
    }
    return result;
}
 
Example 5
Source File: PhoneNumberFormatter.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static String formatNumber(String number, String localNumber)
      throws InvalidNumberException
  {
    if (number == null) {
      throw new InvalidNumberException("Null String passed as number.");
    }

    if (number.contains("@")) {
      throw new InvalidNumberException("Possible attempt to use email address.");
    }

    number = number.replaceAll("[^0-9+]", "");

    if (number.length() == 0) {
      throw new InvalidNumberException("No valid characters found.");
    }

//    if (number.charAt(0) == '+')
//      return number;

    try {
      PhoneNumberUtil util          = PhoneNumberUtil.getInstance();
      PhoneNumber localNumberObject = util.parse(localNumber, null);

      String localCountryCode       = util.getRegionCodeForNumber(localNumberObject);
      Log.w(TAG, "Got local CC: " + localCountryCode);

      PhoneNumber numberObject      = util.parse(number, localCountryCode);
      return util.format(numberObject, PhoneNumberFormat.E164);
    } catch (NumberParseException e) {
      Log.w(TAG, e);
      return impreciseFormatNumber(number, localNumber);
    }
  }
 
Example 6
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 7
Source File: PhoneNumberFormatter.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
public static String formatNumber(String number, String localNumber)
      throws InvalidNumberException
  {
    if (number == null) {
      throw new InvalidNumberException("Null String passed as number.");
    }

    if (number.contains("@")) {
      throw new InvalidNumberException("Possible attempt to use email address.");
    }

    number = number.replaceAll("[^0-9+]", "");

    if (number.length() == 0) {
      throw new InvalidNumberException("No valid characters found.");
    }

//    if (number.charAt(0) == '+')
//      return number;

    try {
      PhoneNumberUtil util          = PhoneNumberUtil.getInstance();
      PhoneNumber localNumberObject = util.parse(localNumber, null);

      String localCountryCode       = util.getRegionCodeForNumber(localNumberObject);
      Log.w(TAG, "Got local CC: " + localCountryCode);

      PhoneNumber numberObject      = util.parse(number, localCountryCode);
      return util.format(numberObject, PhoneNumberFormat.E164);
    } catch (NumberParseException e) {
      Log.w(TAG, e);
      return impreciseFormatNumber(number, localNumber);
    }
  }
 
Example 8
Source File: PhoneNumberFormatter.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
public static String formatNumber(String number, String localNumber)
    throws InvalidNumberException
{
  if (number.contains("@")) {
    throw new InvalidNumberException("Possible attempt to use email address.");
  }

  number = number.replaceAll("[^0-9+]", "");

  if (number.length() == 0) {
    throw new InvalidNumberException("No valid characters found.");
  }

  if (number.charAt(0) == '+')
    return number;

  try {
    PhoneNumberUtil util          = PhoneNumberUtil.getInstance();
    PhoneNumber localNumberObject = util.parse(localNumber, null);

    String localCountryCode       = util.getRegionCodeForNumber(localNumberObject);
    Log.w(TAG, "Got local CC: " + localCountryCode);

    PhoneNumber numberObject      = util.parse(number, localCountryCode);
    return util.format(numberObject, PhoneNumberFormat.E164);
  } catch (NumberParseException e) {
    Log.w(TAG, e);
    return impreciseFormatNumber(number, localNumber);
  }
}