feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 91a7a71636
commit be43148a2b
  1. 10
      app/src/main/java/io/legado/app/help/CrashHandler.kt
  2. 9
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt
  3. 9
      app/src/main/java/io/legado/app/utils/FileUtils.kt
  4. 18
      app/src/main/java/io/legado/app/utils/LogUtils.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())
}
}

@ -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()

@ -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)
}

@ -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

Loading…
Cancel
Save