Merge "Use new phone number match method in Java directly as a new filter in ContactsProvider2."
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java
index 3a0f7db..aad51c7 100644
--- a/src/com/android/providers/contacts/ContactsProvider2.java
+++ b/src/com/android/providers/contacts/ContactsProvider2.java
@@ -121,6 +121,7 @@
 import com.android.common.content.ProjectionMap;
 import com.android.common.content.SyncStateContentProviderHelper;
 import com.android.common.io.MoreCloseables;
+import com.android.i18n.phonenumbers.Phonenumber;
 import com.android.internal.util.ArrayUtils;
 import com.android.providers.contacts.ContactLookupKey.LookupKeySegment;
 import com.android.providers.contacts.ContactsDatabaseHelper.AccountsColumns;
@@ -6707,11 +6708,17 @@
                     boolean foundResult = false;
                     Cursor cursor = doQuery(db, qb, projectionWithNumber, selection, selectionArgs,
                             sortOrder, groupBy, null, limit, cancellationSignal);
+
                     try {
                         if (cursor.getCount() > 0) {
                             foundResult = true;
-                            return PhoneLookupWithStarPrefix
+                            cursor = PhoneLookupWithStarPrefix
                                     .removeNonStarMatchesFromCursor(number, cursor);
+                            if (!mDbHelper.get().getUseStrictPhoneNumberComparisonForTest()) {
+                                cursor = PhoneLookupWithStarPrefix.removeNoMatchPhoneNumber(number,
+                                        cursor, mDbHelper.get().getCurrentCountryIso());
+                            }
+                            return cursor;
                         }
 
                         // Use the fall-back lookup method.
@@ -6724,11 +6731,13 @@
                         // numbers
                         mDbHelper.get().buildFallbackPhoneLookupAndContactQuery(qb, number);
 
-                        final Cursor fallbackCursor = doQuery(db, qb, projectionWithNumber,
+                        Cursor fallbackCursor = doQuery(db, qb, projectionWithNumber,
                                 selection, selectionArgs, sortOrder, groupBy, having, limit,
                                 cancellationSignal);
-                        return PhoneLookupWithStarPrefix.removeNonStarMatchesFromCursor(
+                        fallbackCursor = PhoneLookupWithStarPrefix.removeNonStarMatchesFromCursor(
                                 number, fallbackCursor);
+                        return PhoneLookupWithStarPrefix.removeNoMatchPhoneNumber(number,
+                                fallbackCursor, mDbHelper.get().getCurrentCountryIso());
                     } finally {
                         if (!foundResult) {
                             // We'll be returning a different cursor, so close this one.
diff --git a/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java b/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java
index 7efc891..ca29edc 100644
--- a/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java
+++ b/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java
@@ -15,6 +15,7 @@
  */
 package com.android.providers.contacts;
 
+import com.android.i18n.phonenumbers.Phonenumber;
 import com.android.internal.annotations.VisibleForTesting;
 
 import android.database.Cursor;
@@ -148,4 +149,36 @@
                 return null;
         }
     }
+
+    /**
+     * Check each phone number in the given cursor to detemine if it's a match with the given phone
+     * number. Return the matching ones in a new cursor.
+     * @param number phone number to be match
+     * @param cursor contains a series of number s to be match
+     * @param defaultCountryIso The lowercase two letter ISO 3166-1 country code. It is recommended
+     *                         to pass in {@link TelephonyManager#getNetworkCountryIso()}.
+     * @return A new cursor with all matching phone numbers.
+     */
+    public static Cursor removeNoMatchPhoneNumber(String number, Cursor cursor,
+            String defaultCountryIso) {
+        if (number == null) {
+            return cursor;
+        }
+
+        final MatrixCursor matrixCursor = new MatrixCursor(cursor.getColumnNames());
+
+        cursor.moveToPosition(-1);
+        while (cursor.moveToNext()) {
+            final int numberIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
+            final String numberToMatch = cursor.getString(numberIndex);
+            if (PhoneNumberUtils.areSamePhoneNumber(number, numberToMatch, defaultCountryIso)) {
+                final MatrixCursor.RowBuilder b = matrixCursor.newRow();
+                for (int column = 0; column < cursor.getColumnCount(); column++) {
+                    b.add(cursor.getColumnName(column), cursorValue(cursor, column));
+                }
+            }
+        }
+
+        return matrixCursor;
+    }
 }
diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
index d5643d2..7860b63 100644
--- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
+++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java
@@ -1685,9 +1685,9 @@
         assertStoredValues(lookupUri1, null, null, new ContentValues[] {values, values});
 
         // In the context that 8004664411 is a valid number, "4664411" as a
-        // call id should  match to both "8004664411" and "+18004664411".
+        // call id should not match to either "8004664411" or "+18004664411".
         Uri lookupUri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "4664411");
-        assertEquals(2, getCount(lookupUri2, null, null));
+        assertEquals(0, getCount(lookupUri2, null, null));
 
         // A wrong area code 799 vs 800 should not be matched
         lookupUri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "7994664411");
@@ -1918,13 +1918,13 @@
 
         values.clear();
 
-        // match with international format
+        // No match with international format
         lookupUri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "+1 650 861 0002");
-        assertEquals(1, getCount(lookupUri2, null, null));
+        assertEquals(0, getCount(lookupUri2, null, null));
 
-        // match with national format
+        // No match with national format
         lookupUri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "650 861 0002");
-        assertEquals(1, getCount(lookupUri2, null, null));
+        assertEquals(0, getCount(lookupUri2, null, null));
     }
 
     public void testIntlPhoneLookupUseCases() {
@@ -1943,20 +1943,20 @@
         assertEquals(2, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, fullNumber), null, null));
 
-        // Shorter (local) number with 0 prefix should also match.
-        assertEquals(2, getCount(Uri.withAppendedPath(
+        // Shorter (local) number with 0 prefix should not match.
+        assertEquals(0, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "097427289"), null, null));
 
         // Number with international (+972) prefix should also match.
         assertEquals(1, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "+97297427289"), null, null));
 
-        // Same shorter number with dashes should match.
-        assertEquals(2, getCount(Uri.withAppendedPath(
+        // Same shorter number with dashes should not match.
+        assertEquals(0, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "09-742-7289"), null, null));
 
-        // Same shorter number with spaces should match.
-        assertEquals(2, getCount(Uri.withAppendedPath(
+        // Same shorter number with spaces should not match.
+        assertEquals(0, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "09 742 7289"), null, null));
 
         // Some other number should not match.
@@ -1978,16 +1978,16 @@
         assertEquals(1, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "0796010101"), null, null));
 
-        assertEquals(1, getCount(Uri.withAppendedPath(
+        assertEquals(0, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "+48796010101"), null, null));
 
-        assertEquals(1, getCount(Uri.withAppendedPath(
+        assertEquals(0, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "48796010101"), null, null));
 
-        assertEquals(1, getCount(Uri.withAppendedPath(
+        assertEquals(0, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "4-879-601-0101"), null, null));
 
-        assertEquals(1, getCount(Uri.withAppendedPath(
+        assertEquals(0, getCount(Uri.withAppendedPath(
                 PhoneLookup.CONTENT_FILTER_URI, "4 879 601 0101"), null, null));
     }