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. 35
      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
class TimeElectricityReceiver : BroadcastReceiver() {
class TimeBatteryReceiver : BroadcastReceiver() {
companion object {
fun register(context: Context): TimeElectricityReceiver {
val receiver = TimeElectricityReceiver()
fun register(context: Context): TimeBatteryReceiver {
val receiver = TimeBatteryReceiver()
val filter = IntentFilter()
filter.addAction(Intent.ACTION_TIME_TICK)
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.okButton
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.help.ReadAloud
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 var screenTimeOut: Long = 0
private var timeElectricityReceiver: TimeElectricityReceiver? = null
private var timeBatteryReceiver: TimeBatteryReceiver? = null
override val pageFactory: TextPageFactory get() = page_view.pageFactory
override val headerHeight: Int get() = page_view.curPage.headerHeight
@ -106,15 +106,15 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override fun onResume() {
super.onResume()
upSystemUiVisibility()
timeElectricityReceiver = TimeElectricityReceiver.register(this)
timeBatteryReceiver = TimeBatteryReceiver.register(this)
page_view.upTime()
}
override fun onPause() {
super.onPause()
timeElectricityReceiver?.let {
timeBatteryReceiver?.let {
unregisterReceiver(it)
timeElectricityReceiver = null
timeBatteryReceiver = null
}
upSystemUiVisibility()
}
@ -719,7 +719,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
Help.keepScreenOn(window, true)
return
}
val t = screenTimeOut - getScreenOffTime()
val t = screenTimeOut - sysScreenOffTime
if (t > 0) {
mHandler.removeCallbacks(keepScreenRunnable)
Help.keepScreenOn(window, true)

@ -1,3 +1,4 @@
@file:Suppress("unused")
package io.legado.app.utils
import android.annotation.SuppressLint
@ -68,26 +69,27 @@ fun Context.getCompatColorStateList(@ColorRes id: Int): ColorStateList? =
/**
* 系统息屏时间
*/
fun Context.getScreenOffTime(): Int {
var screenOffTime = 0
try {
screenOffTime = Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
} catch (e: Exception) {
e.printStackTrace()
}
return screenOffTime
val Context.sysScreenOffTime: Int
get() {
var screenOffTime = 0
try {
screenOffTime = Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
} catch (e: Exception) {
e.printStackTrace()
}
return screenOffTime
}
val Context.statusBarHeight: Int
get() {
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return resources.getDimensionPixelSize(resourceId)
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
return resources.getDimensionPixelSize(resourceId)
}
val Context.navigationBarHeight: Int
get() {
val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
return resources.getDimensionPixelSize(resourceId)
val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
return resources.getDimensionPixelSize(resourceId)
}
fun Context.shareText(title: String, text: String) {
@ -150,10 +152,11 @@ fun Context.sysIsDarkMode(): Boolean {
/**
* 获取电量
*/
fun Context.getBettery(): Int {
val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val batteryStatus = registerReceiver(null, iFilter)
return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
val Context.sysBattery: Int
get() {
val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val batteryStatus = registerReceiver(null, iFilter)
return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
}
fun Context.openUrl(url: String) {

Loading…
Cancel
Save