pull/36/head
kunfei 5 years ago
parent b4894d43d3
commit 016e98f485
  1. 3
      app/build.gradle
  2. 10
      app/src/main/java/io/legado/app/App.kt
  3. 5
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt
  4. 5
      app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt
  5. 5
      app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt
  6. 55
      app/src/main/java/io/legado/app/utils/LogUtils.kt

@ -148,4 +148,7 @@ dependencies {
// //
implementation 'com.jaredrummler:colorpicker:1.1.0' implementation 'com.jaredrummler:colorpicker:1.1.0'
//
implementation 'com.apkfuns.logutils:library:1.7.5'
implementation 'com.apkfuns.log2file:log2file:1.3.1'
} }

@ -9,6 +9,8 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import com.apkfuns.log2file.LogFileEngineFactory
import com.apkfuns.logutils.LogUtils
import com.jeremyliao.liveeventbus.LiveEventBus import com.jeremyliao.liveeventbus.LiveEventBus
import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdDownload
import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdReadAloud
@ -16,12 +18,15 @@ import io.legado.app.constant.AppConst.channelIdWeb
import io.legado.app.data.AppDatabase import io.legado.app.data.AppDatabase
import io.legado.app.help.ActivityHelp import io.legado.app.help.ActivityHelp
import io.legado.app.help.CrashHandler import io.legado.app.help.CrashHandler
import io.legado.app.help.FileHelp
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.ThemeStore import io.legado.app.lib.theme.ThemeStore
import io.legado.app.ui.widget.page.ChapterProvider import io.legado.app.ui.widget.page.ChapterProvider
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefInt
import io.legado.app.utils.isNightTheme import io.legado.app.utils.isNightTheme
import java.io.File
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
class App : Application() { class App : Application() {
@ -43,6 +48,11 @@ class App : Application() {
super.onCreate() super.onCreate()
INSTANCE = this INSTANCE = this
CrashHandler().init(this) CrashHandler().init(this)
LogUtils.getLog2FileConfig()
.configLog2FileEnable(getPrefBoolean("recordLog"))
.configLog2FilePath(FileHelp.getCachePath() + File.separator + "logs" + File.separator)
.configLog2FileNameFormat("%d{yyyyMMdd}.log")
.configLogFileEngine(LogFileEngineFactory(this))
db = AppDatabase.createDatabase(INSTANCE) db = AppDatabase.createDatabase(INSTANCE)
packageManager.getPackageInfo(packageName, 0)?.let { packageManager.getPackageInfo(packageName, 0)?.let {
versionCode = it.versionCode versionCode = it.versionCode

@ -4,12 +4,11 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.view.KeyEvent import android.view.KeyEvent
import com.apkfuns.logutils.LogUtils
import io.legado.app.constant.Bus import io.legado.app.constant.Bus
import io.legado.app.help.ActivityHelp import io.legado.app.help.ActivityHelp
import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.utils.LogUtils.MyLogger
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import java.util.logging.Level
/** /**
@ -24,7 +23,7 @@ class MediaButtonReceiver : BroadcastReceiver() {
fun handleIntent(context: Context, intent: Intent): Boolean { fun handleIntent(context: Context, intent: Intent): Boolean {
val intentAction = intent.action val intentAction = intent.action
val keyEventAction = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)?.action val keyEventAction = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)?.action
MyLogger.log(Level.INFO, "耳机按键: $intentAction $keyEventAction") LogUtils.d("耳机按键", "$intentAction $keyEventAction")
if (Intent.ACTION_MEDIA_BUTTON == intentAction) { if (Intent.ACTION_MEDIA_BUTTON == intentAction) {
if (keyEventAction == KeyEvent.ACTION_DOWN) { if (keyEventAction == KeyEvent.ACTION_DOWN) {
readAloud(context) readAloud(context)

@ -6,11 +6,12 @@ import android.view.View
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.apkfuns.logutils.LogUtils
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.utils.LogUtils import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
@ -40,7 +41,7 @@ class ConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChange
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) { when (key) {
"downloadPath" -> BookHelp.upDownloadPath() "downloadPath" -> BookHelp.upDownloadPath()
"recordLog" -> LogUtils.upLevel() "recordLog" -> LogUtils.getLog2FileConfig().configLog2FileEnable(getPrefBoolean("recordLog"))
} }
} }

@ -7,6 +7,7 @@ import android.view.MenuItem
import android.view.View import android.view.View
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import com.apkfuns.logutils.LogUtils
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseFragment import io.legado.app.base.BaseFragment
@ -22,7 +23,7 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity
import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigActivity
import io.legado.app.ui.config.ConfigViewModel import io.legado.app.ui.config.ConfigViewModel
import io.legado.app.ui.replacerule.ReplaceRuleActivity import io.legado.app.ui.replacerule.ReplaceRuleActivity
import io.legado.app.utils.LogUtils import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.startActivity import io.legado.app.utils.startActivity
import kotlinx.android.synthetic.main.view_title_bar.* import kotlinx.android.synthetic.main.view_title_bar.*
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
@ -85,7 +86,7 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) {
) { ) {
when (key) { when (key) {
"isNightTheme" -> App.INSTANCE.applyDayNight() "isNightTheme" -> App.INSTANCE.applyDayNight()
"recordLog" -> LogUtils.upLevel() "recordLog" -> LogUtils.getLog2FileConfig().configLog2FileEnable(getPrefBoolean("recordLog"))
"downloadPath" -> BookHelp.upDownloadPath() "downloadPath" -> BookHelp.upDownloadPath()
} }
} }

@ -1,55 +0,0 @@
package io.legado.app.utils
import android.annotation.SuppressLint
import io.legado.app.App
import io.legado.app.help.FileHelp
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
import java.util.logging.*
import java.util.logging.Formatter
object LogUtils {
const val TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"
val MyLogger: Logger by lazy {
Logger.getGlobal().apply {
addHandler(fileHandler)
}
}
private val fileHandler by lazy {
FileHandler(FileHelp.getCachePath() + File.separator + "app.log", 1024, 2).apply {
formatter = object : Formatter() {
override fun format(record: LogRecord): String {
// 设置文件输出格式
return (getCurrentDateStr(TIME_PATTERN) + ": " + record.message + "\n")
}
}
level = if (App.INSTANCE.getPrefBoolean("recordLog")) {
Level.INFO
} else {
Level.OFF
}
}
}
fun upLevel() {
fileHandler.level = if (App.INSTANCE.getPrefBoolean("recordLog")) {
Level.INFO
} else {
Level.OFF
}
}
/**
* 获取当前时间
*/
@SuppressLint("SimpleDateFormat")
fun getCurrentDateStr(pattern: String): String {
val date = Date()
val sdf = SimpleDateFormat(pattern)
return sdf.format(date)
}
}
Loading…
Cancel
Save