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 e04dc92f74ab3829a5e4b2d25a56b47aece59921..8ed7216650e2223891d45a2469f17a95458ad0af 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,16 +346,20 @@ 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 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; + while (cursor.moveToNext()) { + tables.add(cursor.getString(0)); + } + } finally { + if (cursor != null) { + cursor.close(); + } } - cursor.close(); - for (String table : tables) { String dropQuery = "DELETE FROM " + table; mDb.execute(dropQuery); @@ -347,28 +368,40 @@ public class DbHelper { } 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); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); } + auth.settings = getAuthFeatures(auth); 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 +1206,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 +1228,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 +1251,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 +1274,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 +1316,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 +1361,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 +1403,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 +1426,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 +1452,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 +1482,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 +1511,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 +1543,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 +1600,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 +1644,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 +1666,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 +1688,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,20 +1802,26 @@ 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 (!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; } @@ -1757,14 +1898,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 +1926,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,39 +2043,52 @@ 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); + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + if (!minimalData) { + checkStarMessage(list); + if (isAddReplied) { + addRepliedToMessage(list); } } + return list; } @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); } } - cursor.close(); + } finally { + if (cursor != null) { + cursor.close(); + } + } + setMessageDesc(message); + checkStarMessage(message); + if (isAddReplied && message.isReply()) { + addRepliedToMessage(message); } return message; } @@ -1942,17 +2108,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 +2140,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,22 +2248,28 @@ 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); + } + } + } 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; } } } @@ -2153,37 +2336,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 +2415,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 +2583,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 +2688,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 +2764,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,65 +2820,81 @@ 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); + } } - cursor.close(); - 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(); - 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()) { + members.add(MemberTable.parseCursor(cursor)); } } - cursor.close(); + } finally { + if (cursor != null) { + 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); + } } } } @@ -2854,14 +3089,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 +3111,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 +3221,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 +3312,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 +3360,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,15 +4119,21 @@ 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); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - cursor.close(); - setStaticMessagesDesc(list, type); } + setStaticMessagesDesc(list, type); return list; } @@ -4037,13 +4301,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 +4354,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 +4382,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 +4414,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 +4503,25 @@ 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); + } + } + 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,33 +4543,43 @@ 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 + "'"); + Cursor cursorDesc = null; DescModel stickerDesc = null; - if (cursorDesc != null) { - while (cursorDesc.moveToNext()) { - stickerDesc = DescTable.parseCursor(cursorDesc); + 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 + "'"); + if (cursorDesc != null) { + while (cursorDesc.moveToNext()) { + stickerDesc = DescTable.parseCursor(cursorDesc); + } + } + } finally { + if (cursorDesc != null) { + cursorDesc.close(); } - 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); + 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); - } - if (stickerDesc != null) { Sticker sticker = DescManager.getStickerFromDesc(context, stickerDesc); if (sticker != null) { result.add(sticker); @@ -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 ""; }