Migrate to WorkManager

This commit is contained in:
arkon 2020-03-01 13:06:55 -05:00
parent d9934ad8db
commit d873d653d0
6 changed files with 65 additions and 67 deletions

View file

@ -163,10 +163,7 @@ dependencies {
implementation 'org.jsoup:jsoup:1.12.1' implementation 'org.jsoup:jsoup:1.12.1'
// Job scheduling // Job scheduling
implementation 'com.evernote:android-job:1.4.2' 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

@ -8,13 +8,9 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDex import androidx.multidex.MultiDex
import com.evernote.android.job.JobManager
import eu.kanade.tachiyomi.data.backup.BackupCreatorJob
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.updater.UpdaterJob
import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate
import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.LocaleHelper
import org.acra.ACRA import org.acra.ACRA
@ -42,7 +38,6 @@ open class App : Application(), LifecycleObserver {
Injekt.importModule(AppModule(this)) Injekt.importModule(AppModule(this))
setupAcra() setupAcra()
setupJobManager()
setupNotificationChannels() setupNotificationChannels()
LocaleHelper.updateConfiguration(this, resources.configuration) LocaleHelper.updateConfiguration(this, resources.configuration)
@ -72,21 +67,6 @@ open class App : Application(), LifecycleObserver {
ACRA.init(this) ACRA.init(this)
} }
protected open fun setupJobManager() {
try {
JobManager.create(this).addJobCreator { tag ->
when (tag) {
LibraryUpdateJob.TAG -> LibraryUpdateJob()
UpdaterJob.TAG -> UpdaterJob()
BackupCreatorJob.TAG -> BackupCreatorJob()
else -> null
}
}
} catch (e: Exception) {
Timber.w("Can't initialize job manager")
}
}
protected open fun setupNotificationChannels() { protected open fun setupNotificationChannels() {
Notifications.createChannels(this) Notifications.createChannels(this)
} }

View file

@ -1,23 +1,28 @@
package eu.kanade.tachiyomi.data.backup package eu.kanade.tachiyomi.data.backup
import android.content.Context
import android.net.Uri import android.net.Uri
import com.evernote.android.job.Job import androidx.work.ExistingPeriodicWorkPolicy
import com.evernote.android.job.JobManager import androidx.work.PeriodicWorkRequestBuilder
import com.evernote.android.job.JobRequest import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import java.util.concurrent.TimeUnit
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
class BackupCreatorJob : Job() { class BackupCreatorJob(private val context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) {
override fun onRunJob(params: Params): Result { override fun doWork(): Result {
val preferences = Injekt.get<PreferencesHelper>() val preferences = Injekt.get<PreferencesHelper>()
val backupManager = BackupManager(context) val backupManager = BackupManager(context)
val uri = Uri.parse(preferences.backupsDirectory().getOrDefault()) val uri = Uri.parse(preferences.backupsDirectory().getOrDefault())
val flags = BackupCreateService.BACKUP_ALL val flags = BackupCreateService.BACKUP_ALL
backupManager.createBackup(uri, flags, true) backupManager.createBackup(uri, flags, true)
return Result.SUCCESS return Result.success()
} }
companion object { companion object {
@ -27,16 +32,16 @@ class BackupCreatorJob : Job() {
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) {
JobRequest.Builder(TAG) val request = PeriodicWorkRequestBuilder<BackupCreatorJob>(interval.toLong(), TimeUnit.HOURS)
.setPeriodic(interval * 60 * 60 * 1000L, 10 * 60 * 1000) .addTag(TAG)
.setUpdateCurrent(true)
.build() .build()
.schedule()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} }
} }
fun cancelTask() { fun cancelTask() {
JobManager.instance().cancelAllForTag(TAG) WorkManager.getInstance().cancelAllWorkByTag(TAG)
} }
} }
} }

View file

@ -1,18 +1,25 @@
package eu.kanade.tachiyomi.data.library package eu.kanade.tachiyomi.data.library
import com.evernote.android.job.Job import android.content.Context
import com.evernote.android.job.JobManager import androidx.work.Constraints
import com.evernote.android.job.JobRequest import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import java.util.concurrent.TimeUnit
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
class LibraryUpdateJob : Job() { class LibraryUpdateJob(private val context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) {
override fun onRunJob(params: Params): Result { override fun doWork(): Result {
LibraryUpdateService.start(context) LibraryUpdateService.start(context)
return Job.Result.SUCCESS return Result.success()
} }
companion object { companion object {
@ -25,23 +32,26 @@ class LibraryUpdateJob : Job() {
val restrictions = preferences.libraryUpdateRestriction()!! val restrictions = preferences.libraryUpdateRestriction()!!
val acRestriction = "ac" in restrictions val acRestriction = "ac" in restrictions
val wifiRestriction = if ("wifi" in restrictions) val wifiRestriction = if ("wifi" in restrictions)
JobRequest.NetworkType.UNMETERED NetworkType.UNMETERED
else else
JobRequest.NetworkType.CONNECTED NetworkType.CONNECTED
JobRequest.Builder(TAG) val constraints = Constraints.Builder()
.setPeriodic(interval * 60 * 60 * 1000L, 10 * 60 * 1000)
.setRequiredNetworkType(wifiRestriction) .setRequiredNetworkType(wifiRestriction)
.setRequiresCharging(acRestriction) .setRequiresCharging(acRestriction)
.setRequirementsEnforced(true)
.setUpdateCurrent(true)
.build() .build()
.schedule()
val request = PeriodicWorkRequestBuilder<LibraryUpdateJob>(interval.toLong(), TimeUnit.HOURS)
.addTag(TAG)
.setConstraints(constraints)
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} }
} }
fun cancelTask() { fun cancelTask() {
JobManager.instance().cancelAllForTag(TAG) WorkManager.getInstance().cancelAllWorkByTag(TAG)
} }
} }
} }

View file

@ -1,19 +1,26 @@
package eu.kanade.tachiyomi.data.updater package eu.kanade.tachiyomi.data.updater
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.evernote.android.job.Job import androidx.work.Constraints
import com.evernote.android.job.JobManager import androidx.work.ExistingPeriodicWorkPolicy
import com.evernote.android.job.JobRequest import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.util.system.notificationManager import eu.kanade.tachiyomi.util.system.notificationManager
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
class UpdaterJob : Job() { class UpdaterJob(private val context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) {
override fun onRunJob(params: Params): Result { override fun doWork(): Result {
return runBlocking { return runBlocking {
try { try {
val result = UpdateChecker.getUpdateChecker().checkForUpdate() val result = UpdateChecker.getUpdateChecker().checkForUpdate()
@ -35,9 +42,9 @@ class UpdaterJob : Job() {
PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT))
} }
} }
Result.SUCCESS Result.success()
} catch (e: Exception) { } catch (e: Exception) {
Result.FAILURE Result.failure()
} }
} }
} }
@ -51,17 +58,20 @@ class UpdaterJob : Job() {
const val TAG = "UpdateChecker" const val TAG = "UpdateChecker"
fun setupTask() { fun setupTask() {
JobRequest.Builder(TAG) val constraints = Constraints.Builder()
.setPeriodic(24 * 60 * 60 * 1000, 60 * 60 * 1000) .setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setRequirementsEnforced(true)
.setUpdateCurrent(true)
.build() .build()
.schedule()
val request = PeriodicWorkRequestBuilder<UpdaterJob>(1, TimeUnit.DAYS)
.addTag(TAG)
.setConstraints(constraints)
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request)
} }
fun cancelTask() { fun cancelTask() {
JobManager.instance().cancelAllForTag(TAG) WorkManager.getInstance().cancelAllWorkByTag(TAG)
} }
} }
} }

View file

@ -5,8 +5,4 @@ open class TestApp : App() {
override fun setupAcra() { override fun setupAcra() {
// Do nothing // Do nothing
} }
override fun setupJobManager() {
// Do nothing
}
} }