Search in sources :

Example 16 with NumberParseException

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

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)

Aggregations

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