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' implementation 'org.jsoup:jsoup:1.12.1'
// Job scheduling // 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 // Changelog
implementation 'com.github.gabrielemariotti.changeloglib:changelog:2.1.0' 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 preferences = Injekt.get<PreferencesHelper>()
val interval = prefInterval ?: preferences.backupInterval().getOrDefault() val interval = prefInterval ?: preferences.backupInterval().getOrDefault()
if (interval > 0) { if (interval > 0) {
val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(interval.toLong(), TimeUnit.HOURS) val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(
interval.toLong(), TimeUnit.HOURS,
10, TimeUnit.MINUTES)
.addTag(TAG) .addTag(TAG)
.build() .build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) 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) .setRequiresCharging(acRestriction)
.build() .build()
val request = PeriodicWorkRequestBuilder<LibraryUpdateJob>(interval.toLong(), TimeUnit.HOURS) val request = PeriodicWorkRequestBuilder<LibraryUpdateJob>(
interval.toLong(), TimeUnit.HOURS,
10, TimeUnit.MINUTES)
.addTag(TAG) .addTag(TAG)
.setConstraints(constraints) .setConstraints(constraints)
.build() .build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) 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) .setRequiredNetworkType(NetworkType.CONNECTED)
.build() .build()
val request = PeriodicWorkRequestBuilder<UpdaterJob>(1, TimeUnit.DAYS) val request = PeriodicWorkRequestBuilder<UpdaterJob>(
1, TimeUnit.DAYS,
1, TimeUnit.HOURS)
.addTag(TAG) .addTag(TAG)
.setConstraints(constraints) .setConstraints(constraints)
.build() .build()

View file

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

View file

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