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

Commit d1c6d427 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Prevent from SQLiteException of locked databases

This CL ensures that opened cursors are always closed using
try-with-resources, and also prevents concurrent access to
opened cursors before they are closed using synchronized mehtods.

Bug: 299033978
Test: atest BluetoothInstrumentationTests
Change-Id: I48c04c46745e165142ae9569333b8ec647d3a0c3
parent ae1799cc
Loading
Loading
Loading
Loading
+156 −121
Original line number Diff line number Diff line
@@ -182,19 +182,23 @@ class BluetoothPbapUtils {
        return new VCardComposer(ctx, vType, true);
    }

    public static String getProfileName(Context context) {
        Cursor c = BluetoothMethodProxy.getInstance().contentResolverQuery(
                context.getContentResolver(), Profile.CONTENT_URI,
                new String[]{Profile.DISPLAY_NAME}, null, null, null);
    public static synchronized String getProfileName(Context context) {
        try (Cursor c =
                BluetoothMethodProxy.getInstance()
                        .contentResolverQuery(
                                context.getContentResolver(),
                                Profile.CONTENT_URI,
                                new String[] {Profile.DISPLAY_NAME},
                                null,
                                null,
                                null)) {
            String ownerName = null;
            if (c != null && c.moveToFirst()) {
                ownerName = c.getString(0);
            }
        if (c != null) {
            c.close();
        }
            return ownerName;
        }
    }

    static String createProfileVCard(Context ctx, final int vcardType, final byte[] filter) {
        VCardComposer composer = null;
@@ -271,7 +275,7 @@ class BluetoothPbapUtils {
        handler.sendMessage(handler.obtainMessage(BluetoothPbapService.CONTACTS_LOADED));
    }

    static void updateSecondaryVersionCounter(Context context, Handler handler) {
    static synchronized void updateSecondaryVersionCounter(Context context, Handler handler) {
            /* updatedList stores list of contacts which are added/updated after
             * the time when contacts were last updated. (contactsLastUpdated
             * indicates the time when contact/contacts were last updated and
@@ -280,8 +284,16 @@ class BluetoothPbapUtils {
        HashSet<String> currentContactSet = new HashSet<>();

        String[] projection = {Contacts._ID, Contacts.CONTACT_LAST_UPDATED_TIMESTAMP};
        Cursor c = BluetoothMethodProxy.getInstance().contentResolverQuery(
                context.getContentResolver(), Contacts.CONTENT_URI, projection, null, null, null);
        int currentContactCount = 0;
        try (Cursor c =
                BluetoothMethodProxy.getInstance()
                        .contentResolverQuery(
                                context.getContentResolver(),
                                Contacts.CONTENT_URI,
                                projection,
                                null,
                                null,
                                null)) {

            if (c == null) {
                Log.d(TAG, "Failed to fetch data from contact database");
@@ -295,8 +307,8 @@ class BluetoothPbapUtils {
                }
                currentContactSet.add(contactId);
            }
        int currentContactCount = c.getCount();
        c.close();
            currentContactCount = c.getCount();
        }

        if (V) {
            Log.v(TAG, "updated list =" + updatedList);
@@ -333,9 +345,15 @@ class BluetoothPbapUtils {
            for (String deletedContact : deletedContacts) {
                sContactSet.remove(deletedContact);
                String[] selectionArgs = {deletedContact};
                Cursor dataCursor = BluetoothMethodProxy.getInstance().contentResolverQuery(
                        context.getContentResolver(), Data.CONTENT_URI, dataProjection, whereClause,
                        selectionArgs, null);
                try (Cursor dataCursor =
                        BluetoothMethodProxy.getInstance()
                                .contentResolverQuery(
                                        context.getContentResolver(),
                                        Data.CONTENT_URI,
                                        dataProjection,
                                        whereClause,
                                        selectionArgs,
                                        null)) {

                    if (dataCursor == null) {
                        Log.d(TAG, "Failed to fetch data from contact database");
@@ -349,7 +367,7 @@ class BluetoothPbapUtils {
                        }
                        sTotalFields--;
                    }
                dataCursor.close();
                }
            }

                /* When contacts are updated. i.e. Fields of existing contacts are
@@ -364,9 +382,15 @@ class BluetoothPbapUtils {
                boolean updated = false;

                String[] selectionArgs = {contact};
                Cursor dataCursor = BluetoothMethodProxy.getInstance().contentResolverQuery(
                        context.getContentResolver(), Data.CONTENT_URI, dataProjection, whereClause,
                        selectionArgs, null);
                try (Cursor dataCursor =
                        BluetoothMethodProxy.getInstance()
                                .contentResolverQuery(
                                        context.getContentResolver(),
                                        Data.CONTENT_URI,
                                        dataProjection,
                                        whereClause,
                                        selectionArgs,
                                        null)) {

                    if (dataCursor == null) {
                        Log.d(TAG, "Failed to fetch data from contact database");
@@ -395,8 +419,8 @@ class BluetoothPbapUtils {
                                break;
                        }
                    }
                }
                ContactData cData = new ContactData(nameTmp, phoneTmp, emailTmp, addressTmp);
                dataCursor.close();

                ContactData currentContactData = sContactDataset.get(contact);
                if (currentContactData == null) {
@@ -468,12 +492,23 @@ class BluetoothPbapUtils {
     * isLoad = true indicates its loading all contacts
     * isLoad = false indiacates its caching recently added contact in database*/
    @VisibleForTesting
    static int fetchAndSetContacts(Context context, Handler handler, String[] projection,
            String whereClause, String[] selectionArgs, boolean isLoad) {
    static synchronized int fetchAndSetContacts(
            Context context,
            Handler handler,
            String[] projection,
            String whereClause,
            String[] selectionArgs,
            boolean isLoad) {
        long currentTotalFields = 0, currentSvcFieldCount = 0;
        Cursor c = BluetoothMethodProxy.getInstance().contentResolverQuery(
                context.getContentResolver(), Data.CONTENT_URI, projection, whereClause,
                selectionArgs, null);
        try (Cursor c =
                BluetoothMethodProxy.getInstance()
                        .contentResolverQuery(
                                context.getContentResolver(),
                                Data.CONTENT_URI,
                                projection,
                                whereClause,
                                selectionArgs,
                                null)) {

            /* send delayed message to loadContact when ContentResolver is unable
             * to fetch data from contact database using the specified URI at that
@@ -523,7 +558,7 @@ class BluetoothPbapUtils {
                sContactSet.add(contactId);
                currentTotalFields++;
            }
        c.close();
        }

        /* This code checks if there is any update in contacts after last pbap
         * disconnect has happenned (even if BT is turned OFF during this time)*/