From f17c932e9f38543f5cd870e85c25e3b939e0a76c Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 28 Oct 2024 11:07:56 +0000 Subject: [PATCH] Fix network callback Correctly check if internet was lost before restarting Set failure counter to 1 from the network callback --- .../distributor/nextpush/api/SSEListener.kt | 6 ++-- .../nextpush/services/FailureHandler.kt | 22 +++++------- .../services/RestartNetworkCallback.kt | 36 ++++++++++++++----- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt index 4eb1879..7559b26 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt @@ -132,9 +132,9 @@ class SSEListener(val context: Context) : EventSourceListener() { } if (!AppCompanion.hasInternet.get()) { Log.d(TAG, "No Internet: do not restart") - if (FailureHandler.once(eventSource)) { - clearDebugVars() - } + // It will be restarted when Internet is back + eventSource.cancel() + clearDebugVars() return } if (FailureHandler.newFail(context, eventSource)) { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt index c95dd38..7c60748 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt @@ -66,21 +66,15 @@ object FailureHandler { } } - // Returns true if the fail is from the current eventSource - fun once(eventSource: EventSource?): Boolean { + /** + * Set the counter of failed events to 1. + * + * Used when there is no Internet access. + */ + fun once() { Log.d(TAG, "once/Eventsource: $eventSource") - // ignore fails from a possible old eventSource - // if we are already reconnected - return if (isCurrentEventSource(eventSource)) { - Log.d(TAG, "EventSource is known or null") - nFails.set(1) - this.eventSource.getAndSet(null)?.cancel() - true - } else { - Log.d(TAG, "This is an old EventSource.") - eventSource?.cancel() - false - } + nFails.set(1) + this.eventSource.getAndSet(null)?.cancel() } fun setMaxFails(context: Context) { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartNetworkCallback.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartNetworkCallback.kt index 0891167..e291db2 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartNetworkCallback.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartNetworkCallback.kt @@ -17,9 +17,8 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network override fun onAvailable(network: Network) { Log.d(TAG, "Network is CONNECTED") - if (FailureHandler.hasFailed(orNeverStart = false)) { - Log.d(TAG, "Available: restarting worker") - RestartWorker.run(context, delay = 0) + if (!AppCompanion.hasInternet.getAndSet(true)) { + backOnline() } } @@ -28,19 +27,40 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network networkCapabilities: NetworkCapabilities ) { if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { - if (AppCompanion.hasInternet.getAndSet(true)) { + if (!AppCompanion.hasInternet.getAndSet(true)) { Log.d(TAG, "Network Capabilities changed") - if (FailureHandler.hasFailed(orNeverStart = false)) { - Log.d(TAG, "Internet Cap: restarting worker") - RestartWorker.run(context, delay = 0) - } // else, it retries in max 2sec + backOnline() } + } else { + noInternet() } } override fun onLost(network: Network) { Log.d(TAG, "Network unavailable") + noInternet() + } + + /** + * When there is no internet, we set the failure count to 1 so it can try to reconnect directly + * when connection is back. + * + * We also set [AppCompanion.hasInternet] to false. + */ + private fun noInternet() { AppCompanion.hasInternet.set(false) + FailureHandler.once() + } + + /** + * When there is internet again, we start the [RestartWorker] if a failure is counted. + */ + private fun backOnline() { + // We first check if there is a fail registered, else a worker will run in 2 seconds + if (FailureHandler.hasFailed(orNeverStart = false)) { + Log.d(TAG, "Restarting worker") + RestartWorker.run(context, delay = 0) + } // else, it retries in max 2sec } fun register() {