From ad6330cca8c860db7ab4bd084e9d3d040100e444 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 4 May 2019 16:05:01 +0200 Subject: 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 --- .../java/de/pixart/messenger/entities/Message.java | 39 ++++++++-------------- .../messenger/persistance/DatabaseBackend.java | 15 +++++---- 2 files changed, 21 insertions(+), 33 deletions(-) (limited to 'src') 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 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 list = new ArrayList<>(); while (cursor != null && cursor.moveToNext()) { list.add(new FilePathInfo(cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0)); -- cgit v1.2.3