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 4e40c1d..67d743b 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 @@ -30,8 +30,6 @@ class SSEListener(val context: Context) : EventSourceListener() { it.release() } } - started = false - pinged = false try { Log.d(TAG, "onOpen: " + response.code) } catch (e: Exception) { @@ -85,7 +83,8 @@ class SSEListener(val context: Context) : EventSourceListener() { Log.d(TAG, "onClosed: $eventSource") eventSource.cancel() if (!shouldRestart()) return - FailureHandler.newFail(context, eventSource, started, pinged) + FailureHandler.newFail(context, eventSource) + clearVars() RestartWorker.run(context, delay = 0) } @@ -102,9 +101,11 @@ class SSEListener(val context: Context) : EventSourceListener() { if (!RestartNetworkCallback.hasInternet) { Log.d(TAG, "No Internet: do not restart") FailureHandler.once(eventSource) + clearVars() return } - FailureHandler.newFail(context, eventSource, started, pinged) + FailureHandler.newFail(context, eventSource) + clearVars() val delay = when (FailureHandler.nFails) { 1 -> 2 // 2sec 2 -> 5 // 5sec @@ -133,6 +134,11 @@ class SSEListener(val context: Context) : EventSourceListener() { return true } + private fun clearVars() { + started = false + pinged = false + } + companion object { var lastEventDate: Calendar? = null var keepalive = 900 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 0fc935a..c6d0be3 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 @@ -3,6 +3,7 @@ package org.unifiedpush.distributor.nextpush.services import android.content.Context import android.util.Log import okhttp3.sse.EventSource +import org.unifiedpush.distributor.nextpush.api.SSEListener import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showNoPingNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showWarningNotification @@ -29,7 +30,7 @@ object FailureHandler { nFailsBeforePing = 0 } - fun newFail(context: Context, eventSource: EventSource?, started: Boolean, pinged: Boolean) { + fun newFail(context: Context, eventSource: EventSource?) { Log.d(TAG, "newFail/Eventsource: $eventSource") // ignore fails from a possible old eventSource // if we are already reconnected @@ -39,7 +40,8 @@ object FailureHandler { if (nFails == 2) { showWarningNotification(context) } - if (started && !pinged) { + if (SSEListener.started && !SSEListener.pinged) { + Log.d(TAG, "The service has started and it has never received a ping.") nFailsBeforePing++ if (nFailsBeforePing == 3) { showNoPingNotification(context) @@ -70,6 +72,7 @@ object FailureHandler { fun clearFails() { nFails = 0 + nFailsBeforePing = 0 eventSource = null }