|
|
@ -104,9 +104,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
));
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
private static final List<String> PREF_RELOAD = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
private static final List<String> PREF_RELOAD = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"metered", "roaming", "rlah",
|
|
|
|
"metered", "roaming", "rlah", // force reconnect
|
|
|
|
"socks_enabled", "socks_proxy",
|
|
|
|
"socks_enabled", "socks_proxy", // force reconnect
|
|
|
|
"subscribed_only", "debug"
|
|
|
|
"subscribed_only", // force folder sync
|
|
|
|
|
|
|
|
"badge", "unseen_ignored", // force update badge/widget
|
|
|
|
|
|
|
|
"debug" // force reconnect
|
|
|
|
));
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
static final int PI_ALARM = 1;
|
|
|
|
static final int PI_ALARM = 1;
|
|
|
@ -153,6 +155,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
postValue(result);
|
|
|
|
postValue(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void postDestroy() {
|
|
|
|
|
|
|
|
postValue(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -198,7 +204,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
liveAccountNetworkState.observe(this, new Observer<List<TupleAccountNetworkState>>() {
|
|
|
|
Log.i("### observe");
|
|
|
|
|
|
|
|
liveAccountNetworkState.observeForever(new Observer<List<TupleAccountNetworkState>>() {
|
|
|
|
boolean running = true;
|
|
|
|
boolean running = true;
|
|
|
|
private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
|
|
|
|
private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
|
|
|
|
private Map<TupleAccountNetworkState, Core.State> serviceStates = new Hashtable<>();
|
|
|
|
private Map<TupleAccountNetworkState, Core.State> serviceStates = new Hashtable<>();
|
|
|
@ -206,69 +213,81 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onChanged(List<TupleAccountNetworkState> accountNetworkStates) {
|
|
|
|
public void onChanged(List<TupleAccountNetworkState> accountNetworkStates) {
|
|
|
|
if (!running) {
|
|
|
|
if (accountNetworkStates == null) {
|
|
|
|
Log.i("### not running");
|
|
|
|
for (TupleAccountNetworkState prev : serviceStates.keySet())
|
|
|
|
return;
|
|
|
|
stop(prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
quit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
accountStates.clear();
|
|
|
|
|
|
|
|
serviceStates.clear();
|
|
|
|
|
|
|
|
liveAccountNetworkState.removeObserver(this);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (!running) {
|
|
|
|
|
|
|
|
Log.i("### not running");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
boolean runService = false;
|
|
|
|
int connected = 0;
|
|
|
|
|
|
|
|
int pending = 0;
|
|
|
|
int connected = 0;
|
|
|
|
boolean runService = false;
|
|
|
|
int pending = 0;
|
|
|
|
for (TupleAccountNetworkState current : accountNetworkStates) {
|
|
|
|
for (TupleAccountNetworkState current : accountNetworkStates) {
|
|
|
|
if (current.accountState.shouldRun(current.enabled))
|
|
|
|
if (current.accountState.shouldRun(current.enabled))
|
|
|
|
runService = true;
|
|
|
|
runService = true;
|
|
|
|
if ("connected".equals(current.accountState.state))
|
|
|
|
if ("connected".equals(current.accountState.state))
|
|
|
|
connected++;
|
|
|
|
connected++;
|
|
|
|
pending += current.accountState.operations;
|
|
|
|
pending += current.accountState.operations;
|
|
|
|
|
|
|
|
|
|
|
|
int index = accountStates.indexOf(current);
|
|
|
|
int index = accountStates.indexOf(current);
|
|
|
|
if (index < 0) {
|
|
|
|
if (index < 0) {
|
|
|
|
if (current.canRun()) {
|
|
|
|
if (current.canRun()) {
|
|
|
|
Log.i("### new " + current);
|
|
|
|
Log.i("### new " + current);
|
|
|
|
|
|
|
|
start(current, current.accountState.isEnabled(current.enabled));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
TupleAccountNetworkState prev = accountStates.get(index);
|
|
|
|
|
|
|
|
accountStates.remove(index);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Core.State state = serviceStates.get(current);
|
|
|
|
|
|
|
|
if (state != null)
|
|
|
|
|
|
|
|
state.setNetworkState(current.networkState);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: reload disconnected account on new network available
|
|
|
|
|
|
|
|
// !"connected".equals(current.accountState.state))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (current.reload ||
|
|
|
|
|
|
|
|
prev.canRun() != current.canRun() ||
|
|
|
|
|
|
|
|
!prev.accountState.equals(current.accountState)) {
|
|
|
|
|
|
|
|
Log.i("### changed " + current +
|
|
|
|
|
|
|
|
" reload=" + current.reload +
|
|
|
|
|
|
|
|
" run prev=" + prev.canRun() +
|
|
|
|
|
|
|
|
" run cur=" + current.canRun() +
|
|
|
|
|
|
|
|
" changed=" + !prev.accountState.equals(current.accountState));
|
|
|
|
|
|
|
|
if (prev.canRun())
|
|
|
|
|
|
|
|
stop(prev);
|
|
|
|
|
|
|
|
if (current.canRun())
|
|
|
|
|
|
|
|
start(current, current.accountState.isEnabled(current.enabled));
|
|
|
|
start(current, current.accountState.isEnabled(current.enabled));
|
|
|
|
if (current.accountState.tbd != null)
|
|
|
|
}
|
|
|
|
delete(current);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
TupleAccountNetworkState prev = accountStates.get(index);
|
|
|
|
|
|
|
|
accountStates.remove(index);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Core.State state = serviceStates.get(current);
|
|
|
|
|
|
|
|
if (state != null)
|
|
|
|
|
|
|
|
state.setNetworkState(current.networkState);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: reload disconnected account on new network available
|
|
|
|
|
|
|
|
// !"connected".equals(current.accountState.state))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (current.reload ||
|
|
|
|
|
|
|
|
prev.canRun() != current.canRun() ||
|
|
|
|
|
|
|
|
!prev.accountState.equals(current.accountState)) {
|
|
|
|
|
|
|
|
Log.i("### changed " + current +
|
|
|
|
|
|
|
|
" reload=" + current.reload +
|
|
|
|
|
|
|
|
" run prev=" + prev.canRun() +
|
|
|
|
|
|
|
|
" run cur=" + current.canRun() +
|
|
|
|
|
|
|
|
" changed=" + !prev.accountState.equals(current.accountState));
|
|
|
|
|
|
|
|
if (prev.canRun())
|
|
|
|
|
|
|
|
stop(prev);
|
|
|
|
|
|
|
|
if (current.canRun())
|
|
|
|
|
|
|
|
start(current, current.accountState.isEnabled(current.enabled));
|
|
|
|
|
|
|
|
if (current.accountState.tbd != null)
|
|
|
|
|
|
|
|
delete(current);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (current.accountState.tbd == null)
|
|
|
|
if (current.accountState.tbd == null)
|
|
|
|
accountStates.add(current);
|
|
|
|
accountStates.add(current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (accounts != connected || operations != pending) {
|
|
|
|
if (runService) {
|
|
|
|
accounts = connected;
|
|
|
|
if (accounts != connected || operations != pending) {
|
|
|
|
operations = pending;
|
|
|
|
accounts = connected;
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
operations = pending;
|
|
|
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(accounts, operations).build());
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(accounts, operations).build());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
|
|
|
|
stopSelf();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!runService)
|
|
|
|
|
|
|
|
exit();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync) {
|
|
|
|
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync) {
|
|
|
@ -359,14 +378,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void exit() {
|
|
|
|
private void quit() {
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Service exit");
|
|
|
|
EntityLog.log(ServiceSynchronize.this, "Service quit");
|
|
|
|
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
|
|
|
|
queue.submit(new Runnable() {
|
|
|
|
queue.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
Log.i("### exit");
|
|
|
|
Log.i("### quit");
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
@ -374,7 +392,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
db.folder().setFolderSyncState(op.folder, null);
|
|
|
|
db.folder().setFolderSyncState(op.folder, null);
|
|
|
|
|
|
|
|
|
|
|
|
stopSelf();
|
|
|
|
stopSelf();
|
|
|
|
Log.i("### exited");
|
|
|
|
Log.i("### quited");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -523,7 +541,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
cm.unregisterNetworkCallback(networkCallback);
|
|
|
|
cm.unregisterNetworkCallback(networkCallback);
|
|
|
|
|
|
|
|
|
|
|
|
liveAccountNetworkState.post(new ConnectionHelper.NetworkState());
|
|
|
|
liveAccountNetworkState.postDestroy();
|
|
|
|
|
|
|
|
|
|
|
|
setUnseen(null);
|
|
|
|
setUnseen(null);
|
|
|
|
|
|
|
|
|
|
|
@ -1486,9 +1504,4 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
new Intent(context, ServiceSynchronize.class)
|
|
|
|
.setAction("reset"));
|
|
|
|
.setAction("reset"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void restart(Context context, String reason) {
|
|
|
|
|
|
|
|
context.stopService(new Intent(context, ServiceSynchronize.class));
|
|
|
|
|
|
|
|
eval(context, false, reason);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|