From 03ed6c057d538dd5ddf1eb2637c4781e79524045 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 17 Jan 2020 08:54:16 +0100 Subject: [PATCH] Fixed/improved quit sequence --- .../eu/faircode/email/ServiceSynchronize.java | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index cc930508fc..8f6a2cbc43 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -102,7 +102,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences private MediatorState liveAccountNetworkState = new MediatorState(); private static final long YIELD_DURATION = 200L; // milliseconds - private static final long QUIT_DELAY = 10 * 1000L; // milliseconds + private static final long QUIT_DELAY = 5 * 1000L; // milliseconds private static final int CONNECT_BACKOFF_START = 8; // seconds private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes) private static final int CONNECT_BACKOFF_AlARM = 15; // minutes @@ -171,6 +171,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences liveAccountNetworkState.observeForever(new Observer>() { private boolean fts = false; + private Integer lastQuitId = null; private List accountStates = new ArrayList<>(); private ExecutorService queue = Helper.getBackgroundExecutor(1, "service"); @@ -255,21 +256,21 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences delete(current); } - if (runService) { - if (lastAccounts != accounts || lastOperations != operations) { - lastAccounts = accounts; - lastOperations = operations; - if (operations == 0) { - fts = true; - WorkerFts.init(ServiceSynchronize.this, false); - } else if (fts) { - fts = false; - WorkerFts.cancel(ServiceSynchronize.this); - } - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build()); + if (lastAccounts != accounts || lastOperations != operations) { + lastAccounts = accounts; + lastOperations = operations; + if (operations == 0) { + fts = true; + WorkerFts.init(ServiceSynchronize.this, false); + } else if (fts) { + fts = false; + WorkerFts.cancel(ServiceSynchronize.this); } - } else + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build()); + } + + if (!runService) quit(lastStartId); } } @@ -353,30 +354,34 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } private void quit(final Integer startId) { - EntityLog.log(ServiceSynchronize.this, "Service quit"); + if (lastQuitId != null && lastQuitId.equals(startId)) + return; + lastQuitId = startId; + + EntityLog.log(ServiceSynchronize.this, "Service quit=" + startId); queue.submit(new Runnable() { @Override public void run() { Log.i("### quit"); - try { - Thread.sleep(QUIT_DELAY); - } catch (InterruptedException ex) { - Log.w(ex); - } - - if (startId != null) { - stopSelf(startId); - - if (startId.equals(lastStartId)) { - DB db = DB.getInstance(ServiceSynchronize.this); - List ops = db.operation().getOperations(EntityOperation.SYNC); - for (EntityOperation op : ops) - db.folder().setFolderSyncState(op.folder, null); + if (startId == null) { + // Service destroy + DB db = DB.getInstance(ServiceSynchronize.this); + List ops = db.operation().getOperations(EntityOperation.SYNC); + for (EntityOperation op : ops) + db.folder().setFolderSyncState(op.folder, null); + } else { + // Delay for widget updates + try { + Thread.sleep(QUIT_DELAY); + } catch (InterruptedException ex) { + Log.w(ex); } - EntityLog.log(ServiceSynchronize.this, "### quit requested"); + // Stop service + stopSelf(startId); + EntityLog.log(ServiceSynchronize.this, "Service quited=" + startId); } } });