Queue update network state

This commit is contained in:
M66B 2021-01-16 19:21:31 +01:00
parent 86f2bc0d68
commit 4191add06f
1 changed files with 51 additions and 39 deletions

View File

@ -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,49 +2076,61 @@ 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);
if (active != null && !active.equals(lastActive)) {
if (ConnectionHelper.isConnected(this, active)) {
EntityLog.log(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);
lastActive = null;
lastLost = new Date().getTime();
}
}
if (Objects.equals(network, active)) {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(this);
if (!Objects.equals(lastNetworkState, ns)) {
EntityLog.log(this, reason + ": updating state network=" + active +
" info=" + ConnectionHelper.getNetworkInfo(this, active) + " " + ns);
lastNetworkState = ns;
liveNetworkState.postValue(ns);
}
}
boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable());
if (lastSuitable == null || lastSuitable != isSuitable) {
lastSuitable = isSuitable;
EntityLog.log(this, reason + ": updated suitable=" + lastSuitable);
if (!isBackgroundService(this))
private void updateNetworkState(final Network network, final String reason) {
getMainHandler().post(new Runnable() {
@Override
public void run() {
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build());
Network active = ConnectionHelper.getActiveNetwork(ServiceSynchronize.this);
if (active != null && !active.equals(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(ServiceSynchronize.this, lastActive)) {
EntityLog.log(ServiceSynchronize.this,
reason + ": lost active network=" + lastActive);
lastActive = null;
lastLost = new Date().getTime();
}
}
if (network == null || Objects.equals(network, active)) {
ConnectionHelper.NetworkState ns = ConnectionHelper.getNetworkState(ServiceSynchronize.this);
if (!Objects.equals(lastNetworkState, ns)) {
EntityLog.log(ServiceSynchronize.this,
reason + ": updating state network=" + active +
" info=" + ConnectionHelper.getNetworkInfo(ServiceSynchronize.this, active) + " " + ns);
lastNetworkState = ns;
liveNetworkState.postValue(ns);
}
}
boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable());
if (lastSuitable == null || lastSuitable != isSuitable) {
lastSuitable = isSuitable;
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) {
Log.w(ex);
Log.e(ex);
}
}
}
});
}
private BroadcastReceiver idleModeChangedReceiver = new BroadcastReceiver() {