diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RegistrationCountCache.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RegistrationCountCache.kt index ef44bfa..a1438f0 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RegistrationCountCache.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/RegistrationCountCache.kt @@ -19,11 +19,11 @@ object RegistrationCountCache { nRegistrations = null val count = getCount(context) Log.d(TAG, "Refreshing nRegistrations to $count") - // if n == 1: start the SSE (The fg service starts on boot, but doesn't connect without reg) + // if n == 1: start the SSE (The fg service starts on boot, but doesn't connect without registration) // if n == 0: We keep the foreground service but stop the SSE/RestarWorker // The Runner will starts StartService, which will disconnect the SSE if (count in intArrayOf(0, 1)) { - RestartWorker.run(context, delay = 0, force = true) + RestartWorker.run(context, delay = 0) } ForegroundNotification(context).update() } 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 e291db2..53f239b 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 @@ -53,11 +53,12 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network } /** - * When there is internet again, we start the [RestartWorker] if a failure is counted. + * When there is internet again, we start the [RestartWorker] if a failure is counted + * and there is at least one registration */ 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)) { + if (FailureHandler.hasFailed(orNeverStart = false) && RegistrationCountCache.oneOrMore(context)) { Log.d(TAG, "Restarting worker") RestartWorker.run(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 bc67bed..ec7dfba 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 @@ -65,12 +65,19 @@ class RestartWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, params ) } - fun run(context: Context, delay: Long, force: Boolean = false) { + /** + * Run [RestartWorker.doWork] in [delay] seconds. + * + * We set [AppCompanion.lastEventDate] to `null` to pass the timeout check. + * + * @param delay Delay in ms before running [doWork] to start [StartService] + */ + fun run(context: Context, delay: Long) { getAccount(context) ?: return - if (!force && !RegistrationCountCache.oneOrMore(context)) return val work = OneTimeWorkRequestBuilder().apply { setInitialDelay(delay, TimeUnit.SECONDS) } + // Set to null to pass the timeout check. AppCompanion.lastEventDate = null WorkManager.getInstance(context).enqueueUniqueWork( UNIQUE_ONETIME_WORK_TAG, 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 5d4267d..f72cdca 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 @@ -101,16 +101,19 @@ class StartService : Service() { private set fun startListener(context: Context) { + val hasRegistration = RegistrationCountCache.oneOrMore(context) if (isServiceStarted && !FailureHandler.hasFailed() && - // If there is no registration, this is a request to stop the SSE - RegistrationCountCache.oneOrMore(context) + // We return only if there is one or more registration + // because if there is no registration, this is a request to stop the SSE + hasRegistration ) { return } Log.d(TAG, "Starting the Listener") Log.d(TAG, "Service is started: $isServiceStarted") Log.d(TAG, "nFails: ${FailureHandler.nFails()}") + Log.d(TAG, "Has registration: $hasRegistration") val serviceIntent = Intent(context, StartService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(serviceIntent)