increase cursor window size on Android P when restoring messages

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/Config.java                      |  2 
src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java |  7 
src/main/java/eu/siacs/conversations/utils/CursorUtils.java           | 22 
3 files changed, 26 insertions(+), 5 deletions(-)

Detailed changes

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;
 

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");
             }

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);
+            }
+        }
+    }
+
+}