diff options
author | Mattias Nilsson <Mattias.Nilsson@sony.com> | 2022-12-09 11:06:08 +0100 |
---|---|---|
committer | Mattias Nilsson <mattias.nilsson@sony.com> | 2024-01-24 19:18:07 +0000 |
commit | c14418b2d1064fb215dfc4035b40483c8575dde7 (patch) | |
tree | 6252944e81370cde9f554736f5bcefe3e12bd689 | |
parent | 3acc21a7154ded85273f31cf2e17d4d39b98236d (diff) | |
download | ContactsProvider-c14418b2d1064fb215dfc4035b40483c8575dde7.tar.gz |
Make sure to close cursors
ContactsProvider leaks resources when being used.
Test: Manual
Bug: 263295543
Change-Id: I5c7d448c963bc6b32e97b20b33a5bde5959f6197
-rw-r--r-- | src/com/android/providers/contacts/ContactsProvider2.java | 28 | ||||
-rw-r--r-- | src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java | 26 |
2 files changed, 32 insertions, 22 deletions
diff --git a/src/com/android/providers/contacts/ContactsProvider2.java b/src/com/android/providers/contacts/ContactsProvider2.java index 56162916..7b6dce38 100644 --- a/src/com/android/providers/contacts/ContactsProvider2.java +++ b/src/com/android/providers/contacts/ContactsProvider2.java @@ -2663,6 +2663,7 @@ public class ContactsProvider2 extends AbstractContactsProvider final long now = Clock.getInstance().currentTimeMillis(); final SQLiteDatabase db = mDbHelper.get().getWritableDatabase(); db.beginTransactionNonExclusive(); + Cursor cursor = null; try { // First delete any pre-authorization URIs that are no longer valid. Unfortunately, // this operation will grab a write lock for readonly queries. Since this only @@ -2672,15 +2673,16 @@ public class ContactsProvider2 extends AbstractContactsProvider new String[]{String.valueOf(now)}); // Now check to see if the pre-authorized URI map contains the URI. - final Cursor c = db.query(Tables.PRE_AUTHORIZED_URIS, null, + cursor = db.query(Tables.PRE_AUTHORIZED_URIS, null, PreAuthorizedUris.URI + "=?1", new String[]{uri.toString()}, null, null, null); - final boolean isValid = c.getCount() != 0; + final boolean isValid = cursor.getCount() != 0; db.setTransactionSuccessful(); return isValid; } finally { db.endTransaction(); + MoreCloseables.closeQuietly(cursor); } } return false; @@ -7256,7 +7258,7 @@ public class ContactsProvider2 extends AbstractContactsProvider } finally { if (!foundResult) { // We'll be returning a different cursor, so close this one. - cursor.close(); + MoreCloseables.closeQuietly(cursor); } } } @@ -7448,11 +7450,16 @@ public class ContactsProvider2 extends AbstractContactsProvider // This method will return either primary directory or enterprise directory final long inputDirectoryId = ContentUris.parseId(uri); if (Directory.isEnterpriseDirectoryId(inputDirectoryId)) { - final Cursor cursor = queryCorpContactsProvider( - ContentUris.withAppendedId(Directory.CONTENT_URI, - inputDirectoryId - Directory.ENTERPRISE_DIRECTORY_ID_BASE), - projection, selection, selectionArgs, sortOrder, cancellationSignal); - return rewriteCorpDirectories(cursor); + Cursor cursor = null; + try { + cursor = queryCorpContactsProvider( + ContentUris.withAppendedId(Directory.CONTENT_URI, + inputDirectoryId - Directory.ENTERPRISE_DIRECTORY_ID_BASE), + projection, selection, selectionArgs, sortOrder, cancellationSignal); + return rewriteCorpDirectories(cursor); + } finally { + MoreCloseables.closeQuietly(cursor); + } } else { // As it is not an enterprise directory id, fall back to original API final Uri localUri = ContentUris.withAppendedId(Directory.CONTENT_URI, @@ -7947,11 +7954,10 @@ public class ContactsProvider2 extends AbstractContactsProvider lookupQb.appendWhere(contactIdColumn + "=? AND " + lookupKeyColumn + "=?"); Cursor c = doQuery(db, lookupQb, projection, selection, args, sortOrder, groupBy, null, limit, cancellationSignal); - if (c.getCount() != 0) { + if (c != null && c.getCount() != 0) { return c; } - - c.close(); + MoreCloseables.closeQuietly(c); return null; } diff --git a/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java b/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java index ca29edc6..59e213bb 100644 --- a/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java +++ b/src/com/android/providers/contacts/PhoneLookupWithStarPrefix.java @@ -154,7 +154,7 @@ import android.util.Log; * Check each phone number in the given cursor to detemine if it's a match with the given phone * number. Return the matching ones in a new cursor. * @param number phone number to be match - * @param cursor contains a series of number s to be match + * @param cursor contains a series of numbers to be matched * @param defaultCountryIso The lowercase two letter ISO 3166-1 country code. It is recommended * to pass in {@link TelephonyManager#getNetworkCountryIso()}. * @return A new cursor with all matching phone numbers. @@ -166,19 +166,23 @@ import android.util.Log; } final MatrixCursor matrixCursor = new MatrixCursor(cursor.getColumnNames()); - - cursor.moveToPosition(-1); - while (cursor.moveToNext()) { - final int numberIndex = cursor.getColumnIndex(PhoneLookup.NUMBER); - final String numberToMatch = cursor.getString(numberIndex); - if (PhoneNumberUtils.areSamePhoneNumber(number, numberToMatch, defaultCountryIso)) { - final MatrixCursor.RowBuilder b = matrixCursor.newRow(); - for (int column = 0; column < cursor.getColumnCount(); column++) { - b.add(cursor.getColumnName(column), cursorValue(cursor, column)); + try { + cursor.moveToPosition(-1); + while (cursor.moveToNext()) { + final int numberIndex = cursor.getColumnIndex(PhoneLookup.NUMBER); + final String numberToMatch = cursor.getString(numberIndex); + if (PhoneNumberUtils.areSamePhoneNumber(number, numberToMatch, defaultCountryIso)) { + final MatrixCursor.RowBuilder b = matrixCursor.newRow(); + for (int column = 0; column < cursor.getColumnCount(); column++) { + b.add(cursor.getColumnName(column), cursorValue(cursor, column)); + } } } + } finally { + if (cursor != null) { + cursor.close(); + } } - return matrixCursor; } } |