Merge "[People Service] Catch possible SQLiteException when querying CP2" into tm-qpr-dev am: d6fac7a487 am: 7348bc84c2

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/20838591

Change-Id: I373810f09374ae3d7c50ffa6d44b6c58924165ed
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Jeff Nainaparampil
2023-01-13 22:41:37 +00:00
committed by Automerger Merge Worker
2 changed files with 23 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ import android.annotation.Nullable;
import android.annotation.WorkerThread;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
@@ -149,6 +150,8 @@ class ContactsQueryHelper {
found = true;
}
} catch (SQLiteException exception) {
Slog.w("SQLite exception when querying contacts.", exception);
}
if (found && lookupKey != null && hasPhoneNumber) {
return queryPhoneNumber(lookupKey);

View File

@@ -25,6 +25,7 @@ import static org.mockito.Mockito.when;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
@@ -63,6 +64,7 @@ public final class ContactsQueryHelperTest {
private MatrixCursor mContactsLookupCursor;
private MatrixCursor mPhoneCursor;
private ContactsQueryHelper mHelper;
private ContactsContentProvider contentProvider;
@Before
public void setUp() {
@@ -73,7 +75,7 @@ public final class ContactsQueryHelperTest {
mPhoneCursor = new MatrixCursor(PHONE_COLUMNS);
MockContentResolver contentResolver = new MockContentResolver();
ContactsContentProvider contentProvider = new ContactsContentProvider();
contentProvider = new ContactsContentProvider();
contentProvider.registerCursor(Contacts.CONTENT_URI, mContactsCursor);
contentProvider.registerCursor(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI, mContactsLookupCursor);
@@ -88,6 +90,14 @@ public final class ContactsQueryHelperTest {
mHelper = new ContactsQueryHelper(mContext);
}
@Test
public void testQueryException_returnsFalse() {
contentProvider.setThrowException(true);
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, CONTACT_LOOKUP_KEY);
assertFalse(mHelper.query(contactUri.toString()));
}
@Test
public void testQueryWithUri() {
mContactsCursor.addRow(new Object[] {
@@ -168,10 +178,15 @@ public final class ContactsQueryHelperTest {
private class ContactsContentProvider extends MockContentProvider {
private Map<Uri, Cursor> mUriPrefixToCursorMap = new ArrayMap<>();
private boolean throwException = false;
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
if (throwException) {
throw new SQLiteException();
}
for (Uri prefixUri : mUriPrefixToCursorMap.keySet()) {
if (uri.isPathPrefixMatch(prefixUri)) {
return mUriPrefixToCursorMap.get(prefixUri);
@@ -180,6 +195,10 @@ public final class ContactsQueryHelperTest {
return mUriPrefixToCursorMap.get(uri);
}
public void setThrowException(boolean throwException) {
this.throwException = throwException;
}
private void registerCursor(Uri uriPrefix, Cursor cursor) {
mUriPrefixToCursorMap.put(uriPrefix, cursor);
}