Fixed/improved quit sequence

pull/172/head
M66B 6 years ago
parent 77e4a1d9cb
commit 03ed6c057d

@ -102,7 +102,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private MediatorState liveAccountNetworkState = new MediatorState(); private MediatorState liveAccountNetworkState = new MediatorState();
private static final long YIELD_DURATION = 200L; // milliseconds 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_START = 8; // seconds
private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes) private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes)
private static final int CONNECT_BACKOFF_AlARM = 15; // 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<List<TupleAccountNetworkState>>() { liveAccountNetworkState.observeForever(new Observer<List<TupleAccountNetworkState>>() {
private boolean fts = false; private boolean fts = false;
private Integer lastQuitId = null;
private List<TupleAccountNetworkState> accountStates = new ArrayList<>(); private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
private ExecutorService queue = Helper.getBackgroundExecutor(1, "service"); private ExecutorService queue = Helper.getBackgroundExecutor(1, "service");
@ -255,21 +256,21 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
delete(current); delete(current);
} }
if (runService) { if (lastAccounts != accounts || lastOperations != operations) {
if (lastAccounts != accounts || lastOperations != operations) { lastAccounts = accounts;
lastAccounts = accounts; lastOperations = operations;
lastOperations = operations; if (operations == 0) {
if (operations == 0) { fts = true;
fts = true; WorkerFts.init(ServiceSynchronize.this, false);
WorkerFts.init(ServiceSynchronize.this, false); } else if (fts) {
} else if (fts) { fts = false;
fts = false; WorkerFts.cancel(ServiceSynchronize.this);
WorkerFts.cancel(ServiceSynchronize.this);
}
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
} }
} else NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
}
if (!runService)
quit(lastStartId); quit(lastStartId);
} }
} }
@ -353,30 +354,34 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
private void quit(final Integer startId) { 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() { queue.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
Log.i("### quit"); Log.i("### quit");
try { if (startId == null) {
Thread.sleep(QUIT_DELAY); // Service destroy
} catch (InterruptedException ex) { DB db = DB.getInstance(ServiceSynchronize.this);
Log.w(ex); List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
} for (EntityOperation op : ops)
db.folder().setFolderSyncState(op.folder, null);
if (startId != null) { } else {
stopSelf(startId); // Delay for widget updates
try {
if (startId.equals(lastStartId)) { Thread.sleep(QUIT_DELAY);
DB db = DB.getInstance(ServiceSynchronize.this); } catch (InterruptedException ex) {
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC); Log.w(ex);
for (EntityOperation op : ops)
db.folder().setFolderSyncState(op.folder, null);
} }
EntityLog.log(ServiceSynchronize.this, "### quit requested"); // Stop service
stopSelf(startId);
EntityLog.log(ServiceSynchronize.this, "Service quited=" + startId);
} }
} }
}); });

Loading…
Cancel
Save