pull/480/head
gedoor 4 years ago
parent c0a2b69eea
commit 88779ee756
  1. 4
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  3. 9
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  4. 5
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  5. 63
      app/src/main/java/io/legado/app/help/AppConfig.kt
  6. 7
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  7. 4
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  8. 33
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  9. 7
      app/src/main/res/values/pref_key_value.xml
  10. 4
      app/src/main/res/xml/pref_config_other.xml

@ -18,10 +18,6 @@ object AppConst {
const val UA_NAME = "User-Agent" 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 { val SCRIPT_ENGINE: ScriptEngine by lazy {
ScriptEngineManager().getEngineByName("rhino") ScriptEngineManager().getEngineByName("rhino")
} }

@ -3,6 +3,7 @@ package io.legado.app.constant
object PreferKey { object PreferKey {
const val language = "language" const val language = "language"
const val themeMode = "themeMode" const val themeMode = "themeMode"
const val userAgent = "userAgent"
const val hideStatusBar = "hideStatusBar" const val hideStatusBar = "hideStatusBar"
const val clickActionTL = "clickActionTopLeft" const val clickActionTL = "clickActionTopLeft"
const val clickActionTC = "clickActionTopCenter" const val clickActionTC = "clickActionTopCenter"

@ -5,11 +5,14 @@ import android.text.TextUtils
import androidx.room.* import androidx.room.*
import io.legado.app.App import io.legado.app.App
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.AppConst.userAgent
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.entities.rule.* import io.legado.app.data.entities.rule.*
import io.legado.app.help.AppConfig
import io.legado.app.help.JsExtensions 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 kotlinx.android.parcel.Parcelize
import javax.script.SimpleBindings import javax.script.SimpleBindings
@ -51,7 +54,7 @@ data class BookSource(
@Throws(Exception::class) @Throws(Exception::class)
fun getHeaderMap() = (HashMap<String, String>().apply { fun getHeaderMap() = (HashMap<String, String>().apply {
this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: userAgent this[AppConst.UA_NAME] = AppConfig.userAgent
header?.let { header?.let {
GSON.fromJsonObject<Map<String, String>>( GSON.fromJsonObject<Map<String, String>>(
when { when {

@ -4,12 +4,11 @@ import android.os.Parcelable
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Index import androidx.room.Index
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import io.legado.app.App
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.help.AppConfig
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getPrefString
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import java.util.* import java.util.*
import javax.script.SimpleBindings import javax.script.SimpleBindings
@ -54,7 +53,7 @@ data class RssSource(
@Throws(Exception::class) @Throws(Exception::class)
fun getHeaderMap() = HashMap<String, String>().apply { fun getHeaderMap() = HashMap<String, String>().apply {
this[AppConst.UA_NAME] = App.INSTANCE.getPrefString("user_agent") ?: AppConst.userAgent this[AppConst.UA_NAME] = AppConfig.userAgent
header?.let { header?.let {
GSON.fromJsonObject<Map<String, String>>( GSON.fromJsonObject<Map<String, String>>(
when { when {

@ -12,6 +12,7 @@ import io.legado.app.utils.*
object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
private val context get() = App.INSTANCE private val context get() = App.INSTANCE
val isGooglePlay = context.channel == "google" val isGooglePlay = context.channel == "google"
var userAgent: String = getPrefUserAgent()
var replaceEnableDefault = context.getPrefBoolean(PreferKey.replaceEnableDefault, true) var replaceEnableDefault = context.getPrefBoolean(PreferKey.replaceEnableDefault, true)
var isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" var isEInkMode = context.getPrefString(PreferKey.themeMode) == "3"
var clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2) var clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2)
@ -26,39 +27,28 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) { when (key) {
PreferKey.themeMode -> { PreferKey.themeMode -> isEInkMode = context.getPrefString(PreferKey.themeMode) == "3"
isEInkMode = context.getPrefString(PreferKey.themeMode) == "3" PreferKey.clickActionTL -> clickActionTL =
} context.getPrefInt(PreferKey.clickActionTL, 2)
PreferKey.clickActionTL -> { PreferKey.clickActionTC -> clickActionTC =
clickActionTL = context.getPrefInt(PreferKey.clickActionTL, 2) context.getPrefInt(PreferKey.clickActionTC, 2)
} PreferKey.clickActionTR -> clickActionTR =
PreferKey.clickActionTC -> { context.getPrefInt(PreferKey.clickActionTR, 2)
clickActionTC = context.getPrefInt(PreferKey.clickActionTC, 2) PreferKey.clickActionML -> clickActionML =
} context.getPrefInt(PreferKey.clickActionML, 2)
PreferKey.clickActionTR -> { PreferKey.clickActionMC -> clickActionMC =
clickActionTR = context.getPrefInt(PreferKey.clickActionTR, 2) context.getPrefInt(PreferKey.clickActionMC, 2)
} PreferKey.clickActionMR -> clickActionMR =
PreferKey.clickActionML -> { context.getPrefInt(PreferKey.clickActionMR, 2)
clickActionML = context.getPrefInt(PreferKey.clickActionML, 2) PreferKey.clickActionBL -> clickActionBL =
} context.getPrefInt(PreferKey.clickActionBL, 2)
PreferKey.clickActionMC -> { PreferKey.clickActionBC -> clickActionBC =
clickActionMC = context.getPrefInt(PreferKey.clickActionMC, 2) context.getPrefInt(PreferKey.clickActionBC, 2)
} PreferKey.clickActionBR -> clickActionBR =
PreferKey.clickActionMR -> { context.getPrefInt(PreferKey.clickActionBR, 2)
clickActionMR = context.getPrefInt(PreferKey.clickActionMR, 2) PreferKey.readBodyToLh -> ReadBookConfig.readBodyToLh =
} context.getPrefBoolean(PreferKey.readBodyToLh, true)
PreferKey.clickActionBL -> { PreferKey.userAgent -> userAgent = getPrefUserAgent()
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)
}
} }
} }
@ -157,5 +147,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
val importKeepName get() = context.getPrefBoolean(PreferKey.importKeepName) 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
}
} }

@ -1,6 +1,7 @@
package io.legado.app.help.http package io.legado.app.help.http
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.help.AppConfig
import io.legado.app.help.http.api.HttpGetApi import io.legado.app.help.http.api.HttpGetApi
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
@ -41,7 +42,7 @@ object HttpHelper {
fun simpleGet(url: String, encode: String? = null): String? { fun simpleGet(url: String, encode: String? = null): String? {
NetworkUtils.getBaseUrl(url)?.let { baseUrl -> NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
val response = getApiService<HttpGetApi>(baseUrl, encode) val response = getApiService<HttpGetApi>(baseUrl, encode)
.get(url, mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) .get(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent)))
.execute() .execute()
return response.body() return response.body()
} }
@ -51,7 +52,7 @@ object HttpHelper {
suspend fun simpleGetAsync(url: String, encode: String? = null): String? { suspend fun simpleGetAsync(url: String, encode: String? = null): String? {
NetworkUtils.getBaseUrl(url)?.let { baseUrl -> NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
val response = getApiService<HttpGetApi>(baseUrl, encode) val response = getApiService<HttpGetApi>(baseUrl, encode)
.getAsync(url, mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent))) .getAsync(url, mapOf(Pair(AppConst.UA_NAME, AppConfig.userAgent)))
return response.body() return response.body()
} }
return null return null
@ -61,7 +62,7 @@ object HttpHelper {
NetworkUtils.getBaseUrl(url)?.let { baseUrl -> NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
return getByteRetrofit(baseUrl) return getByteRetrofit(baseUrl)
.create(HttpGetApi::class.java) .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() .body()
} }
return null return null

@ -7,11 +7,11 @@ import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders import com.bumptech.glide.load.model.LazyHeaders
import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.AppConst.SCRIPT_ENGINE
import io.legado.app.constant.AppConst.UA_NAME 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.EXP_PATTERN
import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.constant.AppPattern.JS_PATTERN
import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BaseBook
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.AppConfig
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.help.http.* import io.legado.app.help.http.*
import io.legado.app.help.http.api.HttpGetApi import io.legado.app.help.http.api.HttpGetApi
@ -204,7 +204,7 @@ class AnalyzeUrl(
} }
} }
headerMap[UA_NAME] ?: let { headerMap[UA_NAME] ?: let {
headerMap[UA_NAME] = userAgent headerMap[UA_NAME] = AppConfig.userAgent
} }
when (method) { when (method) {
RequestMethod.GET -> { RequestMethod.GET -> {

@ -1,5 +1,6 @@
package io.legado.app.ui.config package io.legado.app.ui.config
import android.annotation.SuppressLint
import android.app.Activity.RESULT_OK import android.app.Activity.RESULT_OK
import android.content.ComponentName import android.content.ComponentName
import android.content.Intent 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.main.MainActivity
import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.ui.widget.image.CoverImageView
import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.ui.widget.number.NumberPickerDialog
import io.legado.app.ui.widget.text.AutoCompleteTextView
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_edit_text.view.*
import java.io.File import java.io.File
@ -48,6 +51,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
putPrefBoolean(PreferKey.processText, isProcessTextEnabled()) putPrefBoolean(PreferKey.processText, isProcessTextEnabled())
addPreferencesFromResource(R.xml.pref_config_other) addPreferencesFromResource(R.xml.pref_config_other)
upPreferenceSummary(PreferKey.userAgent, AppConfig.userAgent)
upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString()) upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString())
upPreferenceSummary(PreferKey.webPort, webPort.toString()) upPreferenceSummary(PreferKey.webPort, webPort.toString())
upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover)) upPreferenceSummary(PreferKey.defaultCover, getPrefString(PreferKey.defaultCover))
@ -66,6 +70,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
override fun onPreferenceTreeClick(preference: Preference?): Boolean { override fun onPreferenceTreeClick(preference: Preference?): Boolean {
when (preference?.key) { when (preference?.key) {
PreferKey.userAgent -> showUserAgentDialog()
PreferKey.threadCount -> NumberPickerDialog(requireContext()) PreferKey.threadCount -> NumberPickerDialog(requireContext())
.setTitle(getString(R.string.threads_num_title)) .setTitle(getString(R.string.threads_num_title))
.setMaxValue(999) .setMaxValue(999)
@ -117,8 +122,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
} }
PreferKey.showRss -> postEvent(EventBus.SHOW_RSS, "") PreferKey.showRss -> postEvent(EventBus.SHOW_RSS, "")
PreferKey.defaultCover -> upPreferenceSummary( PreferKey.defaultCover -> upPreferenceSummary(
key, key, getPrefString(PreferKey.defaultCover)
getPrefString(PreferKey.defaultCover)
) )
PreferKey.language -> listView.postDelayed({ PreferKey.language -> listView.postDelayed({
LanguageUtils.setConfiguration(App.INSTANCE) LanguageUtils.setConfiguration(App.INSTANCE)
@ -127,6 +131,9 @@ class OtherConfigFragment : BasePreferenceFragment(),
App.INSTANCE.startActivity(intent) App.INSTANCE.startActivity(intent)
Process.killProcess(Process.myPid()) Process.killProcess(Process.myPid())
}, 1000) }, 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() { private fun clearCache() {
requireContext().alert( requireContext().alert(
titleResource = R.string.clear_cache, titleResource = R.string.clear_cache,

@ -2,15 +2,8 @@
<resources> <resources>
<string name="pk_auto_refresh" translatable="false">auto_refresh</string> <string name="pk_auto_refresh" translatable="false">auto_refresh</string>
<string name="pk_requested_direction" translatable="false">list_screen_direction</string> <string name="pk_requested_direction" translatable="false">list_screen_direction</string>
<string name="pk_full_screen" translatable="false">full_screen</string>
<string name="pk_threads_num" translatable="false">threads_num</string>
<string name="pk_user_agent" translatable="false">user_agent</string>
<string name="pk_bookshelf_px" translatable="false">bookshelf_px</string> <string name="pk_bookshelf_px" translatable="false">bookshelf_px</string>
<string name="pk_read_type" translatable="false">read_type</string>
<string name="pk_find_expand_group" translatable="false">expandGroupFind</string>
<string name="pk_default_read" translatable="false">defaultToRead</string> <string name="pk_default_read" translatable="false">defaultToRead</string>
<string name="pk_auto_download" translatable="false">autoDownload</string>
<string name="pk_check_update" translatable="false">checkUpdate</string>
<string name="legado_gzh" translatable="false">开源阅读</string> <string name="legado_gzh" translatable="false">开源阅读</string>
<string name="source_rule_url" translatable="false">https://alanskycn.gitee.io/teachme/</string> <string name="source_rule_url" translatable="false">https://alanskycn.gitee.io/teachme/</string>

@ -51,6 +51,10 @@
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
app:layout="@layout/view_preference_category"> app:layout="@layout/view_preference_category">
<io.legado.app.ui.widget.prefs.Preference
android:key="userAgent"
android:title="UserAgent" />
<io.legado.app.ui.widget.prefs.SwitchPreference <io.legado.app.ui.widget.prefs.SwitchPreference
android:defaultValue="true" android:defaultValue="true"
android:key="replaceEnableDefault" android:key="replaceEnableDefault"

Loading…
Cancel
Save