1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.dialer.enrichedcall; 17 18 import android.support.annotation.NonNull; 19 20 /** Utility for comparing phone numbers. */ 21 public class FuzzyPhoneNumberMatcher { 22 23 private static final int REQUIRED_MATCHED_DIGITS = 7; 24 25 /** 26 * Returns {@code true} if the given numbers can be interpreted to be the same. 27 * 28 * <p>This method is called numerous times when rendering the call log. Using string methods is 29 * too slow, so character by character matching is used instead. 30 */ matches(@onNull String lhs, @NonNull String rhs)31 public static boolean matches(@NonNull String lhs, @NonNull String rhs) { 32 int aIndex = lhs.length() - 1; 33 int bIndex = rhs.length() - 1; 34 35 int matchedDigits = 0; 36 37 while (aIndex >= 0 && bIndex >= 0) { 38 if (!Character.isDigit(lhs.charAt(aIndex))) { 39 --aIndex; 40 continue; 41 } 42 if (!Character.isDigit(rhs.charAt(bIndex))) { 43 --bIndex; 44 continue; 45 } 46 if (lhs.charAt(aIndex) != rhs.charAt(bIndex)) { 47 return false; 48 } 49 --aIndex; 50 --bIndex; 51 ++matchedDigits; 52 } 53 54 return matchedDigits >= REQUIRED_MATCHED_DIGITS; 55 } 56 } 57