From cfae78eba36d4a6af668118445753558c8e4e53c Mon Sep 17 00:00:00 2001 From: Rafael da Veiga Cabral Date: Thu, 21 May 2020 18:00:22 -0300 Subject: [PATCH 1/3] NY9868: [AN]: Use try and finally to protect the cursorFeatures at DbHelper --- .../mobile/communicator/data/db/DbHelper.java | 1338 +++++++++++------ 1 file changed, 837 insertions(+), 501 deletions(-) diff --git a/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java b/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java index e04dc92f74..a98c8f51af 100644 --- a/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java +++ b/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java @@ -182,34 +182,45 @@ public class DbHelper { } public PushNotificationCounter getPush(@NonNull String uniqueId) { - Cursor cursor = mDb.query("SELECT * FROM " + PushTable.TABLE_NAME + " WHERE " + PushTable.Column.UNIQUE_ID + " = '" + uniqueId + "'"); - if (cursor != null) { - PushNotificationCounter p = null; - if (cursor.getCount() != 0) { - cursor.moveToFirst(); - p = PushTable.parseCursor(cursor); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + PushTable.TABLE_NAME + " WHERE " + PushTable.Column.UNIQUE_ID + " = '" + uniqueId + "'"); + if (cursor != null) { + PushNotificationCounter p = null; + if (cursor.getCount() != 0) { + cursor.moveToFirst(); + p = PushTable.parseCursor(cursor); + } + return p; + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); - return p; } return null; } public PushNotificationCounter getPush(@NonNull String uniqueId, @NonNull String type) { - Cursor cursor = mDb.query("SELECT * FROM " + PushTable.TABLE_NAME + " WHERE " + PushTable.Column.UNIQUE_ID + " = '" + uniqueId + "'"); - if (cursor == null || cursor.getCount() == 0) { - if (cursor != null) cursor.close(); - PushNotificationCounter p = new PushNotificationCounter(); - p.count = 1; - p.uniqueId = uniqueId; - p.type = type; - p.id = mDb.insert(PushTable.TABLE_NAME, SQLiteDatabase.CONFLICT_REPLACE, PushTable.toContentValues(p)); - return p; - } else { - cursor.moveToFirst(); - PushNotificationCounter p = PushTable.parseCursor(cursor); - cursor.close(); - return p; + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + PushTable.TABLE_NAME + " WHERE " + PushTable.Column.UNIQUE_ID + " = '" + uniqueId + "'"); + if (cursor == null || cursor.getCount() == 0) { + PushNotificationCounter p = new PushNotificationCounter(); + p.count = 1; + p.uniqueId = uniqueId; + p.type = type; + p.id = mDb.insert(PushTable.TABLE_NAME, SQLiteDatabase.CONFLICT_REPLACE, PushTable.toContentValues(p)); + return p; + } else { + cursor.moveToFirst(); + PushNotificationCounter p = PushTable.parseCursor(cursor); + return p; + } + } finally { + if (cursor != null) { + cursor.close(); + } } } @@ -304,16 +315,22 @@ public class DbHelper { @VisibleForTesting public List getLocalContacts() { List localContacts = new ArrayList<>(); - Cursor cursor = mDb.query("SELECT * FROM " - + LocalContactsTable.TABLE_NAME + " WHERE " - + LocalContactsTable.Column.PHONE_ID + " !='' ORDER BY " - + LocalContactsTable.Column.DISPLAY_NAME + " ASC"); - if (cursor != null) { - while (cursor.moveToNext()) { - LocalContact localContact = LocalContactsTable.parseCursor(cursor); - localContacts.add(localContact); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + + LocalContactsTable.TABLE_NAME + " WHERE " + + LocalContactsTable.Column.PHONE_ID + " !='' ORDER BY " + + LocalContactsTable.Column.DISPLAY_NAME + " ASC"); + if (cursor != null) { + while (cursor.moveToNext()) { + LocalContact localContact = LocalContactsTable.parseCursor(cursor); + localContacts.add(localContact); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return localContacts; } @@ -329,46 +346,64 @@ public class DbHelper { public Completable clearTables() { return Completable.fromAction(() -> { - Cursor cursor = mDb.query("SELECT name FROM sqlite_master WHERE type IS 'table'" + - " AND name NOT IN ('sqlite_master', 'sqlite_sequence', 'android_metadata')"); - if (cursor == null) return; - String[] tables = new String[cursor.getCount()]; - int i = 0; - while (cursor.moveToNext()) { - tables[i++] = cursor.getString(0); - } - cursor.close(); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT name FROM sqlite_master WHERE type IS 'table'" + + " AND name NOT IN ('sqlite_master', 'sqlite_sequence', 'android_metadata')"); + if (cursor == null) return; + String[] tables = new String[cursor.getCount()]; + int i = 0; + while (cursor.moveToNext()) { + tables[i++] = cursor.getString(0); + } - for (String table : tables) { - String dropQuery = "DELETE FROM " + table; - mDb.execute(dropQuery); + for (String table : tables) { + String dropQuery = "DELETE FROM " + table; + mDb.execute(dropQuery); + } + } finally { + if (cursor != null) { + cursor.close(); + } } }); } public AuthModel getAuth() { - Cursor cursor = mDb.query("SELECT * FROM " + AuthTable.TABLE_NAME); + Cursor cursor = null; AuthModel auth = null; - if (cursor != null) { - if (cursor.getCount() > 0) { - cursor.moveToFirst(); - auth = AuthTable.parseCursor(cursor); - auth.settings = getAuthFeatures(auth); + try { + cursor = mDb.query("SELECT * FROM " + AuthTable.TABLE_NAME); + if (cursor != null) { + if (cursor.getCount() > 0) { + cursor.moveToFirst(); + auth = AuthTable.parseCursor(cursor); + auth.settings = getAuthFeatures(auth); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return auth; } public ProfileModel getProfile() { ProfileModel profile = null; - Cursor cursor = mDb.query("SELECT * FROM " + ProfileTable.TABLE_NAME); - if (cursor != null) { - if (cursor.getCount() > 0) { - cursor.moveToFirst(); - profile = ProfileTable.parseCursor(cursor); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ProfileTable.TABLE_NAME); + if (cursor != null) { + if (cursor.getCount() > 0) { + cursor.moveToFirst(); + profile = ProfileTable.parseCursor(cursor); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return profile; } @@ -1173,15 +1208,21 @@ public class DbHelper { public List getAuthFeatures(AuthModel auth) { ArrayList services = new ArrayList<>(); - final Cursor cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_AUTH + "' AND " + - FeatureTable.Column.TARGET_ID + " = '" + auth.devKey + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - final FeatureModel feature = FeatureTable.parseCursor(cursor); - services.add(feature); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_AUTH + "' AND " + + FeatureTable.Column.TARGET_ID + " = '" + auth.devKey + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + final FeatureModel feature = FeatureTable.parseCursor(cursor); + services.add(feature); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return services; } @@ -1189,15 +1230,21 @@ public class DbHelper { private List getContactFeatures(String ids) { ArrayList services = new ArrayList<>(); if (StringUtils.isNotEmpty(ids)) { - final Cursor cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_CONTACT + "' AND " + - FeatureTable.Column.TARGET_ID + " IN (" + ids + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final FeatureModel feature = FeatureTable.parseCursor(cursor); - services.add(feature); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_CONTACT + "' AND " + + FeatureTable.Column.TARGET_ID + " IN (" + ids + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final FeatureModel feature = FeatureTable.parseCursor(cursor); + services.add(feature); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return services; @@ -1206,15 +1253,21 @@ public class DbHelper { private List getJobFeatures(String ids) { List services = new ArrayList<>(); if (StringUtils.isNotEmpty(ids)) { - final Cursor cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_JOB + "' AND " + - FeatureTable.Column.TARGET_ID + " IN (" + ids + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final FeatureModel feature = FeatureTable.parseCursor(cursor); - services.add(feature); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_JOB + "' AND " + + FeatureTable.Column.TARGET_ID + " IN (" + ids + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final FeatureModel feature = FeatureTable.parseCursor(cursor); + services.add(feature); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return services; @@ -1223,14 +1276,20 @@ public class DbHelper { private List getContactsByIDs(String ids) { List contacts = new ArrayList<>(); if (StringUtils.isNotEmpty(ids)) { - final Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + - ContactsTable.Column.PHONE_ID + " IN (" + ids + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final ContactModel feature = ContactsTable.parseCursor(cursor); - contacts.add(feature); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + + ContactsTable.Column.PHONE_ID + " IN (" + ids + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final ContactModel feature = ContactsTable.parseCursor(cursor); + contacts.add(feature); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return contacts; @@ -1259,32 +1318,44 @@ public class DbHelper { } private void getMemberFeatures(MemberModel member) { - final Cursor cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE (" + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_MEMBER + "' OR " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_CONTACT + "')" + - " AND " + - "(" + FeatureTable.Column.TARGET_ID + " = '" + member.id + "' OR " + - FeatureTable.Column.TARGET_ID + " = '" + member.phone_id + "')" ); - if (cursor != null) { - while (cursor.moveToNext()) { - member.settings.add(FeatureTable.parseCursor(cursor)); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE (" + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_MEMBER + "' OR " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_CONTACT + "')" + + " AND " + + "(" + FeatureTable.Column.TARGET_ID + " = '" + member.id + "' OR " + + FeatureTable.Column.TARGET_ID + " = '" + member.phone_id + "')" ); + if (cursor != null) { + while (cursor.moveToNext()) { + member.settings.add(FeatureTable.parseCursor(cursor)); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } private void setMemberServices(MemberModel member) { ArrayList services = new ArrayList<>(); if (member != null) { - final Cursor cursor = mDb.query("SELECT * FROM " + ServiceTable.TABLE_NAME + " WHERE " + - ServiceTable.Column.TARGET_TYPE + " = '" + ServiceTable.TYPE_MEMBER + "' AND " + - ServiceTable.Column.TARGET_ID + " = '" + member.id + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - final ServiceModel service = ServiceTable.parseCursor(cursor); - member.services.add(ServiceTable.parseCursor(cursor)); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ServiceTable.TABLE_NAME + " WHERE " + + ServiceTable.Column.TARGET_TYPE + " = '" + ServiceTable.TYPE_MEMBER + "' AND " + + ServiceTable.Column.TARGET_ID + " = '" + member.id + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + final ServiceModel service = ServiceTable.parseCursor(cursor); + member.services.add(ServiceTable.parseCursor(cursor)); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } } @@ -1292,17 +1363,23 @@ public class DbHelper { private List getMemberFeatures(String ids) { ArrayList services = new ArrayList<>(); if (StringUtils.isNotEmpty(ids)) { - final Cursor cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE (" + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_MEMBER + "' OR " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_CONTACT + "')" + - " AND " + - FeatureTable.Column.TARGET_ID + " IN ( " + ids + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final FeatureModel feature = FeatureTable.parseCursor(cursor); - services.add(feature); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE (" + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_MEMBER + "' OR " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_CONTACT + "')" + + " AND " + + FeatureTable.Column.TARGET_ID + " IN ( " + ids + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final FeatureModel feature = FeatureTable.parseCursor(cursor); + services.add(feature); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return services; @@ -1328,15 +1405,21 @@ public class DbHelper { private List getServices(String ids, int type) { ArrayList services = new ArrayList<>(); if (StringUtils.isNotEmpty(ids)) { - final Cursor cursor = mDb.query("SELECT * FROM " + ServiceTable.TABLE_NAME + " WHERE " + - ServiceTable.Column.TARGET_TYPE + " = '" + type + "' AND " + - ServiceTable.Column.TARGET_ID + " IN ( " + ids + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final ServiceModel service = ServiceTable.parseCursor(cursor); - services.add(service); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ServiceTable.TABLE_NAME + " WHERE " + + ServiceTable.Column.TARGET_TYPE + " = '" + type + "' AND " + + ServiceTable.Column.TARGET_ID + " IN ( " + ids + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final ServiceModel service = ServiceTable.parseCursor(cursor); + services.add(service); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return services; @@ -1345,16 +1428,22 @@ public class DbHelper { public ContactModel getContactsByNickname(String nickname) { ContactModel contact = null; if (StringUtils.isNotEmpty(nickname)) { - final Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE LOWER(" + - ContactsTable.Column.NICK + ") = '" + nickname.toLowerCase() + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - contact = ContactsTable.parseCursor(cursor); - break; + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE LOWER(" + + ContactsTable.Column.NICK + ") = '" + nickname.toLowerCase() + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + contact = ContactsTable.parseCursor(cursor); + break; + } + if (contact != null) { + setContactData(contact); + } } - cursor.close(); - if (contact != null) { - setContactData(contact); + } finally { + if (cursor != null) { + cursor.close(); } } } @@ -1365,16 +1454,22 @@ public class DbHelper { ContactModel contact = null; if (StringUtils.isNotEmpty(phoneNumber)) { String phone = phoneNumber + "_%"; - final Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + - ContactsTable.Column.PHONE_ID + " LIKE '" + phone + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - contact = ContactsTable.parseCursor(cursor); - break; + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + + ContactsTable.Column.PHONE_ID + " LIKE '" + phone + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + contact = ContactsTable.parseCursor(cursor); + break; + } + if (contact != null) { + setContactData(contact); + } } - cursor.close(); - if (contact != null) { - setContactData(contact); + } finally { + if (cursor != null) { + cursor.close(); } } } @@ -1389,21 +1484,27 @@ public class DbHelper { public ContactModel getContactByPhoneId(String phoneId, String activeRosterId) { ContactModel contact = null; if (StringUtils.isNotEmpty(phoneId)) { - final Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + - ContactsTable.Column.PHONE_ID + " = '" + phoneId + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - contact = ContactsTable.parseCursor(cursor); - break; - } - cursor.close(); - if (contact != null) { - setContactData(contact); - if (activeRosterId != null) { - ArrayList cs = new ArrayList<>(Arrays.asList(contact)); - loadContactsMessagesByRosterId(cs, activeRosterId); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + + ContactsTable.Column.PHONE_ID + " = '" + phoneId + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + contact = ContactsTable.parseCursor(cursor); + break; + } + if (contact != null) { + setContactData(contact); + if (activeRosterId != null) { + ArrayList cs = new ArrayList<>(Arrays.asList(contact)); + loadContactsMessagesByRosterId(cs, activeRosterId); + } } } + } finally { + if (cursor != null) { + cursor.close(); + } } } return contact; @@ -1412,16 +1513,22 @@ public class DbHelper { public ContactModel getContactsByEmail(String email) { ContactModel contact = null; if (StringUtils.isNotEmpty(email)) { - final Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + - ContactsTable.Column.EMAIL + " = '" + email + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - contact = ContactsTable.parseCursor(cursor); - break; + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + + ContactsTable.Column.EMAIL + " = '" + email + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + contact = ContactsTable.parseCursor(cursor); + break; + } + if (contact != null) { + setContactData(contact); + } } - cursor.close(); - if (contact != null) { - setContactData(contact); + } finally { + if (cursor != null) { + cursor.close(); } } } @@ -1438,15 +1545,21 @@ public class DbHelper { private List getRoomFeatures(String roomIds) { ArrayList services = new ArrayList<>(); if (StringUtils.isNotEmpty(roomIds)) { - final Cursor cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_ROOM + "' AND " + - FeatureTable.Column.TARGET_ID + " IN ( " + roomIds + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final FeatureModel feature = FeatureTable.parseCursor(cursor); - services.add(feature); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_ROOM + "' AND " + + FeatureTable.Column.TARGET_ID + " IN ( " + roomIds + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final FeatureModel feature = FeatureTable.parseCursor(cursor); + services.add(feature); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return services; @@ -1489,14 +1602,20 @@ public class DbHelper { private ArrayList getSimpleRosterByProfile(ProfileModel profile) { ArrayList rosters = new ArrayList<>(); if (profile != null) { - final Cursor cursor = mDb.query("SELECT * FROM " + RosterTable.TABLE_NAME + " WHERE " + - RosterTable.Column.PROFILE_ID + " = '" + profile.phone + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - final RosterModel roster = RosterTable.parseCursor(cursor); - rosters.add(roster); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + RosterTable.TABLE_NAME + " WHERE " + + RosterTable.Column.PROFILE_ID + " = '" + profile.phone + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + final RosterModel roster = RosterTable.parseCursor(cursor); + rosters.add(roster); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return rosters; @@ -1527,15 +1646,21 @@ public class DbHelper { public ContactModel getContactsByRosterIdAndPhoneId(@NonNull String rosterId, String phoneId ) { ContactModel contactModel = null; String rosterIds = "'" + rosterId + "'"; - Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + - ContactsTable.Column.ROSTER_ID + " IN (" + rosterIds + ") AND " + - ContactsTable.Column.PHONE_ID + " == '" + phoneId + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - contactModel = ContactsTable.parseCursor(cursor); - break; + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + + ContactsTable.Column.ROSTER_ID + " IN (" + rosterIds + ") AND " + + ContactsTable.Column.PHONE_ID + " == '" + phoneId + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + contactModel = ContactsTable.parseCursor(cursor); + break; + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return contactModel; } @@ -1543,14 +1668,20 @@ public class DbHelper { public int getContactsCountByRosterIdAndStatus(@NonNull String rosterId, String status ) { int count = 0; String rosterIds = "'" + rosterId + "'"; - Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + - ContactsTable.Column.ROSTER_ID + " IN (" + rosterIds + ") AND " + - ContactsTable.Column.STATUS + " == '" + status + "'"); - if (cursor != null) { - if (cursor.getCount() > 0) { - count = cursor.getCount(); - }; - cursor.close(); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + + ContactsTable.Column.ROSTER_ID + " IN (" + rosterIds + ") AND " + + ContactsTable.Column.STATUS + " == '" + status + "'"); + if (cursor != null) { + if (cursor.getCount() > 0) { + count = cursor.getCount(); + }; + } + } finally { + if (cursor != null) { + cursor.close(); + } } return count; } @@ -1559,16 +1690,22 @@ public class DbHelper { @NonNull String activeRosterId) { String rosterIds = "'" + rosterId + "'"; List contacts = new ArrayList<>(); - Cursor cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + - ContactsTable.Column.ROSTER_ID + " IN (" + rosterIds + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - ContactModel contact = ContactsTable.parseCursor(cursor); - contacts.add(contact); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactsTable.TABLE_NAME + " WHERE " + + ContactsTable.Column.ROSTER_ID + " IN (" + rosterIds + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + ContactModel contact = ContactsTable.parseCursor(cursor); + contacts.add(contact); + } + setContactsData(contacts, false); + loadContactsMessagesByRosterId(contacts, activeRosterId); + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); - setContactsData(contacts, false); - loadContactsMessagesByRosterId(contacts, activeRosterId); } return contacts; } @@ -1667,18 +1804,24 @@ public class DbHelper { long limit) { List contacts = new ArrayList<>(); String roster_Id = StringUtils.getIdFromRosterId(rosterId); - Cursor cursor = mDb.query(getMyContactsWithFiltersQuery(roster_Id, showRequested, - onlyTimeline, isForCotacts, newerThan, limit)); - if (cursor != null) { - while (cursor.moveToNext()) { - contacts.add(ContactsTable.parseCursor(cursor)); - } - cursor.close(); - if (!isForCotacts) { - contacts = getMyContactsMessagesWithFilters(rosterId, contacts, true, newerThan); + Cursor cursor = null; + try { + cursor = mDb.query(getMyContactsWithFiltersQuery(roster_Id, showRequested, + onlyTimeline, isForCotacts, newerThan, limit)); + if (cursor != null) { + while (cursor.moveToNext()) { + contacts.add(ContactsTable.parseCursor(cursor)); + } + if (!isForCotacts) { + contacts = getMyContactsMessagesWithFilters(rosterId, contacts, true, newerThan); + } + if (!onlyTimeline) { + setContactsData(contacts, !isForCotacts); + } } - if (!onlyTimeline) { - setContactsData(contacts, !isForCotacts); + } finally { + if (cursor != null) { + cursor.close(); } } return contacts; @@ -1757,14 +1900,20 @@ public class DbHelper { @Nullable public RoomModel getChatRoomByRoomIdWithData(String roomId, boolean withData) { - Cursor cursor = mDb.query("SELECT * FROM " + RoomTable.TABLE_NAME + - " WHERE " + RoomTable.Column._ID + " = '" + roomId + "'"); + Cursor cursor = null; RoomModel room = null; - if (cursor != null) { - if (cursor.moveToFirst()) { - room = RoomTable.parseCursor(cursor); + try { + cursor = mDb.query("SELECT * FROM " + RoomTable.TABLE_NAME + + " WHERE " + RoomTable.Column._ID + " = '" + roomId + "'"); + if (cursor != null) { + if (cursor.moveToFirst()) { + room = RoomTable.parseCursor(cursor); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } if (withData && room != null) { List rooms = new ArrayList<>(Arrays.asList(room)); @@ -1779,16 +1928,22 @@ public class DbHelper { boolean onlyTimeline, boolean onlyActive) { List rooms = new ArrayList<>(); - Cursor cursor = mDb.query(sql); - if (cursor != null) { - while (cursor.moveToNext()) { - RoomModel room = RoomTable.parseCursor(cursor); - if ((!onlyActive || (onlyActive && room.isActiveRoom()))) { - rooms.add(room); + Cursor cursor = null; + try { + cursor = mDb.query(sql); + if (cursor != null) { + while (cursor.moveToNext()) { + RoomModel room = RoomTable.parseCursor(cursor); + if ((!onlyActive || (onlyActive && room.isActiveRoom()))) { + rooms.add(room); + } } + setRoomData(rooms, onlyTimeline); + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); - setRoomData(rooms, onlyTimeline); } return rooms; } @@ -1890,20 +2045,26 @@ public class DbHelper { boolean isAddReplied, boolean minimalData) { List list = new ArrayList<>(); - Cursor cursor = mDb.query(query); - if (cursor != null) { - while (cursor.moveToNext()) { - MessageModel message = MessagesTable.parseCursor(cursor); - list.add(message); - } - cursor.close(); - setMessagesDesc(list); - if (!minimalData) { - checkStarMessage(list); - if (isAddReplied) { - addRepliedToMessage(list); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + while (cursor.moveToNext()) { + MessageModel message = MessagesTable.parseCursor(cursor); + list.add(message); + } + setMessagesDesc(list); + if (!minimalData) { + checkStarMessage(list); + if (isAddReplied) { + addRepliedToMessage(list); + } } } + } finally { + if (cursor != null) { + cursor.close(); + } } return list; } @@ -1911,18 +2072,24 @@ public class DbHelper { @Nullable private MessageModel getMessage(String query, boolean isAddReplied) { MessageModel message = null; - Cursor cursor = mDb.query(query); - if (cursor != null) { - if (cursor.getCount() > 0) { - cursor.moveToFirst(); - message = MessagesTable.parseCursor(cursor); - setMessageDesc(message); - checkStarMessage(message); - if (isAddReplied && message.isReply()) { - addRepliedToMessage(message); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + if (cursor.getCount() > 0) { + cursor.moveToFirst(); + message = MessagesTable.parseCursor(cursor); + setMessageDesc(message); + checkStarMessage(message); + if (isAddReplied && message.isReply()) { + addRepliedToMessage(message); + } } } - cursor.close(); + } finally { + if (cursor != null) { + cursor.close(); + } } return message; } @@ -1942,17 +2109,23 @@ public class DbHelper { String query = "SELECT * FROM " + StarTable.TABLE_NAME + " WHERE " + StarTable.Column.MESSAGE_ID + " IN (" + ids + ") AND " + StarTable.Column.STATUS + " != '" + StarModel.Statuses.REMOVE.atom + "'"; - Cursor cursor = mDb.query(query); - if (cursor != null) { - if (cursor.getCount() > 0) { - while (cursor.moveToNext()) { - StarModel star = StarTable.parseCursor(cursor); - starMessageId.add(star.messageId); - starId.put(star.messageId, star.serverId); - starClientId.put(star.messageId, star.clientId); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + if (cursor.getCount() > 0) { + while (cursor.moveToNext()) { + StarModel star = StarTable.parseCursor(cursor); + starMessageId.add(star.messageId); + starId.put(star.messageId, star.serverId); + starClientId.put(star.messageId, star.clientId); + } } } - cursor.close(); + } finally { + if (cursor != null) { + cursor.close(); + } } String messagesId = TextUtils.join(", ", starMessageId); for (MessageModel message : list) { @@ -1968,14 +2141,19 @@ public class DbHelper { String query = "SELECT * FROM " + StarTable.TABLE_NAME + " WHERE " + StarTable.Column.MESSAGE_ID + " = " + message.serverId + " AND " + StarTable.Column.STATUS + " != '" + StarModel.Statuses.REMOVE.atom + "'"; - Cursor cursor = mDb.query(query); - message.isStarred = cursor != null && cursor.getCount() > 0; - if (message.isStarred) { - cursor.moveToNext(); - message.starServerId = StarTable.getServerId(cursor); - message.starClientId = StarTable.getClientId(cursor); + Cursor cursor = null; + try { + cursor = mDb.query(query); + message.isStarred = cursor != null && cursor.getCount() > 0; + if (message.isStarred) { + cursor.moveToNext(); + message.starServerId = StarTable.getServerId(cursor); + message.starClientId = StarTable.getClientId(cursor); + } + } finally { + if (cursor != null) + cursor.close(); } - if (cursor != null) cursor.close(); } private void addRepliedToMessage(List list) { @@ -2071,25 +2249,31 @@ public class DbHelper { StarTable.Column.STATUS + " = '" + StarModel.Statuses.REMOVE.atom + "'"; List list = new ArrayList<>(); List ids = new ArrayList<>(); - Cursor cursor = mDb.query(queryInto); - if (cursor != null) { - while (cursor.moveToNext()) { - StarModel star = StarTable.parseCursor(cursor); - ids.add(String.valueOf(star.messageId)); - list.add(star); - } - cursor.close(); - if (!list.isEmpty()) { - List messages = getStaticMessageByIds(TextUtils.join(", ", ids), MessageModel.StaticTypes.STAR); - for (StarModel star : list) { - for (MessageModel message : messages) { - if (message.serverId != null && message.serverId.equals(star.messageId)) { - star.message = message; - break; + Cursor cursor = null; + try { + cursor = mDb.query(queryInto); + if (cursor != null) { + while (cursor.moveToNext()) { + StarModel star = StarTable.parseCursor(cursor); + ids.add(String.valueOf(star.messageId)); + list.add(star); + } + if (!list.isEmpty()) { + List messages = getStaticMessageByIds(TextUtils.join(", ", ids), MessageModel.StaticTypes.STAR); + for (StarModel star : list) { + for (MessageModel message : messages) { + if (message.serverId != null && message.serverId.equals(star.messageId)) { + star.message = message; + break; + } } } } } + } finally { + if (cursor != null) { + cursor.close(); + } } return list; } @@ -2153,37 +2337,49 @@ public class DbHelper { ArrayList descFeatures = new ArrayList<>(); String query = "SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + FeatureTable.Column.TARGET_ID + " = '" + descId + "'"; - Cursor cursorFeature = mDb.query(query); - if (cursorFeature != null) { - while (cursorFeature.moveToNext()) { - desc.data.add(FeatureTable.parseCursor(cursorFeature)); + Cursor cursorFeature = null; + try { + cursorFeature = mDb.query(query); + if (cursorFeature != null) { + while (cursorFeature.moveToNext()) { + desc.data.add(FeatureTable.parseCursor(cursorFeature)); + } + } + } finally { + if (cursorFeature != null) { + cursorFeature.close(); } - cursorFeature.close(); } } } private void setMessagesDescByIds(List messageList, String ids) { if (!StringUtils.isNotEmpty(ids)) return; - Cursor cursorDesc = mDb.query("SELECT * FROM " + DescTable.TABLE_NAME + " WHERE " + - DescTable.Column.TARGET_ID + " IN (" + ids + ") AND " + - DescTable.Column.TARGET_TYPE + " = " + DescTable.TYPE_MESSAGE); - List descList = new ArrayList<>(); - if (cursorDesc != null) { - while (cursorDesc.moveToNext()) { - DescModel desc = DescTable.parseCursor(cursorDesc); - descList.add(desc); - } - cursorDesc.close(); - } - if (!messageList.isEmpty() && !descList.isEmpty()) { - for (MessageModel msg : messageList) { - for (DescModel desc : descList) { - if (desc.targetId.equals(String.valueOf(msg.localId))) { - msg.files.add(desc); + Cursor cursorDesc = null; + try { + cursorDesc = mDb.query("SELECT * FROM " + DescTable.TABLE_NAME + " WHERE " + + DescTable.Column.TARGET_ID + " IN (" + ids + ") AND " + + DescTable.Column.TARGET_TYPE + " = " + DescTable.TYPE_MESSAGE); + List descList = new ArrayList<>(); + if (cursorDesc != null) { + while (cursorDesc.moveToNext()) { + DescModel desc = DescTable.parseCursor(cursorDesc); + descList.add(desc); + } + } + if (!messageList.isEmpty() && !descList.isEmpty()) { + for (MessageModel msg : messageList) { + for (DescModel desc : descList) { + if (desc.targetId.equals(String.valueOf(msg.localId))) { + msg.files.add(desc); + } } + setDescFeatures(msg.files); } - setDescFeatures(msg.files); + } + } finally { + if (cursorDesc != null) { + cursorDesc.close(); } } } @@ -2220,13 +2416,19 @@ public class DbHelper { private List getDesc(String query) { List descs = new ArrayList<>(); - Cursor cursor = mDb.query(query); - if (cursor != null) { - while (cursor.moveToNext()) { - DescModel desc = DescTable.parseCursor(cursor); - descs.add(desc); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + while (cursor.moveToNext()) { + DescModel desc = DescTable.parseCursor(cursor); + descs.add(desc); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } setDescFeatures(descs); return descs; @@ -2382,15 +2584,21 @@ public class DbHelper { private List getTagsByRosters(String ids) { ArrayList stars = new ArrayList<>(); if (StringUtils.isNotEmpty(ids)) { - final Cursor cursor = mDb.query( + Cursor cursor = null; + try { + cursor = mDb.query( "SELECT * FROM " + TagTable.TABLE_NAME + " WHERE " + - TagTable.Column.ROSTER_ID + " IN (" + ids + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final TagModel tag = TagTable.parseCursor(cursor); - stars.add(tag); + TagTable.Column.ROSTER_ID + " IN (" + ids + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final TagModel tag = TagTable.parseCursor(cursor); + stars.add(tag); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return stars; @@ -2481,15 +2689,21 @@ public class DbHelper { public TimelineSettingsModel getTimelineSettingsByRoster(String rosterId) { TimelineSettingsModel timelines = null; if (rosterId == null) return timelines; - final Cursor cursor = mDb.query( + Cursor cursor = null; + try { + cursor = mDb.query( "SELECT * FROM " + TimelineSettingsTable.TABLE_NAME + " WHERE " + - TimelineSettingsTable.Column.ROSTER_ID + "= '" + rosterId + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - timelines = TimelineSettingsTable.parseCursor(cursor); - break; + TimelineSettingsTable.Column.ROSTER_ID + "= '" + rosterId + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + timelines = TimelineSettingsTable.parseCursor(cursor); + break; + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return timelines; } @@ -2551,13 +2765,19 @@ public class DbHelper { MessagesTable.Column.LOCAL_ID + " = '" + message.localId + "' limit 1"; } else return false; MessageModel resMessage = null; - Cursor cursor = mDb.query(queryInto); - if (cursor != null) { - if (cursor.getCount() > 0) { - cursor.moveToFirst(); - resMessage = MessagesTable.parseCursor(cursor); + Cursor cursor = null; + try { + cursor = mDb.query(queryInto); + if (cursor != null) { + if (cursor.getCount() > 0) { + cursor.moveToFirst(); + resMessage = MessagesTable.parseCursor(cursor); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return resMessage != null; } @@ -2601,21 +2821,27 @@ public class DbHelper { private List getMembersByRoom(String ids, boolean minimalData, boolean loadMemberFeatures) { ArrayList members = new ArrayList<>(); if (StringUtils.isNotEmpty(ids)) { - final Cursor cursor = mDb.query( + Cursor cursor = null; + try { + cursor = mDb.query( "SELECT * FROM " + MemberTable.TABLE_NAME + " WHERE " + - MemberTable.Column.ROOM_ID + " IN (" + ids + ")" + - " ORDER BY " + MemberTable.Column.UPDATE + " DESC"); - if (cursor != null) { - while (cursor.moveToNext()) { - MemberModel member = getMember(cursor); - members.add(member); + MemberTable.Column.ROOM_ID + " IN (" + ids + ")" + + " ORDER BY " + MemberTable.Column.UPDATE + " DESC"); + if (cursor != null) { + while (cursor.moveToNext()) { + MemberModel member = getMember(cursor); + members.add(member); + } + if (!minimalData) { + getMemberFeatures(members); + setMemberServices(members); + } else if (loadMemberFeatures) { + getMemberFeatures(members); + } } - cursor.close(); - if (!minimalData) { - getMemberFeatures(members); - setMemberServices(members); - } else if (loadMemberFeatures) { - getMemberFeatures(members); + } finally { + if (cursor != null) { + cursor.close(); } } } @@ -2626,40 +2852,46 @@ public class DbHelper { if (room != null) { long time = System.currentTimeMillis(); - final Cursor cursor = mDb.query( + Cursor cursor = null; + try { + cursor = mDb.query( "SELECT * FROM " + MemberTable.TABLE_NAME + " WHERE " + - MemberTable.Column.ROOM_ID + " = '" + room.id + "'" + - " ORDER BY " + MemberTable.Column.UPDATE + " DESC" ); - //////////////////////////////////////////////////////////////////////////////////////////// - // TEST TEST TEST - if (BuildConfig.DEBUG) { - //Timber.i("######## TEST_POINT_"+(minimalData? 7 : 6) +": getObsChatRoomsFiltered - setRoomData ->getMembersByRoom 1: " + (System.currentTimeMillis() - time) + ", forList= " + minimalData); - time = System.currentTimeMillis(); - } - //////////////////////////////////////////////////////////////////////////////////////////// - if (cursor != null) { - while (cursor.moveToNext()) { - MemberModel member = MemberTable.parseCursor(cursor); - getMemberFeatures(member); - //////////////////////////////////////////////////////////////////////////////////////////// - // TEST TEST TEST - if (BuildConfig.DEBUG) { - //Timber.i("######## TEST_POINT_"+(minimalData? 7 : 6) +": getObsChatRoomsFiltered - setRoomData ->getMembersByRoom 2: " + (System.currentTimeMillis() - time) + ", forList= " + minimalData); - time = System.currentTimeMillis(); - } - //////////////////////////////////////////////////////////////////////////////////////////// - if (!minimalData) { - setMemberServices(member); - } - if (MemberModel.Statuses.MEMBER.equals(member.status)) { - room.members.add(member); - } else if (MemberModel.Statuses.ADMIN.equals(member.status) || MemberModel.Statuses.OWNER.equals(member.status)) { - room.admins.add(member); - } else if (MemberModel.Statuses.REMOVED.equals(member.status)) { - room.removed.add(member); + MemberTable.Column.ROOM_ID + " = '" + room.id + "'" + + " ORDER BY " + MemberTable.Column.UPDATE + " DESC" ); + //////////////////////////////////////////////////////////////////////////////////////////// + // TEST TEST TEST + if (BuildConfig.DEBUG) { + //Timber.i("######## TEST_POINT_"+(minimalData? 7 : 6) +": getObsChatRoomsFiltered - setRoomData ->getMembersByRoom 1: " + (System.currentTimeMillis() - time) + ", forList= " + minimalData); + time = System.currentTimeMillis(); + } + //////////////////////////////////////////////////////////////////////////////////////////// + if (cursor != null) { + while (cursor.moveToNext()) { + MemberModel member = MemberTable.parseCursor(cursor); + getMemberFeatures(member); + //////////////////////////////////////////////////////////////////////////////////////////// + // TEST TEST TEST + if (BuildConfig.DEBUG) { + //Timber.i("######## TEST_POINT_"+(minimalData? 7 : 6) +": getObsChatRoomsFiltered - setRoomData ->getMembersByRoom 2: " + (System.currentTimeMillis() - time) + ", forList= " + minimalData); + time = System.currentTimeMillis(); + } + //////////////////////////////////////////////////////////////////////////////////////////// + if (!minimalData) { + setMemberServices(member); + } + if (MemberModel.Statuses.MEMBER.equals(member.status)) { + room.members.add(member); + } else if (MemberModel.Statuses.ADMIN.equals(member.status) || MemberModel.Statuses.OWNER.equals(member.status)) { + room.admins.add(member); + } else if (MemberModel.Statuses.REMOVED.equals(member.status)) { + room.removed.add(member); + } } } - cursor.close(); + } finally { + if (cursor != null) { + cursor.close(); + } } } } @@ -2854,14 +3086,20 @@ public class DbHelper { private List getRoomLinks(String roomIds) { ArrayList links = new ArrayList<>(); if (StringUtils.isNotEmpty(roomIds)) { - final Cursor cursor = mDb.query("SELECT * FROM " + LinkTable.TABLE_NAME + " WHERE " + - LinkTable.Column.ROOM_ID + " IN (" + roomIds + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final LinkModel link = LinkTable.parseCursor(cursor); - links.add(link); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + LinkTable.TABLE_NAME + " WHERE " + + LinkTable.Column.ROOM_ID + " IN (" + roomIds + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final LinkModel link = LinkTable.parseCursor(cursor); + links.add(link); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return links; @@ -2870,15 +3108,21 @@ public class DbHelper { public List getRoomLinksByType(String roomIds, BertAtom type) { ArrayList links = new ArrayList<>(); if (StringUtils.isNotEmpty(roomIds)) { - final Cursor cursor = mDb.query("SELECT * FROM " + LinkTable.TABLE_NAME + " WHERE " + - LinkTable.Column.ROOM_ID + " IN (" + roomIds + ") AND " + LinkTable.Column.TYPE + - " = '" + type.atom + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - final LinkModel link = LinkTable.parseCursor(cursor); - links.add(link); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + LinkTable.TABLE_NAME + " WHERE " + + LinkTable.Column.ROOM_ID + " IN (" + roomIds + ") AND " + LinkTable.Column.TYPE + + " = '" + type.atom + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + final LinkModel link = LinkTable.parseCursor(cursor); + links.add(link); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return links; @@ -2974,7 +3218,6 @@ public class DbHelper { status = DbQueryStatuses.UPDATE; } mDb.update(tableName, SQLiteDatabase.CONFLICT_NONE, cv, where); - if (!cursor.isClosed()) cursor.close(); } else { insertId = mDb.insert(tableName, SQLiteDatabase.CONFLICT_REPLACE, cv); status = DbQueryStatuses.INSERT; @@ -3066,12 +3309,18 @@ public class DbHelper { builder.append(where); } String query = builder.toString(); - Cursor cursor = mDb.query(query); - if (cursor != null) { - if (cursor.getCount() > 0) { - cntRows = mDb.update(tableName, SQLiteDatabase.CONFLICT_NONE, cv, where); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + if (cursor.getCount() > 0) { + cntRows = mDb.update(tableName, SQLiteDatabase.CONFLICT_NONE, cv, where); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return cntRows; } @@ -3108,14 +3357,20 @@ public class DbHelper { private List getRoomsById(String ids) { List rooms = new ArrayList<>(); - Cursor cursor = mDb.query("SELECT * FROM " + RoomTable.TABLE_NAME + - " WHERE " + RoomTable.Column._ID + " IN (" + ids + ")"); - if (cursor != null) { - while (cursor.moveToNext()) { - final RoomModel room = RoomTable.parseCursor(cursor); - rooms.add(room); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + RoomTable.TABLE_NAME + + " WHERE " + RoomTable.Column._ID + " IN (" + ids + ")"); + if (cursor != null) { + while (cursor.moveToNext()) { + final RoomModel room = RoomTable.parseCursor(cursor); + rooms.add(room); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } setRoomData(rooms, false); return rooms; @@ -3861,14 +4116,20 @@ public class DbHelper { private List getStaticMessages(String query, int type) { List list = new ArrayList<>(); - Cursor cursor = mDb.query(query); - if (cursor != null) { - while (cursor.moveToNext()) { - MessageModel message = StaticMessagesTable.parseCursor(cursor); - list.add(message); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + while (cursor.moveToNext()) { + MessageModel message = StaticMessagesTable.parseCursor(cursor); + list.add(message); + } + setStaticMessagesDesc(list, type); + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); - setStaticMessagesDesc(list, type); } return list; } @@ -4037,13 +4298,19 @@ public class DbHelper { String query = "SELECT * FROM " + JobTable.TABLE_NAME + " WHERE " + JobTable.Column.STATUS + " IN (" + statusesDelim + ") " + " ORDER BY " + JobTable.Column.TIME + " DESC"; - Cursor cursor = mDb.query(query); - if (cursor != null) { - while (cursor.moveToNext()) { - JobModel job = JobTable.parseCursor(cursor); - jobs.add(job); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + while (cursor.moveToNext()) { + JobModel job = JobTable.parseCursor(cursor); + jobs.add(job); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return setJobsData(jobs); @@ -4084,13 +4351,19 @@ public class DbHelper { " AND " + MessagesTable.TABLE_NAME + "." + MessagesTable.Column.FROM + " = '" + rosterId + "'" + " ORDER BY " + MessagesTable.Column.CREATED + " DESC"; if (limit > 0) query += " LIMIT " + limit; - Cursor cursor = mDb.query(query); - if (cursor != null) { - while (cursor.moveToNext()) { - DescModel desc = DescTable.parseCursor(cursor); - descs.add(desc); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + while (cursor.moveToNext()) { + DescModel desc = DescTable.parseCursor(cursor); + descs.add(desc); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } setDescFeatures(descs); return descs; @@ -4106,13 +4379,19 @@ public class DbHelper { " AND " + MessagesTable.TABLE_NAME + "." + MessagesTable.Column.FROM + " = '" + rosterId + "'" + " ORDER BY " + MessagesTable.Column.CREATED + " DESC"; if (limit > 0) query += " LIMIT " + limit; - Cursor cursor = mDb.query(query); - if (cursor != null) { - while (cursor.moveToNext()) { - DescModel desc = DescTable.parseCursor(cursor); - descs.add(desc); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + while (cursor.moveToNext()) { + DescModel desc = DescTable.parseCursor(cursor); + descs.add(desc); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } setDescFeatures(descs); return descs; @@ -4132,13 +4411,19 @@ public class DbHelper { " WHERE " + DescTable.Column.MIME + " = '" + DescModel.MimeTypes.Thumb + "'" + " AND " + DescTable.Column.TARGET_TYPE + " = " + DescTable.TYPE_MESSAGE + " AND " + DescTable.Column.TARGET_ID + " IN (" + ids + ")"; - Cursor cursor = mDb.query(query); - if (cursor != null) { - while (cursor.moveToNext()) { - DescModel desc = DescTable.parseCursor(cursor); - thumbList.add(desc); + Cursor cursor = null; + try { + cursor = mDb.query(query); + if (cursor != null) { + while (cursor.moveToNext()) { + DescModel desc = DescTable.parseCursor(cursor); + thumbList.add(desc); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } setDescFeatures(mediaList); setDescFeatures(thumbList); @@ -4215,19 +4500,26 @@ public class DbHelper { private void setStickerPackDescs(StickerPackModel stickerPack) { String stickerPackId = String.valueOf(stickerPack.serverId); - Cursor cursorDescs = mDb.query("SELECT * FROM " + DescTable.TABLE_NAME + " WHERE " + - DescTable.Column.TARGET_TYPE + " = '" + DescTable.TYPE_STICKER_PACK + "' AND " + - DescTable.Column.TARGET_ID + " = '" + stickerPackId + "'"); - List descList = new ArrayList<>(); - if (cursorDescs != null) { - while (cursorDescs.moveToNext()) { - DescModel desc = DescTable.parseCursor(cursorDescs); - descList.add(desc); + Cursor cursorDescs = null; + try { + cursorDescs = mDb.query("SELECT * FROM " + DescTable.TABLE_NAME + " WHERE " + + DescTable.Column.TARGET_TYPE + " = '" + DescTable.TYPE_STICKER_PACK + "' AND " + + DescTable.Column.TARGET_ID + " = '" + stickerPackId + "'"); + List descList = new ArrayList<>(); + if (cursorDescs != null) { + while (cursorDescs.moveToNext()) { + DescModel desc = DescTable.parseCursor(cursorDescs); + descList.add(desc); + } + cursorDescs.close(); + } + stickerPack.stickers.clear(); + stickerPack.stickers.addAll(descList); + } finally { + if (cursorDescs != null) { + cursorDescs.close(); } - cursorDescs.close(); } - stickerPack.stickers.clear(); - stickerPack.stickers.addAll(descList); } private void setStickerPackDescFeatures(DescModel stickerPackDesc) { @@ -4249,36 +4541,48 @@ public class DbHelper { public List getRecentStickers(Context context, ArrayList stickerIdentificators) { List result = new ArrayList<>(); for (String stickerIdentificator : stickerIdentificators) { - Cursor cursorDesc = mDb.query("SELECT * FROM " + DescTable.TABLE_NAME + " WHERE " + - DescTable.Column.MIME + " = '" + DescModel.MimeTypes.Sticker + "' AND " + - DescTable.Column.TARGET_TYPE + " = '" + DescTable.TYPE_STICKER_PACK + "' AND " + - DescTable.Column.UNIQUE_ID + " = '" + stickerIdentificator + "'"); - DescModel stickerDesc = null; - if (cursorDesc != null) { - while (cursorDesc.moveToNext()) { - stickerDesc = DescTable.parseCursor(cursorDesc); + Cursor cursorDesc = null; + try { + cursorDesc = mDb.query("SELECT * FROM " + DescTable.TABLE_NAME + " WHERE " + + DescTable.Column.MIME + " = '" + DescModel.MimeTypes.Sticker + "' AND " + + DescTable.Column.TARGET_TYPE + " = '" + DescTable.TYPE_STICKER_PACK + "' AND " + + DescTable.Column.UNIQUE_ID + " = '" + stickerIdentificator + "'"); + DescModel stickerDesc = null; + if (cursorDesc != null) { + while (cursorDesc.moveToNext()) { + stickerDesc = DescTable.parseCursor(cursorDesc); + } } - cursorDesc.close(); - } - if (stickerDesc != null) { - List features = new ArrayList<>(); - Cursor cursorFeatures = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_STICKER + "'" + - " AND " + FeatureTable.Column.TARGET_ID + " = '" + stickerDesc.id + "'"); - if (cursorFeatures != null) { - while (cursorFeatures.moveToNext()) { - FeatureModel feature = FeatureTable.parseCursor(cursorFeatures); - features.add(feature); + if (stickerDesc != null) { + List features = new ArrayList<>(); + Cursor cursorFeatures = null; + try { + cursorFeatures = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_STICKER + "'" + + " AND " + FeatureTable.Column.TARGET_ID + " = '" + stickerDesc.id + "'"); + if (cursorFeatures != null) { + while (cursorFeatures.moveToNext()) { + FeatureModel feature = FeatureTable.parseCursor(cursorFeatures); + features.add(feature); + } + } + } finally { + if (cursorFeatures != null) { + cursorFeatures.close(); + } } - cursorFeatures.close(); + stickerDesc.data.clear(); + stickerDesc.data.addAll(features); } - stickerDesc.data.clear(); - stickerDesc.data.addAll(features); - } - if (stickerDesc != null) { - Sticker sticker = DescManager.getStickerFromDesc(context, stickerDesc); - if (sticker != null) { - result.add(sticker); + if (stickerDesc != null) { + Sticker sticker = DescManager.getStickerFromDesc(context, stickerDesc); + if (sticker != null) { + result.add(sticker); + } + } + } finally { + if (cursorDesc != null) { + cursorDesc.close(); } } } @@ -4287,13 +4591,19 @@ public class DbHelper { public StickerPackModel getLastStickerPack() { StickerPackModel stickerPack = null; - Cursor cursorStickerPack = mDb.query("SELECT * FROM " + StickerPacksTable.TABLE_NAME + - " ORDER BY " + StickerPacksTable.Column.STICKER_PACK_UPDATED + - " DESC LIMIT 1"); - if (cursorStickerPack != null) { - if (cursorStickerPack.moveToFirst()) - stickerPack = StickerPacksTable.parseCursor(cursorStickerPack); - cursorStickerPack.close(); + Cursor cursorStickerPack = null; + try { + cursorStickerPack = mDb.query("SELECT * FROM " + StickerPacksTable.TABLE_NAME + + " ORDER BY " + StickerPacksTable.Column.STICKER_PACK_UPDATED + + " DESC LIMIT 1"); + if (cursorStickerPack != null) { + if (cursorStickerPack.moveToFirst()) + stickerPack = StickerPacksTable.parseCursor(cursorStickerPack); + } + } finally { + if (cursorStickerPack != null) { + cursorStickerPack.close(); + } } return stickerPack; } @@ -4790,27 +5100,39 @@ public class DbHelper { + HistoryRangeTable.TABLE_NAME + " WHERE " + HistoryRangeTable.Column.CHAT_ID + " = '" + chatId + "' AND " + HistoryRangeTable.Column.TYPE + " = '" + type.toLowerCase() + "'"; - Cursor cursor = mDb.query(args); - if (cursor != null) { - if (cursor.moveToFirst()) { - result = cursor.getInt(cursor.getColumnIndexOrThrow(HistoryRangeTable.Column.IS_ACTUAL_START)) == 1; + Cursor cursor = null; + try { + cursor = mDb.query(args); + if (cursor != null) { + if (cursor.moveToFirst()) { + result = cursor.getInt(cursor.getColumnIndexOrThrow(HistoryRangeTable.Column.IS_ACTUAL_START)) == 1; + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return result; } public boolean isReachEndInHistoryRange(String chatId, String type) { boolean result = false; - Cursor cursor = mDb.query("SELECT " + HistoryRangeTable.Column.IS_REACH_END + " FROM " - + HistoryRangeTable.TABLE_NAME + " WHERE " - + HistoryRangeTable.Column.CHAT_ID + " = '" + chatId + "' AND " - + HistoryRangeTable.Column.TYPE + " = '" + type.toLowerCase() + "'"); - if (cursor != null) { - if (cursor.moveToFirst()) { - result = (cursor.getInt(cursor.getColumnIndexOrThrow(HistoryRangeTable.Column.IS_REACH_END)) == 1); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT " + HistoryRangeTable.Column.IS_REACH_END + " FROM " + + HistoryRangeTable.TABLE_NAME + " WHERE " + + HistoryRangeTable.Column.CHAT_ID + " = '" + chatId + "' AND " + + HistoryRangeTable.Column.TYPE + " = '" + type.toLowerCase() + "'"); + if (cursor != null) { + if (cursor.moveToFirst()) { + result = (cursor.getInt(cursor.getColumnIndexOrThrow(HistoryRangeTable.Column.IS_REACH_END)) == 1); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } return result; } @@ -4885,14 +5207,20 @@ public class DbHelper { private List getContactDataList(String accountId) { ArrayList contactInfo = new ArrayList<>(); if (StringUtils.isNotEmpty(accountId)) { - final Cursor cursor = mDb.query("SELECT * FROM " + ContactInfoTable.TABLE_NAME + " WHERE " + - ContactInfoTable.Column.ACCOUNT_ID + " = '" + accountId + "'"); - if (cursor != null) { - while (cursor.moveToNext()) { - final UserContactData contactData = ContactInfoTable.parseCursor(cursor); - contactInfo.add(contactData); + Cursor cursor = null; + try { + cursor = mDb.query("SELECT * FROM " + ContactInfoTable.TABLE_NAME + " WHERE " + + ContactInfoTable.Column.ACCOUNT_ID + " = '" + accountId + "'"); + if (cursor != null) { + while (cursor.moveToNext()) { + final UserContactData contactData = ContactInfoTable.parseCursor(cursor); + contactInfo.add(contactData); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } } return contactInfo; @@ -4926,18 +5254,22 @@ public class DbHelper { public IAccount getAccount() { AccountImpl account = null; + Cursor cursor = null; try { - Cursor cursor = mDb.query("SELECT * FROM " + AccountTable.TABLE_NAME); + cursor = mDb.query("SELECT * FROM " + AccountTable.TABLE_NAME); if (cursor != null) { if (cursor.getCount() > 0) { cursor.moveToFirst(); account = AccountTable.parseCursor(cursor); account.setContactsInfo(getContactDataList(account.getAccountId())); } - cursor.close(); } } catch (Exception e) { Timber.e(e, "Error get account"); + } finally { + if (cursor != null) { + cursor.close(); + } } return account; } @@ -4956,17 +5288,21 @@ public class DbHelper { public String getRefreshToken() { String token = ""; + Cursor cursor = null; try { - final Cursor cursor = mDb.query("SELECT * FROM " + RefreshTokenTable.TABLE_NAME); + cursor = mDb.query("SELECT * FROM " + RefreshTokenTable.TABLE_NAME); if (cursor != null) { while (cursor.moveToNext()) { token = RefreshTokenTable.parseCursor(cursor); } - cursor.close(); return token; } } catch (Exception e) { Timber.e(e, "Error get token"); + } finally { + if (cursor != null) { + cursor.close(); + } } return ""; } -- GitLab From bbc34950ad63e67a9b3bdfb2fbcc84e5cf4f7c80 Mon Sep 17 00:00:00 2001 From: Rafael da Veiga Cabral Date: Wed, 27 May 2020 17:59:55 -0300 Subject: [PATCH 2/3] NY9868: [AN]: Move the deletion to after the cursor is closed. --- .../com/nynja/mobile/communicator/data/db/DbHelper.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java b/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java index a98c8f51af..a3daa30e3c 100644 --- a/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java +++ b/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java @@ -356,16 +356,15 @@ public class DbHelper { while (cursor.moveToNext()) { tables[i++] = cursor.getString(0); } - - for (String table : tables) { - String dropQuery = "DELETE FROM " + table; - mDb.execute(dropQuery); - } } finally { if (cursor != null) { cursor.close(); } } + for (String table : tables) { + String dropQuery = "DELETE FROM " + table; + mDb.execute(dropQuery); + } }); } -- GitLab From af399c552ad1093b69879625e99b7fdfced0a07c Mon Sep 17 00:00:00 2001 From: Rafael da Veiga Cabral Date: Wed, 27 May 2020 18:01:50 -0300 Subject: [PATCH 3/3] NY9868: [AN]: Prevent nested cursors --- .../mobile/communicator/data/db/DbHelper.java | 173 +++++++++--------- 1 file changed, 87 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java b/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java index a3daa30e3c..8ed7216650 100644 --- a/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java +++ b/app/src/main/java/com/nynja/mobile/communicator/data/db/DbHelper.java @@ -347,14 +347,13 @@ public class DbHelper { public Completable clearTables() { return Completable.fromAction(() -> { Cursor cursor = null; + List tables = new ArrayList(); try { cursor = mDb.query("SELECT name FROM sqlite_master WHERE type IS 'table'" + " AND name NOT IN ('sqlite_master', 'sqlite_sequence', 'android_metadata')"); if (cursor == null) return; - String[] tables = new String[cursor.getCount()]; - int i = 0; while (cursor.moveToNext()) { - tables[i++] = cursor.getString(0); + tables.add(cursor.getString(0)); } } finally { if (cursor != null) { @@ -377,7 +376,6 @@ public class DbHelper { if (cursor.getCount() > 0) { cursor.moveToFirst(); auth = AuthTable.parseCursor(cursor); - auth.settings = getAuthFeatures(auth); } } } finally { @@ -385,6 +383,7 @@ public class DbHelper { cursor.close(); } } + auth.settings = getAuthFeatures(auth); return auth; } @@ -1811,18 +1810,18 @@ public class DbHelper { while (cursor.moveToNext()) { contacts.add(ContactsTable.parseCursor(cursor)); } - if (!isForCotacts) { - contacts = getMyContactsMessagesWithFilters(rosterId, contacts, true, newerThan); - } - if (!onlyTimeline) { - setContactsData(contacts, !isForCotacts); - } } } finally { if (cursor != null) { cursor.close(); } } + if (!isForCotacts) { + contacts = getMyContactsMessagesWithFilters(rosterId, contacts, true, newerThan); + } + if (!onlyTimeline) { + setContactsData(contacts, !isForCotacts); + } return contacts; } @@ -2053,18 +2052,19 @@ public class DbHelper { list.add(message); } setMessagesDesc(list); - if (!minimalData) { - checkStarMessage(list); - if (isAddReplied) { - addRepliedToMessage(list); - } - } } } finally { if (cursor != null) { cursor.close(); } } + if (!minimalData) { + checkStarMessage(list); + if (isAddReplied) { + addRepliedToMessage(list); + } + } + return list; } @@ -2078,11 +2078,6 @@ public class DbHelper { if (cursor.getCount() > 0) { cursor.moveToFirst(); message = MessagesTable.parseCursor(cursor); - setMessageDesc(message); - checkStarMessage(message); - if (isAddReplied && message.isReply()) { - addRepliedToMessage(message); - } } } } finally { @@ -2090,6 +2085,11 @@ public class DbHelper { cursor.close(); } } + setMessageDesc(message); + checkStarMessage(message); + if (isAddReplied && message.isReply()) { + addRepliedToMessage(message); + } return message; } @@ -2257,23 +2257,23 @@ public class DbHelper { ids.add(String.valueOf(star.messageId)); list.add(star); } - if (!list.isEmpty()) { - List messages = getStaticMessageByIds(TextUtils.join(", ", ids), MessageModel.StaticTypes.STAR); - for (StarModel star : list) { - for (MessageModel message : messages) { - if (message.serverId != null && message.serverId.equals(star.messageId)) { - star.message = message; - break; - } - } - } - } } } finally { if (cursor != null) { cursor.close(); } } + if (!list.isEmpty()) { + List messages = getStaticMessageByIds(TextUtils.join(", ", ids), MessageModel.StaticTypes.STAR); + for (StarModel star : list) { + for (MessageModel message : messages) { + if (message.serverId != null && message.serverId.equals(star.messageId)) { + star.message = message; + break; + } + } + } + } return list; } @@ -2831,24 +2831,26 @@ public class DbHelper { MemberModel member = getMember(cursor); members.add(member); } - if (!minimalData) { - getMemberFeatures(members); - setMemberServices(members); - } else if (loadMemberFeatures) { - getMemberFeatures(members); - } } } finally { if (cursor != null) { cursor.close(); } } + if (!minimalData) { + getMemberFeatures(members); + setMemberServices(members); + } else if (loadMemberFeatures) { + getMemberFeatures(members); + } + } return members; } private void getMembersByRoom(RoomModel room, boolean minimalData) { if (room != null) { + List members = new ArrayList(); long time = System.currentTimeMillis(); Cursor cursor = null; @@ -2866,25 +2868,7 @@ public class DbHelper { //////////////////////////////////////////////////////////////////////////////////////////// if (cursor != null) { while (cursor.moveToNext()) { - MemberModel member = MemberTable.parseCursor(cursor); - getMemberFeatures(member); - //////////////////////////////////////////////////////////////////////////////////////////// - // TEST TEST TEST - if (BuildConfig.DEBUG) { - //Timber.i("######## TEST_POINT_"+(minimalData? 7 : 6) +": getObsChatRoomsFiltered - setRoomData ->getMembersByRoom 2: " + (System.currentTimeMillis() - time) + ", forList= " + minimalData); - time = System.currentTimeMillis(); - } - //////////////////////////////////////////////////////////////////////////////////////////// - if (!minimalData) { - setMemberServices(member); - } - if (MemberModel.Statuses.MEMBER.equals(member.status)) { - room.members.add(member); - } else if (MemberModel.Statuses.ADMIN.equals(member.status) || MemberModel.Statuses.OWNER.equals(member.status)) { - room.admins.add(member); - } else if (MemberModel.Statuses.REMOVED.equals(member.status)) { - room.removed.add(member); - } + members.add(MemberTable.parseCursor(cursor)); } } } finally { @@ -2892,6 +2876,26 @@ public class DbHelper { cursor.close(); } } + for (MemberModel member : members) { + getMemberFeatures(member); + //////////////////////////////////////////////////////////////////////////////////////////// + // TEST TEST TEST + if (BuildConfig.DEBUG) { + //Timber.i("######## TEST_POINT_"+(minimalData? 7 : 6) +": getObsChatRoomsFiltered - setRoomData ->getMembersByRoom 2: " + (System.currentTimeMillis() - time) + ", forList= " + minimalData); + time = System.currentTimeMillis(); + } + //////////////////////////////////////////////////////////////////////////////////////////// + if (!minimalData) { + setMemberServices(member); + } + if (MemberModel.Statuses.MEMBER.equals(member.status)) { + room.members.add(member); + } else if (MemberModel.Statuses.ADMIN.equals(member.status) || MemberModel.Statuses.OWNER.equals(member.status)) { + room.admins.add(member); + } else if (MemberModel.Statuses.REMOVED.equals(member.status)) { + room.removed.add(member); + } + } } } @@ -4123,13 +4127,13 @@ public class DbHelper { MessageModel message = StaticMessagesTable.parseCursor(cursor); list.add(message); } - setStaticMessagesDesc(list, type); } } finally { if (cursor != null) { cursor.close(); } } + setStaticMessagesDesc(list, type); return list; } @@ -4510,7 +4514,6 @@ public class DbHelper { DescModel desc = DescTable.parseCursor(cursorDescs); descList.add(desc); } - cursorDescs.close(); } stickerPack.stickers.clear(); stickerPack.stickers.addAll(descList); @@ -4541,47 +4544,45 @@ public class DbHelper { List result = new ArrayList<>(); for (String stickerIdentificator : stickerIdentificators) { Cursor cursorDesc = null; + DescModel stickerDesc = null; try { cursorDesc = mDb.query("SELECT * FROM " + DescTable.TABLE_NAME + " WHERE " + DescTable.Column.MIME + " = '" + DescModel.MimeTypes.Sticker + "' AND " + DescTable.Column.TARGET_TYPE + " = '" + DescTable.TYPE_STICKER_PACK + "' AND " + DescTable.Column.UNIQUE_ID + " = '" + stickerIdentificator + "'"); - DescModel stickerDesc = null; if (cursorDesc != null) { while (cursorDesc.moveToNext()) { stickerDesc = DescTable.parseCursor(cursorDesc); } } - if (stickerDesc != null) { - List features = new ArrayList<>(); - Cursor cursorFeatures = null; - try { - cursorFeatures = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + - FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_STICKER + "'" + - " AND " + FeatureTable.Column.TARGET_ID + " = '" + stickerDesc.id + "'"); - if (cursorFeatures != null) { - while (cursorFeatures.moveToNext()) { - FeatureModel feature = FeatureTable.parseCursor(cursorFeatures); - features.add(feature); - } - } - } finally { - if (cursorFeatures != null) { - cursorFeatures.close(); + } finally { + if (cursorDesc != null) { + cursorDesc.close(); + } + } + if (stickerDesc != null) { + List features = new ArrayList<>(); + Cursor cursorFeatures = null; + try { + cursorFeatures = mDb.query("SELECT * FROM " + FeatureTable.TABLE_NAME + " WHERE " + + FeatureTable.Column.TYPE + " = '" + FeatureTable.TYPE_STICKER + "'" + + " AND " + FeatureTable.Column.TARGET_ID + " = '" + stickerDesc.id + "'"); + if (cursorFeatures != null) { + while (cursorFeatures.moveToNext()) { + FeatureModel feature = FeatureTable.parseCursor(cursorFeatures); + features.add(feature); } } - stickerDesc.data.clear(); - stickerDesc.data.addAll(features); - } - if (stickerDesc != null) { - Sticker sticker = DescManager.getStickerFromDesc(context, stickerDesc); - if (sticker != null) { - result.add(sticker); + } finally { + if (cursorFeatures != null) { + cursorFeatures.close(); } } - } finally { - if (cursorDesc != null) { - cursorDesc.close(); + stickerDesc.data.clear(); + stickerDesc.data.addAll(features); + Sticker sticker = DescManager.getStickerFromDesc(context, stickerDesc); + if (sticker != null) { + result.add(sticker); } } } -- GitLab