Search in sources :

Example 11 with PhoneNumber

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

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.
     */
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);
        if (KOREA_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) && (pn.getCountryCode() == util.getCountryCodeForRegion(KOREA_ISO_COUNTRY_CODE)) && (pn.getCountryCodeSource() == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)) {
            /**
                 * Need to reformat any local Korean phone numbers (when the user is in Korea) with
                 * country code to corresponding national format which would replace the leading
                 * +82 with 0.
                 */
            result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
        } else if (JAPAN_ISO_COUNTRY_CODE.equalsIgnoreCase(defaultCountryIso) && pn.getCountryCode() == util.getCountryCodeForRegion(JAPAN_ISO_COUNTRY_CODE) && (pn.getCountryCodeSource() == PhoneNumber.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)) {
            /**
                 * Need to reformat Japanese phone numbers (when user is in Japan) with the national
                 * dialing format.
                 */
            result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
        } else {
            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 12 with PhoneNumber

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

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 13 with PhoneNumber

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

the class PhoneNumberUtils method createTtsSpan.

/**
     * Create a {@code TtsSpan} for the supplied {@code String}.
     *
     * @param phoneNumberString A {@code String} the entirety of which represents a phone number.
     * @return A {@code TtsSpan} for {@param phoneNumberString}.
     */
public static TtsSpan createTtsSpan(String phoneNumberString) {
    if (phoneNumberString == null) {
        return null;
    }
    // Parse the phone number
    final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    PhoneNumber phoneNumber = null;
    try {
        // Don't supply a defaultRegion so this fails for non-international numbers because
        // we don't want to TalkBalk to read a country code (e.g. +1) if it is not already
        // present
        phoneNumber = phoneNumberUtil.parse(phoneNumberString, /* defaultRegion */
        null);
    } catch (NumberParseException ignored) {
    }
    // Build a telephone tts span
    final TtsSpan.TelephoneBuilder builder = new TtsSpan.TelephoneBuilder();
    if (phoneNumber == null) {
        // Strip separators otherwise TalkBack will be silent
        // (this behavior was observed with TalkBalk 4.0.2 from their alpha channel)
        builder.setNumberParts(splitAtNonNumerics(phoneNumberString));
    } else {
        if (phoneNumber.hasCountryCode()) {
            builder.setCountryCode(Integer.toString(phoneNumber.getCountryCode()));
        }
        builder.setNumberParts(Long.toString(phoneNumber.getNationalNumber()));
    }
    return builder.build();
}
Also used : TtsSpan(android.text.style.TtsSpan) PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Example 14 with PhoneNumber

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

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.
     */
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)

Example 15 with PhoneNumber

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

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)

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