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,31 +2076,38 @@ 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() {
@Override
public void run() {
try {
Network active = ConnectionHelper.getActiveNetwork(ServiceSynchronize.this);
if (active != null && !active.equals(lastActive)) { if (active != null && !active.equals(lastActive)) {
if (ConnectionHelper.isConnected(this, active)) { if (ConnectionHelper.isConnected(ServiceSynchronize.this, active)) {
EntityLog.log(this, reason + ": new active network=" + active + "/" + lastActive); EntityLog.log(ServiceSynchronize.this,
reason + ": new active network=" + active + "/" + lastActive);
lastActive = active; lastActive = active;
} }
} else if (lastActive != null) { } else if (lastActive != null) {
if (!ConnectionHelper.isConnected(this, lastActive)) { if (!ConnectionHelper.isConnected(ServiceSynchronize.this, lastActive)) {
EntityLog.log(this, reason + ": lost active network=" + lastActive); EntityLog.log(ServiceSynchronize.this,
reason + ": lost active network=" + lastActive);
lastActive = null; lastActive = null;
lastLost = new Date().getTime(); lastLost = new Date().getTime();
} }
} }
if (Objects.equals(network, active)) { if (network == null || Objects.equals(network, active)) {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(this); ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
if (!Objects.equals(lastNetworkState, ns)) { if (!Objects.equals(lastNetworkState, ns)) {
EntityLog.log(this, reason + ": updating state network=" + active + EntityLog.log(ServiceSynchronize.this,
" info=" + ConnectionHelper.getNetworkInfo(this, active) + " " + ns); reason + ": updating state network=" + active +
" info=" + ConnectionHelper.getNetworkInfo(ServiceSynchronize.this, active) + " " + ns);
lastNetworkState = ns; lastNetworkState = ns;
liveNetworkState.postValue(ns); liveNetworkState.postValue(ns);
} }
@ -2109,9 +2116,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable()); boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable());
if (lastSuitable == null || lastSuitable != isSuitable) { if (lastSuitable == null || lastSuitable != isSuitable) {
lastSuitable = isSuitable; lastSuitable = isSuitable;
EntityLog.log(this, reason + ": updated suitable=" + lastSuitable); EntityLog.log(ServiceSynchronize.this, reason + ": updated suitable=" + lastSuitable);
if (!isBackgroundService(this)) if (!isBackgroundService(ServiceSynchronize.this))
try { try {
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());
@ -2119,6 +2126,11 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.w(ex); Log.w(ex);
} }
} }
} catch (Throwable ex) {
Log.e(ex);
}
}
});
} }
private BroadcastReceiver idleModeChangedReceiver = new BroadcastReceiver() { private BroadcastReceiver idleModeChangedReceiver = new BroadcastReceiver() {

Loading…
Cancel
Save