Tweak WorkManager task cancellation, add flex times

This commit is contained in:
arkon 2020-03-02 19:04:57 -05:00
parent d873d653d0
commit a3dd5c1e92
6 changed files with 18 additions and 24 deletions

View File

@ -163,7 +163,9 @@ dependencies {
implementation 'org.jsoup:jsoup:1.12.1'
// Job scheduling
implementation 'android.arch.work:work-runtime:2.3.2'
final workManagerVersion = '2.3.2'
implementation "android.arch.work:work-runtime:$workManagerVersion"
implementation "android.arch.work:work-runtime-ktx:$workManagerVersion"
// Changelog
implementation 'com.github.gabrielemariotti.changeloglib:changelog:2.1.0'

View File

@ -32,16 +32,16 @@ class BackupCreatorJob(private val context: Context, workerParams: WorkerParamet
val preferences = Injekt.get<PreferencesHelper>()
val interval = prefInterval ?: preferences.backupInterval().getOrDefault()
if (interval > 0) {
val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(interval.toLong(), TimeUnit.HOURS)
val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(
interval.toLong(), TimeUnit.HOURS,
10, TimeUnit.MINUTES)
.addTag(TAG)
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} else {
WorkManager.getInstance().cancelAllWorkByTag(TAG)
}
}
fun cancelTask() {
WorkManager.getInstance().cancelAllWorkByTag(TAG)
}
}
}

View File

@ -41,17 +41,17 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
.setRequiresCharging(acRestriction)
.build()
val request = PeriodicWorkRequestBuilder<LibraryUpdateJob>(interval.toLong(), TimeUnit.HOURS)
val request = PeriodicWorkRequestBuilder<LibraryUpdateJob>(
interval.toLong(), TimeUnit.HOURS,
10, TimeUnit.MINUTES)
.addTag(TAG)
.setConstraints(constraints)
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} else {
WorkManager.getInstance().cancelAllWorkByTag(TAG)
}
}
fun cancelTask() {
WorkManager.getInstance().cancelAllWorkByTag(TAG)
}
}
}

View File

@ -62,7 +62,9 @@ class UpdaterJob(private val context: Context, workerParams: WorkerParameters) :
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val request = PeriodicWorkRequestBuilder<UpdaterJob>(1, TimeUnit.DAYS)
val request = PeriodicWorkRequestBuilder<UpdaterJob>(
1, TimeUnit.DAYS,
1, TimeUnit.HOURS)
.addTag(TAG)
.setConstraints(constraints)
.build()

View File

@ -105,13 +105,8 @@ class SettingsBackupController : SettingsController() {
summary = "%s"
onChange { newValue ->
// Always cancel the previous task, it seems that sometimes they are not updated
BackupCreatorJob.cancelTask()
val interval = (newValue as String).toInt()
if (interval > 0) {
BackupCreatorJob.setupTask(interval)
}
BackupCreatorJob.setupTask(interval)
true
}
}

View File

@ -86,13 +86,8 @@ class SettingsLibraryController : SettingsController() {
summary = "%s"
onChange { newValue ->
// Always cancel the previous task, it seems that sometimes they are not updated.
LibraryUpdateJob.cancelTask()
val interval = (newValue as String).toInt()
if (interval > 0) {
LibraryUpdateJob.setupTask(interval)
}
LibraryUpdateJob.setupTask(interval)
true
}
}