diff --git a/app/src/main/java/io/legado/app/constant/AppLog.kt b/app/src/main/java/io/legado/app/constant/AppLog.kt new file mode 100644 index 000000000..038027342 --- /dev/null +++ b/app/src/main/java/io/legado/app/constant/AppLog.kt @@ -0,0 +1,24 @@ +package io.legado.app.constant + +import android.annotation.SuppressLint +import java.text.SimpleDateFormat +import java.util.* + +object AppLog { + + @SuppressLint("ConstantLocale") + private val logTimeFormat = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) + + val logs = arrayListOf() + + fun addLog(log: String?) { + log ?: return + synchronized(logs) { + if (logs.size > 1000) { + logs.removeLastOrNull() + } + logs.add(0, logTimeFormat.format(Date()) + " " + log) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/CacheBook.kt b/app/src/main/java/io/legado/app/model/CacheBook.kt index ab88bbfab..8515e6541 100644 --- a/app/src/main/java/io/legado/app/model/CacheBook.kt +++ b/app/src/main/java/io/legado/app/model/CacheBook.kt @@ -1,7 +1,7 @@ package io.legado.app.model -import android.annotation.SuppressLint import android.content.Context +import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus import io.legado.app.constant.IntentAction import io.legado.app.data.appDb @@ -15,8 +15,6 @@ import io.legado.app.utils.postEvent import io.legado.app.utils.startService import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay -import java.text.SimpleDateFormat -import java.util.* import java.util.concurrent.ConcurrentHashMap import kotlin.coroutines.CoroutineContext @@ -24,12 +22,8 @@ class CacheBook(var bookSource: BookSource, var book: Book) { companion object { - val logs = arrayListOf() val cacheBookMap = ConcurrentHashMap() - @SuppressLint("ConstantLocale") - private val logTimeFormat = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) - @Synchronized fun getOrCreate(bookUrl: String): CacheBook? { val book = appDb.bookDao.getBook(bookUrl) ?: return null @@ -60,16 +54,6 @@ class CacheBook(var bookSource: BookSource, var book: Book) { return cacheBook } - fun addLog(log: String?) { - log ?: return - synchronized(logs) { - if (logs.size > 1000) { - logs.removeLastOrNull() - } - logs.add(0, logTimeFormat.format(Date()) + " " + log) - } - } - fun start(context: Context, bookUrl: String, start: Int, end: Int) { context.startService { action = IntentAction.start @@ -185,7 +169,7 @@ class CacheBook(var bookSource: BookSource, var book: Book) { if (errorDownloadMap[index] ?: 0 < 3) { waitDownloadSet.add(index) } else { - addLog("${book.name}-${chapterTitle} ${error.localizedMessage}") + AppLog.addLog("${book.name}-${chapterTitle} ${error.localizedMessage}") } } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index b23f4e0b5..d90147355 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -11,6 +11,7 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.charsets +import io.legado.app.constant.AppLog import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.data.appDb @@ -136,7 +137,7 @@ class CacheActivity : VMBaseActivity() R.id.menu_export_type -> showExportTypeConfig() R.id.menu_export_charset -> showCharsetConfig() R.id.menu_log -> - TextListDialog.show(supportFragmentManager, getString(R.string.log), CacheBook.logs) + TextListDialog.show(supportFragmentManager, getString(R.string.log), AppLog.logs) else -> if (item.groupId == R.id.menu_group) { binding.titleBar.subtitle = item.title groupId = appDb.bookGroupDao.getByName(item.title.toString())?.groupId ?: 0