Fixed premature quit in some situations

pull/184/head
M66B 4 years ago
parent 954196d018
commit 57402f7cd4

@ -95,7 +95,6 @@ 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;
@ -192,7 +191,8 @@ 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 int lastEventId = 0;
private int lastQuitId = -1;
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");
@ -211,6 +211,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} else { } else {
int accounts = 0; int accounts = 0;
int operations = 0; int operations = 0;
boolean event = false;
boolean runService = false; boolean runService = false;
for (TupleAccountNetworkState current : accountNetworkStates) { for (TupleAccountNetworkState current : accountNetworkStates) {
Log.d("### evaluating " + current); Log.d("### evaluating " + current);
@ -238,6 +239,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
" tbd=" + current.accountState.tbd + " tbd=" + current.accountState.tbd +
" state=" + current.accountState.state + " state=" + current.accountState.state +
" type=" + current.networkState.getType()); " type=" + current.networkState.getType());
event = true;
start(current, current.accountState.isEnabled(current.enabled), false); start(current, current.accountState.isEnabled(current.enabled), false);
} }
} else { } else {
@ -270,26 +272,39 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
" force=" + force + " force=" + force +
" stop=" + prev.canRun() + " stop=" + prev.canRun() +
" start=" + current.canRun() + " start=" + current.canRun() +
" sync=" + current.accountState.isEnabled(current.enabled) + "/" + sync + " sync=" + sync +
" enabled=" + current.accountState.isEnabled(current.enabled) +
" should=" + current.accountState.shouldRun(current.enabled) +
" changed=" + !prev.accountState.equals(current.accountState) + " changed=" + !prev.accountState.equals(current.accountState) +
" enabled=" + current.accountState.synchronize + " synchronize=" + current.accountState.synchronize +
" ondemand=" + current.accountState.ondemand + " ondemand=" + current.accountState.ondemand +
" folders=" + current.accountState.folders + " folders=" + current.accountState.folders +
" ops=" + current.accountState.operations + " ops=" + current.accountState.operations +
" tbd=" + current.accountState.tbd + " tbd=" + current.accountState.tbd +
" state=" + current.accountState.state + " state=" + current.accountState.state +
" type=" + prev.networkState.getType() + "/" + current.networkState.getType()); " type=" + prev.networkState.getType() + "/" + current.networkState.getType());
if (prev.canRun()) if (prev.canRun()) {
event = true;
stop(prev); stop(prev);
if (current.canRun()) }
if (current.canRun()) {
event = true;
start(current, current.accountState.isEnabled(current.enabled) || sync, force); start(current, current.accountState.isEnabled(current.enabled) || sync, force);
}
} }
} }
if (current.accountState.tbd == null) if (current.accountState.tbd == null)
accountStates.add(current); accountStates.add(current);
else else {
event = true;
delete(current); delete(current);
}
}
if (event) {
lastEventId++;
EntityLog.log(ServiceSynchronize.this, "### eventId=" + lastEventId);
} }
if (lastAccounts != accounts || lastOperations != operations) { if (lastAccounts != accounts || lastOperations != operations) {
@ -325,8 +340,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
} }
if (!runService) if (!runService && lastQuitId != lastEventId) {
quit(lastStartId); lastQuitId = lastEventId;
EntityLog.log(ServiceSynchronize.this, "### quitting startId=" + lastEventId);
quit(lastEventId);
}
} }
} }
@ -409,23 +427,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}); });
} }
private void quit(final Integer startId) { private void quit(final Integer eventId) {
if (startId != null && lastOperations > 0)
return;
if (lastQuitId != null && lastQuitId.equals(startId))
return;
lastQuitId = startId;
EntityLog.log(ServiceSynchronize.this, "Service quit startId=" + startId);
queue.submit(new Runnable() { queue.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
Log.i("### quit startId=" + startId); EntityLog.log(ServiceSynchronize.this, "### quit eventId=" + eventId);
if (startId == null) { if (eventId == null) {
// Service destroy // Service destroy
DB db = DB.getInstance(ServiceSynchronize.this); DB db = DB.getInstance(ServiceSynchronize.this);
List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC); List<EntityOperation> ops = db.operation().getOperations(EntityOperation.SYNC);
@ -439,10 +447,14 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.w(ex); Log.w(ex);
} }
if (!eventId.equals(lastEventId)) {
EntityLog.log(ServiceSynchronize.this, "### quit cancelled eventId=" + eventId + "/" + lastEventId);
return;
}
// Stop service // Stop service
boolean stopped = stopSelfResult(startId); stopSelf();
EntityLog.log(ServiceSynchronize.this, EntityLog.log(ServiceSynchronize.this, "### stop self eventId=" + eventId);
"Service quited=" + stopped + " startId=" + startId);
} }
} }
}); });
@ -686,7 +698,6 @@ 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