From f6f472ed4854fb653a7ef03d31bfb9061b1d13f7 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 5 Nov 2019 11:10:54 +0800 Subject: [PATCH] up --- .../app/receiver/MediaButtonReceiver.kt | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) 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 1df892cda..330956351 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -4,11 +4,18 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.view.KeyEvent +import io.legado.app.App import io.legado.app.constant.Bus +import io.legado.app.data.entities.Book import io.legado.app.help.ActivityHelp import io.legado.app.ui.audio.AudioPlayActivity import io.legado.app.ui.book.read.ReadBookActivity 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 /** @@ -33,15 +40,24 @@ class MediaButtonReceiver : BroadcastReceiver() { } private fun readAloud(context: Context) { - if (ActivityHelp.isExist(AudioPlayActivity::class.java)) { - postEvent(Bus.AUDIO_PLAY_BUTTON, true) - } else if (!ActivityHelp.isExist(ReadBookActivity::class.java)) { - val intent = Intent(context, ReadBookActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - intent.putExtra("readAloud", true) - context.startActivity(intent) - } else { - postEvent(Bus.READ_ALOUD_BUTTON, true) + when { + ActivityHelp.isExist(AudioPlayActivity::class.java) -> + postEvent(Bus.AUDIO_PLAY_BUTTON, true) + ActivityHelp.isExist(ReadBookActivity::class.java) -> + postEvent(Bus.READ_ALOUD_BUTTON, true) + else -> { + GlobalScope.launch(Main) { + val lastBook: Book? = withContext(IO) { + App.db.bookDao().lastReadBook + } + lastBook?.let { + val intent = Intent(context, ReadBookActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.putExtra("readAloud", true) + context.startActivity(intent) + } + } + } } } }