|
|
@ -2,15 +2,11 @@ package io.legado.app.utils |
|
|
|
|
|
|
|
|
|
|
|
import io.legado.app.exception.RegexTimeoutException |
|
|
|
import io.legado.app.exception.RegexTimeoutException |
|
|
|
import io.legado.app.help.CrashHandler |
|
|
|
import io.legado.app.help.CrashHandler |
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
|
|
|
|
import kotlinx.coroutines.MainScope |
|
|
|
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine |
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine |
|
|
|
import splitties.init.appCtx |
|
|
|
import splitties.init.appCtx |
|
|
|
|
|
|
|
import kotlin.concurrent.thread |
|
|
|
import kotlin.coroutines.resume |
|
|
|
import kotlin.coroutines.resume |
|
|
|
|
|
|
|
|
|
|
|
private val scope = MainScope() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 带有超时检测的正则替换 |
|
|
|
* 带有超时检测的正则替换 |
|
|
|
* 超时重启apk,线程不能强制结束,只能重启apk |
|
|
|
* 超时重启apk,线程不能强制结束,只能重启apk |
|
|
@ -18,7 +14,7 @@ private val scope = MainScope() |
|
|
|
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 job = scope.launch(IO) { |
|
|
|
val thread = thread { |
|
|
|
try { |
|
|
|
try { |
|
|
|
val result = regex.replace(charSequence, replacement) |
|
|
|
val result = regex.replace(charSequence, replacement) |
|
|
|
block.resume(result) |
|
|
|
block.resume(result) |
|
|
@ -27,14 +23,16 @@ suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Lon |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
mainHandler.postDelayed({ |
|
|
|
mainHandler.postDelayed({ |
|
|
|
if (job.isActive) { |
|
|
|
if (thread.isAlive) { |
|
|
|
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) |
|
|
|
appCtx.longToastOnUi(timeoutMsg) |
|
|
|
appCtx.longToastOnUi(timeoutMsg) |
|
|
|
CrashHandler.saveCrashInfo2File(exception) |
|
|
|
CrashHandler.saveCrashInfo2File(exception) |
|
|
|
mainHandler.postDelayed({ |
|
|
|
mainHandler.postDelayed({ |
|
|
|
appCtx.restart() |
|
|
|
if (thread.isAlive) { |
|
|
|
|
|
|
|
appCtx.restart() |
|
|
|
|
|
|
|
} |
|
|
|
}, 3000) |
|
|
|
}, 3000) |
|
|
|
} |
|
|
|
} |
|
|
|
}, timeout) |
|
|
|
}, timeout) |
|
|
|