Mangadex: fix potential anr and remove ua pref (#4935)
* Mangadex: fix potential anr and remove ua pref * remove unused stuff * don't remove tachi ua
This commit is contained in:
parent
3b826e0365
commit
0af3536837
|
@ -1,7 +1,7 @@
|
||||||
ext {
|
ext {
|
||||||
extName = 'MangaDex'
|
extName = 'MangaDex'
|
||||||
extClass = '.MangaDexFactory'
|
extClass = '.MangaDexFactory'
|
||||||
extVersionCode = 193
|
extVersionCode = 194
|
||||||
isNsfw = true
|
isNsfw = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -138,13 +138,6 @@ object MDConstants {
|
||||||
return "${altTitlesInDescPref}_$dexLang"
|
return "${altTitlesInDescPref}_$dexLang"
|
||||||
}
|
}
|
||||||
|
|
||||||
private const val customUserAgentPref = "customUserAgent"
|
|
||||||
fun getCustomUserAgentPrefKey(dexLang: String): String {
|
|
||||||
return "${customUserAgentPref}_$dexLang"
|
|
||||||
}
|
|
||||||
|
|
||||||
val defaultUserAgent = "Tachiyomi " + System.getProperty("http.agent")
|
|
||||||
|
|
||||||
private const val tagGroupContent = "content"
|
private const val tagGroupContent = "content"
|
||||||
private const val tagGroupFormat = "format"
|
private const val tagGroupFormat = "format"
|
||||||
private const val tagGroupGenre = "genre"
|
private const val tagGroupGenre = "genre"
|
||||||
|
|
|
@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.extension.all.mangadex
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.preference.EditTextPreference
|
import androidx.preference.EditTextPreference
|
||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
import androidx.preference.MultiSelectListPreference
|
import androidx.preference.MultiSelectListPreference
|
||||||
|
@ -56,6 +55,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
|
|
||||||
private val preferences: SharedPreferences by lazy {
|
private val preferences: SharedPreferences by lazy {
|
||||||
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
||||||
|
.sanitizeExistingUuidPrefs()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val helper = MangaDexHelper(lang)
|
private val helper = MangaDexHelper(lang)
|
||||||
|
@ -67,6 +67,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
"Keiyoushi"
|
"Keiyoushi"
|
||||||
|
|
||||||
val builder = super.headersBuilder().apply {
|
val builder = super.headersBuilder().apply {
|
||||||
|
set("User-Agent", "Tachiyomi " + System.getProperty("http.agent"))
|
||||||
set("Referer", "$baseUrl/")
|
set("Referer", "$baseUrl/")
|
||||||
set("Origin", baseUrl)
|
set("Origin", baseUrl)
|
||||||
set("Extra", extraHeader)
|
set("Extra", extraHeader)
|
||||||
|
@ -78,13 +79,8 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
override val client = network.client.newBuilder()
|
override val client = network.client.newBuilder()
|
||||||
.rateLimit(3)
|
.rateLimit(3)
|
||||||
.addInterceptor(MdAtHomeReportInterceptor(network.client, headers))
|
.addInterceptor(MdAtHomeReportInterceptor(network.client, headers))
|
||||||
.addInterceptor(MdUserAgentInterceptor(preferences, dexLang))
|
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
init {
|
|
||||||
preferences.sanitizeExistingUuidPrefs()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Popular manga section
|
// Popular manga section
|
||||||
|
|
||||||
override fun popularMangaRequest(page: Int): Request {
|
override fun popularMangaRequest(page: Int): Request {
|
||||||
|
@ -395,7 +391,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
// Manga Details section
|
// Manga Details section
|
||||||
|
|
||||||
override fun getMangaUrl(manga: SManga): String {
|
override fun getMangaUrl(manga: SManga): String {
|
||||||
return baseUrl + manga.url + "/" + helper.titleToSlug(manga.title)
|
return baseUrl + manga.url.replace("/manga/", "/title/") + "/" + helper.titleToSlug(manga.title)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -761,30 +757,6 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val userAgentPref = EditTextPreference(screen.context).apply {
|
|
||||||
key = MDConstants.getCustomUserAgentPrefKey(dexLang)
|
|
||||||
title = helper.intl["set_custom_useragent"]
|
|
||||||
summary = helper.intl["set_custom_useragent_summary"]
|
|
||||||
dialogMessage = helper.intl.format(
|
|
||||||
"set_custom_useragent_dialog",
|
|
||||||
MDConstants.defaultUserAgent,
|
|
||||||
)
|
|
||||||
|
|
||||||
setDefaultValue(MDConstants.defaultUserAgent)
|
|
||||||
|
|
||||||
setOnPreferenceChangeListener { _, newValue ->
|
|
||||||
try {
|
|
||||||
Headers.Builder().add("User-Agent", newValue as String)
|
|
||||||
summary = newValue
|
|
||||||
true
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
val errorMessage = helper.intl.format("set_custom_useragent_error_invalid", e.message)
|
|
||||||
Toast.makeText(screen.context, errorMessage, Toast.LENGTH_LONG).show()
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
screen.addPreference(coverQualityPref)
|
screen.addPreference(coverQualityPref)
|
||||||
screen.addPreference(tryUsingFirstVolumeCoverPref)
|
screen.addPreference(tryUsingFirstVolumeCoverPref)
|
||||||
screen.addPreference(dataSaverPref)
|
screen.addPreference(dataSaverPref)
|
||||||
|
@ -794,7 +766,6 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
screen.addPreference(originalLanguagePref)
|
screen.addPreference(originalLanguagePref)
|
||||||
screen.addPreference(blockedGroupsPref)
|
screen.addPreference(blockedGroupsPref)
|
||||||
screen.addPreference(blockedUploaderPref)
|
screen.addPreference(blockedUploaderPref)
|
||||||
screen.addPreference(userAgentPref)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getFilterList(): FilterList =
|
override fun getFilterList(): FilterList =
|
||||||
|
@ -869,20 +840,14 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
private val SharedPreferences.altTitlesInDesc
|
private val SharedPreferences.altTitlesInDesc
|
||||||
get() = getBoolean(MDConstants.getAltTitlesInDescPrefKey(dexLang), false)
|
get() = getBoolean(MDConstants.getAltTitlesInDescPrefKey(dexLang), false)
|
||||||
|
|
||||||
private val SharedPreferences.customUserAgent
|
|
||||||
get() = getString(
|
|
||||||
MDConstants.getCustomUserAgentPrefKey(dexLang),
|
|
||||||
MDConstants.defaultUserAgent,
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Previous versions of the extension allowed invalid UUID values to be stored in the
|
* Previous versions of the extension allowed invalid UUID values to be stored in the
|
||||||
* preferences. This method clear invalid UUIDs in case the user have updated from
|
* preferences. This method clear invalid UUIDs in case the user have updated from
|
||||||
* a previous version with that behaviour.
|
* a previous version with that behaviour.
|
||||||
*/
|
*/
|
||||||
private fun SharedPreferences.sanitizeExistingUuidPrefs() {
|
private fun SharedPreferences.sanitizeExistingUuidPrefs(): SharedPreferences {
|
||||||
if (getBoolean(MDConstants.getHasSanitizedUuidsPrefKey(dexLang), false)) {
|
if (getBoolean(MDConstants.getHasSanitizedUuidsPrefKey(dexLang), false)) {
|
||||||
return
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
val blockedGroups = getString(MDConstants.getBlockedGroupsPrefKey(dexLang), "")!!
|
val blockedGroups = getString(MDConstants.getBlockedGroupsPrefKey(dexLang), "")!!
|
||||||
|
@ -902,5 +867,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St
|
||||||
.putString(MDConstants.getBlockedUploaderPrefKey(dexLang), blockedUploaders)
|
.putString(MDConstants.getBlockedUploaderPrefKey(dexLang), blockedUploaders)
|
||||||
.putBoolean(MDConstants.getHasSanitizedUuidsPrefKey(dexLang), true)
|
.putBoolean(MDConstants.getHasSanitizedUuidsPrefKey(dexLang), true)
|
||||||
.apply()
|
.apply()
|
||||||
|
|
||||||
|
return this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
package eu.kanade.tachiyomi.extension.all.mangadex
|
|
||||||
|
|
||||||
import android.content.SharedPreferences
|
|
||||||
import okhttp3.Interceptor
|
|
||||||
import okhttp3.Response
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interceptor to set custom useragent for MangaDex
|
|
||||||
*/
|
|
||||||
class MdUserAgentInterceptor(
|
|
||||||
private val preferences: SharedPreferences,
|
|
||||||
private val dexLang: String,
|
|
||||||
) : Interceptor {
|
|
||||||
|
|
||||||
private val SharedPreferences.customUserAgent
|
|
||||||
get() = getString(
|
|
||||||
MDConstants.getCustomUserAgentPrefKey(dexLang),
|
|
||||||
MDConstants.defaultUserAgent,
|
|
||||||
)
|
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
|
||||||
val originalRequest = chain.request()
|
|
||||||
|
|
||||||
val newUserAgent = preferences.customUserAgent
|
|
||||||
?: return chain.proceed(originalRequest)
|
|
||||||
|
|
||||||
val originalHeaders = originalRequest.headers
|
|
||||||
|
|
||||||
val modifiedHeaders = originalHeaders.newBuilder()
|
|
||||||
.set("User-Agent", newUserAgent)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
val modifiedRequest = originalRequest.newBuilder()
|
|
||||||
.headers(modifiedHeaders)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
return chain.proceed(modifiedRequest)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue