diff options
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java b/src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java index ea7edb99..4157d6f8 100644 --- a/src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java +++ b/src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java @@ -525,8 +525,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { Cursor cursor = db.query(Conversation.TABLENAME, null, Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID + " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null); - if (cursor.getCount() == 0) + if (cursor.getCount() == 0) { + cursor.close(); return null; + } cursor.moveToFirst(); Conversation conversation = Conversation.fromCursor(cursor); cursor.close(); @@ -576,12 +578,15 @@ public class DatabaseBackend extends SQLiteOpenHelper { try { cursor.moveToFirst(); int count = cursor.getInt(0); - cursor.close(); return (count > 0); } catch (SQLiteCantOpenDatabaseException e) { return true; // better safe than sorry } catch (RuntimeException e) { return true; // better safe than sorry + } finally { + if (cursor != null) { + cursor.close(); + } } } @@ -643,11 +648,12 @@ public class DatabaseBackend extends SQLiteOpenHelper { } public Pair<Long, String> getLastMessageReceived(Account account) { + Cursor cursor = null; try { SQLiteDatabase db = this.getReadableDatabase(); String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) order by messages.timesent desc limit 1"; String[] args = {account.getUuid()}; - Cursor cursor = db.rawQuery(sql, args); + cursor = db.rawQuery(sql, args); if (cursor.getCount() == 0) { return null; } else { @@ -656,6 +662,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { } } catch (Exception e) { return null; + } finally { + if (cursor != null) { + cursor.close(); + } } } |