This commit is contained in:
Horis 2023-11-24 19:14:52 +08:00
parent bd4aa95ff5
commit 8df1cb8ab6

View File

@ -4,8 +4,10 @@ import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Looper
import android.webkit.WebSettings
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.LocalConfig
@ -37,9 +39,26 @@ class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler {
* uncaughtException 回调函数
*/
override fun uncaughtException(thread: Thread, ex: Throwable) {
ReadAloud.stop(context)
handleException(ex)
mDefaultHandler?.uncaughtException(thread, ex)
if (shouldAbsorb(ex)) {
AppLog.put("发生未捕获的异常\n${ex.localizedMessage}", ex)
Looper.loop()
} else {
ReadAloud.stop(context)
handleException(ex)
mDefaultHandler?.uncaughtException(thread, ex)
}
}
private fun shouldAbsorb(e: Throwable): Boolean {
return when {
e::class.simpleName == "CannotDeliverBroadcastException" -> true
e is SecurityException && e.message?.contains(
"nor current process has android.permission.OBSERVE_GRANT_REVOKE_PERMISSIONS",
true
) == true -> true
else -> false
}
}
/**
@ -107,7 +126,8 @@ class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler {
val time = format.format(Date())
val fileName = "crash-$time-$timestamp.log"
try {
val backupPath = AppConfig.backupPath ?: throw NoStackTraceException("备份路径未配置")
val backupPath = AppConfig.backupPath
?: throw NoStackTraceException("备份路径未配置")
val uri = Uri.parse(backupPath)
val fileDoc = FileDoc.fromUri(uri, true)
fileDoc.createFileIfNotExist(fileName, "crash")