Random UA: fix NullPointerException (#17131)
Random UA: remove dependency on uninitialized properties
This commit is contained in:
parent
5cdb939669
commit
be155c280f
|
@ -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)"
|
||||
|
||||
|
|
|
@ -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<Application>().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 {
|
||||
|
|
|
@ -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<Application>().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 {
|
||||
|
|
Loading…
Reference in New Issue