Search in sources :

Example 6 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project android_frameworks_base by ParanoidAndroid.

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.
     *
     * @hide
     */
public static String formatNumberToE164(String phoneNumber, String defaultCountryIso) {
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    String result = null;
    try {
        PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso);
        if (util.isValidNumber(pn)) {
            result = util.format(pn, PhoneNumberFormat.E164);
        }
    } catch (NumberParseException e) {
    }
    return result;
}
Also used : PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Example 7 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project XobotOS by xamarin.

the class CallerInfo method getGeoDescription.

/**
     * @return a geographical description string for the specified number.
     * @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder
     */
private static String getGeoDescription(Context context, String number) {
    if (VDBG)
        Log.v(TAG, "getGeoDescription('" + number + "')...");
    if (TextUtils.isEmpty(number)) {
        return null;
    }
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
    Locale locale = context.getResources().getConfiguration().locale;
    String countryIso = getCurrentCountryIso(context, locale);
    PhoneNumber pn = null;
    try {
        if (VDBG)
            Log.v(TAG, "parsing '" + number + "' for countryIso '" + countryIso + "'...");
        pn = util.parse(number, countryIso);
        if (VDBG)
            Log.v(TAG, "- parsed number: " + pn);
    } catch (NumberParseException e) {
        Log.w(TAG, "getGeoDescription: NumberParseException for incoming number '" + number + "'");
    }
    if (pn != null) {
        String description = geocoder.getDescriptionForNumber(pn, locale);
        if (VDBG)
            Log.v(TAG, "- got description: '" + description + "'");
        return description;
    } else {
        return null;
    }
}
Also used : Locale(java.util.Locale) PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumberOfflineGeocoder(com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Example 8 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project android_frameworks_base by ResurrectionRemix.

the class CallerInfo method getGeoDescription.

/**
     * @return a geographical description string for the specified number.
     * @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder
     */
public static String getGeoDescription(Context context, String number) {
    if (VDBG)
        Rlog.v(TAG, "getGeoDescription('" + number + "')...");
    if (TextUtils.isEmpty(number)) {
        return null;
    }
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
    Locale locale = context.getResources().getConfiguration().locale;
    String countryIso = getCurrentCountryIso(context, locale);
    PhoneNumber pn = null;
    try {
        if (VDBG)
            Rlog.v(TAG, "parsing '" + number + "' for countryIso '" + countryIso + "'...");
        pn = util.parse(number, countryIso);
        if (VDBG)
            Rlog.v(TAG, "- parsed number: " + pn);
    } catch (NumberParseException e) {
        Rlog.w(TAG, "getGeoDescription: NumberParseException for incoming number '" + number + "'");
    }
    if (pn != null) {
        String description = geocoder.getDescriptionForNumber(pn, locale);
        if (VDBG)
            Rlog.v(TAG, "- got description: '" + description + "'");
        return description;
    } else {
        return null;
    }
}
Also used : Locale(java.util.Locale) PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumberOfflineGeocoder(com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Example 9 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project XobotOS by xamarin.

the class PhoneNumberUtils method formatNumber.

/**
     * Format a phone number.
     * <p>
     * If the given number doesn't have the country code, the phone will be
     * formatted to the default country's convention.
     *
     * @param phoneNumber
     *            the number to be formatted.
     * @param defaultCountryIso
     *            the ISO 3166-1 two letters country code whose convention will
     *            be used if the given number doesn't have the country code.
     * @return the formatted number, or null if the given number is not valid.
     *
     * @hide
     */
public static String formatNumber(String phoneNumber, String defaultCountryIso) {
    // Do not attempt to format numbers that start with a hash or star symbol.
    if (phoneNumber.startsWith("#") || phoneNumber.startsWith("*")) {
        return phoneNumber;
    }
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    String result = null;
    try {
        PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso);
        result = util.formatInOriginalFormat(pn, defaultCountryIso);
    } catch (NumberParseException e) {
    }
    return result;
}
Also used : PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Example 10 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project android_frameworks_base by ParanoidAndroid.

the class PhoneNumberUtils method formatNumber.

/**
     * Format the phone number only if the given number hasn't been formatted.
     * <p>
     * The number which has only dailable character is treated as not being
     * formatted.
     *
     * @param phoneNumber
     *            the number to be formatted.
     * @param phoneNumberE164
     *            the E164 format number whose country code is used if the given
     *            phoneNumber doesn't have the country code.
     * @param defaultCountryIso
     *            the ISO 3166-1 two letters country code whose convention will
     *            be used if the phoneNumberE164 is null or invalid, or if phoneNumber
     *            contains IDD.
     * @return the formatted number if the given number has been formatted,
     *            otherwise, return the given number.
     *
     * @hide
     */
public static String formatNumber(String phoneNumber, String phoneNumberE164, String defaultCountryIso) {
    int len = phoneNumber.length();
    for (int i = 0; i < len; i++) {
        if (!isDialable(phoneNumber.charAt(i))) {
            return phoneNumber;
        }
    }
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    // Get the country code from phoneNumberE164
    if (phoneNumberE164 != null && phoneNumberE164.length() >= 2 && phoneNumberE164.charAt(0) == '+') {
        try {
            // The number to be parsed is in E164 format, so the default region used doesn't
            // matter.
            PhoneNumber pn = util.parse(phoneNumberE164, "ZZ");
            String regionCode = util.getRegionCodeForNumber(pn);
            if (!TextUtils.isEmpty(regionCode) && // This makes sure phoneNumber doesn't contain an IDD
            normalizeNumber(phoneNumber).indexOf(phoneNumberE164.substring(1)) <= 0) {
                defaultCountryIso = regionCode;
            }
        } catch (NumberParseException e) {
        }
    }
    String result = formatNumber(phoneNumber, defaultCountryIso);
    return result != null ? result : phoneNumber;
}
Also used : PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Aggregations

PhoneNumber (com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)22 NumberParseException (com.android.i18n.phonenumbers.NumberParseException)16 PhoneNumberUtil (com.android.i18n.phonenumbers.PhoneNumberUtil)16 PhoneNumberOfflineGeocoder (com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder)6 Locale (java.util.Locale)6 TtsSpan (android.text.style.TtsSpan)4