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,7 +256,6 @@ 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;
@ -269,7 +269,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build()); nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
} }
} else
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");
if (startId == null) {
// Service destroy
DB db = DB.getInstance(ServiceSynchronize.this);
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
for (EntityOperation op : ops)
db.folder().setFolderSyncState(op.folder, null);
} else {
// Delay for widget updates
try { try {
Thread.sleep(QUIT_DELAY); Thread.sleep(QUIT_DELAY);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Log.w(ex); Log.w(ex);
} }
if (startId != null) { // Stop service
stopSelf(startId); stopSelf(startId);
EntityLog.log(ServiceSynchronize.this, "Service quited=" + startId);
if (startId.equals(lastStartId)) {
DB db = DB.getInstance(ServiceSynchronize.this);
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
for (EntityOperation op : ops)
db.folder().setFolderSyncState(op.folder, null);
}
EntityLog.log(ServiceSynchronize.this, "### quit requested");
} }
} }
}); });

Loading…
Cancel
Save