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

Loading…
Cancel
Save