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