From be155c280f9f5533cfffd6a8e85c335210aae0c9 Mon Sep 17 00:00:00 2001 From: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> Date: Sun, 16 Jul 2023 19:25:49 +0500 Subject: [PATCH] Random UA: fix NullPointerException (#17131) Random UA: remove dependency on uninitialized properties --- .../lib/randomua/RandomUserAgentPreference.kt | 125 ++++++++---------- .../tachiyomi/multisrc/madara/Madara.kt | 29 ++-- .../multisrc/mangathemesia/MangaThemesia.kt | 29 ++-- 3 files changed, 86 insertions(+), 97 deletions(-) diff --git a/lib/randomua/src/main/java/eu/kanade/tachiyomi/lib/randomua/RandomUserAgentPreference.kt b/lib/randomua/src/main/java/eu/kanade/tachiyomi/lib/randomua/RandomUserAgentPreference.kt index 931209ebd..a92e67fa9 100644 --- a/lib/randomua/src/main/java/eu/kanade/tachiyomi/lib/randomua/RandomUserAgentPreference.kt +++ b/lib/randomua/src/main/java/eu/kanade/tachiyomi/lib/randomua/RandomUserAgentPreference.kt @@ -7,77 +7,64 @@ import androidx.preference.ListPreference import androidx.preference.PreferenceScreen import okhttp3.Headers -class RandomUserAgentPreference( - private val preferences: SharedPreferences, -) { + /** * Helper function to return UserAgentType based on SharedPreference value */ - fun getPrefUAType(): UserAgentType { - return when (preferences.getString(PREF_KEY_RANDOM_UA, "off")) { - "mobile" -> UserAgentType.MOBILE - "desktop" -> UserAgentType.DESKTOP - else -> UserAgentType.OFF - } - } - - /** - * Helper function to return custom UserAgent from SharedPreference - */ - fun getPrefCustomUA(): String? { - return preferences.getString(PREF_KEY_CUSTOM_UA, null) - } - - /** - * Helper function to add Random User-Agent settings to SharedPreference - * - * @param screen, PreferenceScreen from `setupPreferenceScreen` - */ - fun addPreferenceToScreen( - screen: PreferenceScreen, - ) { - val customUA = EditTextPreference(screen.context).apply { - key = PREF_KEY_CUSTOM_UA - title = TITLE_CUSTOM_UA - summary = CUSTOM_UA_SUMMARY - setVisible(getPrefUAType() == UserAgentType.OFF) - setOnPreferenceChangeListener { _, newValue -> - try { - Headers.Builder().add("User-Agent", newValue as String).build() - true - } catch (e: IllegalArgumentException) { - Toast.makeText(screen.context, "User Agent invalid:${e.message}", Toast.LENGTH_LONG).show() - false - } - } - } - - val randomUA = ListPreference(screen.context).apply { - key = PREF_KEY_RANDOM_UA - title = TITLE_RANDOM_UA - entries = RANDOM_UA_ENTRIES - entryValues = RANDOM_UA_VALUES - summary = "%s" - setDefaultValue("off") - setOnPreferenceChangeListener { _, newVal -> - val showCustomUAPref = newVal as String == "off" - customUA.setVisible(showCustomUAPref) - true - } - } - - screen.addPreference(randomUA) - screen.addPreference(customUA) - } - - companion object { - const val TITLE_RANDOM_UA = "Random User-Agent (Requires Restart)" - const val PREF_KEY_RANDOM_UA = "pref_key_random_ua_" - val RANDOM_UA_ENTRIES = arrayOf("OFF", "Desktop", "Mobile") - val RANDOM_UA_VALUES = arrayOf("off", "desktop", "mobile") - - const val TITLE_CUSTOM_UA = "Custom User-Agent" - const val PREF_KEY_CUSTOM_UA = "pref_key_custom_ua_" - const val CUSTOM_UA_SUMMARY = "Leave blank to use application default user-agent. (Requires Restart)" +fun SharedPreferences.getPrefUAType(): UserAgentType { + return when (getString(PREF_KEY_RANDOM_UA, "off")) { + "mobile" -> UserAgentType.MOBILE + "desktop" -> UserAgentType.DESKTOP + else -> UserAgentType.OFF } } + +/** + * Helper function to return custom UserAgent from SharedPreference + */ +fun SharedPreferences.getPrefCustomUA(): String? { + return getString(PREF_KEY_CUSTOM_UA, null) +} + +/** + * Helper function to add Random User-Agent settings to SharedPreference + * + * @param screen, PreferenceScreen from `setupPreferenceScreen` + */ +fun addRandomUAPreferenceToScreen( + screen: PreferenceScreen, +) { + ListPreference(screen.context).apply { + key = PREF_KEY_RANDOM_UA + title = TITLE_RANDOM_UA + entries = RANDOM_UA_ENTRIES + entryValues = RANDOM_UA_VALUES + summary = "%s" + setDefaultValue("off") + }.also(screen::addPreference) + + EditTextPreference(screen.context).apply { + key = PREF_KEY_CUSTOM_UA + title = TITLE_CUSTOM_UA + summary = CUSTOM_UA_SUMMARY + setOnPreferenceChangeListener { _, newValue -> + try { + Headers.Builder().add("User-Agent", newValue as String).build() + true + } catch (e: IllegalArgumentException) { + Toast.makeText(screen.context, "User Agent invalid:${e.message}", Toast.LENGTH_LONG).show() + false + } + } + }.also(screen::addPreference) +} + +const val TITLE_RANDOM_UA = "Random User-Agent (Requires Restart)" +const val PREF_KEY_RANDOM_UA = "pref_key_random_ua_" +val RANDOM_UA_ENTRIES = arrayOf("OFF", "Desktop", "Mobile") +val RANDOM_UA_VALUES = arrayOf("off", "desktop", "mobile") + +const val TITLE_CUSTOM_UA = "Custom User-Agent (Requires Restart)" +const val PREF_KEY_CUSTOM_UA = "pref_key_custom_ua_" +const val CUSTOM_UA_SUMMARY = "Leave blank to use application default user-agent (IGNORED if Random User-Agent is enabled)" + diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt index 40580d4ee..87a14ffd8 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt @@ -5,7 +5,9 @@ import android.content.SharedPreferences import android.util.Base64 import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.lib.cryptoaes.CryptoAES -import eu.kanade.tachiyomi.lib.randomua.RandomUserAgentPreference +import eu.kanade.tachiyomi.lib.randomua.addRandomUAPreferenceToScreen +import eu.kanade.tachiyomi.lib.randomua.getPrefCustomUA +import eu.kanade.tachiyomi.lib.randomua.getPrefUAType import eu.kanade.tachiyomi.lib.randomua.setRandomUserAgent import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.POST @@ -26,6 +28,7 @@ import kotlinx.serialization.json.jsonPrimitive import okhttp3.CacheControl import okhttp3.FormBody import okhttp3.HttpUrl.Companion.toHttpUrlOrNull +import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import org.jsoup.nodes.Document @@ -51,20 +54,18 @@ abstract class Madara( Injekt.get().getSharedPreferences("source_$id", 0x0000) } - private val randomUAPrefHelper: RandomUserAgentPreference by lazy { - RandomUserAgentPreference(preferences) - } - override val supportsLatest = true - override val client = network.cloudflareClient.newBuilder() - .setRandomUserAgent( - randomUAPrefHelper.getPrefUAType(), - randomUAPrefHelper.getPrefCustomUA(), - ) - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .build() + override val client: OkHttpClient by lazy { + network.cloudflareClient.newBuilder() + .setRandomUserAgent( + preferences.getPrefUAType(), + preferences.getPrefCustomUA(), + ) + .connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .build() + } override fun headersBuilder() = super.headersBuilder() .add("Referer", "$baseUrl/") @@ -950,7 +951,7 @@ abstract class Madara( } override fun setupPreferenceScreen(screen: PreferenceScreen) { - randomUAPrefHelper.addPreferenceToScreen(screen) + addRandomUAPreferenceToScreen(screen) } companion object { diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mangathemesia/MangaThemesia.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mangathemesia/MangaThemesia.kt index cb0a97ca8..b57b844d0 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mangathemesia/MangaThemesia.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/mangathemesia/MangaThemesia.kt @@ -3,7 +3,9 @@ package eu.kanade.tachiyomi.multisrc.mangathemesia import android.app.Application import android.content.SharedPreferences import androidx.preference.PreferenceScreen -import eu.kanade.tachiyomi.lib.randomua.RandomUserAgentPreference +import eu.kanade.tachiyomi.lib.randomua.addRandomUAPreferenceToScreen +import eu.kanade.tachiyomi.lib.randomua.getPrefCustomUA +import eu.kanade.tachiyomi.lib.randomua.getPrefUAType import eu.kanade.tachiyomi.lib.randomua.setRandomUserAgent import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.POST @@ -23,6 +25,7 @@ import okhttp3.FormBody import okhttp3.HttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrlOrNull +import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import org.jsoup.nodes.Document @@ -50,22 +53,20 @@ abstract class MangaThemesia( Injekt.get().getSharedPreferences("source_$id", 0x0000) } - private val randomUAPrefHelper: RandomUserAgentPreference by lazy { - RandomUserAgentPreference(preferences) - } - protected open val json: Json by injectLazy() override val supportsLatest = true - override val client = network.cloudflareClient.newBuilder() - .setRandomUserAgent( - randomUAPrefHelper.getPrefUAType(), - randomUAPrefHelper.getPrefCustomUA(), - ) - .connectTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .build() + override val client: OkHttpClient by lazy { + network.cloudflareClient.newBuilder() + .setRandomUserAgent( + preferences.getPrefUAType(), + preferences.getPrefCustomUA(), + ) + .connectTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .build() + } open val projectPageString = "/project" @@ -499,7 +500,7 @@ abstract class MangaThemesia( override fun imageUrlParse(document: Document): String = throw UnsupportedOperationException("Not Used") override fun setupPreferenceScreen(screen: PreferenceScreen) { - randomUAPrefHelper.addPreferenceToScreen(screen) + addRandomUAPreferenceToScreen(screen) } companion object {