From be43148a2bff967aea974fcd64a55362e0ca9705 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 17 Feb 2020 21:31:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/CrashHandler.kt | 10 ++++++++-- .../io/legado/app/help/storage/WebDavHelp.kt | 9 +++------ .../main/java/io/legado/app/utils/FileUtils.kt | 9 +++++++-- .../main/java/io/legado/app/utils/LogUtils.kt | 18 ++++++++---------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/CrashHandler.kt b/app/src/main/java/io/legado/app/help/CrashHandler.kt index f597bb846..b59609e18 100644 --- a/app/src/main/java/io/legado/app/help/CrashHandler.kt +++ b/app/src/main/java/io/legado/app/help/CrashHandler.kt @@ -14,6 +14,7 @@ import java.io.PrintWriter import java.io.StringWriter import java.text.SimpleDateFormat import java.util.* +import java.util.concurrent.TimeUnit /** * 异常管理类 @@ -140,8 +141,13 @@ class CrashHandler : Thread.UncaughtExceptionHandler { val timestamp = System.currentTimeMillis() val time = format.format(Date()) val fileName = "crash-$time-$timestamp.log" - mContext?.externalCacheDir?.let { - FileUtils.createFileIfNotExist(it, fileName, "crash") + mContext?.externalCacheDir?.let { rootFile -> + FileUtils.getDirFile(rootFile, "crash").listFiles()?.forEach { + if (it.lastModified() < System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7)) { + it.delete() + } + } + FileUtils.createFileIfNotExist(rootFile, fileName, "crash") .writeText(sb.toString()) } } diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index 0843dbb5d..a1386f9f7 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -23,10 +23,7 @@ import kotlin.math.min object WebDavHelp { private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" - private val zipFilePath = FileUtils.getCachePath() + "/backup" + ".zip" - private val unzipFilesPath by lazy { - FileUtils.getCachePath() - } + private val zipFilePath = "${FileUtils.getCachePath()}${File.separator}backup.zip" private fun getWebDavUrl(): String { var url = App.INSTANCE.getPrefString(PreferKey.webDavUrl) @@ -88,8 +85,8 @@ object WebDavHelp { val file = WebDav(it + "legado/" + name) file.downloadTo(zipFilePath, true) @Suppress("BlockingMethodInNonBlockingContext") - ZipUtils.unzipFile(zipFilePath, unzipFilesPath) - Restore.restore(unzipFilesPath) + ZipUtils.unzipFile(zipFilePath, Backup.backupPath) + Restore.restore(Backup.backupPath) } }.onSuccess { success.invoke() diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 2b4cbce61..59b5c4d24 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -54,8 +54,13 @@ object FileUtils { return file } - fun getFile(file: File, fileName: String, vararg subDirs: String): File { - val filePath = getPath(file, fileName, *subDirs) + fun getFile(root: File, fileName: String, vararg subDirs: String): File { + val filePath = getPath(root, fileName, *subDirs) + return File(filePath) + } + + fun getDirFile(root: File, vararg subDirs: String): File { + val filePath = getPath(root, subDirs = *subDirs) return File(filePath) } diff --git a/app/src/main/java/io/legado/app/utils/LogUtils.kt b/app/src/main/java/io/legado/app/utils/LogUtils.kt index 8c5070d11..e70813dee 100644 --- a/app/src/main/java/io/legado/app/utils/LogUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LogUtils.kt @@ -2,7 +2,6 @@ package io.legado.app.utils import android.annotation.SuppressLint import io.legado.app.App -import java.io.File import java.text.SimpleDateFormat import java.util.* import java.util.logging.* @@ -24,18 +23,17 @@ object LogUtils { private val logger: Logger by lazy { Logger.getGlobal().apply { - addHandler(fileHandler) + fileHandler?.let { + addHandler(it) + } } } private val fileHandler by lazy { - val logFolder = FileUtils.getCachePath() + File.separator + "logs" - FileUtils.createFolderIfNotExist(logFolder) - FileHandler( - logFolder + File.separator + "app.log", - 10240, - 10 - ).apply { + val root = App.INSTANCE.externalCacheDir ?: return@lazy null + val logFolder = FileUtils.createFolderIfNotExist(root, "logs") + val logPath = FileUtils.getPath(logFolder, "appLog") + FileHandler(logPath, 10240, 10).apply { formatter = object : Formatter() { override fun format(record: LogRecord): String { // 设置文件输出格式 @@ -51,7 +49,7 @@ object LogUtils { } fun upLevel() { - fileHandler.level = if (App.INSTANCE.getPrefBoolean("recordLog")) { + fileHandler?.level = if (App.INSTANCE.getPrefBoolean("recordLog")) { Level.INFO } else { Level.OFF