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.io.StringWriter
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit
/** /**
* 异常管理类 * 异常管理类
@ -140,8 +141,13 @@ class CrashHandler : 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"
mContext?.externalCacheDir?.let { mContext?.externalCacheDir?.let { rootFile ->
FileUtils.createFileIfNotExist(it, fileName, "crash") 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()) .writeText(sb.toString())
} }
} }

@ -23,10 +23,7 @@ import kotlin.math.min
object WebDavHelp { object WebDavHelp {
private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/"
private val zipFilePath = FileUtils.getCachePath() + "/backup" + ".zip" private val zipFilePath = "${FileUtils.getCachePath()}${File.separator}backup.zip"
private val unzipFilesPath by lazy {
FileUtils.getCachePath()
}
private fun getWebDavUrl(): String { private fun getWebDavUrl(): String {
var url = App.INSTANCE.getPrefString(PreferKey.webDavUrl) var url = App.INSTANCE.getPrefString(PreferKey.webDavUrl)
@ -88,8 +85,8 @@ object WebDavHelp {
val file = WebDav(it + "legado/" + name) val file = WebDav(it + "legado/" + name)
file.downloadTo(zipFilePath, true) file.downloadTo(zipFilePath, true)
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
ZipUtils.unzipFile(zipFilePath, unzipFilesPath) ZipUtils.unzipFile(zipFilePath, Backup.backupPath)
Restore.restore(unzipFilesPath) Restore.restore(Backup.backupPath)
} }
}.onSuccess { }.onSuccess {
success.invoke() success.invoke()

@ -54,8 +54,13 @@ object FileUtils {
return file return file
} }
fun getFile(file: File, fileName: String, vararg subDirs: String): File { fun getFile(root: File, fileName: String, vararg subDirs: String): File {
val filePath = getPath(file, fileName, *subDirs) 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) return File(filePath)
} }

@ -2,7 +2,6 @@ package io.legado.app.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
import io.legado.app.App import io.legado.app.App
import java.io.File
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import java.util.logging.* import java.util.logging.*
@ -24,18 +23,17 @@ object LogUtils {
private val logger: Logger by lazy { private val logger: Logger by lazy {
Logger.getGlobal().apply { Logger.getGlobal().apply {
addHandler(fileHandler) fileHandler?.let {
addHandler(it)
}
} }
} }
private val fileHandler by lazy { private val fileHandler by lazy {
val logFolder = FileUtils.getCachePath() + File.separator + "logs" val root = App.INSTANCE.externalCacheDir ?: return@lazy null
FileUtils.createFolderIfNotExist(logFolder) val logFolder = FileUtils.createFolderIfNotExist(root, "logs")
FileHandler( val logPath = FileUtils.getPath(logFolder, "appLog")
logFolder + File.separator + "app.log", FileHandler(logPath, 10240, 10).apply {
10240,
10
).apply {
formatter = object : Formatter() { formatter = object : Formatter() {
override fun format(record: LogRecord): String { override fun format(record: LogRecord): String {
// 设置文件输出格式 // 设置文件输出格式
@ -51,7 +49,7 @@ object LogUtils {
} }
fun upLevel() { fun upLevel() {
fileHandler.level = if (App.INSTANCE.getPrefBoolean("recordLog")) { fileHandler?.level = if (App.INSTANCE.getPrefBoolean("recordLog")) {
Level.INFO Level.INFO
} else { } else {
Level.OFF Level.OFF

Loading…
Cancel
Save