From ddbdfcd52277f966a0db24993adbe6b651250801 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 12 Apr 2022 08:15:31 +0200 Subject: [PATCH] Close cursors --- .../eu/faircode/email/FragmentCompose.java | 29 ++++---- .../java/eu/faircode/email/FtsDbHelper.java | 9 ++- .../java/eu/faircode/email/WorkerFts.java | 73 ++++++++++--------- 3 files changed, 57 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 3f2b0623aa..5f3629352a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1109,7 +1109,7 @@ public class FragmentCompose extends FragmentBase { boolean contacts = Helper.hasPermission(getContext(), Manifest.permission.READ_CONTACTS); if (contacts) { - Cursor cursor = resolver.query( + try (Cursor cursor = resolver.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ ContactsContract.Contacts.DISPLAY_NAME, @@ -1122,20 +1122,21 @@ public class FragmentCompose extends FragmentBase { " OR LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ") GLOB ?" + " OR " + ContactsContract.CommonDataKinds.Email.DATA + " LIKE ?)", new String[]{wildcard, glob, wildcard}, - null); + null)) { - while (cursor != null && cursor.moveToNext()) { - EntityContact item = new EntityContact(); - item.id = 0L; - item.name = cursor.getString(0); - item.email = cursor.getString(1); - item.avatar = cursor.getString(2); - item.times_contacted = (cursor.getInt(3) == 0 ? 0 : Integer.MAX_VALUE); - item.last_contacted = 0L; - EntityContact existing = map.get(item.email); - if (existing == null || - (existing.avatar == null && item.avatar != null)) - map.put(item.email, item); + while (cursor != null && cursor.moveToNext()) { + EntityContact item = new EntityContact(); + item.id = 0L; + item.name = cursor.getString(0); + item.email = cursor.getString(1); + item.avatar = cursor.getString(2); + item.times_contacted = (cursor.getInt(3) == 0 ? 0 : Integer.MAX_VALUE); + item.last_contacted = 0L; + EntityContact existing = map.get(item.email); + if (existing == null || + (existing.avatar == null && item.avatar != null)) + map.put(item.email, item); + } } } diff --git a/app/src/main/java/eu/faircode/email/FtsDbHelper.java b/app/src/main/java/eu/faircode/email/FtsDbHelper.java index 3e29050948..dddbbc3d26 100644 --- a/app/src/main/java/eu/faircode/email/FtsDbHelper.java +++ b/app/src/main/java/eu/faircode/email/FtsDbHelper.java @@ -124,14 +124,15 @@ public class FtsDbHelper extends SQLiteOpenHelper { static List getSuggestions(SQLiteDatabase db, String query, int max) { List result = new ArrayList<>(); - Cursor cursor = db.query( + try (Cursor cursor = db.query( "SELECT term FROM message_terms" + " WHERE term LIKE ?" + " ORDER BY cnt" + " LIMIT " + max, - new Object[]{query}); - while (cursor != null && cursor.moveToNext()) - result.add(cursor.getString(0)); + new Object[]{query})) { + while (cursor != null && cursor.moveToNext()) + result.add(cursor.getString(0)); + } return result; } diff --git a/app/src/main/java/eu/faircode/email/WorkerFts.java b/app/src/main/java/eu/faircode/email/WorkerFts.java index 6c424d43e1..e52f99549f 100644 --- a/app/src/main/java/eu/faircode/email/WorkerFts.java +++ b/app/src/main/java/eu/faircode/email/WorkerFts.java @@ -67,44 +67,45 @@ public class WorkerFts extends Worker { SQLiteDatabase sdb = FtsDbHelper.getInstance(context); - Cursor cursor = db.message().getMessageFts(); - while (cursor != null && cursor.moveToNext()) - try { - long id = cursor.getLong(0); - Log.i("FTS index=" + id); - - ids.add(id); - - EntityMessage message = db.message().getMessage(id); - if (message == null) { - Log.i("FTS gone"); - continue; - } - - File file = message.getFile(context); - String text = HtmlHelper.getFullText(file); - if (text == null) - text = ""; - - boolean fts = prefs.getBoolean("fts", false); - if (!fts) - break; - + try (Cursor cursor = db.message().getMessageFts()) { + while (cursor != null && cursor.moveToNext()) try { - sdb.beginTransaction(); - FtsDbHelper.insert(sdb, message, text); - sdb.setTransactionSuccessful(); - } finally { - sdb.endTransaction(); + long id = cursor.getLong(0); + Log.i("FTS index=" + id); + + ids.add(id); + + EntityMessage message = db.message().getMessage(id); + if (message == null) { + Log.i("FTS gone"); + continue; + } + + File file = message.getFile(context); + String text = HtmlHelper.getFullText(file); + if (text == null) + text = ""; + + boolean fts = prefs.getBoolean("fts", false); + if (!fts) + break; + + try { + sdb.beginTransaction(); + FtsDbHelper.insert(sdb, message, text); + sdb.setTransactionSuccessful(); + } finally { + sdb.endTransaction(); + } + + indexed++; + + if (ids.size() > INDEX_BATCH_SIZE) + markIndexed(db, ids); + } catch (Throwable ex) { + Log.e(ex); } - - indexed++; - - if (ids.size() > INDEX_BATCH_SIZE) - markIndexed(db, ids); - } catch (Throwable ex) { - Log.e(ex); - } + } markIndexed(db, ids);