From a8c4d0d3ce7c5b58e954eba1f1b13c03c6e9c021 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 25 Mar 2019 17:34:48 +0000 Subject: [PATCH] Explicitly send NOOP when polling --- .../eu/faircode/email/ServiceSynchronize.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index cd26ad220c..860ad679ce 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -35,9 +35,11 @@ import android.os.Build; import android.os.Handler; import android.os.PowerManager; +import com.sun.mail.iap.ProtocolException; import com.sun.mail.imap.IMAPFolder; import com.sun.mail.imap.IMAPMessage; import com.sun.mail.imap.IMAPStore; +import com.sun.mail.imap.protocol.IMAPProtocol; import java.io.IOException; import java.text.DateFormat; @@ -504,7 +506,7 @@ public class ServiceSynchronize extends LifecycleService { final IMAPStore istore = (IMAPStore) isession.getStore(account.getProtocol()); - final Map folders = new HashMap<>(); + final Map folders = new HashMap<>(); List idlers = new ArrayList<>(); List owners = new ArrayList<>(); try { @@ -636,7 +638,7 @@ public class ServiceSynchronize extends LifecycleService { db.folder().setFolderState(folder.id, "connecting"); - final Folder ifolder = istore.getFolder(folder.name); + final IMAPFolder ifolder = (IMAPFolder) istore.getFolder(folder.name); try { ifolder.open(Folder.READ_WRITE); } catch (MessagingException ex) { @@ -685,7 +687,7 @@ public class ServiceSynchronize extends LifecycleService { message = Core.synchronizeMessage( ServiceSynchronize.this, account, folder, - (IMAPFolder) ifolder, (IMAPMessage) imessage, + ifolder, (IMAPMessage) imessage, false, db.rule().getEnabledRules(folder.id)); db.setTransactionSuccessful(); @@ -695,7 +697,7 @@ public class ServiceSynchronize extends LifecycleService { if (db.folder().getFolderDownload(folder.id)) Core.downloadMessage(ServiceSynchronize.this, - folder, (IMAPFolder) ifolder, + folder, ifolder, (IMAPMessage) imessage, message.id, state); } catch (MessageRemovedException ex) { Log.w(folder.name, ex); @@ -730,7 +732,7 @@ public class ServiceSynchronize extends LifecycleService { Log.i(folder.name + " messages removed"); for (Message imessage : e.getMessages()) try { - long uid = ((IMAPFolder) ifolder).getUID(imessage); + long uid = ifolder.getUID(imessage); DB db = DB.getInstance(ServiceSynchronize.this); int count = db.message().deleteMessage(folder.id, uid); @@ -775,7 +777,7 @@ public class ServiceSynchronize extends LifecycleService { message = Core.synchronizeMessage( ServiceSynchronize.this, account, folder, - (IMAPFolder) ifolder, (IMAPMessage) e.getMessage(), + ifolder, (IMAPMessage) e.getMessage(), false, db.rule().getEnabledRules(folder.id)); db.setTransactionSuccessful(); @@ -785,7 +787,7 @@ public class ServiceSynchronize extends LifecycleService { if (db.folder().getFolderDownload(folder.id)) Core.downloadMessage(ServiceSynchronize.this, - folder, (IMAPFolder) ifolder, + folder, ifolder, (IMAPMessage) e.getMessage(), message.id, state); } catch (MessageRemovedException ex) { Log.w(folder.name, ex); @@ -819,7 +821,7 @@ public class ServiceSynchronize extends LifecycleService { Log.i(folder.name + " start idle"); while (state.running()) { Log.i(folder.name + " do idle"); - ((IMAPFolder) ifolder).idle(false); + ifolder.idle(false); } } catch (Throwable ex) { Log.e(folder.name, ex); @@ -952,16 +954,25 @@ public class ServiceSynchronize extends LifecycleService { AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); try { while (state.running()) { - if (!istore.isConnected()) + if (!istore.isConnected()) // Sends store NOOP throw new StoreClosedException(istore); for (EntityFolder folder : folders.keySet()) if (folder.synchronize) if (!folder.poll && capIdle) { - if (!folders.get(folder).isOpen()) + if (!folders.get(folder).isOpen()) // Sends folder NOOP throw new FolderClosedException(folders.get(folder)); - } else + } else { + folders.get(folder).doCommand(new IMAPFolder.ProtocolCommand() { + @Override + public Object doCommand(IMAPProtocol protocol) throws ProtocolException { + protocol.noop(); + return null; + } + }); + EntityOperation.sync(this, folder.id, false); + } // Successfully connected: reset back off time backoff = CONNECT_BACKOFF_START;