From ede67ec7813fb2c6fcda485adf82f11b55e3562c Mon Sep 17 00:00:00 2001 From: Jay Shrauner Date: Thu, 11 Sep 2014 15:03:36 -0700 Subject: Fix cursor leaks Bug:17472228 Change-Id: Ia34252e1a4e0ba0193c8a13006121972e889d27d --- src/com/android/dialer/SpecialCharSequenceMgr.java | 49 ++++++------ src/com/android/dialer/calllog/CallLogAdapter.java | 9 ++- .../dialer/database/DialerDatabaseHelper.java | 89 +++++++++++----------- .../interactions/PhoneNumberInteraction.java | 47 ++++++------ 4 files changed, 104 insertions(+), 90 deletions(-) (limited to 'src') diff --git a/src/com/android/dialer/SpecialCharSequenceMgr.java b/src/com/android/dialer/SpecialCharSequenceMgr.java index 0b6e88145..37c038399 100644 --- a/src/com/android/dialer/SpecialCharSequenceMgr.java +++ b/src/com/android/dialer/SpecialCharSequenceMgr.java @@ -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,34 +378,38 @@ public class SpecialCharSequenceMgr { */ @Override protected void onNotNullableQueryComplete(int token, Object cookie, Cursor c) { - sPreviousAdnQueryHandler = null; - if (mCanceled) { - return; - } + try { + sPreviousAdnQueryHandler = null; + if (mCanceled) { + return; + } - SimContactQueryCookie sc = (SimContactQueryCookie) cookie; + SimContactQueryCookie sc = (SimContactQueryCookie) cookie; - // close the progress dialog. - sc.progressDialog.dismiss(); + // close the progress dialog. + sc.progressDialog.dismiss(); - // get the EditText to update or see if the request was cancelled. - EditText text = sc.getTextField(); + // get the EditText to update or see if the request was cancelled. + EditText text = sc.getTextField(); - // if the textview is valid, and the cursor is valid and postionable - // on the Nth number, then we update the text field and display a - // toast indicating the caller name. - if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) { - String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME)); - String number = c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME)); + // if the textview is valid, and the cursor is valid and postionable + // on the Nth number, then we update the text field and display a + // toast indicating the caller name. + if ((c != null) && (text != null) && (c.moveToPosition(sc.contactNum))) { + String name = c.getString(c.getColumnIndexOrThrow(ADN_NAME_COLUMN_NAME)); + String number = c.getString(c.getColumnIndexOrThrow(ADN_PHONE_NUMBER_COLUMN_NAME)); - // fill the text in. - text.getText().replace(0, 0, number); + // fill the text in. + text.getText().replace(0, 0, number); - // display the name as a toast - Context context = sc.progressDialog.getContext(); - name = context.getString(R.string.menu_callNumber, name); - Toast.makeText(context, name, Toast.LENGTH_SHORT) - .show(); + // display the name as a toast + Context context = sc.progressDialog.getContext(); + name = context.getString(R.string.menu_callNumber, name); + Toast.makeText(context, name, Toast.LENGTH_SHORT) + .show(); + } + } finally { + MoreCloseables.closeQuietly(c); } } diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java index 921a1c4e8..a90fc556b 100644 --- a/src/com/android/dialer/calllog/CallLogAdapter.java +++ b/src/com/android/dialer/calllog/CallLogAdapter.java @@ -1299,10 +1299,13 @@ public class CallLogAdapter extends GroupingListAdapter Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number), PhoneQuery._PROJECTION, null, null, null); if (phonesCursor != null) { - if (phonesCursor.moveToFirst()) { - matchingNumber = phonesCursor.getString(PhoneQuery.MATCHED_NUMBER); + try { + if (phonesCursor.moveToFirst()) { + matchingNumber = phonesCursor.getString(PhoneQuery.MATCHED_NUMBER); + } + } finally { + phonesCursor.close(); } - phonesCursor.close(); } } catch (Exception e) { // Use the number from the call log diff --git a/src/com/android/dialer/database/DialerDatabaseHelper.java b/src/com/android/dialer/database/DialerDatabaseHelper.java index 95249a6f0..511c2a7bc 100644 --- a/src/com/android/dialer/database/DialerDatabaseHelper.java +++ b/src/com/android/dialer/database/DialerDatabaseHelper.java @@ -441,17 +441,19 @@ 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; - try { - if (cursor.moveToFirst()) { - value = cursor.getString(0); + if (cursor != null) { + try { + if (cursor.moveToFirst()) { + value = cursor.getString(0); + } + } finally { + cursor.close(); } - } finally { - cursor.close(); } return value != null ? value : defaultValue; } catch (SQLiteException e) { @@ -770,14 +772,6 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper { final Cursor updatedContactCursor = mContext.getContentResolver().query(PhoneQuery.URI, PhoneQuery.PROJECTION, PhoneQuery.SELECTION, new String[]{lastUpdateMillis}, null); - - /** Sets the time after querying the database as the current update time. */ - final Long currentMillis = System.currentTimeMillis(); - - if (DEBUG) { - stopWatch.lap("Queried the Contacts database"); - } - if (updatedContactCursor == null) { if (DEBUG) { Log.e(TAG, "SmartDial query received null for cursor"); @@ -785,18 +779,25 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper { return; } - /** Prevents the app from reading the dialer database when updating. */ - sInUpdate.getAndSet(true); + /** Sets the time after querying the database as the current update time. */ + final Long currentMillis = System.currentTimeMillis(); - /** Removes contacts that have been deleted. */ - removeDeletedContacts(db, lastUpdateMillis); - removePotentiallyCorruptedContacts(db, lastUpdateMillis); + try { + if (DEBUG) { + stopWatch.lap("Queried the Contacts database"); + } - if (DEBUG) { - stopWatch.lap("Finished deleting deleted entries"); - } + /** Prevents the app from reading the dialer database when updating. */ + sInUpdate.getAndSet(true); + + /** Removes contacts that have been deleted. */ + removeDeletedContacts(db, lastUpdateMillis); + removePotentiallyCorruptedContacts(db, lastUpdateMillis); + + if (DEBUG) { + 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 (DEBUG) { - stopWatch.lap("Queried the smart dial table for contact names"); - } - if (nameCursor != null) { try { + if (DEBUG) { + stopWatch.lap("Queried the smart dial table for contact names"); + } + /** Inserts prefixes of names into the prefix table.*/ insertNamePrefixes(db, nameCursor); if (DEBUG) { @@ -936,25 +937,27 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper { " LIKE '" + looseQuery + "')" + " ORDER BY " + SmartDialSortingOrder.SORT_ORDER, new String[] {currentTimeStamp}); - - if (DEBUG) { - stopWatch.lap("Prefix query completed"); + if (cursor == null) { + return result; } + try { + if (DEBUG) { + stopWatch.lap("Prefix query completed"); + } - /** Gets the column ID from the cursor.*/ - final int columnDataId = 0; - final int columnDisplayNamePrimary = 1; - final int columnPhotoId = 2; - final int columnNumber = 3; - final int columnId = 4; - final int columnLookupKey = 5; - if (DEBUG) { - stopWatch.lap("Found column IDs"); - } + /** Gets the column ID from the cursor.*/ + final int columnDataId = 0; + final int columnDisplayNamePrimary = 1; + final int columnPhotoId = 2; + final int columnNumber = 3; + final int columnId = 4; + final int columnLookupKey = 5; + if (DEBUG) { + stopWatch.lap("Found column IDs"); + } - final Set duplicates = new HashSet(); - int counter = 0; - try { + final Set duplicates = new HashSet(); + int counter = 0; if (DEBUG) { stopWatch.lap("Moved cursor to start"); } diff --git a/src/com/android/dialer/interactions/PhoneNumberInteraction.java b/src/com/android/dialer/interactions/PhoneNumberInteraction.java index dc35b0611..b61a496c2 100644 --- a/src/com/android/dialer/interactions/PhoneNumberInteraction.java +++ b/src/com/android/dialer/interactions/PhoneNumberInteraction.java @@ -380,13 +380,17 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { @Override public void onLoadComplete(Loader loader, Cursor cursor) { - if (cursor == null || !isSafeToCommitTransactions()) { + if (cursor == null) { onDismiss(); return; } - ArrayList phoneList = new ArrayList(); - String primaryPhone = null; try { + ArrayList phoneList = new ArrayList(); + String primaryPhone = null; + if (!isSafeToCommitTransactions()) { + onDismiss(); + return; + } while (cursor.moveToNext()) { if (mContactId == UNKNOWN_CONTACT_ID) { mContactId = cursor.getLong(CONTACT_ID); @@ -408,27 +412,26 @@ public class PhoneNumberInteraction implements OnLoadCompleteListener { phoneList.add(item); } - } finally { - cursor.close(); - } - - if (mUseDefault && primaryPhone != null) { - performAction(primaryPhone); - onDismiss(); - return; - } - Collapser.collapseList(phoneList, mContext); + if (mUseDefault && primaryPhone != null) { + performAction(primaryPhone); + onDismiss(); + return; + } - if (phoneList.size() == 0) { - onDismiss(); - } else if (phoneList.size() == 1) { - PhoneItem item = phoneList.get(0); - onDismiss(); - performAction(item.phoneNumber); - } else { - // There are multiple candidates. Let the user choose one. - showDisambiguationDialog(phoneList); + Collapser.collapseList(phoneList, mContext); + if (phoneList.size() == 0) { + onDismiss(); + } else if (phoneList.size() == 1) { + PhoneItem item = phoneList.get(0); + onDismiss(); + performAction(item.phoneNumber); + } else { + // There are multiple candidates. Let the user choose one. + showDisambiguationDialog(phoneList); + } + } finally { + cursor.close(); } } -- cgit v1.2.3