From 8a70c3353fa6a1851561e7dedf4e9702750ca3af Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 16 Oct 2021 11:10:05 -0400 Subject: [PATCH] Change global update unmetered connection restriction to connected to Wi-Fi (closes #6117) --- .../data/library/LibraryUpdateJob.kt | 24 +++++++++++-------- .../data/preference/PreferenceValues.kt | 2 +- .../data/preference/PreferencesHelper.kt | 2 +- .../ui/setting/SettingsDownloadController.kt | 2 +- .../ui/setting/SettingsLibraryController.kt | 10 ++++---- app/src/main/res/values/strings.xml | 3 +-- 6 files changed, 23 insertions(+), 20 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 1d605fe6a6..d6db778fc4 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 @@ -9,8 +9,9 @@ import androidx.work.WorkManager import androidx.work.Worker import androidx.work.WorkerParameters import eu.kanade.tachiyomi.data.preference.CHARGING +import eu.kanade.tachiyomi.data.preference.ONLY_ON_WIFI import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.UNMETERED_NETWORK +import eu.kanade.tachiyomi.util.system.isConnectedToWifi import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import java.util.concurrent.TimeUnit @@ -19,6 +20,11 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet Worker(context, workerParams) { override fun doWork(): Result { + val preferences = Injekt.get() + if (requiresWifiConnection(preferences) && !context.isConnectedToWifi()) { + Result.failure() + } + return if (LibraryUpdateService.start(context)) { Result.success() } else { @@ -34,16 +40,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet val interval = prefInterval ?: preferences.libraryUpdateInterval().get() if (interval > 0) { val restrictions = preferences.libraryUpdateRestriction().get() - val acRestriction = CHARGING in restrictions - val wifiRestriction = if (UNMETERED_NETWORK in restrictions) { - NetworkType.UNMETERED - } else { - NetworkType.CONNECTED - } - val constraints = Constraints.Builder() - .setRequiredNetworkType(wifiRestriction) - .setRequiresCharging(acRestriction) + .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiresCharging(CHARGING in restrictions) .build() val request = PeriodicWorkRequestBuilder( @@ -61,5 +60,10 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet WorkManager.getInstance(context).cancelAllWorkByTag(TAG) } } + + fun requiresWifiConnection(preferences: PreferencesHelper): Boolean { + val restrictions = preferences.libraryUpdateRestriction().get() + return 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 4da7952418..41a5f1831f 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 @@ -2,7 +2,7 @@ package eu.kanade.tachiyomi.data.preference import eu.kanade.tachiyomi.R -const val UNMETERED_NETWORK = "wifi" +const val ONLY_ON_WIFI = "wifi" const val CHARGING = "ac" /** 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 48b145d015..ac737be494 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 @@ -236,7 +236,7 @@ class PreferencesHelper(val context: Context) { fun libraryUpdateInterval() = flowPrefs.getInt(Keys.libraryUpdateInterval, 24) - fun libraryUpdateRestriction() = flowPrefs.getStringSet(Keys.libraryUpdateRestriction, setOf(UNMETERED_NETWORK)) + fun libraryUpdateRestriction() = flowPrefs.getStringSet(Keys.libraryUpdateRestriction, setOf(ONLY_ON_WIFI)) fun libraryUpdateCategories() = flowPrefs.getStringSet(Keys.libraryUpdateCategories, emptySet()) fun libraryUpdateCategoriesExclude() = flowPrefs.getStringSet(Keys.libraryUpdateCategoriesExclude, emptySet()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt index 03df452ef4..8bb4c80312 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadController.kt @@ -66,7 +66,7 @@ class SettingsDownloadController : SettingsController() { } switchPreference { key = Keys.downloadOnlyOverWifi - titleRes = R.string.pref_download_only_over_wifi + titleRes = R.string.connected_to_wifi defaultValue = true } preferenceCategory { 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 8889fb7db4..6e4fd5c8ca 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 @@ -12,8 +12,8 @@ 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.CHARGING +import eu.kanade.tachiyomi.data.preference.ONLY_ON_WIFI import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import eu.kanade.tachiyomi.data.preference.UNMETERED_NETWORK import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.databinding.PrefLibraryColumnsBinding @@ -161,9 +161,9 @@ class SettingsLibraryController : SettingsController() { multiSelectListPreference { key = Keys.libraryUpdateRestriction titleRes = R.string.pref_library_update_restriction - entriesRes = arrayOf(R.string.network_unmetered, R.string.charging) - entryValues = arrayOf(UNMETERED_NETWORK, CHARGING) - defaultValue = setOf(UNMETERED_NETWORK) + entriesRes = arrayOf(R.string.connected_to_wifi, R.string.charging) + entryValues = arrayOf(ONLY_ON_WIFI, CHARGING) + defaultValue = setOf(ONLY_ON_WIFI) preferences.libraryUpdateInterval().asImmediateFlow { isVisible = it > 0 } .launchIn(viewScope) @@ -179,7 +179,7 @@ class SettingsLibraryController : SettingsController() { .sorted() .map { when (it) { - UNMETERED_NETWORK -> context.getString(R.string.network_unmetered) + ONLY_ON_WIFI -> context.getString(R.string.connected_to_wifi) CHARGING -> context.getString(R.string.charging) else -> it } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5bac470b3c..c1fc992376 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -221,7 +221,7 @@ Weekly Update order Update restrictions - Unmetered network + Only on Wi-Fi Charging Restrictions: %s Only update ongoing manga @@ -384,7 +384,6 @@ Download location - Only download over Wi-Fi Delete chapters After marked as read Automatically after reading