From fca4a721da597a500e4886c2cabf2456b49cff2f Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 30 Jul 2020 09:40:21 -0500 Subject: [PATCH] Make workers non-cancellable --- app/src/main/java/org/tasks/injection/BaseWorker.kt | 12 ++++++++---- app/src/main/java/org/tasks/jobs/RepeatingWorker.kt | 10 +++++++--- app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/tasks/injection/BaseWorker.kt b/app/src/main/java/org/tasks/injection/BaseWorker.kt index 347594866..1a06b7ab9 100644 --- a/app/src/main/java/org/tasks/injection/BaseWorker.kt +++ b/app/src/main/java/org/tasks/injection/BaseWorker.kt @@ -1,20 +1,24 @@ package org.tasks.injection import android.content.Context -import androidx.work.CoroutineWorker +import androidx.work.Worker import androidx.work.WorkerParameters +import kotlinx.coroutines.runBlocking import org.tasks.analytics.Firebase import timber.log.Timber abstract class BaseWorker( internal val context: Context, workerParams: WorkerParameters, - internal val firebase: Firebase) : CoroutineWorker(context, workerParams) { + internal val firebase: Firebase +) : Worker(context, workerParams) { - override suspend fun doWork(): Result { + override fun doWork(): Result { Timber.d("%s.doWork()", javaClass.simpleName) return try { - run() + runBlocking { + run() + } } catch (e: Exception) { firebase.reportException(e) Result.failure() diff --git a/app/src/main/java/org/tasks/jobs/RepeatingWorker.kt b/app/src/main/java/org/tasks/jobs/RepeatingWorker.kt index 9586a4df8..0f049d397 100644 --- a/app/src/main/java/org/tasks/jobs/RepeatingWorker.kt +++ b/app/src/main/java/org/tasks/jobs/RepeatingWorker.kt @@ -2,17 +2,21 @@ package org.tasks.jobs import android.content.Context import androidx.work.WorkerParameters +import kotlinx.coroutines.runBlocking import org.tasks.analytics.Firebase import org.tasks.injection.BaseWorker abstract class RepeatingWorker internal constructor( context: Context, workerParams: WorkerParameters, - firebase: Firebase) : BaseWorker(context, workerParams, firebase) { + firebase: Firebase +) : BaseWorker(context, workerParams, firebase) { - override suspend fun doWork(): Result { + override fun doWork(): Result { val result = super.doWork() - scheduleNext() + runBlocking { + scheduleNext() + } return result } diff --git a/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt b/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt index 5c3f8e3cc..22b55b92d 100644 --- a/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt +++ b/app/src/main/java/org/tasks/jobs/WorkManagerImpl.kt @@ -210,7 +210,7 @@ class WorkManagerImpl constructor( .build() @SuppressLint("EnqueueWork") - private suspend fun enqueueUnique(key: String, c: Class, time: Long) { + private suspend fun enqueueUnique(key: String, c: Class, time: Long) { val delay = time - DateUtilities.now() val builder = OneTimeWorkRequest.Builder(c) if (delay > 0) {