From 4191add06fe3b9b0128a41928377d65be7df132e Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 16 Jan 2021 19:21:31 +0100 Subject: [PATCH] Queue update network state --- .../eu/faircode/email/ServiceSynchronize.java | 86 +++++++++++-------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index e5bf886134..92b7dd9185 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -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(); - } - } + 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 (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); - } - } + 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(); + } + } - boolean isSuitable = (lastNetworkState != null && lastNetworkState.isSuitable()); - if (lastSuitable == null || lastSuitable != isSuitable) { - lastSuitable = isSuitable; - EntityLog.log(this, reason + ": updated suitable=" + lastSuitable); + 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); + } + } - if (!isBackgroundService(this)) - try { - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(lastAccounts, lastOperations).build()); + 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() {