Search in sources :

Example 21 with PhoneNumber

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

the class PhoneNumberUtil method isNumberMatch.

/**
   * Takes two phone numbers and compares them for equality.
   *
   * <p>Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero for Italian numbers
   * and any extension present are the same.
   * Returns NSN_MATCH if either or both has no region specified, and the NSNs and extensions are
   * the same.
   * Returns SHORT_NSN_MATCH if either or both has no region specified, or the region specified is
   * the same, and one NSN could be a shorter version of the other number. This includes the case
   * where one has an extension specified, and the other does not.
   * Returns NO_MATCH otherwise.
   * For example, the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH.
   * The numbers +1 345 657 1234 and 345 657 are a NO_MATCH.
   *
   * @param firstNumberIn  first number to compare
   * @param secondNumberIn  second number to compare
   *
   * @return  NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH or EXACT_MATCH depending on the level of equality
   *     of the two numbers, described in the method definition.
   */
public MatchType isNumberMatch(PhoneNumber firstNumberIn, PhoneNumber secondNumberIn) {
    // Make copies of the phone number so that the numbers passed in are not edited.
    PhoneNumber firstNumber = new PhoneNumber();
    firstNumber.mergeFrom(firstNumberIn);
    PhoneNumber secondNumber = new PhoneNumber();
    secondNumber.mergeFrom(secondNumberIn);
    // First clear raw_input, country_code_source and preferred_domestic_carrier_code fields and any
    // empty-string extensions so that we can use the proto-buffer equality method.
    firstNumber.clearRawInput();
    firstNumber.clearCountryCodeSource();
    firstNumber.clearPreferredDomesticCarrierCode();
    secondNumber.clearRawInput();
    secondNumber.clearCountryCodeSource();
    secondNumber.clearPreferredDomesticCarrierCode();
    if (firstNumber.hasExtension() && firstNumber.getExtension().length() == 0) {
        firstNumber.clearExtension();
    }
    if (secondNumber.hasExtension() && secondNumber.getExtension().length() == 0) {
        secondNumber.clearExtension();
    }
    // Early exit if both had extensions and these are different.
    if (firstNumber.hasExtension() && secondNumber.hasExtension() && !firstNumber.getExtension().equals(secondNumber.getExtension())) {
        return MatchType.NO_MATCH;
    }
    int firstNumberCountryCode = firstNumber.getCountryCode();
    int secondNumberCountryCode = secondNumber.getCountryCode();
    // Both had country_code specified.
    if (firstNumberCountryCode != 0 && secondNumberCountryCode != 0) {
        if (firstNumber.exactlySameAs(secondNumber)) {
            return MatchType.EXACT_MATCH;
        } else if (firstNumberCountryCode == secondNumberCountryCode && isNationalNumberSuffixOfTheOther(firstNumber, secondNumber)) {
            // shorter variant of the other.
            return MatchType.SHORT_NSN_MATCH;
        }
        // This is not a match.
        return MatchType.NO_MATCH;
    }
    // Checks cases where one or both country_code fields were not specified. To make equality
    // checks easier, we first set the country_code fields to be equal.
    firstNumber.setCountryCode(secondNumberCountryCode);
    // If all else was the same, then this is an NSN_MATCH.
    if (firstNumber.exactlySameAs(secondNumber)) {
        return MatchType.NSN_MATCH;
    }
    if (isNationalNumberSuffixOfTheOther(firstNumber, secondNumber)) {
        return MatchType.SHORT_NSN_MATCH;
    }
    return MatchType.NO_MATCH;
}
Also used : PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)

Example 22 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber 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

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