Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cfab7f4d authored by Jeff Nainaparampil's avatar Jeff Nainaparampil
Browse files

[People Service] Catch possible `SQLiteException` when querying CP2

Following a similar pattern from [ValidateNotificationPeople](http://cs/android-internal/frameworks/base/services/core/java/com/android/server/notification/ValidateNotificationPeople.java;l=500-513;rcl=0741d64d094a3a075146b4e18f10e01cc5c1d8b2), we catch a possible `SQLiteException` when querying CP2 for people data that might occur when CP2 is occurring high write contention (such as during a device locale change).

Bug: 252333206
Change-Id: I65190e2496fc49168048fdd34208c5d55bfb4048
parent fa71876c
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -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);
+20 −1
Original line number Diff line number Diff line
@@ -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);
        }