Search in sources :

Example 1 with MatchType

use of com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType in project android_packages_apps_Dialer by LineageOS.

the class DialerPhoneNumberUtil method isMatch.

/**
 * Returns true if the two numbers:
 *
 * <ul>
 *   <li>were parseable by libphonenumber (see {@link #parse(String, String)}),
 *   <li>are a {@link MatchType#SHORT_NSN_MATCH}, {@link MatchType#NSN_MATCH}, or {@link
 *       MatchType#EXACT_MATCH}, and
 *   <li>have the same post-dial digits.
 * </ul>
 *
 * <p>If either number is not parseable, returns true if their raw inputs have the same network
 * and post-dial portions.
 *
 * <p>An empty number is never considered to match another number.
 *
 * @see PhoneNumberUtil#isNumberMatch(PhoneNumber, PhoneNumber)
 */
@WorkerThread
public boolean isMatch(@NonNull DialerPhoneNumber firstNumberIn, @NonNull DialerPhoneNumber secondNumberIn) {
    Assert.isWorkerThread();
    // An empty number should not be combined with any other number.
    if (firstNumberIn.getNormalizedNumber().isEmpty() || secondNumberIn.getNormalizedNumber().isEmpty()) {
        return false;
    }
    // Two numbers with different countries should not match.
    if (!firstNumberIn.getCountryIso().equals(secondNumberIn.getCountryIso())) {
        return false;
    }
    PhoneNumber phoneNumber1 = null;
    try {
        phoneNumber1 = phoneNumberUtil.parse(firstNumberIn.getNormalizedNumber(), firstNumberIn.getCountryIso());
    } catch (NumberParseException e) {
    // fall through
    }
    PhoneNumber phoneNumber2 = null;
    try {
        phoneNumber2 = phoneNumberUtil.parse(secondNumberIn.getNormalizedNumber(), secondNumberIn.getCountryIso());
    } catch (NumberParseException e) {
    // fall through
    }
    // fallback to basic textual matching.
    if (isServiceNumber(firstNumberIn.getNormalizedNumber()) || isServiceNumber(secondNumberIn.getNormalizedNumber()) || phoneNumber1 == null || phoneNumber2 == null) {
        return firstNumberIn.getNormalizedNumber().equals(secondNumberIn.getNormalizedNumber());
    }
    // doesn't match "55555" (due to those being a SHORT_NSN_MATCH below).
    if (shortNumberInfo.isPossibleShortNumber(phoneNumber1) || shortNumberInfo.isPossibleShortNumber(phoneNumber2)) {
        return firstNumberIn.getNormalizedNumber().equals(secondNumberIn.getNormalizedNumber());
    }
    // Both numbers are parseable, use more sophisticated libphonenumber matching.
    MatchType matchType = phoneNumberUtil.isNumberMatch(phoneNumber1, phoneNumber2);
    return (matchType == MatchType.SHORT_NSN_MATCH || matchType == MatchType.NSN_MATCH || matchType == MatchType.EXACT_MATCH) && firstNumberIn.getPostDialPortion().equals(secondNumberIn.getPostDialPortion());
}
Also used : MatchType(com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType) DialerPhoneNumber(com.android.dialer.DialerPhoneNumber) PhoneNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) WorkerThread(android.support.annotation.WorkerThread)

Aggregations

WorkerThread (android.support.annotation.WorkerThread)1 DialerPhoneNumber (com.android.dialer.DialerPhoneNumber)1 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)1 MatchType (com.google.i18n.phonenumbers.PhoneNumberUtil.MatchType)1 PhoneNumber (com.google.i18n.phonenumbers.Phonenumber.PhoneNumber)1