diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index ba51ce764..5cbb5a48b 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -18,10 +18,6 @@ object AppConst { const val UA_NAME = "User-Agent" - val userAgent: String by lazy { - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" - } - val SCRIPT_ENGINE: ScriptEngine by lazy { ScriptEngineManager().getEngineByName("rhino") } diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 548356466..22a96517a 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -3,6 +3,7 @@ package io.legado.app.constant object PreferKey { const val language = "language" const val themeMode = "themeMode" + const val userAgent = "userAgent" const val hideStatusBar = "hideStatusBar" const val clickActionTL = "clickActionTopLeft" const val clickActionTC = "clickActionTopCenter" diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 29ffdcfd1..1169ba690 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -5,11 +5,14 @@ import android.text.TextUtils import androidx.room.* import io.legado.app.App import io.legado.app.constant.AppConst -import io.legado.app.constant.AppConst.userAgent import io.legado.app.constant.BookType import io.legado.app.data.entities.rule.* +import io.legado.app.help.AppConfig import io.legado.app.help.JsExtensions -import io.legado.app.utils.* +import io.legado.app.utils.ACache +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import io.legado.app.utils.splitNotBlank import kotlinx.android.parcel.Parcelize import javax.script.SimpleBindings @@ -51,7 +54,7 @@ data class BookSource( @Throws(Exception::class) fun getHeaderMap() = (HashMap().apply { - this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent + this[AppConst.UA_NAME] = AppConfig.userAgent header?.let { GSON.fromJsonObject>( when { diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index 90418f0c0..0b40b3f13 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -4,12 +4,11 @@ import android.os.Parcelable import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey -import io.legado.app.App import io.legado.app.constant.AppConst +import io.legado.app.help.AppConfig import io.legado.app.help.JsExtensions import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject -import io.legado.app.utils.getPrefString import kotlinx.android.parcel.Parcelize import java.util.* import javax.script.SimpleBindings @@ -54,7 +53,7 @@ data class RssSource( @Throws(Exception::class) fun getHeaderMap() = HashMap().apply { - this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent + this[AppConst.UA_NAME] = AppConfig.userAgent header?.let { GSON.fromJsonObject>( when { diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt index 3ff4035e6..bf15b1781 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -12,6 +12,7 @@ import io.legado.app.utils.* object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { private val context get() = App.INSTANCE val isGooglePlay = context.channel == "google" + var userAgent: String = getPrefUserAgent() var replaceEnableDefault = context.getPrefBoolean(PreferKey.replaceEnableDefault, true) var isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" var clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2) @@ -26,39 +27,28 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { when (key) { - PreferKey.themeMode -> { - isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" - } - PreferKey.clickActionTL -> { - clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2) - } - PreferKey.clickActionTC -> { - clickActionTC = context.getPrefInt(PreferKey.clickActionTC, 2) - } - PreferKey.clickActionTR -> { - clickActionTR = context.getPrefInt(PreferKey.clickActionTR, 2) - } - PreferKey.clickActionML -> { - clickActionML = context.getPrefInt(PreferKey.clickActionML, 2) - } - PreferKey.clickActionMC -> { - clickActionMC = context.getPrefInt(PreferKey.clickActionMC, 2) - } - PreferKey.clickActionMR -> { - clickActionMR = context.getPrefInt(PreferKey.clickActionMR, 2) - } - PreferKey.clickActionBL -> { - clickActionBL = context.getPrefInt(PreferKey.clickActionBL, 2) - } - PreferKey.clickActionBC -> { - clickActionBC = context.getPrefInt(PreferKey.clickActionBC, 2) - } - PreferKey.clickActionBR -> { - clickActionBR = context.getPrefInt(PreferKey.clickActionBR, 2) - } - PreferKey.readBodyToLh -> { - ReadBookConfig.readBodyToLh = context.getPrefBoolean(PreferKey.readBodyToLh, true) - } + PreferKey.themeMode -> isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" + PreferKey.clickActionTL -> clickActionTL = + context.getPrefInt(PreferKey.clickActionTL, 2) + PreferKey.clickActionTC -> clickActionTC = + context.getPrefInt(PreferKey.clickActionTC, 2) + PreferKey.clickActionTR -> clickActionTR = + context.getPrefInt(PreferKey.clickActionTR, 2) + PreferKey.clickActionML -> clickActionML = + context.getPrefInt(PreferKey.clickActionML, 2) + PreferKey.clickActionMC -> clickActionMC = + context.getPrefInt(PreferKey.clickActionMC, 2) + PreferKey.clickActionMR -> clickActionMR = + context.getPrefInt(PreferKey.clickActionMR, 2) + PreferKey.clickActionBL -> clickActionBL = + context.getPrefInt(PreferKey.clickActionBL, 2) + PreferKey.clickActionBC -> clickActionBC = + context.getPrefInt(PreferKey.clickActionBC, 2) + PreferKey.clickActionBR -> clickActionBR = + context.getPrefInt(PreferKey.clickActionBR, 2) + PreferKey.readBodyToLh -> ReadBookConfig.readBodyToLh = + context.getPrefBoolean(PreferKey.readBodyToLh, true) + PreferKey.userAgent -> userAgent = getPrefUserAgent() } } @@ -157,5 +147,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { val importKeepName get() = context.getPrefBoolean(PreferKey.importKeepName) + private fun getPrefUserAgent(): String { + val ua = context.getPrefString(PreferKey.userAgent) + if (ua.isNullOrBlank()) { + return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36" + } + return ua + } } diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index a226b46d6..c6568f50e 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -1,6 +1,7 @@ package io.legado.app.help.http import io.legado.app.constant.AppConst +import io.legado.app.help.AppConfig import io.legado.app.help.http.api.HttpGetApi import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.suspendCancellableCoroutine @@ -41,7 +42,7 @@ object HttpHelper { fun simpleGet(url: String, encode: String? = null): String? { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> val response = getApiService(baseUrl, encode) - .get(url, mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) + .get(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) .execute() return response.body() } @@ -51,7 +52,7 @@ object HttpHelper { suspend fun simpleGetAsync(url: String, encode: String? = null): String? { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> val response = getApiService(baseUrl, encode) - .getAsync(url, mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) + .getAsync(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) return response.body() } return null @@ -61,7 +62,7 @@ object HttpHelper { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> return getByteRetrofit(baseUrl) .create(HttpGetApi::class.java) - .getMapByteAsync(url, mapOf(), mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) + .getMapByteAsync(url, mapOf(), mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent))) .body() } return null diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 16d9f3d29..aa4e7ec17 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -7,11 +7,11 @@ import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.LazyHeaders import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.AppConst.UA_NAME -import io.legado.app.constant.AppConst.userAgent import io.legado.app.constant.AppPattern.EXP_PATTERN import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BookChapter +import io.legado.app.help.AppConfig import io.legado.app.help.JsExtensions import io.legado.app.help.http.* import io.legado.app.help.http.api.HttpGetApi @@ -204,7 +204,7 @@ class AnalyzeUrl( } } headerMap[UA_NAME] ?: let { - headerMap[UA_NAME] = userAgent + headerMap[UA_NAME] = AppConfig.userAgent } when (method) { RequestMethod.GET -> { diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index e3a6d7e9f..eba591739 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.config +import android.annotation.SuppressLint import android.app.Activity.RESULT_OK import android.content.ComponentName import android.content.Intent @@ -29,7 +30,9 @@ import io.legado.app.service.WebService import io.legado.app.ui.main.MainActivity import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.number.NumberPickerDialog +import io.legado.app.ui.widget.text.AutoCompleteTextView import io.legado.app.utils.* +import kotlinx.android.synthetic.main.dialog_edit_text.view.* import java.io.File @@ -48,6 +51,7 @@ class OtherConfigFragment : BasePreferenceFragment(), override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { putPrefBoolean(PreferKey.processText, isProcessTextEnabled()) addPreferencesFromResource(R.xml.pref_config_other) + upPreferenceSummary(PreferKey.userAgent, AppConfig.userAgent) upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString()) upPreferenceSummary(PreferKey.webPort, webPort.toString()) upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover)) @@ -66,6 +70,7 @@ class OtherConfigFragment : BasePreferenceFragment(), override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { + PreferKey.userAgent -> showUserAgentDialog() PreferKey.threadCount -> NumberPickerDialog(requireContext()) .setTitle(getString(R.string.threads_num_title)) .setMaxValue(999) @@ -117,8 +122,7 @@ class OtherConfigFragment : BasePreferenceFragment(), } PreferKey.showRss -> postEvent(EventBus.SHOW_RSS, "") PreferKey.defaultCover -> upPreferenceSummary( - key, - getPrefString(PreferKey.defaultCover) + key, getPrefString(PreferKey.defaultCover) ) PreferKey.language -> listView.postDelayed({ LanguageUtils.setConfiguration(App.INSTANCE) @@ -127,6 +131,9 @@ class OtherConfigFragment : BasePreferenceFragment(), App.INSTANCE.startActivity(intent) Process.killProcess(Process.myPid()) }, 1000) + PreferKey.userAgent -> listView.post { + upPreferenceSummary(PreferKey.userAgent, AppConfig.userAgent) + } } } @@ -145,6 +152,28 @@ class OtherConfigFragment : BasePreferenceFragment(), } } + @SuppressLint("InflateParams") + private fun showUserAgentDialog() { + alert("UserAgent") { + var editText: AutoCompleteTextView? = null + customView { + layoutInflater.inflate(R.layout.dialog_edit_text, null).apply { + edit_view.setText(AppConfig.userAgent) + editText = edit_view + } + } + okButton { + val userAgent = editText?.text?.toString() + if (userAgent.isNullOrBlank()) { + removePref(PreferKey.userAgent) + } else { + putPrefString(PreferKey.userAgent, userAgent) + } + } + noButton() + }.show() + } + private fun clearCache() { requireContext().alert( titleResource = R.string.clear_cache, diff --git a/app/src/main/res/values/pref_key_value.xml b/app/src/main/res/values/pref_key_value.xml index a0247482b..266c4cb51 100644 --- a/app/src/main/res/values/pref_key_value.xml +++ b/app/src/main/res/values/pref_key_value.xml @@ -2,15 +2,8 @@ auto_refresh list_screen_direction - full_screen - threads_num - user_agent bookshelf_px - read_type - expandGroupFind defaultToRead - autoDownload - checkUpdate 开源阅读 https://alanskycn.gitee.io/teachme/ diff --git a/app/src/main/res/xml/pref_config_other.xml b/app/src/main/res/xml/pref_config_other.xml index 1a1541988..7077d665b 100644 --- a/app/src/main/res/xml/pref_config_other.xml +++ b/app/src/main/res/xml/pref_config_other.xml @@ -51,6 +51,10 @@ app:iconSpaceReserved="false" app:layout="@layout/view_preference_category"> + +