mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-20 03:19:18 +01:00
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
This commit is contained in:
parent
e97956ee69
commit
4a7317ad70
@ -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"
|
||||
|
@ -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)
|
||||
|
@ -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<PreferencesHelper>()
|
||||
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<AutoUpdaterJob>()
|
||||
.addTag(TAG)
|
||||
.setConstraints(constraints)
|
||||
.build()
|
||||
|
||||
WorkManager.getInstance(context)
|
||||
.enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, request)
|
||||
}
|
||||
|
||||
fun cancelTask(context: Context) {
|
||||
WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
|
||||
}
|
||||
}
|
||||
}
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -658,6 +658,11 @@
|
||||
<string name="starting_screen">Starting screen</string>
|
||||
<string name="back_to_start">Back to start</string>
|
||||
<string name="pressing_back_to_start">Pressing back to starting screen</string>
|
||||
<string name="auto_updates">Auto-updates</string>
|
||||
<string name="auto_update_app">Auto-update app</string>
|
||||
<string name="over_wifi_only">Over Wi-Fi only</string>
|
||||
<string name="over_any_network">Over any network</string>
|
||||
<string name="dont_auto_update">Don\'t auto-update</string>
|
||||
|
||||
<string name="app_shortcuts">App shortcuts</string>
|
||||
<string name="show_recent_sources">Show recently used sources</string>
|
||||
|
Loading…
Reference in New Issue
Block a user