From 5a657a09f2309a47e3f0da5ab4725ff1af60088e Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 16 Sep 2018 20:15:43 +0000 Subject: [PATCH] Reduce data usage --- .../eu/faircode/email/ServiceSynchronize.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 7c9f5f0cb1..546e62c153 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -1312,6 +1312,7 @@ public class ServiceSynchronize extends LifecycleService { FetchProfile fp = new FetchProfile(); fp.add(UIDFolder.FetchProfileItem.UID); + fp.add(FetchProfile.Item.FLAGS); ifolder.fetch(imessages, fp); long fetch = SystemClock.elapsedRealtime(); @@ -1341,7 +1342,7 @@ public class ServiceSynchronize extends LifecycleService { } fp.add(FetchProfile.Item.ENVELOPE); - fp.add(FetchProfile.Item.FLAGS); + // fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); // body structure // fp.add(UIDFolder.FetchProfileItem.UID); fp.add(IMAPFolder.FetchProfileItem.HEADERS); @@ -1357,7 +1358,17 @@ public class ServiceSynchronize extends LifecycleService { Log.i(Helper.TAG, folder.name + " update " + from + " .. " + i); Message[] isub = Arrays.copyOfRange(imessages, from, i + 1); - ifolder.fetch(isub, fp); + + // Full fetch new/changed messages only + List full = new ArrayList<>(); + for (Message imessage : isub) { + long uid = ifolder.getUID(imessage); + EntityMessage message = db.message().getMessageByUid(folder.id, uid); + if (message == null) + full.add(imessage); + } + Log.i(Helper.TAG, folder.name + " fetch headers=" + full.size()); + ifolder.fetch(full.toArray(new Message[0]), fp); for (int j = isub.length - 1; j >= 0; j--) try { @@ -1371,7 +1382,9 @@ public class ServiceSynchronize extends LifecycleService { } catch (Throwable ex) { Log.e(Helper.TAG, folder.name + " " + ex + "\n" + Log.getStackTraceString(ex)); } finally { - ((IMAPMessage) isub[j]).invalidateHeaders(); + // Reduce memory usage + if (full.contains(isub[j])) + ((IMAPMessage) isub[j]).invalidateHeaders(); } } @@ -1386,7 +1399,7 @@ public class ServiceSynchronize extends LifecycleService { Log.i(Helper.TAG, folder.name + " download " + from + " .. " + i); Message[] isub = Arrays.copyOfRange(imessages, from, i + 1); - ifolder.fetch(isub, fp); + // Fetch on demand for (int j = isub.length - 1; j >= 0; j--) try {