From 4a7317ad7032e97f8db61d5631ad8a2728037071 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Sun, 18 Jul 2021 21:31:07 -0400 Subject: [PATCH] New option to auto update app silently for a12 Default is only on wi-fi/unmeted networked, can be set to update on any network or never --- .../data/preference/PreferenceKeys.kt | 2 + .../data/preference/PreferencesHelper.kt | 3 + .../tachiyomi/data/updater/AutoUpdaterJob.kt | 83 +++++++++++++++++++ .../tachiyomi/data/updater/UpdaterJob.kt | 9 ++ .../tachiyomi/data/updater/UpdaterService.kt | 1 + .../ui/setting/SettingsGeneralController.kt | 16 ++++ app/src/main/res/values/strings.xml | 5 ++ 7 files changed, 119 insertions(+) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/updater/AutoUpdaterJob.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 78dba72b07..a8320b8fd7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -229,6 +229,8 @@ object PreferenceKeys { const val incognitoMode = "incognito_mode" + const val shouldAutoUpdate = "should_auto_update" + const val defaultChapterFilterByRead = "default_chapter_filter_by_read" const val defaultChapterFilterByDownloaded = "default_chapter_filter_by_downloaded" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 795b60fae9..5b35dedefe 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -13,6 +13,7 @@ import com.tfcporciuncula.flow.FlowSharedPreferences import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.track.TrackService +import eu.kanade.tachiyomi.data.updater.AutoUpdaterJob import eu.kanade.tachiyomi.ui.library.filter.FilterBottomSheet import eu.kanade.tachiyomi.ui.reader.settings.OrientationType import eu.kanade.tachiyomi.ui.reader.settings.PageLayout @@ -426,6 +427,8 @@ class PreferencesHelper(val context: Context) { fun hasPromptedBeforeUpdateAll() = flowPrefs.getBoolean("has_prompted_update_all", false) + fun shouldAutoUpdate() = prefs.getInt(Keys.shouldAutoUpdate, AutoUpdaterJob.ONLY_ON_UNMETERED) + fun filterChapterByRead() = flowPrefs.getInt(Keys.defaultChapterFilterByRead, Manga.SHOW_ALL) fun filterChapterByDownloaded() = flowPrefs.getInt(Keys.defaultChapterFilterByDownloaded, Manga.SHOW_ALL) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/AutoUpdaterJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AutoUpdaterJob.kt new file mode 100644 index 0000000000..8accacc6bb --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/AutoUpdaterJob.kt @@ -0,0 +1,83 @@ +package eu.kanade.tachiyomi.data.updater + +import android.app.ActivityManager +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE +import android.content.Context +import androidx.core.app.NotificationCompat +import androidx.work.Constraints +import androidx.work.CoroutineWorker +import androidx.work.ExistingWorkPolicy +import androidx.work.NetworkType +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.WorkManager +import androidx.work.WorkerParameters +import eu.kanade.tachiyomi.data.notification.Notifications +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.util.system.notificationManager +import kotlinx.coroutines.coroutineScope +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get + +class AutoUpdaterJob(private val context: Context, workerParams: WorkerParameters) : + CoroutineWorker(context, workerParams) { + + override suspend fun doWork(): Result = coroutineScope { + try { + val result = UpdateChecker.getUpdateChecker().checkForUpdate() + if (result is UpdateResult.NewUpdate<*> && !UpdaterService.isRunning()) { + UpdaterNotifier(context).cancel() + UpdaterNotifier.releasePageUrl = result.release.releaseLink + UpdaterService.start(context, result.release.downloadLink, false) + } + Result.success() + } catch (e: Exception) { + Result.failure() + } + } + + fun foregrounded(): Boolean { + val appProcessInfo = ActivityManager.RunningAppProcessInfo() + ActivityManager.getMyMemoryState(appProcessInfo) + return appProcessInfo.importance == IMPORTANCE_FOREGROUND || appProcessInfo.importance == IMPORTANCE_VISIBLE + } + + fun NotificationCompat.Builder.update(block: NotificationCompat.Builder.() -> Unit) { + block() + context.notificationManager.notify(Notifications.ID_UPDATER, build()) + } + + companion object { + private const val TAG = "AutoUpdateRunner" + const val ALWAYS = 0 + const val ONLY_ON_UNMETERED = 1 + const val NEVER = 2 + + fun setupTask(context: Context) { + val preferences = Injekt.get() + val restrictions = preferences.shouldAutoUpdate() + val wifiRestriction = if (restrictions == ONLY_ON_UNMETERED) { + NetworkType.UNMETERED + } else { + NetworkType.CONNECTED + } + + val constraints = Constraints.Builder() + .setRequiredNetworkType(wifiRestriction) + .setRequiresDeviceIdle(true) + .build() + + val request = OneTimeWorkRequestBuilder() + .addTag(TAG) + .setConstraints(constraints) + .build() + + WorkManager.getInstance(context) + .enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request) + } + + fun cancelTask(context: Context) { + WorkManager.getInstance(context).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 41f9129081..c23f7bd419 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,6 +1,7 @@ package eu.kanade.tachiyomi.data.updater import android.content.Context +import android.os.Build import androidx.core.app.NotificationCompat import androidx.work.Constraints import androidx.work.CoroutineWorker @@ -10,8 +11,10 @@ import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.WorkerParameters import eu.kanade.tachiyomi.data.notification.Notifications +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.system.notificationManager import kotlinx.coroutines.coroutineScope +import uy.kohesive.injekt.injectLazy import java.util.concurrent.TimeUnit class UpdaterJob(private val context: Context, workerParams: WorkerParameters) : @@ -19,8 +22,14 @@ class UpdaterJob(private val context: Context, workerParams: WorkerParameters) : override suspend fun doWork(): Result = coroutineScope { try { + val preferences: PreferencesHelper by injectLazy() val result = UpdateChecker.getUpdateChecker().checkForUpdate() if (result is UpdateResult.NewUpdate<*>) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && + preferences.shouldAutoUpdate() != AutoUpdaterJob.NEVER + ) { + AutoUpdaterJob.setupTask(context) + } UpdaterNotifier(context).promptUpdate( result.release.info, result.release.downloadLink, diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt index cd1e17f397..81e9189aa1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/updater/UpdaterService.kt @@ -9,6 +9,7 @@ import android.os.Build import android.os.IBinder import android.os.PowerManager import androidx.core.content.edit +import androidx.preference.PreferenceManager import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.notification.Notifications diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index 678fd27699..a9f1a0830a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.ui.setting +import android.os.Build import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatDelegate @@ -8,6 +9,7 @@ import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.data.preference.asImmediateFlowIn +import eu.kanade.tachiyomi.data.updater.AutoUpdaterJob import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.appDelegateNightMode @@ -270,6 +272,20 @@ class SettingsGeneralController : SettingsController() { defaultValue = "" } } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && isUpdaterEnabled) { + preferenceCategory { + titleRes = R.string.auto_updates + + intListPreference(activity) { + key = Keys.shouldAutoUpdate + titleRes = R.string.auto_update_app + entryRange = 0..2 + entriesRes = arrayOf(R.string.over_any_network, R.string.over_wifi_only, R.string.dont_auto_update) + defaultValue = AutoUpdaterJob.ONLY_ON_UNMETERED + } + } + } } override fun onDestroyView(view: View) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1e7ce05b7f..c18f34433b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -658,6 +658,11 @@ Starting screen Back to start Pressing back to starting screen + Auto-updates + Auto-update app + Over Wi-Fi only + Over any network + Don\'t auto-update App shortcuts Show recently used sources