Queue update network state

pull/191/head
M66B 4 years ago
parent 86f2bc0d68
commit 4191add06f

@ -730,7 +730,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
liveAccountNetworkState.post(command); liveAccountNetworkState.post(command);
} else if (PREF_RELOAD.contains(key) || ConnectionHelper.PREF_NETWORK.contains(key)) { } else if (PREF_RELOAD.contains(key) || ConnectionHelper.PREF_NETWORK.contains(key)) {
if (ConnectionHelper.PREF_NETWORK.contains(key)) if (ConnectionHelper.PREF_NETWORK.contains(key))
updateNetworkState(ConnectionHelper.getActiveNetwork(this), "preference"); updateNetworkState(null, "preference");
Bundle command = new Bundle(); Bundle command = new Bundle();
command.putString("pref", key); command.putString("pref", key);
command.putString("name", "reload"); command.putString("name", "reload");
@ -861,7 +861,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
boolean force = intent.getBooleanExtra("force", false); boolean force = intent.getBooleanExtra("force", false);
if (force) { if (force) {
lastLost = 0; lastLost = 0;
updateNetworkState(ConnectionHelper.getActiveNetwork(this), "force"); updateNetworkState(null, "force");
} }
Bundle command = new Bundle(); Bundle command = new Bundle();
@ -2076,49 +2076,61 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
lastLost = 0; lastLost = 0;
} }
Network active = ConnectionHelper.getActiveNetwork(ServiceSynchronize.this); updateNetworkState(null, "connectivity");
updateNetworkState(active, "connectivity");
} }
}; };
private synchronized void updateNetworkState(Network network, String reason) { private void updateNetworkState(final Network network, final String reason) {
Network active = ConnectionHelper.getActiveNetwork(this); getMainHandler().post(new Runnable() {
if (active != null && !active.equals(lastActive)) { @Override
if (ConnectionHelper.isConnected(this, active)) { public void run() {
EntityLog.log(this, reason + ": new active network=" + active + "/" + lastActive); try {
lastActive = active; Network active = ConnectionHelper.getActiveNetwork(ServiceSynchronize.this);
}
} else if (lastActive != null) {
if (!ConnectionHelper.isConnected(this, lastActive)) {
EntityLog.log(this, reason + ": lost active network=" + lastActive);
lastActive = null;
lastLost = new Date().getTime();
}
}
if (Objects.equals(network, active)) { if (active != null && !active.equals(lastActive)) {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(this); if (ConnectionHelper.isConnected(ServiceSynchronize.this, active)) {
if (!Objects.equals(lastNetworkState, ns)) { EntityLog.log(ServiceSynchronize.this,
EntityLog.log(this, reason + ": updating state network=" + active + reason + ": new active network=" + active + "/" + lastActive);
" info=" + ConnectionHelper.getNetworkInfo(this, active) + " " + ns); lastActive = active;
lastNetworkState = ns; }
liveNetworkState.postValue(ns); } else if (lastActive != null) {
} if (!ConnectionHelper.isConnected(ServiceSynchronize.this, lastActive)) {
} EntityLog.log(ServiceSynchronize.this,
reason + ": lost active network=" + lastActive);
lastActive = null;
lastLost = new Date().getTime();
}
}
boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable()); if (network == null || Objects.equals(network, active)) {
if (lastSuitable == null || lastSuitable != isSuitable) { ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
lastSuitable = isSuitable; if (!Objects.equals(lastNetworkState, ns)) {
EntityLog.log(this, reason + ": updated suitable=" + lastSuitable); EntityLog.log(ServiceSynchronize.this,
reason + ": updating state network=" + active +
" info=" + ConnectionHelper.getNetworkInfo(ServiceSynchronize.this, active) + " " + ns);
lastNetworkState = ns;
liveNetworkState.postValue(ns);
}
}
if (!isBackgroundService(this)) boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable());
try { if (lastSuitable == null || lastSuitable != isSuitable) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); lastSuitable = isSuitable;
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build()); EntityLog.log(ServiceSynchronize.this, reason + ": updated suitable=" + lastSuitable);
if (!isBackgroundService(ServiceSynchronize.this))
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
} catch (Throwable ex) {
Log.w(ex);
}
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.e(ex);
} }
} }
});
} }
private BroadcastReceiver idleModeChangedReceiver = new BroadcastReceiver() { private BroadcastReceiver idleModeChangedReceiver = new BroadcastReceiver() {

Loading…
Cancel
Save