use of com.google.i18n.phonenumbers.PhoneNumberUtil in project libsignal-service-java by signalapp.
the class PhoneNumberFormatter method formatNumber.
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.");
}
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);
}
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project Signal-Android by signalapp.
the class DeleteAccountFragment method handleRegionUpdated.
private void handleRegionUpdated(@Nullable String regionCode) {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
countryFormatter = regionCode != null ? util.getAsYouTypeFormatter(regionCode) : null;
reformatText(number.getText());
if (!TextUtils.isEmpty(regionCode) && !"ZZ".equals(regionCode)) {
number.requestFocus();
int numberLength = number.getText().length();
number.getInput().setSelection(numberLength, numberLength);
countryCode.setText(String.valueOf(util.getCountryCodeForRegion(regionCode)));
}
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project Signal-Android by signalapp.
the class CurrencyUtil method getCurrencyByE164.
@Nullable
public static Currency getCurrencyByE164(@NonNull String e164) {
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber number;
try {
number = PhoneNumberUtil.getInstance().parse(e164, "");
} catch (NumberParseException e) {
return null;
}
String regionCodeForNumber = phoneNumberUtil.getRegionCodeForNumber(number);
for (Locale l : Locale.getAvailableLocales()) {
if (l.getCountry().equals(regionCodeForNumber)) {
return getCurrencyByLocale(l);
}
}
return null;
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project Signal-Android by signalapp.
the class NumberViewState method getCountryDisplayName.
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.getRegionDisplayNameLegacy(regionCode);
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project Signal-Android by WhisperSystems.
the class RegistrationActivity method initializeNumber.
private void initializeNumber() {
PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance();
String localNumber = Util.getDeviceE164Number(this);
try {
if (!TextUtils.isEmpty(localNumber)) {
Phonenumber.PhoneNumber localNumberObject = numberUtil.parse(localNumber, null);
if (localNumberObject != null) {
this.countryCode.setText(String.valueOf(localNumberObject.getCountryCode()));
this.number.setText(String.valueOf(localNumberObject.getNationalNumber()));
}
} else {
String simCountryIso = Util.getSimCountryIso(this);
if (!TextUtils.isEmpty(simCountryIso)) {
this.countryCode.setText(numberUtil.getCountryCodeForRegion(simCountryIso) + "");
}
}
} catch (NumberParseException npe) {
Log.w(TAG, npe);
}
}
Aggregations