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" />