优化
This commit is contained in:
@@ -8,21 +8,32 @@ import io.legado.app.help.IntentData
|
|||||||
import io.legado.app.ui.association.VerificationCodeActivity
|
import io.legado.app.ui.association.VerificationCodeActivity
|
||||||
import io.legado.app.ui.browser.WebViewActivity
|
import io.legado.app.ui.browser.WebViewActivity
|
||||||
import io.legado.app.utils.startActivity
|
import io.legado.app.utils.startActivity
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
|
import java.util.concurrent.locks.LockSupport
|
||||||
|
import kotlin.time.Duration.Companion.minutes
|
||||||
|
|
||||||
|
|
||||||
object SourceVerificationHelp {
|
object SourceVerificationHelp {
|
||||||
|
|
||||||
private var key: String = ""
|
private val waitTime = 1.minutes.inWholeNanoseconds
|
||||||
|
|
||||||
|
private fun getKey(source: BaseSource) = getKey(source.getKey())
|
||||||
|
fun getKey(sourceKey: String) = "${sourceKey}_verificationResult"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取书源验证结果
|
* 获取书源验证结果
|
||||||
* 图片验证码 防爬 滑动验证码 点击字符 等等
|
* 图片验证码 防爬 滑动验证码 点击字符 等等
|
||||||
*/
|
*/
|
||||||
fun getVerificationResult(source: BaseSource?, url: String, title: String, useBrowser: Boolean): String {
|
fun getVerificationResult(
|
||||||
source ?: throw NoStackTraceException("getVerificationResult parameter source cannot be null")
|
source: BaseSource?,
|
||||||
return runBlocking {
|
url: String,
|
||||||
key = "${source.getKey()}_verificationResult"
|
title: String,
|
||||||
|
useBrowser: Boolean
|
||||||
|
): String {
|
||||||
|
source
|
||||||
|
?: throw NoStackTraceException("getVerificationResult parameter source cannot be null")
|
||||||
|
|
||||||
|
val key = getKey(source)
|
||||||
CacheManager.delete(key)
|
CacheManager.delete(key)
|
||||||
|
|
||||||
if (!useBrowser) {
|
if (!useBrowser) {
|
||||||
@@ -30,33 +41,40 @@ object SourceVerificationHelp {
|
|||||||
putExtra("imageUrl", url)
|
putExtra("imageUrl", url)
|
||||||
putExtra("sourceOrigin", source.getKey())
|
putExtra("sourceOrigin", source.getKey())
|
||||||
putExtra("sourceName", source.getTag())
|
putExtra("sourceName", source.getTag())
|
||||||
|
IntentData.put(key, Thread.currentThread())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
startBrowser(source, url, title, true)
|
startBrowser(source, url, title, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
var waitUserInput = false
|
var waitUserInput = false
|
||||||
while(CacheManager.get(key) == null) {
|
while (CacheManager.get(key) == null) {
|
||||||
if (!waitUserInput) {
|
if (!waitUserInput) {
|
||||||
AppLog.putDebug("等待返回验证结果...")
|
AppLog.putDebug("等待返回验证结果...")
|
||||||
waitUserInput = true
|
waitUserInput = true
|
||||||
}
|
}
|
||||||
|
LockSupport.parkNanos(this, waitTime)
|
||||||
}
|
}
|
||||||
CacheManager.get(key)!!.let {
|
|
||||||
|
return CacheManager.get(key)!!.let {
|
||||||
it.ifBlank {
|
it.ifBlank {
|
||||||
throw NoStackTraceException("验证结果为空")
|
throw NoStackTraceException("验证结果为空")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动内置浏览器
|
* 启动内置浏览器
|
||||||
@param saveResult 保存网页源代码到数据库
|
* @param saveResult 保存网页源代码到数据库
|
||||||
*/
|
*/
|
||||||
fun startBrowser(source: BaseSource?, url: String, title: String, saveResult: Boolean? = false) {
|
fun startBrowser(
|
||||||
|
source: BaseSource?,
|
||||||
|
url: String,
|
||||||
|
title: String,
|
||||||
|
saveResult: Boolean? = false
|
||||||
|
) {
|
||||||
source ?: throw NoStackTraceException("startBrowser parameter source cannot be null")
|
source ?: throw NoStackTraceException("startBrowser parameter source cannot be null")
|
||||||
key = "${source.getKey()}_verificationResult"
|
val key = getKey(source)
|
||||||
appCtx.startActivity<WebViewActivity> {
|
appCtx.startActivity<WebViewActivity> {
|
||||||
putExtra("title", title)
|
putExtra("title", title)
|
||||||
putExtra("url", url)
|
putExtra("url", url)
|
||||||
@@ -64,10 +82,14 @@ object SourceVerificationHelp {
|
|||||||
putExtra("sourceName", source.getTag())
|
putExtra("sourceName", source.getTag())
|
||||||
putExtra("sourceVerificationEnable", saveResult)
|
putExtra("sourceVerificationEnable", saveResult)
|
||||||
IntentData.put(url, source.getHeaderMap(true))
|
IntentData.put(url, source.getHeaderMap(true))
|
||||||
|
IntentData.put(key, Thread.currentThread())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkResult() {
|
|
||||||
|
fun checkResult(key: String) {
|
||||||
CacheManager.get(key) ?: CacheManager.putMemory(key, "")
|
CacheManager.get(key) ?: CacheManager.putMemory(key, "")
|
||||||
|
val thread = IntentData.get<Thread>(key)
|
||||||
|
LockSupport.unpark(thread)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,22 +56,22 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification
|
|||||||
setLayout(1f, ViewGroup.LayoutParams.WRAP_CONTENT)
|
setLayout(1f, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
private var sourceOrigin: String? = null
|
||||||
|
private var key = ""
|
||||||
|
|
||||||
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?): Unit = binding.run {
|
||||||
initMenu()
|
initMenu()
|
||||||
binding.run {
|
val arguments = arguments ?: return@run
|
||||||
toolBar.setBackgroundColor(primaryColor)
|
toolBar.setBackgroundColor(primaryColor)
|
||||||
arguments?.let { arguments ->
|
|
||||||
toolBar.subtitle = arguments.getString("sourceName")
|
toolBar.subtitle = arguments.getString("sourceName")
|
||||||
val sourceOrigin = arguments.getString("sourceOrigin")
|
sourceOrigin = arguments.getString("sourceOrigin")
|
||||||
arguments.getString("imageUrl")?.let { imageUrl ->
|
key = SourceVerificationHelp.getKey(sourceOrigin!!)
|
||||||
|
val imageUrl = arguments.getString("imageUrl") ?: return@run
|
||||||
loadImage(imageUrl, sourceOrigin)
|
loadImage(imageUrl, sourceOrigin)
|
||||||
verificationCodeImageView.setOnClickListener {
|
verificationCodeImageView.setOnClickListener {
|
||||||
showDialogFragment(PhotoDialog(imageUrl, sourceOrigin))
|
showDialogFragment(PhotoDialog(imageUrl, sourceOrigin))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun initMenu() {
|
private fun initMenu() {
|
||||||
binding.toolBar.setOnMenuItemClickListener(this)
|
binding.toolBar.setOnMenuItemClickListener(this)
|
||||||
@@ -118,20 +118,16 @@ class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification
|
|||||||
override fun onMenuItemClick(item: MenuItem): Boolean {
|
override fun onMenuItemClick(item: MenuItem): Boolean {
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.menu_ok -> {
|
R.id.menu_ok -> {
|
||||||
val sourceOrigin = arguments?.getString("sourceOrigin")
|
|
||||||
val key = "${sourceOrigin}_verificationResult"
|
|
||||||
val verificationCode = binding.verificationCode.text.toString()
|
val verificationCode = binding.verificationCode.text.toString()
|
||||||
verificationCode.let {
|
CacheManager.putMemory(key, verificationCode)
|
||||||
CacheManager.putMemory(key, it)
|
|
||||||
dismiss()
|
dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
SourceVerificationHelp.checkResult()
|
SourceVerificationHelp.checkResult(key)
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
activity?.finish()
|
activity?.finish()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,8 +172,12 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
|
|||||||
return super.onKeyUp(keyCode, event)
|
return super.onKeyUp(keyCode, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun finish() {
|
||||||
|
SourceVerificationHelp.checkResult(viewModel.key)
|
||||||
|
super.finish()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
SourceVerificationHelp.checkResult()
|
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
binding.webView.destroy()
|
binding.webView.destroy()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import io.legado.app.help.CacheManager
|
|||||||
import io.legado.app.help.IntentData
|
import io.legado.app.help.IntentData
|
||||||
import io.legado.app.help.http.newCallResponseBody
|
import io.legado.app.help.http.newCallResponseBody
|
||||||
import io.legado.app.help.http.okHttpClient
|
import io.legado.app.help.http.okHttpClient
|
||||||
|
import io.legado.app.help.source.SourceVerificationHelp
|
||||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||||
import io.legado.app.utils.DocumentUtils
|
import io.legado.app.utils.DocumentUtils
|
||||||
import io.legado.app.utils.FileUtils
|
import io.legado.app.utils.FileUtils
|
||||||
@@ -30,6 +31,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
val headerMap: HashMap<String, String> = hashMapOf()
|
val headerMap: HashMap<String, String> = hashMapOf()
|
||||||
var sourceVerificationEnable: Boolean = false
|
var sourceVerificationEnable: Boolean = false
|
||||||
var sourceOrigin: String = ""
|
var sourceOrigin: String = ""
|
||||||
|
var key = ""
|
||||||
|
|
||||||
fun initData(
|
fun initData(
|
||||||
intent: Intent,
|
intent: Intent,
|
||||||
@@ -39,6 +41,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
val url = intent.getStringExtra("url")
|
val url = intent.getStringExtra("url")
|
||||||
?: throw NoStackTraceException("url不能为空")
|
?: throw NoStackTraceException("url不能为空")
|
||||||
sourceOrigin = intent.getStringExtra("sourceOrigin") ?: ""
|
sourceOrigin = intent.getStringExtra("sourceOrigin") ?: ""
|
||||||
|
key = SourceVerificationHelp.getKey(sourceOrigin)
|
||||||
sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false)
|
sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false)
|
||||||
val headerMapF = IntentData.get<Map<String, String>>(url)
|
val headerMapF = IntentData.get<Map<String, String>>(url)
|
||||||
val analyzeUrl = AnalyzeUrl(url, headerMapF = headerMapF)
|
val analyzeUrl = AnalyzeUrl(url, headerMapF = headerMapF)
|
||||||
|
|||||||
Reference in New Issue
Block a user