Always run RestartWorker even without registration

The only reason we had that check was in case of network
change without registrations. The check has moved to this
class
This commit is contained in:
sim 2024-10-29 14:40:41 +00:00
parent c851d69baa
commit 7d8b142ef4
4 changed files with 19 additions and 8 deletions

View File

@ -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()
}

View File

@ -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

View File

@ -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<RestartWorker>().apply {
setInitialDelay(delay, TimeUnit.SECONDS)
}
// Set to null to pass the timeout check.
AppCompanion.lastEventDate = null
WorkManager.getInstance(context).enqueueUniqueWork(
UNIQUE_ONETIME_WORK_TAG,

View File

@ -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)