diff --git a/app/src/main/java/eu/faircode/email/DaoOperation.java b/app/src/main/java/eu/faircode/email/DaoOperation.java index 786db60752..6d5d081940 100644 --- a/app/src/main/java/eu/faircode/email/DaoOperation.java +++ b/app/src/main/java/eu/faircode/email/DaoOperation.java @@ -72,6 +72,9 @@ public interface DaoOperation { @Query("SELECT * FROM operation ORDER BY id") List getOperations(); + @Query("SELECT * FROM operation WHERE name = :name") + List getOperations(String name); + @Query("SELECT * FROM operation WHERE error IS NOT NULL") List getOperationsError(); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index ce1a5d12d6..af6271c8ee 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -41,6 +41,9 @@ import com.sun.mail.imap.IMAPFolder; import com.sun.mail.imap.IMAPMessage; import com.sun.mail.imap.IMAPStore; +import org.json.JSONArray; +import org.json.JSONException; + import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -1065,6 +1068,8 @@ public class ServiceSynchronize extends LifecycleService { @Override public void run() { + DB db = DB.getInstance(ServiceSynchronize.this); + try { wl.acquire(); @@ -1074,9 +1079,7 @@ public class ServiceSynchronize extends LifecycleService { if (doStop) stop(); - DB db = DB.getInstance(ServiceSynchronize.this); - - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); for (EntityAccount account : db.account().getAccountsTbd()) nm.deleteNotificationChannel(EntityAccount.getNotificationChannelName(account.id)); @@ -1103,6 +1106,17 @@ public class ServiceSynchronize extends LifecycleService { } if (queued == 0 && !isEnabled()) { EntityLog.log(ServiceSynchronize.this, "Service stop"); + List ops = db.operation().getOperations(EntityOperation.SYNC); + for (EntityOperation op : ops) + try { + JSONArray jargs = new JSONArray(op.args); + if (!jargs.getBoolean(3) /* foreground */) { + Log.i("Deleting bacground SYNC args=" + jargs); + db.operation().deleteOperation(op.id); + } + } catch (JSONException ex) { + Log.e(ex); + } stopSelf(); } }