From 8b4829996af2c125d5ba24cad6bf6634ab554d8c Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 9 Mar 2021 15:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/AppConfig.kt | 2 ++ .../app/receiver/MediaButtonReceiver.kt | 33 +++++++------------ .../app/receiver/SharedReceiverActivity.kt | 4 +-- app/src/main/res/xml/shortcuts.xml | 2 +- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/AppConfig.kt b/app/src/main/java/io/legado/app/help/AppConfig.kt index 3b110e589..a0d2dff56 100644 --- a/app/src/main/java/io/legado/app/help/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/AppConfig.kt @@ -175,6 +175,8 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { val preDownload get() = appCtx.getPrefBoolean(PreferKey.preDownload, true) + val mediaButtonOnExit get() = appCtx.getPrefBoolean("mediaButtonOnExit", true) + private fun getPrefUserAgent(): String { val ua = appCtx.getPrefString(PreferKey.userAgent) if (ua.isNullOrBlank()) { diff --git a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt index 8dbb836a9..beded7255 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -6,8 +6,8 @@ import android.content.Intent import android.view.KeyEvent import io.legado.app.constant.EventBus import io.legado.app.data.appDb -import io.legado.app.data.entities.Book import io.legado.app.help.ActivityHelp +import io.legado.app.help.AppConfig import io.legado.app.service.AudioPlayService import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.AudioPlay @@ -15,13 +15,7 @@ import io.legado.app.service.help.ReadAloud import io.legado.app.ui.audio.AudioPlayActivity import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.main.MainActivity -import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.postEvent -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext /** @@ -61,7 +55,7 @@ class MediaButtonReceiver : BroadcastReceiver() { return true } - fun readAloud(context: Context) { + fun readAloud(context: Context, isMediaKey: Boolean = true) { when { BaseReadAloudService.isRun -> if (BaseReadAloudService.isPlay()) { ReadAloud.pause(context) @@ -79,24 +73,19 @@ class MediaButtonReceiver : BroadcastReceiver() { postEvent(EventBus.MEDIA_BUTTON, true) ActivityHelp.isExist(AudioPlayActivity::class.java) -> postEvent(EventBus.MEDIA_BUTTON, true) - else -> if (context.getPrefBoolean("mediaButtonOnExit", true)) { - GlobalScope.launch(Main) { - val lastBook: Book? = withContext(IO) { - appDb.bookDao.lastReadBook - } - lastBook?.let { - if (!ActivityHelp.isExist(MainActivity::class.java)) { - Intent(context, MainActivity::class.java).let { - it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - context.startActivity(it) - } - } - Intent(context, ReadBookActivity::class.java).let { + else -> if (AppConfig.mediaButtonOnExit || !isMediaKey) { + appDb.bookDao.lastReadBook?.let { + if (!ActivityHelp.isExist(MainActivity::class.java)) { + Intent(context, MainActivity::class.java).let { it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - it.putExtra("readAloud", true) context.startActivity(it) } } + Intent(context, ReadBookActivity::class.java).let { + it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + it.putExtra("readAloud", true) + context.startActivity(it) + } } } } diff --git a/app/src/main/java/io/legado/app/receiver/SharedReceiverActivity.kt b/app/src/main/java/io/legado/app/receiver/SharedReceiverActivity.kt index 1e08c3c6f..a6bb0af91 100644 --- a/app/src/main/java/io/legado/app/receiver/SharedReceiverActivity.kt +++ b/app/src/main/java/io/legado/app/receiver/SharedReceiverActivity.kt @@ -41,8 +41,8 @@ class SharedReceiverActivity : AppCompatActivity() { } } } - intent.getStringExtra("action") == "mediaButton" -> { - MediaButtonReceiver.readAloud(appCtx) + intent.getStringExtra("action") == "readAloud" -> { + MediaButtonReceiver.readAloud(appCtx, false) } } } diff --git a/app/src/main/res/xml/shortcuts.xml b/app/src/main/res/xml/shortcuts.xml index 6e16843c7..2e6e4f6cb 100644 --- a/app/src/main/res/xml/shortcuts.xml +++ b/app/src/main/res/xml/shortcuts.xml @@ -36,7 +36,7 @@ android:targetClass="io.legado.app.receiver.SharedReceiverActivity"> + android:value="readAloud" />