Prevent race condition on quit

pull/172/head
M66B 5 years ago
parent df59723f08
commit a77f73ea71

@ -90,6 +90,7 @@ import me.leolin.shortcutbadger.ShortcutBadger;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND; import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
public class ServiceSynchronize extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener { public class ServiceSynchronize extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private Integer lastStartId = null;
private Boolean lastSuitable = null; private Boolean lastSuitable = null;
private long lastLost = 0; private long lastLost = 0;
private int lastAccounts = 0; private int lastAccounts = 0;
@ -179,7 +180,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
for (TupleAccountNetworkState prev : accountStates) for (TupleAccountNetworkState prev : accountStates)
stop(prev); stop(prev);
quit(); quit(null);
accountStates.clear(); accountStates.clear();
coreStates.clear(); coreStates.clear();
@ -261,7 +262,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build()); nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
} }
} else } else
quit(); quit(lastStartId);
} }
} }
@ -343,7 +344,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}); });
} }
private void quit() { private void quit(final Integer startId) {
EntityLog.log(ServiceSynchronize.this, "Service quit"); EntityLog.log(ServiceSynchronize.this, "Service quit");
queue.submit(new Runnable() { queue.submit(new Runnable() {
@ -357,13 +358,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.w(ex); Log.w(ex);
} }
DB db = DB.getInstance(ServiceSynchronize.this); if (startId != null) {
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC); stopSelf(startId);
for (EntityOperation op : ops)
db.folder().setFolderSyncState(op.folder, null); 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);
}
stopSelf(); EntityLog.log(ServiceSynchronize.this, "### quit requested");
EntityLog.log(ServiceSynchronize.this, "### quited"); }
} }
}); });
} }
@ -577,6 +583,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
lastStartId = startId;
String action = (intent == null ? null : intent.getAction()); String action = (intent == null ? null : intent.getAction());
String reason = (intent == null ? null : intent.getStringExtra("reason")); String reason = (intent == null ? null : intent.getStringExtra("reason"));
EntityLog.log(ServiceSynchronize.this, "### Service command " + intent + EntityLog.log(ServiceSynchronize.this, "### Service command " + intent +

Loading…
Cancel
Save