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 6a5bf94..e1bacd4 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 @@ -12,6 +12,7 @@ import org.unifiedpush.distributor.nextpush.api.response.SSEResponse import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteAppFromSSE import org.unifiedpush.distributor.nextpush.distributor.Distributor.sendMessage import org.unifiedpush.distributor.nextpush.services.FailureHandler +import org.unifiedpush.distributor.nextpush.services.RestartNetworkCallback import org.unifiedpush.distributor.nextpush.services.RestartWorker import org.unifiedpush.distributor.nextpush.services.StartService import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.createStartErrorNotification @@ -85,6 +86,11 @@ class SSEListener(val context: Context) : EventSourceListener() { response?.let { Log.d(TAG, "onFailure: ${it.code}") } + if (!RestartNetworkCallback.hasInternet) { + Log.d(TAG, "No Internet: do not restart") + FailureHandler.once(eventSource) + return + } FailureHandler.newFail(context, eventSource) val delay = when (FailureHandler.nFails) { 1 -> 2 // 2sec 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 a41e406..83807a8 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 @@ -29,13 +29,24 @@ object FailureHandler { if (this.eventSource == null || this.eventSource == eventSource) { Log.d(TAG, "EventSource is known or null") nFails++ - if (hasFailed(twice = true)) { + if (nFails == 2) { createWarningNotification(context) } this.eventSource = null } } + fun once(eventSource: EventSource?) { + Log.d(TAG, "once/Eventsource: $eventSource") + // ignore fails from a possible old eventSource + // if we are already reconnected + if (this.eventSource == null || this.eventSource == eventSource) { + Log.d(TAG, "EventSource is known or null") + nFails = 1 + this.eventSource = null + } + } + fun setMaxFails(context: Context) { // We set nFails to max to not restart the worker // and keep it running @@ -49,9 +60,8 @@ object FailureHandler { eventSource = null } - fun hasFailed(twice: Boolean = false, orNeverStart: Boolean = true): Boolean { + fun hasFailed(orNeverStart: Boolean = true): Boolean { // nFails > 0 to be sure it is not actually restarting - return if (orNeverStart) { eventSource == null } else { false } || - nFails > if (twice) { 1 } else { 0 } + return if (orNeverStart) { eventSource == null } else { false } || nFails > 0 } } 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 31b1d12..7d4dc2c 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 @@ -14,7 +14,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network override fun onAvailable(network: Network) { Log.d(TAG, "Network is CONNECTED") - if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) { + if (FailureHandler.hasFailed(orNeverStart = false)) { Log.d(TAG, "Available: restarting worker") RestartWorker.run(context, delay = 0) } @@ -28,7 +28,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network if (!hasInternet) { hasInternet = true Log.d(TAG, "Network Capabilities changed") - if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) { + if (FailureHandler.hasFailed(orNeverStart = false)) { Log.d(TAG, "Internet Cap: restarting worker") RestartWorker.run(context, delay = 0) } // else, it retries in max 2sec @@ -71,6 +71,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network companion object { private var registered = false - private var hasInternet = false + var hasInternet = false + private set } }