diff options
author | Grace Jia <xiaotonj@google.com> | 2021-01-15 14:02:53 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-01-15 14:02:53 +0000 |
commit | 653cf4be1a2e56bc22928839c84b723d6a367007 (patch) | |
tree | 1d0e6394891fc59bd12f939cfd0537a78ad1f56e | |
parent | b562da046c06bf565a2676a948b1b63432aa9e19 (diff) | |
parent | 35c681b6ed5d0ca049e4808612148f78dc603d86 (diff) | |
download | ContactsProvider-653cf4be1a2e56bc22928839c84b723d6a367007.tar.gz |
Merge "Use new phone number match method in Java directly as a new filter in ContactsProvider2." am: 03db749ca3 am: 8884eb88a6 am: 35c681b6ed
Original change: https://android-review.googlesource.com/c/platform/packages/providers/ContactsProvider/+/1449515
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: Iaca4012e9d4491259a20710965a815af339df266
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 a87dc971..befafc05 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -123,6 +123,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; @@ -6715,11 +6716,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. @@ -6732,11 +6739,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 0f5b652d..7efc2f47 100644 --- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java +++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java @@ -1678,9 +1678,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"); @@ -1911,13 +1911,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() { @@ -1936,20 +1936,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. @@ -1971,16 +1971,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)); } |