From 8552838bdae29b6b6001b5efd958dd00fcdf7443 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Mon, 19 Dec 2022 00:14:06 +0700 Subject: [PATCH] Update WorkManager (#8772) --- .github/renovate.json | 1 - .../tachiyomi/data/backup/BackupCreatorJob.kt | 16 ++++++++++++++-- .../tachiyomi/data/library/LibraryUpdateJob.kt | 4 +++- .../tachiyomi/data/updater/AppUpdateJob.kt | 2 +- .../tachiyomi/extension/ExtensionUpdateJob.kt | 2 +- gradle/androidx.versions.toml | 2 +- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.github/renovate.json b/.github/renovate.json index 8a719f245..6abe04a22 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -5,7 +5,6 @@ "schedule": ["every sunday"], "ignoreDeps": [ "androidx.core:core-splashscreen", - "androidx.work:work-runtime-ktx", "com.android.tools:r8", "com.google.guava:guava", "com.github.commandiron:WheelPickerCompose" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt index c920191f7..2a5aec38e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt @@ -6,6 +6,7 @@ import androidx.core.net.toUri import androidx.work.CoroutineWorker import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingWorkPolicy +import androidx.work.ForegroundInfo import androidx.work.OneTimeWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkInfo @@ -25,14 +26,21 @@ import java.util.concurrent.TimeUnit class BackupCreatorJob(private val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) { + private val notifier = BackupNotifier(context) + override suspend fun doWork(): Result { val backupPreferences = Injekt.get() - val notifier = BackupNotifier(context) val uri = inputData.getString(LOCATION_URI_KEY)?.toUri() ?: backupPreferences.backupsDirectory().get().toUri() val flags = inputData.getInt(BACKUP_FLAGS_KEY, BackupConst.BACKUP_ALL) val isAutoBackup = inputData.getBoolean(IS_AUTO_BACKUP_KEY, true) + try { + setForeground(getForegroundInfo()) + } catch (e: IllegalStateException) { + logcat(LogPriority.ERROR, e) { "Not allowed to run on foreground service" } + } + context.notificationManager.notify(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build()) return try { val location = BackupManager(context).createBackup(uri, flags, isAutoBackup) @@ -47,6 +55,10 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet } } + override suspend fun getForegroundInfo(): ForegroundInfo { + return ForegroundInfo(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build()) + } + companion object { fun isManualJobRunning(context: Context): Boolean { val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG_MANUAL).get() @@ -68,7 +80,7 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet .setInputData(workDataOf(IS_AUTO_BACKUP_KEY to true)) .build() - workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.REPLACE, request) + workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request) } else { workManager.cancelUniqueWork(TAG_AUTO) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt index c5e10b036..51759c3d1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt @@ -59,7 +59,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet .setConstraints(constraints) .build() - WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) + // Re-enqueue work because of common support suggestion to change + // the settings on the desired time to schedule it at that time + WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, request) } else { WorkManager.getInstance(context).cancelAllWorkByTag(TAG) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt index 9786fa0d2..602c173ec 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AppUpdateJob.kt @@ -51,7 +51,7 @@ class AppUpdateJob(private val context: Context, workerParams: WorkerParameters) .setConstraints(constraints) .build() - WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) + WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.UPDATE, request) } fun cancelTask(context: Context) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionUpdateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionUpdateJob.kt index a07a75b67..f0a02954f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionUpdateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionUpdateJob.kt @@ -85,7 +85,7 @@ class ExtensionUpdateJob(private val context: Context, workerParams: WorkerParam .setConstraints(constraints) .build() - WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) + WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.UPDATE, request) } else { WorkManager.getInstance(context).cancelAllWorkByTag(TAG) } diff --git a/gradle/androidx.versions.toml b/gradle/androidx.versions.toml index 874fafe9d..f32e0145c 100644 --- a/gradle/androidx.versions.toml +++ b/gradle/androidx.versions.toml @@ -19,7 +19,7 @@ lifecycle-common = { module = "androidx.lifecycle:lifecycle-common", version.ref lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "lifecycle_version" } lifecycle-runtimektx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycle_version" } -work-runtime = "androidx.work:work-runtime-ktx:2.6.0" +work-runtime = "androidx.work:work-runtime-ktx:2.8.0-rc01" guava = "com.google.guava:guava:31.1-android" paging-runtime = "androidx.paging:paging-runtime:3.1.1"