From 2e6672db4feee343096996294d0f0d68da9d9e16 Mon Sep 17 00:00:00 2001 From: sim Date: Sat, 25 Feb 2023 13:38:22 +0100 Subject: [PATCH] Clean how startService is stopped --- .../nextpush/activities/MainActivity.kt | 4 +- .../distributor/nextpush/api/Api.kt | 1 + .../services/RestartNetworkCallback.kt | 9 +++-- .../nextpush/services/StartService.kt | 40 +++++++++---------- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt index 791feaf..d2904a4 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt @@ -30,6 +30,7 @@ import org.unifiedpush.distributor.nextpush.account.AccountUtils.ssoAccount import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteApp import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteDevice import org.unifiedpush.distributor.nextpush.distributor.Distributor.getDb +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 @@ -158,7 +159,8 @@ class MainActivity : AppCompatActivity() { private fun restart() { Log.d(TAG, "Restarting the Listener") - StartService.service?.stopService { + FailureHandler.clearFails() + StartService.stopService { RestartWorker.start(this, delay = 0) } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt index aa1f851..d02e65a 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt @@ -41,6 +41,7 @@ object Api { } fun apiDestroy() { + Log.d(TAG, "Destroying API") provider?.destroyProvider() provider = null source?.cancel() 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 32e6de7..9e8e7b4 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 @@ -10,7 +10,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG import java.lang.Exception class RestartNetworkCallback(val context: Context) : ConnectivityManager.NetworkCallback() { - var connectivityManager: ConnectivityManager? = null + private var connectivityManager: ConnectivityManager? = null override fun onAvailable(network: Network) { Log.d(TAG, "Network is CONNECTED") @@ -32,8 +32,10 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network } fun register() { - Log.d(TAG, "Registering Network Callback") - connectivityManager ?: run { + connectivityManager?.let { + Log.d(TAG, "ConnectivityManager already registered") + } ?: run { + Log.d(TAG, "Registering new ConnectivityManager") try { connectivityManager = ( context.getSystemService(Service.CONNECTIVITY_SERVICE) as ConnectivityManager @@ -47,6 +49,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network } fun unregister() { + Log.d(TAG, "Unregistering ConnectivityManager") connectivityManager?.unregisterNetworkCallback(this) connectivityManager = null } 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 4b48a7c..d0dee70 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 @@ -10,7 +10,6 @@ import android.util.Log import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException import com.nextcloud.android.sso.helper.SingleAccountHelper -import okhttp3.sse.EventSource import org.unifiedpush.distributor.nextpush.account.AccountUtils.nextcloudAppNotInstalledDialog import org.unifiedpush.distributor.nextpush.account.AccountUtils.ssoAccount import org.unifiedpush.distributor.nextpush.api.Api.apiDestroy @@ -21,18 +20,20 @@ import org.unifiedpush.distributor.nextpush.utils.TAG class StartService : Service() { - companion object { + companion object StartServiceCompanion { private const val WAKE_LOCK_TAG = "NextPush:StartService:lock" - var service: StartService? = null + private var service: StartService? = null var isServiceStarted = false + private set var wakeLock: PowerManager.WakeLock? = null + private set fun startListener(context: Context) { if (isServiceStarted && !FailureHandler.hasFailed()) return Log.d(TAG, "Starting the Listener") Log.d(TAG, "Service is started: $isServiceStarted") - Log.d(TAG, "nFails: $FailureHandler.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,6 +41,13 @@ class StartService : Service() { context.startService(serviceIntent) } } + + fun stopService(block: () -> Unit = {}) { + Log.d(TAG, "Stopping Service") + isServiceStarted = false + service?.stopSelf() + block() + } } private val networkCallback = RestartNetworkCallback(this) @@ -65,32 +73,22 @@ class StartService : Service() { } override fun onDestroy() { - Log.d(TAG, "Destroyed") - if (isServiceStarted) { - apiDestroy() - Log.d(TAG, "onDestroy: restarting worker") - RestartWorker.start(this, delay = 0) - } else { - stopService() - } - } - - fun stopService(block: () -> Unit = {}) { - Log.d(TAG, "Stopping Service") - isServiceStarted = false - FailureHandler.clearFails() + Log.d(TAG, "Destroying Service") apiDestroy() - networkCallback.unregister() wakeLock?.let { while (it.isHeld) { it.release() } } - stopSelf() - block() + if (isServiceStarted) { + Log.d(TAG, "onDestroy: restarting worker") + RestartWorker.start(this, delay = 0) + } } private fun startService() { + // If the service is running and we don't have any fail + // In case somehow startService is called when everything is fine if (isServiceStarted && !FailureHandler.hasFailed()) return isServiceStarted = true