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     return lastSevenDigitsCharacterByCharacterMatches(lhs, rhs);
33   }
34 
35   /**
36    * This strategy examines the numbers character by character starting from the end. If the last
37    * {@link #REQUIRED_MATCHED_DIGITS} match, it returns {@code true}.
38    */
lastSevenDigitsCharacterByCharacterMatches( @onNull String lhs, @NonNull String rhs)39   private static boolean lastSevenDigitsCharacterByCharacterMatches(
40       @NonNull String lhs, @NonNull String rhs) {
41     int lhsIndex = lhs.length() - 1;
42     int rhsIndex = rhs.length() - 1;
43 
44     int matchedDigits = 0;
45 
46     while (lhsIndex >= 0 && rhsIndex >= 0) {
47       if (!Character.isDigit(lhs.charAt(lhsIndex))) {
48         --lhsIndex;
49         continue;
50       }
51       if (!Character.isDigit(rhs.charAt(rhsIndex))) {
52         --rhsIndex;
53         continue;
54       }
55       if (lhs.charAt(lhsIndex) != rhs.charAt(rhsIndex)) {
56         break;
57       }
58       --lhsIndex;
59       --rhsIndex;
60       ++matchedDigits;
61     }
62 
63     return matchedDigits >= REQUIRED_MATCHED_DIGITS;
64   }
65 }
66