From c76a136d3f11aff50c15f1e25555f24fe300f15f Mon Sep 17 00:00:00 2001 From: Chris <52449218+shadow578@users.noreply.github.com> Date: Sat, 28 May 2022 15:09:53 +0200 Subject: [PATCH] Fix global update ignoring network constraint (#7188) * update library update network constraint logic * add explicit 'only on unmetered network' update constraint (cherry picked from commit 63238b388d1af3a0036f1d9a43cb4d2e87aabf5e) --- .../tachiyomi/data/library/LibraryUpdateJob.kt | 17 +++++------------ .../data/preference/PreferenceValues.kt | 1 + .../ui/setting/SettingsLibraryController.kt | 13 ++++--------- app/src/main/res/values/strings.xml | 1 + 4 files changed, 11 insertions(+), 21 deletions(-) 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 be2c51ab2..43f8d538d 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 @@ -8,10 +8,7 @@ import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.Worker import androidx.work.WorkerParameters -import eu.kanade.tachiyomi.data.preference.DEVICE_BATTERY_NOT_LOW -import eu.kanade.tachiyomi.data.preference.DEVICE_CHARGING -import eu.kanade.tachiyomi.data.preference.DEVICE_ONLY_ON_WIFI -import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.* import eu.kanade.tachiyomi.util.system.isConnectedToWifi import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -22,8 +19,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet override fun doWork(): Result { val preferences = Injekt.get() - if (requiresWifiConnection(preferences) && !context.isConnectedToWifi()) { - Result.failure() + val restrictions = preferences.libraryUpdateDeviceRestriction().get() + if ((DEVICE_ONLY_ON_WIFI in restrictions) && !context.isConnectedToWifi()) { + return Result.failure() } return if (LibraryUpdateService.start(context)) { @@ -42,7 +40,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet if (interval > 0) { val restrictions = preferences.libraryUpdateDeviceRestriction().get() val constraints = Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiredNetworkType(if (DEVICE_NETWORK_NOT_METERED in restrictions) { NetworkType.UNMETERED } else { NetworkType.CONNECTED }) .setRequiresCharging(DEVICE_CHARGING in restrictions) .setRequiresBatteryNotLow(DEVICE_BATTERY_NOT_LOW in restrictions) .build() @@ -62,10 +60,5 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet WorkManager.getInstance(context).cancelAllWorkByTag(TAG) } } - - fun requiresWifiConnection(preferences: PreferencesHelper): Boolean { - val restrictions = preferences.libraryUpdateDeviceRestriction().get() - return DEVICE_ONLY_ON_WIFI in restrictions - } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt index 28cadea0c..ecff434c9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.data.preference import eu.kanade.tachiyomi.R const val DEVICE_ONLY_ON_WIFI = "wifi" +const val DEVICE_NETWORK_NOT_METERED = "network_not_metered" const val DEVICE_CHARGING = "ac" const val DEVICE_BATTERY_NOT_LOW = "battery_not_low" diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index 70511823c..f0c7a4af7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt @@ -11,13 +11,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.library.LibraryUpdateJob -import eu.kanade.tachiyomi.data.preference.DEVICE_BATTERY_NOT_LOW -import eu.kanade.tachiyomi.data.preference.DEVICE_CHARGING -import eu.kanade.tachiyomi.data.preference.DEVICE_ONLY_ON_WIFI -import eu.kanade.tachiyomi.data.preference.MANGA_HAS_UNREAD -import eu.kanade.tachiyomi.data.preference.MANGA_NON_COMPLETED -import eu.kanade.tachiyomi.data.preference.MANGA_NON_READ -import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.* import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.databinding.PrefLibraryColumnsBinding import eu.kanade.tachiyomi.ui.base.controller.DialogController @@ -160,8 +154,8 @@ class SettingsLibraryController : SettingsController() { multiSelectListPreference { bindTo(preferences.libraryUpdateDeviceRestriction()) titleRes = R.string.pref_library_update_restriction - entriesRes = arrayOf(R.string.connected_to_wifi, R.string.charging, R.string.battery_not_low) - entryValues = arrayOf(DEVICE_ONLY_ON_WIFI, DEVICE_CHARGING, DEVICE_BATTERY_NOT_LOW) + entriesRes = arrayOf(R.string.connected_to_wifi, R.string.network_not_metered, R.string.charging, R.string.battery_not_low) + entryValues = arrayOf(DEVICE_ONLY_ON_WIFI, DEVICE_NETWORK_NOT_METERED, DEVICE_CHARGING, DEVICE_BATTERY_NOT_LOW) visibleIf(preferences.libraryUpdateInterval()) { it > 0 } @@ -177,6 +171,7 @@ class SettingsLibraryController : SettingsController() { .map { when (it) { DEVICE_ONLY_ON_WIFI -> context.getString(R.string.connected_to_wifi) + DEVICE_NETWORK_NOT_METERED -> context.getString(R.string.network_not_metered) DEVICE_CHARGING -> context.getString(R.string.charging) DEVICE_BATTERY_NOT_LOW -> context.getString(R.string.battery_not_low) else -> it diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 47cea213b..1b75f2657 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -222,6 +222,7 @@ Weekly Automatic updates device restrictions Only on Wi-Fi + Only on unmetered Network Charging Battery not low Restrictions: %s