feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 8af0e0e311
commit 6b49b0c6af
  1. 6
      app/src/main/java/io/legado/app/receiver/TimeBatteryReceiver.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 7
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -9,12 +9,12 @@ import io.legado.app.constant.EventBus
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
class TimeElectricityReceiver : BroadcastReceiver() { class TimeBatteryReceiver : BroadcastReceiver() {
companion object { companion object {
fun register(context: Context): TimeElectricityReceiver { fun register(context: Context): TimeBatteryReceiver {
val receiver = TimeElectricityReceiver() val receiver = TimeBatteryReceiver()
val filter = IntentFilter() val filter = IntentFilter()
filter.addAction(Intent.ACTION_TIME_TICK) filter.addAction(Intent.ACTION_TIME_TICK)
filter.addAction(Intent.ACTION_BATTERY_CHANGED) filter.addAction(Intent.ACTION_BATTERY_CHANGED)

@ -26,7 +26,7 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.okButton import io.legado.app.lib.dialogs.okButton
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.receiver.TimeElectricityReceiver import io.legado.app.receiver.TimeBatteryReceiver
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook import io.legado.app.service.help.ReadBook
@ -81,7 +81,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
private val keepScreenRunnable: Runnable = Runnable { Help.keepScreenOn(window, false) } private val keepScreenRunnable: Runnable = Runnable { Help.keepScreenOn(window, false) }
private var screenTimeOut: Long = 0 private var screenTimeOut: Long = 0
private var timeElectricityReceiver: TimeElectricityReceiver? = null private var timeBatteryReceiver: TimeBatteryReceiver? = null
override val pageFactory: TextPageFactory get() = page_view.pageFactory override val pageFactory: TextPageFactory get() = page_view.pageFactory
override val headerHeight: Int get() = page_view.curPage.headerHeight override val headerHeight: Int get() = page_view.curPage.headerHeight
@ -106,15 +106,15 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
upSystemUiVisibility() upSystemUiVisibility()
timeElectricityReceiver = TimeElectricityReceiver.register(this) timeBatteryReceiver = TimeBatteryReceiver.register(this)
page_view.upTime() page_view.upTime()
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
timeElectricityReceiver?.let { timeBatteryReceiver?.let {
unregisterReceiver(it) unregisterReceiver(it)
timeElectricityReceiver = null timeBatteryReceiver = null
} }
upSystemUiVisibility() upSystemUiVisibility()
} }
@ -719,7 +719,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
Help.keepScreenOn(window, true) Help.keepScreenOn(window, true)
return return
} }
val t = screenTimeOut - getScreenOffTime() val t = screenTimeOut - sysScreenOffTime
if (t > 0) { if (t > 0) {
mHandler.removeCallbacks(keepScreenRunnable) mHandler.removeCallbacks(keepScreenRunnable)
Help.keepScreenOn(window, true) Help.keepScreenOn(window, true)

@ -1,3 +1,4 @@
@file:Suppress("unused")
package io.legado.app.utils package io.legado.app.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
@ -68,7 +69,8 @@ fun Context.getCompatColorStateList(@ColorRes id: Int): ColorStateList? =
/** /**
* 系统息屏时间 * 系统息屏时间
*/ */
fun Context.getScreenOffTime(): Int { val Context.sysScreenOffTime: Int
get() {
var screenOffTime = 0 var screenOffTime = 0
try { try {
screenOffTime = Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT) screenOffTime = Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
@ -150,7 +152,8 @@ fun Context.sysIsDarkMode(): Boolean {
/** /**
* 获取电量 * 获取电量
*/ */
fun Context.getBettery(): Int { val Context.sysBattery: Int
get() {
val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val batteryStatus = registerReceiver(null, iFilter) val batteryStatus = registerReceiver(null, iFilter)
return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1

Loading…
Cancel
Save