Update WorkManager (#8772)

This commit is contained in:
Ivan Iskandar 2022-12-19 00:14:06 +07:00 committed by GitHub
parent 46417fe427
commit 8552838bda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 7 deletions

View file

@ -5,7 +5,6 @@
"schedule": ["every sunday"], "schedule": ["every sunday"],
"ignoreDeps": [ "ignoreDeps": [
"androidx.core:core-splashscreen", "androidx.core:core-splashscreen",
"androidx.work:work-runtime-ktx",
"com.android.tools:r8", "com.android.tools:r8",
"com.google.guava:guava", "com.google.guava:guava",
"com.github.commandiron:WheelPickerCompose" "com.github.commandiron:WheelPickerCompose"

View file

@ -6,6 +6,7 @@ import androidx.core.net.toUri
import androidx.work.CoroutineWorker import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingWorkPolicy import androidx.work.ExistingWorkPolicy
import androidx.work.ForegroundInfo
import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkInfo import androidx.work.WorkInfo
@ -25,14 +26,21 @@ import java.util.concurrent.TimeUnit
class BackupCreatorJob(private val context: Context, workerParams: WorkerParameters) : class BackupCreatorJob(private val context: Context, workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams) { CoroutineWorker(context, workerParams) {
private val notifier = BackupNotifier(context)
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
val backupPreferences = Injekt.get<BackupPreferences>() val backupPreferences = Injekt.get<BackupPreferences>()
val notifier = BackupNotifier(context)
val uri = inputData.getString(LOCATION_URI_KEY)?.toUri() val uri = inputData.getString(LOCATION_URI_KEY)?.toUri()
?: backupPreferences.backupsDirectory().get().toUri() ?: backupPreferences.backupsDirectory().get().toUri()
val flags = inputData.getInt(BACKUP_FLAGS_KEY, BackupConst.BACKUP_ALL) val flags = inputData.getInt(BACKUP_FLAGS_KEY, BackupConst.BACKUP_ALL)
val isAutoBackup = inputData.getBoolean(IS_AUTO_BACKUP_KEY, true) 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()) context.notificationManager.notify(Notifications.ID_BACKUP_PROGRESS, notifier.showBackupProgress().build())
return try { return try {
val location = BackupManager(context).createBackup(uri, flags, isAutoBackup) 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 { companion object {
fun isManualJobRunning(context: Context): Boolean { fun isManualJobRunning(context: Context): Boolean {
val list = WorkManager.getInstance(context).getWorkInfosByTag(TAG_MANUAL).get() 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)) .setInputData(workDataOf(IS_AUTO_BACKUP_KEY to true))
.build() .build()
workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.REPLACE, request) workManager.enqueueUniquePeriodicWork(TAG_AUTO, ExistingPeriodicWorkPolicy.UPDATE, request)
} else { } else {
workManager.cancelUniqueWork(TAG_AUTO) workManager.cancelUniqueWork(TAG_AUTO)
} }

View file

@ -59,7 +59,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
.setConstraints(constraints) .setConstraints(constraints)
.build() .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 { } else {
WorkManager.getInstance(context).cancelAllWorkByTag(TAG) WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
} }

View file

@ -51,7 +51,7 @@ class AppUpdateJob(private val context: Context, workerParams: WorkerParameters)
.setConstraints(constraints) .setConstraints(constraints)
.build() .build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.UPDATE, request)
} }
fun cancelTask(context: Context) { fun cancelTask(context: Context) {

View file

@ -85,7 +85,7 @@ class ExtensionUpdateJob(private val context: Context, workerParams: WorkerParam
.setConstraints(constraints) .setConstraints(constraints)
.build() .build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) WorkManager.getInstance(context).enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.UPDATE, request)
} else { } else {
WorkManager.getInstance(context).cancelAllWorkByTag(TAG) WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
} }

View file

@ -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-process = { module = "androidx.lifecycle:lifecycle-process", version.ref = "lifecycle_version" }
lifecycle-runtimektx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", 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" guava = "com.google.guava:guava:31.1-android"
paging-runtime = "androidx.paging:paging-runtime:3.1.1" paging-runtime = "androidx.paging:paging-runtime:3.1.1"