diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-05-04 16:05:01 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-05-04 16:05:01 +0200 |
commit | ad6330cca8c860db7ab4bd084e9d3d040100e444 (patch) | |
tree | 0a5b9a690b0bc96275240ae8ac3bdd5f3af2621a /src/main | |
parent | ba03ee41a83d33ed8550633f9e48eb5808cc8bf8 (diff) |
go forward through cursor in message restore
We have seen some weird CursorIndexNotFoundException that we were unable to reproduce.
We assume that going forward (moveToNext()) through the cursor instead of (moveToPrevious() fixes that issue
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/entities/Message.java | 39 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java | 15 |
2 files changed, 21 insertions, 33 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index 2a864f914..89236b0fd 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -186,35 +186,11 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable } public static Message fromCursor(Cursor cursor, Conversation conversation) { - Jid jid; - try { - String value = cursor.getString(cursor.getColumnIndex(COUNTERPART)); - if (value != null) { - jid = Jid.of(value); - } else { - jid = null; - } - } catch (IllegalArgumentException e) { - jid = null; - } catch (IllegalStateException e) { - return null; // message too long? - } - Jid trueCounterpart; - try { - String value = cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)); - if (value != null) { - trueCounterpart = Jid.of(value); - } else { - trueCounterpart = null; - } - } catch (IllegalArgumentException e) { - trueCounterpart = null; - } return new Message(conversation, cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(CONVERSATION)), - jid, - trueCounterpart, + fromString(cursor.getString(cursor.getColumnIndex(COUNTERPART))), + fromString(cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART))), cursor.getString(cursor.getColumnIndex(BODY)), cursor.getLong(cursor.getColumnIndex(TIME_SENT)), cursor.getInt(cursor.getColumnIndex(ENCRYPTION)), @@ -235,6 +211,17 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable cursor.getInt(cursor.getColumnIndex(FILE_DELETED)) > 0); } + private static Jid fromString(String value) { + try { + if (value != null) { + return Jid.of(value); + } + } catch (IllegalArgumentException e) { + return null; + } + return null; + } + public static Message createStatusMessage(Conversation conversation, String body) { final Message message = new Message(conversation); message.setType(Message.TYPE_STATUS); diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index 37f577079..6fc6d7569 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -780,14 +780,15 @@ public class DatabaseBackend extends SQLiteOpenHelper { null, null, Message.TIME_SENT + " DESC", String.valueOf(limit)); } - if (cursor.getCount() > 0) { - cursor.moveToLast(); - do { - Message message = Message.fromCursor(cursor, conversation); + while (cursor.moveToNext()) { + try { + final Message message = Message.fromCursor(cursor, conversation); if (message != null && !message.isMessageDeleted()) { - list.add(message); + list.add(0, message); } - } while (cursor.moveToPrevious()); + } catch (Exception e) { + Log.e(Config.LOGTAG, "unable to restore message"); + } } cursor.close(); return list; @@ -891,7 +892,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { public List<FilePathInfo> getFilePathInfo() { final SQLiteDatabase db = this.getReadableDatabase(); - final Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID, Message.RELATIVE_FILE_PATH, Message.DELETED}, "type in (1,2,5) and "+Message.RELATIVE_FILE_PATH+" is not null", null, null, null, null); + final Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID, Message.RELATIVE_FILE_PATH, Message.DELETED}, "type in (1,2,5) and " + Message.RELATIVE_FILE_PATH + " is not null", null, null, null, null); final List<FilePathInfo> list = new ArrayList<>(); while (cursor != null && cursor.moveToNext()) { list.add(new FilePathInfo(cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0)); |