From 55f589bea73ee4ce31f7e973bc9e232dfa875b4d Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 29 Oct 2022 21:47:46 +0200 Subject: [PATCH] Added download operation for fast fetch --- app/src/main/java/eu/faircode/email/Core.java | 36 ++++++++++++++++--- .../java/eu/faircode/email/DaoOperation.java | 1 + .../eu/faircode/email/EntityOperation.java | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index d4c0ccba9e..a4ab4d7ea2 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -297,6 +297,15 @@ class Core { } break; + case EntityOperation.DOWNLOAD: + if (EntityOperation.DOWNLOAD.equals(next.name)) { + JSONArray jnext = new JSONArray(next.args); + // Same uid + if (jargs.getLong(0) == jnext.getLong(0)) + skip = true; + } + break; + case EntityOperation.MOVE: if (group && message.uid != null && @@ -540,6 +549,10 @@ class Core { onRule(context, jargs, message); break; + case EntityOperation.DOWNLOAD: + onDownload(context, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state); + break; + default: throw new IllegalArgumentException("Unknown operation=" + op.name); } @@ -1785,6 +1798,8 @@ class Core { if (download) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean fast_fetch = prefs.getBoolean("fast_fetch", false); + + boolean async = false; if (fast_fetch) { long maxSize = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE); if (maxSize == 0) @@ -1793,19 +1808,23 @@ class Core { if (!message.content) if (state.getNetworkState().isUnmetered() || (message.size != null && message.size < maxSize)) - EntityOperation.queue(context, message, EntityOperation.BODY); + async = true; List attachments = db.attachment().getAttachments(message.id); for (EntityAttachment attachment : attachments) if (!attachment.available) if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize)) - EntityOperation.queue(context, message, EntityOperation.ATTACHMENT, attachment.id); + async = true; if (download_eml && (message.raw == null || !message.raw) && (state.getNetworkState().isUnmetered() || (message.total != null && message.total < maxSize))) - EntityOperation.queue(context, message, EntityOperation.RAW); - } else + async = true; + } + + if (async && message.uid != null) + EntityOperation.queue(context, message, EntityOperation.DOWNLOAD, message.uid); + else downloadMessage(context, account, folder, istore, ifolder, imessage, message.id, state, stats); } } @@ -2933,6 +2952,15 @@ class Core { rule.execute(context, message); } + private static void onDownload(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException, JSONException { + long uid = jargs.getLong(0); + if (!Objects.equals(uid, message.uid)) + throw new IllegalArgumentException("Different uid" + uid + "/" + message.uid); + + MimeMessage imessage = (MimeMessage) ifolder.getMessageByUID(uid); + downloadMessage(context, account, folder, istore, ifolder, imessage, message.id, state, new SyncStats()); + } + private static void onSynchronizeMessages( Context context, JSONArray jargs, EntityAccount account, final EntityFolder folder, diff --git a/app/src/main/java/eu/faircode/email/DaoOperation.java b/app/src/main/java/eu/faircode/email/DaoOperation.java index 72d9d82d82..3f35ac1719 100644 --- a/app/src/main/java/eu/faircode/email/DaoOperation.java +++ b/app/src/main/java/eu/faircode/email/DaoOperation.java @@ -40,6 +40,7 @@ public interface DaoOperation { // Other operations: seen, answered, flag, keyword, label, subscribe, send, rule " WHEN operation.name = '" + EntityOperation.SYNC + "' AND folder.account IS NOT NULL THEN 1" + " WHEN operation.name = '" + EntityOperation.FETCH + "' THEN 2" + + " WHEN operation.name = '" + EntityOperation.DOWNLOAD + "' THEN 3" + " WHEN operation.name = '" + EntityOperation.EXISTS + "' THEN 3" + " WHEN operation.name = '" + EntityOperation.REPORT + "' THEN 3" + " WHEN operation.name = '" + EntityOperation.COPY + "' THEN 4" + diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index 60f2db8d36..e755a135bc 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -103,6 +103,7 @@ public class EntityOperation { static final String PURGE = "purge"; static final String EXPUNGE = "expunge"; static final String REPORT = "report"; + static final String DOWNLOAD = "download"; private static final int MAX_FETCH = 100; // operations private static final long FORCE_WITHIN = 30 * 1000; // milliseconds