崩溃日志优先保存到备份文件夹,方便外部应用查看
This commit is contained in:
@@ -2,14 +2,14 @@ package io.legado.app.help
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.webkit.WebSettings
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.model.ReadAloud
|
||||
import io.legado.app.utils.FileUtils
|
||||
import io.legado.app.utils.getFile
|
||||
import io.legado.app.utils.longToastOnUi
|
||||
import io.legado.app.utils.stackTraceStr
|
||||
import io.legado.app.utils.*
|
||||
import splitties.init.appCtx
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
@@ -105,14 +105,25 @@ class CrashHandler(val context: Context) : Thread.UncaughtExceptionHandler {
|
||||
val timestamp = System.currentTimeMillis()
|
||||
val time = format.format(Date())
|
||||
val fileName = "crash-$time-$timestamp.log"
|
||||
appCtx.externalCacheDir?.let { rootFile ->
|
||||
rootFile.getFile("crash").listFiles()?.forEach {
|
||||
if (it.lastModified() < System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7)) {
|
||||
it.delete()
|
||||
}
|
||||
}
|
||||
FileUtils.createFileIfNotExist(rootFile, "crash", fileName)
|
||||
try {
|
||||
val backupPath = AppConfig.backupPath ?: throw NoStackTraceException("备份路径未配置")
|
||||
val uri = Uri.parse(backupPath)
|
||||
val fileDoc = FileDoc.fromUri(uri, true)
|
||||
fileDoc.createFileIfNotExist(fileName, "crash")
|
||||
.writeText(sb.toString())
|
||||
} catch (e: Exception) {
|
||||
appCtx.externalCacheDir?.let { rootFile ->
|
||||
rootFile.getFile("crash").listFiles()?.forEach {
|
||||
if (it.lastModified() < System.currentTimeMillis() - TimeUnit.DAYS.toMillis(
|
||||
7
|
||||
)
|
||||
) {
|
||||
it.delete()
|
||||
}
|
||||
}
|
||||
FileUtils.createFileIfNotExist(rootFile, "crash", fileName)
|
||||
.writeText(sb.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,10 @@ object DocumentUtils {
|
||||
fun createFileIfNotExist(
|
||||
root: DocumentFile,
|
||||
fileName: String,
|
||||
mimeType: String = "",
|
||||
vararg subDirs: String
|
||||
): DocumentFile? {
|
||||
val parent: DocumentFile? = createFolderIfNotExist(root, *subDirs)
|
||||
return parent?.findFile(fileName) ?: parent?.createFile(mimeType, fileName)
|
||||
return parent?.findFile(fileName) ?: parent?.createFile("", fileName)
|
||||
}
|
||||
|
||||
fun createFolderIfNotExist(root: DocumentFile, vararg subDirs: String): DocumentFile? {
|
||||
|
||||
@@ -183,12 +183,11 @@ fun FileDoc.find(name: String, depth: Int = 0): FileDoc? {
|
||||
|
||||
fun FileDoc.createFileIfNotExist(
|
||||
fileName: String,
|
||||
mimeType: String = "",
|
||||
vararg subDirs: String
|
||||
): FileDoc {
|
||||
return if (uri.isContentScheme()) {
|
||||
val documentFile = DocumentFile.fromTreeUri(appCtx, uri)!!
|
||||
val tmp = DocumentUtils.createFileIfNotExist(documentFile, fileName, mimeType, *subDirs)!!
|
||||
val tmp = DocumentUtils.createFileIfNotExist(documentFile, fileName, *subDirs)!!
|
||||
FileDoc.fromDocumentFile(tmp)
|
||||
} else {
|
||||
val path = FileUtils.getPath(uri.path!!, *subDirs) + File.separator + fileName
|
||||
@@ -235,6 +234,14 @@ fun FileDoc.exists(): Boolean {
|
||||
}
|
||||
}
|
||||
|
||||
fun FileDoc.writeText(text: String) {
|
||||
if (uri.isContentScheme()) {
|
||||
uri.writeText(appCtx, text)
|
||||
} else {
|
||||
File(uri.path!!).writeText(text)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DocumentFile 的 listFiles() 非常的慢,尽量不要使用
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user