Skip to content

Commit

Permalink
崩溃日志优先保存到备份文件夹,方便外部应用查看
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Mar 29, 2023
1 parent 1ca0222 commit d59ef46
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
31 changes: 21 additions & 10 deletions app/src/main/java/io/legado/app/help/CrashHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
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())
}
FileUtils.createFileIfNotExist(rootFile, "crash", fileName)
.writeText(sb.toString())
}
}

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/io/legado/app/utils/DocumentUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/io/legado/app/utils/FileDocExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() 非常的慢,尽量不要使用
*/
Expand Down

0 comments on commit d59ef46

Please sign in to comment.