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);
}
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project qksms by moezbhatti.
the class PhoneNumberUtils method formatNumberToE164.
/**
* Format the given phoneNumber to the E.164 representation.
* <p>
* The given phone number must have an area code and could have a country
* code.
* <p>
* The defaultCountryIso is used to validate the given number and generate
* the E.164 phone number if the given number doesn't have a country code.
*
* @param phoneNumber
* the phone number to format
* @param defaultCountryIso
* the ISO 3166-1 two letters country code
* @return the E.164 representation, or null if the given phone number is
* not valid.
*/
@SuppressLint("Override")
public static String formatNumberToE164(String phoneNumber, String defaultCountryIso) {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
String result = null;
try {
Phonenumber.PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso);
if (util.isValidNumber(pn)) {
result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.E164);
}
} catch (NumberParseException e) {
}
return result;
}
Aggregations