pull/1822/head
kunfei 3 years ago
parent be6c8f8a38
commit e2932b3833
  1. 6
      app/src/main/java/io/legado/app/utils/RegexExtensions.kt

@ -9,6 +9,8 @@ import kotlinx.coroutines.suspendCancellableCoroutine
import splitties.init.appCtx import splitties.init.appCtx
import kotlin.coroutines.resume import kotlin.coroutines.resume
private val scope = MainScope()
/** /**
* 带有超时检测的正则替换 * 带有超时检测的正则替换
* 超时重启apk,线程不能强制结束,只能重启apk * 超时重启apk,线程不能强制结束,只能重启apk
@ -16,7 +18,7 @@ import kotlin.coroutines.resume
suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String { suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String {
val charSequence = this val charSequence = this
return suspendCancellableCoroutine { block -> return suspendCancellableCoroutine { block ->
val scope = MainScope().launch(IO) { val job = scope.launch(IO) {
try { try {
val result = regex.replace(charSequence, replacement) val result = regex.replace(charSequence, replacement)
block.resume(result) block.resume(result)
@ -25,7 +27,7 @@ suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Lon
} }
} }
mainHandler.postDelayed({ mainHandler.postDelayed({
if (scope.isActive) { if (job.isActive) {
val timeoutMsg = "替换超时,将在3秒后重启应用\n替换规则$regex\n替换内容:${this}" val timeoutMsg = "替换超时,将在3秒后重启应用\n替换规则$regex\n替换内容:${this}"
val exception = RegexTimeoutException(timeoutMsg) val exception = RegexTimeoutException(timeoutMsg)
block.cancel(exception) block.cancel(exception)

Loading…
Cancel
Save