From d873d653d0b54e653829832d8c07e95e1a41ea77 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 1 Mar 2020 13:06:55 -0500 Subject: [PATCH] Migrate to WorkManager --- app/build.gradle | 5 +-- app/src/main/java/eu/kanade/tachiyomi/App.kt | 20 ---------- .../tachiyomi/data/backup/BackupCreatorJob.kt | 27 +++++++------ .../data/library/LibraryUpdateJob.kt | 38 ++++++++++++------- .../tachiyomi/data/updater/UpdaterJob.kt | 38 ++++++++++++------- .../test/java/eu/kanade/tachiyomi/TestApp.kt | 4 -- 6 files changed, 65 insertions(+), 67 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d64ec56b3d..b3754f863d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -163,10 +163,7 @@ dependencies { implementation 'org.jsoup:jsoup:1.12.1' // Job scheduling - implementation 'com.evernote:android-job:1.4.2' - final workManagerVersion = '2.3.2' - implementation "android.arch.work:work-runtime:$workManagerVersion" - implementation "android.arch.work:work-runtime-ktx:$workManagerVersion" + implementation 'android.arch.work:work-runtime:2.3.2' // Changelog implementation 'com.github.gabrielemariotti.changeloglib:changelog:2.1.0' diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index ba6a1e0358..7809198e76 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -8,13 +8,9 @@ import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.OnLifecycleEvent import androidx.lifecycle.ProcessLifecycleOwner 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.preference.PreferencesHelper 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.util.system.LocaleHelper import org.acra.ACRA @@ -42,7 +38,6 @@ open class App : Application(), LifecycleObserver { Injekt.importModule(AppModule(this)) setupAcra() - setupJobManager() setupNotificationChannels() LocaleHelper.updateConfiguration(this, resources.configuration) @@ -72,21 +67,6 @@ open class App : Application(), LifecycleObserver { 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() { Notifications.createChannels(this) } 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 a1f5aca922..13b1df8650 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 @@ -1,23 +1,28 @@ package eu.kanade.tachiyomi.data.backup +import android.content.Context import android.net.Uri -import com.evernote.android.job.Job -import com.evernote.android.job.JobManager -import com.evernote.android.job.JobRequest +import androidx.work.ExistingPeriodicWorkPolicy +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.getOrDefault +import java.util.concurrent.TimeUnit import uy.kohesive.injekt.Injekt 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() val backupManager = BackupManager(context) val uri = Uri.parse(preferences.backupsDirectory().getOrDefault()) val flags = BackupCreateService.BACKUP_ALL backupManager.createBackup(uri, flags, true) - return Result.SUCCESS + return Result.success() } companion object { @@ -27,16 +32,16 @@ class BackupCreatorJob : Job() { val preferences = Injekt.get() val interval = prefInterval ?: preferences.backupInterval().getOrDefault() if (interval > 0) { - JobRequest.Builder(TAG) - .setPeriodic(interval * 60 * 60 * 1000L, 10 * 60 * 1000) - .setUpdateCurrent(true) + val request = PeriodicWorkRequestBuilder(interval.toLong(), TimeUnit.HOURS) + .addTag(TAG) .build() - .schedule() + + WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) } } fun cancelTask() { - JobManager.instance().cancelAllForTag(TAG) + WorkManager.getInstance().cancelAllWorkByTag(TAG) } } } 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 1b992850a8..47a131def3 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 @@ -1,18 +1,25 @@ package eu.kanade.tachiyomi.data.library -import com.evernote.android.job.Job -import com.evernote.android.job.JobManager -import com.evernote.android.job.JobRequest +import android.content.Context +import androidx.work.Constraints +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.getOrDefault +import java.util.concurrent.TimeUnit import uy.kohesive.injekt.Injekt 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) - return Job.Result.SUCCESS + return Result.success() } companion object { @@ -25,23 +32,26 @@ class LibraryUpdateJob : Job() { val restrictions = preferences.libraryUpdateRestriction()!! val acRestriction = "ac" in restrictions val wifiRestriction = if ("wifi" in restrictions) - JobRequest.NetworkType.UNMETERED + NetworkType.UNMETERED else - JobRequest.NetworkType.CONNECTED + NetworkType.CONNECTED - JobRequest.Builder(TAG) - .setPeriodic(interval * 60 * 60 * 1000L, 10 * 60 * 1000) + val constraints = Constraints.Builder() .setRequiredNetworkType(wifiRestriction) .setRequiresCharging(acRestriction) - .setRequirementsEnforced(true) - .setUpdateCurrent(true) .build() - .schedule() + + val request = PeriodicWorkRequestBuilder(interval.toLong(), TimeUnit.HOURS) + .addTag(TAG) + .setConstraints(constraints) + .build() + + WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) } } fun cancelTask() { - JobManager.instance().cancelAllForTag(TAG) + WorkManager.getInstance().cancelAllWorkByTag(TAG) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt index b0542e0f16..3b9a1f2059 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterJob.kt @@ -1,19 +1,26 @@ package eu.kanade.tachiyomi.data.updater import android.app.PendingIntent +import android.content.Context import android.content.Intent import androidx.core.app.NotificationCompat -import com.evernote.android.job.Job -import com.evernote.android.job.JobManager -import com.evernote.android.job.JobRequest +import androidx.work.Constraints +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.R import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.util.system.notificationManager +import java.util.concurrent.TimeUnit 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 { try { val result = UpdateChecker.getUpdateChecker().checkForUpdate() @@ -35,9 +42,9 @@ class UpdaterJob : Job() { PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) } } - Result.SUCCESS + Result.success() } catch (e: Exception) { - Result.FAILURE + Result.failure() } } } @@ -51,17 +58,20 @@ class UpdaterJob : Job() { const val TAG = "UpdateChecker" fun setupTask() { - JobRequest.Builder(TAG) - .setPeriodic(24 * 60 * 60 * 1000, 60 * 60 * 1000) - .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED) - .setRequirementsEnforced(true) - .setUpdateCurrent(true) + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) .build() - .schedule() + + val request = PeriodicWorkRequestBuilder(1, TimeUnit.DAYS) + .addTag(TAG) + .setConstraints(constraints) + .build() + + WorkManager.getInstance().enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, request) } fun cancelTask() { - JobManager.instance().cancelAllForTag(TAG) + WorkManager.getInstance().cancelAllWorkByTag(TAG) } } } diff --git a/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt b/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt index cbac3701fe..35a74ceb22 100644 --- a/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt +++ b/app/src/test/java/eu/kanade/tachiyomi/TestApp.kt @@ -5,8 +5,4 @@ open class TestApp : App() { override fun setupAcra() { // Do nothing } - - override fun setupJobManager() { - // Do nothing - } }