From a99d7a74003d40bb3ec8b670052bfd397312322e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 2 Dec 2019 13:20:53 +0100 Subject: [PATCH] increase cursor window size on Android P when restoring messages --- .../java/eu/siacs/conversations/Config.java | 2 +- .../persistance/DatabaseBackend.java | 7 +++--- .../conversations/utils/CursorUtils.java | 22 +++++++++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/utils/CursorUtils.java diff --git a/src/main/java/eu/siacs/conversations/Config.java b/src/main/java/eu/siacs/conversations/Config.java index b4d7d506e..016f44870 100644 --- a/src/main/java/eu/siacs/conversations/Config.java +++ b/src/main/java/eu/siacs/conversations/Config.java @@ -90,7 +90,7 @@ public final class Config { public static final int REFRESH_UI_INTERVAL = 500; public static final int MAX_DISPLAY_MESSAGE_CHARS = 4096; - public static final int MAX_STORAGE_MESSAGE_CHARS = 1024 * 1024; //1MB + public static final int MAX_STORAGE_MESSAGE_CHARS = 2 * 1024 * 1024; //2MB public static final long MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000; diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index bc4fde0ae..5b0db42d7 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -52,6 +52,7 @@ import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.services.QuickConversationsService; import eu.siacs.conversations.services.ShortcutService; import eu.siacs.conversations.utils.CryptoHelper; +import eu.siacs.conversations.utils.CursorUtils; import eu.siacs.conversations.utils.FtsUtils; import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.Resolver; @@ -754,12 +755,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { null, null, Message.TIME_SENT + " DESC", String.valueOf(limit)); } + CursorUtils.upgradeCursorWindowSize(cursor); while (cursor.moveToNext()) { try { - final Message message = Message.fromCursor(cursor, conversation); - if (message != null) { - list.add(0, message); - } + list.add(0, Message.fromCursor(cursor, conversation)); } catch (Exception e) { Log.e(Config.LOGTAG,"unable to restore message"); } diff --git a/src/main/java/eu/siacs/conversations/utils/CursorUtils.java b/src/main/java/eu/siacs/conversations/utils/CursorUtils.java new file mode 100644 index 000000000..d54bd7c02 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/utils/CursorUtils.java @@ -0,0 +1,22 @@ +package eu.siacs.conversations.utils; + +import android.database.AbstractWindowedCursor; +import android.database.Cursor; +import android.database.CursorWindow; +import android.database.sqlite.SQLiteCursor; + +public class CursorUtils { + + public static void upgradeCursorWindowSize(final Cursor cursor) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) { + if (cursor instanceof AbstractWindowedCursor) { + final AbstractWindowedCursor windowedCursor = (AbstractWindowedCursor) cursor; + windowedCursor.setWindow(new CursorWindow("8k", 8 * 1024 * 1024)); + } + if (cursor instanceof SQLiteCursor) { + ((SQLiteCursor) cursor).setFillWindowForwardOnly(true); + } + } + } + +}