Search in sources :

Example 6 with PhoneNumberUtil

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);
    }
}
Also used : PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) Phonenumber(com.google.i18n.phonenumbers.Phonenumber)

Example 7 with PhoneNumberUtil

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;
}
Also used : PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) Phonenumber(com.google.i18n.phonenumbers.Phonenumber) SuppressLint(android.annotation.SuppressLint)

Aggregations

PhoneNumberUtil (com.google.i18n.phonenumbers.PhoneNumberUtil)7 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)4 Phonenumber (com.google.i18n.phonenumbers.Phonenumber)4 SuppressLint (android.annotation.SuppressLint)2 PhoneNumberMatch (com.google.i18n.phonenumbers.PhoneNumberMatch)1 Contact (com.moez.QKSMS.data.Contact)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1