From 4cdeb061cadee1e1e72fbe9c7f53dbc8cf39c4cd Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 16 Aug 2019 11:20:46 +0800 Subject: [PATCH] up --- .../java/io/legado/app/constant/Status.kt | 2 +- .../app/ui/readbook/ReadBookActivity.kt | 26 ++++++++++- .../legado/app/ui/readbook/ReadBottomMenu.kt | 4 +- .../java/io/legado/app/utils/SystemUtils.kt | 45 +++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/SystemUtils.kt diff --git a/app/src/main/java/io/legado/app/constant/Status.kt b/app/src/main/java/io/legado/app/constant/Status.kt index 0f1bff978..a20ee0e34 100644 --- a/app/src/main/java/io/legado/app/constant/Status.kt +++ b/app/src/main/java/io/legado/app/constant/Status.kt @@ -4,5 +4,5 @@ object Status { const val STOP = 0 const val PLAY = 1 const val NEXT = 2 - + const val PAUSE = 3 } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt index 55266c02e..26dd7ed36 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt @@ -10,7 +10,10 @@ import androidx.core.view.isVisible import io.legado.app.App import io.legado.app.R 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.service.ReadAloudService import io.legado.app.ui.changesource.ChangeSourceDialog import io.legado.app.ui.chapterlist.ChapterListActivity import io.legado.app.ui.replacerule.ReplaceRuleActivity @@ -30,6 +33,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea private lateinit var menuTopOut: Animation private lateinit var menuBottomIn: Animation private lateinit var menuBottomOut: Animation + private var readAloudStatus = Status.STOP override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) @@ -92,8 +96,8 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } - override fun onMediaButton() { - + override fun clickReadAloud() { + onClickReadAloud() } override fun autoPage() { @@ -204,4 +208,22 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea 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(Bus.ALOUD_STATE) { readAloudStatus = it } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBottomMenu.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBottomMenu.kt index 461396919..82461268c 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBottomMenu.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBottomMenu.kt @@ -69,7 +69,7 @@ class ReadBottomMenu : FrameLayout { fab_read_aloud_timer.onClick { } //朗读 - fab_read_aloud.onClick { callback?.onMediaButton() } + fab_read_aloud.onClick { callback?.clickReadAloud() } //长按停止朗读 fab_read_aloud.onLongClick { ReadAloudService.stop(context) @@ -155,7 +155,7 @@ class ReadBottomMenu : FrameLayout { interface Callback { fun skipToPage(page: Int) - fun onMediaButton() + fun clickReadAloud() fun autoPage() diff --git a/app/src/main/java/io/legado/app/utils/SystemUtils.kt b/app/src/main/java/io/legado/app/utils/SystemUtils.kt new file mode 100644 index 000000000..91121c000 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/SystemUtils.kt @@ -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) { + } + + } + } +}