Search in sources :

Example 16 with PhoneNumber

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

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

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

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)
        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 18 with PhoneNumber

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

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

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

the class PhoneNumberUtil method truncateTooLongNumber.

/**
   * Attempts to extract a valid number from a phone number that is too long to be valid, and resets
   * the PhoneNumber object passed in to that valid version. If no valid number could be extracted,
   * the PhoneNumber object passed in will not be modified.
   * @param number a PhoneNumber object which contains a number that is too long to be valid.
   * @return  true if a valid phone number can be successfully extracted.
   */
public boolean truncateTooLongNumber(PhoneNumber number) {
    if (isValidNumber(number)) {
        return true;
    }
    PhoneNumber numberCopy = new PhoneNumber();
    numberCopy.mergeFrom(number);
    long nationalNumber = number.getNationalNumber();
    do {
        nationalNumber /= 10;
        numberCopy.setNationalNumber(nationalNumber);
        if (isPossibleNumberWithReason(numberCopy) == ValidationResult.TOO_SHORT || nationalNumber == 0) {
            return false;
        }
    } while (!isValidNumber(numberCopy));
    number.setNationalNumber(nationalNumber);
    return true;
}
Also used : PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)

Example 20 with PhoneNumber

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

the class PhoneNumberUtil method parseAndKeepRawInput.

/**
   * Parses a string and returns it in proto buffer format. This method differs from {@link #parse}
   * in that it always populates the raw_input field of the protocol buffer with numberToParse as
   * well as the country_code_source field.
   *
   * @param numberToParse     number that we are attempting to parse. This can contain formatting
   *                          such as +, ( and -, as well as a phone number extension.
   * @param defaultRegion     region that we are expecting the number to be from. This is only used
   *                          if the number being parsed is not written in international format.
   *                          The country calling code for the number in this case would be stored
   *                          as that of the default region supplied.
   * @return                  a phone number proto buffer filled with the parsed number
   * @throws NumberParseException  if the string is not considered to be a viable phone number or if
   *                               no default region was supplied
   */
public PhoneNumber parseAndKeepRawInput(String numberToParse, String defaultRegion) throws NumberParseException {
    PhoneNumber phoneNumber = new PhoneNumber();
    parseAndKeepRawInput(numberToParse, defaultRegion, phoneNumber);
    return phoneNumber;
}
Also used : PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)

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