From 842e011b31d05c3ff335351162191cc300b64bb6 Mon Sep 17 00:00:00 2001 From: sim Date: Fri, 24 Feb 2023 19:30:01 +0100 Subject: [PATCH] Make FailureHandler an object --- .../distributor/nextpush/api/SSEListener.kt | 11 +++++++---- .../distributor/nextpush/services/FailureHandler.kt | 10 ++++++---- .../nextpush/services/RestartNetworkCallback.kt | 4 ++-- .../distributor/nextpush/services/RestartWorker.kt | 2 +- .../distributor/nextpush/services/StartService.kt | 12 +++++------- 5 files changed, 21 insertions(+), 18 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 8791a90..7dfe88a 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 @@ -10,6 +10,9 @@ import okhttp3.sse.EventSourceListener 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.RestartWorker +import org.unifiedpush.distributor.nextpush.services.StartService import org.unifiedpush.distributor.nextpush.utils.TAG import java.lang.Exception import java.util.Calendar @@ -22,7 +25,7 @@ class SSEListener(val context: Context) : EventSourceListener() { } override fun onOpen(eventSource: EventSource, response: Response) { - StartService.newEventSource(context, eventSource) + FailureHandler.newEventSource(context, eventSource) StartService.wakeLock?.let { while (it.isHeld) { it.release() @@ -71,7 +74,7 @@ class SSEListener(val context: Context) : EventSourceListener() { return } Log.d(TAG, "onClosed: $eventSource") - StartService.newFail(context, eventSource) + FailureHandler.newFail(context, eventSource) RestartWorker.start(context, delay = 0) } @@ -86,8 +89,8 @@ class SSEListener(val context: Context) : EventSourceListener() { response?.let { Log.d(TAG, "onFailure: ${it.code}") } - StartService.newFail(context, eventSource) - val delay = when (StartService.nFails) { + FailureHandler.newFail(context, eventSource) + val delay = when (FailureHandler.nFails) { 1 -> 2 // 2sec 2 -> 20 // 20sec 3 -> 60 // 1min 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 05a71ee..f42f949 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 @@ -7,12 +7,13 @@ import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.createWarnin import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification import org.unifiedpush.distributor.nextpush.utils.TAG -interface FailureHandler { +object FailureHandler { - var nFails: Int + var nFails = 0 + private set // This is the last eventSource opened - var eventSource: EventSource? + private var eventSource: EventSource? = null fun newEventSource(context: Context, eventSource: EventSource) { Log.d(TAG, "newEvent/Eventsource: $eventSource") @@ -23,7 +24,8 @@ interface FailureHandler { fun newFail(context: Context, eventSource: EventSource?) { Log.d(TAG, "newFail/Eventsource: $eventSource") - // no eventSource or the last opened + // 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++ 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 1de6dd9..32e6de7 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 (StartService.hasFailed(twice = true, orNeverStart = false)) { + if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) { Log.d(TAG, "networkCallback: restarting worker") RestartWorker.start(context, delay = 0) } @@ -25,7 +25,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network networkCapabilities: NetworkCapabilities ) { Log.d(TAG, "Network Capabilities changed") - if (StartService.hasFailed(twice = true, orNeverStart = false)) { + if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) { Log.d(TAG, "networkCallback: restarting worker") RestartWorker.start(context, delay = 0) } // else, it retries in max 2sec diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartWorker.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartWorker.kt index 1d06a01..77b584f 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartWorker.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RestartWorker.kt @@ -39,7 +39,7 @@ class RestartWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, params Log.d(TAG, "restartDate: ${restartDate.time}") if (currentDate.after(restartDate)) { Log.d(TAG, "Restarting") - StartService.setMaxFails(applicationContext) // Max, will keep using the current worker + FailureHandler.setMaxFails(applicationContext) // Max, will keep using the current worker StartService.startListener(applicationContext) } } ?: run { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt index d5c0731..4b48a7c 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt @@ -21,7 +21,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG class StartService : Service() { - companion object : FailureHandler { + companion object { private const val WAKE_LOCK_TAG = "NextPush:StartService:lock" var service: StartService? = null @@ -29,10 +29,10 @@ class StartService : Service() { var wakeLock: PowerManager.WakeLock? = null fun startListener(context: Context) { - if (isServiceStarted && !hasFailed()) return + if (isServiceStarted && !FailureHandler.hasFailed()) return Log.d(TAG, "Starting the Listener") Log.d(TAG, "Service is started: $isServiceStarted") - Log.d(TAG, "nFails: $nFails") + Log.d(TAG, "nFails: $FailureHandler.nFails") val serviceIntent = Intent(context, StartService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(serviceIntent) @@ -40,8 +40,6 @@ class StartService : Service() { context.startService(serviceIntent) } } - override var nFails = 0 - override var eventSource: EventSource? = null } private val networkCallback = RestartNetworkCallback(this) @@ -80,7 +78,7 @@ class StartService : Service() { fun stopService(block: () -> Unit = {}) { Log.d(TAG, "Stopping Service") isServiceStarted = false - clearFails() + FailureHandler.clearFails() apiDestroy() networkCallback.unregister() wakeLock?.let { @@ -93,7 +91,7 @@ class StartService : Service() { } private fun startService() { - if (isServiceStarted && !hasFailed()) return + if (isServiceStarted && !FailureHandler.hasFailed()) return isServiceStarted = true try {