diff options
author | Grace Jia <xiaotonj@google.com> | 2021-01-14 22:22:31 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-01-14 22:22:31 +0000 |
commit | 8884eb88a6a7ecd9a8ce184e6a2d9c4bc25f91ba (patch) | |
tree | 24eb9322733491e4fd267c130f3c40c8aec38927 | |
parent | c6566339fd16ffbdee2cb0307cae2a4e263d24cb (diff) | |
parent | 03db749ca3ec3e0c10f69707f0ed2fbe8bb6ad9a (diff) | |
download | ContactsProvider-8884eb88a6a7ecd9a8ce184e6a2d9c4bc25f91ba.tar.gz |
Merge "Use new phone number match method in Java directly as a new filter in ContactsProvider2." am: 03db749ca3
Original change: https://android-review.googlesource.com/c/platform/packages/providers/ContactsProvider/+/1449515
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: Id1e0ea238997818bd628e197e401609a9f052b0b
3 files changed, 61 insertions, 19 deletions
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index 3a0f7db8..aad51c72 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -121,6 +121,7 @@ import android.util.Log; 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 @@ public class ContactsProvider2 extends AbstractContactsProvider 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 @@ public class ContactsProvider2 extends AbstractContactsProvider // 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 7efc8910..ca29edc6 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 @@ import android.util.Log; 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 d5643d25..7860b63d 100644 --- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java +++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java @@ -1685,9 +1685,9 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { 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 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { 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 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { 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 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { 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)); } |