pull/32/head
kunfei 5 years ago
parent 1dfda8ffde
commit 4cdeb061ca
  1. 2
      app/src/main/java/io/legado/app/constant/Status.kt
  2. 26
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  3. 4
      app/src/main/java/io/legado/app/ui/readbook/ReadBottomMenu.kt
  4. 45
      app/src/main/java/io/legado/app/utils/SystemUtils.kt

@ -4,5 +4,5 @@ object Status {
const val STOP = 0 const val STOP = 0
const val PLAY = 1 const val PLAY = 1
const val NEXT = 2 const val NEXT = 2
const val PAUSE = 3
} }

@ -10,7 +10,10 @@ import androidx.core.view.isVisible
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.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.Bus
import io.legado.app.constant.Status
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.service.ReadAloudService
import io.legado.app.ui.changesource.ChangeSourceDialog import io.legado.app.ui.changesource.ChangeSourceDialog
import io.legado.app.ui.chapterlist.ChapterListActivity import io.legado.app.ui.chapterlist.ChapterListActivity
import io.legado.app.ui.replacerule.ReplaceRuleActivity import io.legado.app.ui.replacerule.ReplaceRuleActivity
@ -30,6 +33,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
private lateinit var menuTopOut: Animation private lateinit var menuTopOut: Animation
private lateinit var menuBottomIn: Animation private lateinit var menuBottomIn: Animation
private lateinit var menuBottomOut: Animation private lateinit var menuBottomOut: Animation
private var readAloudStatus = Status.STOP
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
@ -92,8 +96,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
override fun onMediaButton() { override fun clickReadAloud() {
onClickReadAloud()
} }
override fun autoPage() { override fun autoPage() {
@ -204,4 +208,22 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
override fun changeTo(book: Book) { override fun changeTo(book: Book) {
} }
private fun onClickReadAloud() {
if (!ReadAloudService.isRun) {
readAloudStatus = Status.STOP
SystemUtils.ignoreBatteryOptimization(this)
}
when (readAloudStatus) {
Status.STOP -> {
}
Status.PLAY -> ReadAloudService.pause(this)
Status.PAUSE -> ReadAloudService.resume(this)
}
}
override fun observeLiveBus() {
super.observeLiveBus()
observeEvent<Int>(Bus.ALOUD_STATE) { readAloudStatus = it }
}
} }

@ -69,7 +69,7 @@ class ReadBottomMenu : FrameLayout {
fab_read_aloud_timer.onClick { } fab_read_aloud_timer.onClick { }
//朗读 //朗读
fab_read_aloud.onClick { callback?.onMediaButton() } fab_read_aloud.onClick { callback?.clickReadAloud() }
//长按停止朗读 //长按停止朗读
fab_read_aloud.onLongClick { fab_read_aloud.onLongClick {
ReadAloudService.stop(context) ReadAloudService.stop(context)
@ -155,7 +155,7 @@ class ReadBottomMenu : FrameLayout {
interface Callback { interface Callback {
fun skipToPage(page: Int) fun skipToPage(page: Int)
fun onMediaButton() fun clickReadAloud()
fun autoPage() fun autoPage()

@ -0,0 +1,45 @@
package io.legado.app.utils
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Context.POWER_SERVICE
import android.content.Intent
import android.net.Uri
import android.os.PowerManager
import android.provider.Settings
object SystemUtils {
fun getScreenOffTime(context: Context): Int {
var screenOffTime = 0
try {
screenOffTime = Settings.System.getInt(
context.contentResolver,
Settings.System.SCREEN_OFF_TIMEOUT
)
} catch (e: Exception) {
e.printStackTrace()
}
return screenOffTime
}
fun ignoreBatteryOptimization(activity: Activity) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) return
val powerManager = activity.getSystemService(POWER_SERVICE) as PowerManager
val hasIgnored = powerManager.isIgnoringBatteryOptimizations(activity.packageName)
// 判断当前APP是否有加入电池优化的白名单,如果没有,弹出加入电池优化的白名单的设置对话框。
if (!hasIgnored) {
try {
@SuppressLint("BatteryLife")
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
intent.data = Uri.parse("package:" + activity.packageName)
activity.startActivity(intent)
} catch (ignored: Throwable) {
}
}
}
}
Loading…
Cancel
Save