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

Commit ede67ec7 authored by Jay Shrauner's avatar Jay Shrauner
Browse files

Fix cursor leaks

Bug:17472228
Change-Id: Ia34252e1a4e0ba0193c8a13006121972e889d27d
parent 3dc60179
Loading
Loading
Loading
Loading
+27 −22
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.android.common.io.MoreCloseables;
import com.android.contacts.common.database.NoNullCursorAsyncQueryHandler;

/**
@@ -377,6 +378,7 @@ public class SpecialCharSequenceMgr {
         */
        @Override
        protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) {
            try {
                sPreviousAdnQueryHandler = null;
                if (mCanceled) {
                    return;
@@ -406,6 +408,9 @@ public class SpecialCharSequenceMgr {
                    Toast.makeText(context, name, Toast.LENGTH_SHORT)
                        .show();
                }
            } finally {
                MoreCloseables.closeQuietly(c);
            }
        }

        public void cancel() {
+6 −3
Original line number Diff line number Diff line
@@ -1299,11 +1299,14 @@ public class CallLogAdapter extends GroupingListAdapter
                        Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number),
                        PhoneQuery._PROJECTION, null, null, null);
                if (phonesCursor != null) {
                    try {
                        if (phonesCursor.moveToFirst()) {
                            matchingNumber = phonesCursor.getString(PhoneQuery.MATCHED_NUMBER);
                        }
                    } finally {
                        phonesCursor.close();
                    }
                }
            } catch (Exception e) {
                // Use the number from the call log
            }
+46 −43
Original line number Diff line number Diff line
@@ -441,11 +441,12 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {

    public String getProperty(SQLiteDatabase db, String key, String defaultValue) {
        try {
            String value = null;
            final Cursor cursor = db.query(Tables.PROPERTIES,
                    new String[] {PropertiesColumns.PROPERTY_VALUE},
                            PropertiesColumns.PROPERTY_KEY + "=?",
                    new String[] {key}, null, null, null);
            String value = null;
            if (cursor != null) {
                try {
                    if (cursor.moveToFirst()) {
                        value = cursor.getString(0);
@@ -453,6 +454,7 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
                } finally {
                    cursor.close();
                }
            }
            return value != null ? value : defaultValue;
        } catch (SQLiteException e) {
            return defaultValue;
@@ -770,21 +772,21 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
            final Cursor updatedContactCursor = mContext.getContentResolver().query(PhoneQuery.URI,
                    PhoneQuery.PROJECTION, PhoneQuery.SELECTION,
                    new String[]{lastUpdateMillis}, null);
            if (updatedContactCursor == null) {
                if (DEBUG) {
                    Log.e(TAG, "SmartDial query received null for cursor");
                }
                return;
            }

            /** Sets the time after querying the database as the current update time. */
            final Long currentMillis = System.currentTimeMillis();

            try {
                if (DEBUG) {
                    stopWatch.lap("Queried the Contacts database");
                }

            if (updatedContactCursor == null) {
                if (DEBUG) {
                    Log.e(TAG, "SmartDial query received null for cursor");
                }
                return;
            }

                /** Prevents the app from reading the dialer database when updating. */
                sInUpdate.getAndSet(true);

@@ -796,7 +798,6 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
                    stopWatch.lap("Finished deleting deleted entries");
                }

            try {
                /** If the database did not exist before, jump through deletion as there is nothing
                 * to delete.
                 */
@@ -830,12 +831,12 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
                    " WHERE " + SmartDialDbColumns.LAST_SMARTDIAL_UPDATE_TIME +
                    " = " + Long.toString(currentMillis),
                    new String[] {});
            if (nameCursor != null) {
                try {
                    if (DEBUG) {
                        stopWatch.lap("Queried the smart dial table for contact names");
                    }

            if (nameCursor != null) {
                try {
                    /** Inserts prefixes of names into the prefix table.*/
                    insertNamePrefixes(db, nameCursor);
                    if (DEBUG) {
@@ -936,7 +937,10 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
                    " LIKE '" + looseQuery + "')" +
                " ORDER BY " + SmartDialSortingOrder.SORT_ORDER,
                new String[] {currentTimeStamp});

        if (cursor == null) {
            return result;
        }
        try {
            if (DEBUG) {
                stopWatch.lap("Prefix query completed");
            }
@@ -954,7 +958,6 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {

            final Set<ContactMatch> duplicates = new HashSet<ContactMatch>();
            int counter = 0;
        try {
            if (DEBUG) {
                stopWatch.lap("Moved cursor to start");
            }
+25 −22
Original line number Diff line number Diff line
@@ -380,13 +380,17 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener<Cursor> {

    @Override
    public void onLoadComplete(Loader<Cursor> loader, Cursor cursor) {
        if (cursor == null || !isSafeToCommitTransactions()) {
        if (cursor == null) {
            onDismiss();
            return;
        }
        try {
            ArrayList<PhoneItem> phoneList = new ArrayList<PhoneItem>();
            String primaryPhone = null;
        try {
            if (!isSafeToCommitTransactions()) {
                onDismiss();
                return;
            }
            while (cursor.moveToNext()) {
                if (mContactId == UNKNOWN_CONTACT_ID) {
                    mContactId = cursor.getLong(CONTACT_ID);
@@ -408,9 +412,6 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener<Cursor> {

                phoneList.add(item);
            }
        } finally {
            cursor.close();
        }

            if (mUseDefault && primaryPhone != null) {
                performAction(primaryPhone);
@@ -419,7 +420,6 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener<Cursor> {
            }

            Collapser.collapseList(phoneList, mContext);

            if (phoneList.size() == 0) {
                onDismiss();
            } else if (phoneList.size() == 1) {
@@ -430,6 +430,9 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener<Cursor> {
                // There are multiple candidates. Let the user choose one.
                showDisambiguationDialog(phoneList);
            }
        } finally {
            cursor.close();
        }
    }

    private boolean isSafeToCommitTransactions() {