This commit is contained in:
Horis committed 2023-11-24 19:14:52 +08:00
1 parent a79bc703c3
commit 610c0cefba
1 file changed
+24 -4
@@ -4,8 +4,10 @@ import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Looper
import android.webkit.WebSettings import android.webkit.WebSettings
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.LocalConfig import io.legado.app.help.config.LocalConfig
@@ -37,9 +39,26 @@ class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler {
* uncaughtException 回调函数 * uncaughtException 回调函数
*/ */
override fun uncaughtException(thread: Thread, ex: Throwable) { override fun uncaughtException(thread: Thread, ex: Throwable) {
ReadAloud.stop(context) if (shouldAbsorb(ex)) {
handleException(ex) AppLog.put("发生未捕获的异常\n${ex.localizedMessage}", ex)
mDefaultHandler?.uncaughtException(thread, 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 time = format.format(Date())
val fileName = "crash-$time-$timestamp.log" val fileName = "crash-$time-$timestamp.log"
try { try {
val backupPath = AppConfig.backupPath ?: throw NoStackTraceException("备份路径未配置") val backupPath = AppConfig.backupPath
?: throw NoStackTraceException("备份路径未配置")
val uri = Uri.parse(backupPath) val uri = Uri.parse(backupPath)
val fileDoc = FileDoc.fromUri(uri, true) val fileDoc = FileDoc.fromUri(uri, true)
fileDoc.createFileIfNotExist(fileName, "crash") fileDoc.createFileIfNotExist(fileName, "crash")