From 8f7d7937fae6ea39322c479211b3953e015c9e3a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 11 Sep 2021 13:12:57 +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 --- .../java/io/legado/app/model/ReadAloud.kt | 8 --- .../main/java/io/legado/app/model/ReadBook.kt | 15 +++--- .../app/receiver/MediaButtonReceiver.kt | 20 +++---- .../app/service/BaseReadAloudService.kt | 52 ++++++++----------- .../app/service/HttpReadAloudService.kt | 5 +- .../app/ui/book/read/ReadBookViewModel.kt | 9 +--- 6 files changed, 42 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/ReadAloud.kt b/app/src/main/java/io/legado/app/model/ReadAloud.kt index 85ef28a20..a4dafbe4f 100644 --- a/app/src/main/java/io/legado/app/model/ReadAloud.kt +++ b/app/src/main/java/io/legado/app/model/ReadAloud.kt @@ -33,18 +33,10 @@ object ReadAloud { fun play( context: Context, - title: String, - subtitle: String, - pageIndex: Int, - dataKey: String, play: Boolean = true ) { val intent = Intent(context, aloudClass) intent.action = IntentAction.play - intent.putExtra("title", title) - intent.putExtra("subtitle", subtitle) - intent.putExtra("pageIndex", pageIndex) - intent.putExtra("dataKey", dataKey) intent.putExtra("play", play) context.startService(intent) } diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index 0c90eb1bb..942695e90 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -5,7 +5,10 @@ import com.github.liuyueyi.quick.transfer.ChineseUtils import io.legado.app.constant.BookType import io.legado.app.data.appDb import io.legado.app.data.entities.* -import io.legado.app.help.* +import io.legado.app.help.AppConfig +import io.legado.app.help.BookHelp +import io.legado.app.help.ContentProcessor +import io.legado.app.help.ReadBookConfig import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.storage.AppWebDav import io.legado.app.model.webBook.WebBook @@ -46,6 +49,7 @@ object ReadBook : CoroutineScope by MainScope() { ReadBook.book = book readRecord.bookName = book.name readRecord.readTime = appDb.readRecordDao.getReadTime(book.name) ?: 0 + chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl) durChapterIndex = book.durChapterIndex durChapterPos = book.durChapterPos isLocalBook = book.origin == BookType.local @@ -200,13 +204,8 @@ object ReadBook : CoroutineScope by MainScope() { * 朗读 */ fun readAloud(play: Boolean = true) { - val book = book - val textChapter = curTextChapter - if (book != null && textChapter != null) { - val key = IntentDataHelp.putData(textChapter) - ReadAloud.play( - appCtx, book.name, textChapter.title, durPageIndex(), key, play - ) + book?.let { + ReadAloud.play(appCtx, play) } } 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 78309fbab..0c66e9181 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -15,10 +15,8 @@ import io.legado.app.service.AudioPlayService import io.legado.app.service.BaseReadAloudService import io.legado.app.ui.book.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 io.legado.app.utils.startActivity /** @@ -87,16 +85,14 @@ class MediaButtonReceiver : BroadcastReceiver() { postEvent(EventBus.MEDIA_BUTTON, true) LifecycleHelp.isExistActivity(AudioPlayActivity::class.java) -> postEvent(EventBus.MEDIA_BUTTON, true) - else -> if (AppConfig.mediaButtonOnExit || !isMediaKey || LifecycleHelp.activitySize() > 0) { - appDb.bookDao.lastReadBook?.let { - if (!LifecycleHelp.isExistActivity(MainActivity::class.java)) { - context.startActivity { - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - } - } - context.startActivity { - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - putExtra("readAloud", true) + else -> if (AppConfig.mediaButtonOnExit || LifecycleHelp.activitySize() > 0 || !isMediaKey) { + if (ReadBook.book != null) { + ReadBook.readAloud() + } else { + appDb.bookDao.lastReadBook?.let { + ReadBook.resetData(it) + ReadBook.curTextChapter ?: ReadBook.loadContent(false) + ReadBook.readAloud() } } } diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index b20ab99fe..cc80c239c 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -16,7 +16,6 @@ import androidx.media.AudioFocusRequestCompat import io.legado.app.R import io.legado.app.base.BaseService import io.legado.app.constant.* -import io.legado.app.help.IntentDataHelp import io.legado.app.help.MediaHelp import io.legado.app.model.ReadAloud import io.legado.app.model.ReadBook @@ -46,8 +45,6 @@ abstract class BaseReadAloudService : BaseService(), private var mFocusRequest: AudioFocusRequestCompat? = null private var broadcastReceiver: BroadcastReceiver? = null private lateinit var mediaSessionCompat: MediaSessionCompat - private var title: String = "" - private var subtitle: String = "" internal val contentList = arrayListOf() internal var nowSpeak: Int = 0 internal var readAloudNumber: Int = 0 @@ -84,11 +81,9 @@ abstract class BaseReadAloudService : BaseService(), intent?.action?.let { action -> when (action) { IntentAction.play -> { - title = intent.getStringExtra("title") ?: "" - subtitle = intent.getStringExtra("subtitle") ?: "" - pageIndex = intent.getIntExtra("pageIndex", 0) + pageIndex = ReadBook.durPageIndex() newReadAloud( - intent.getStringExtra("dataKey"), + ReadBook.curTextChapter, intent.getBooleanExtra("play", true) ) } @@ -106,29 +101,26 @@ abstract class BaseReadAloudService : BaseService(), } @CallSuper - open fun newReadAloud(dataKey: String?, play: Boolean) { - dataKey?.let { - textChapter = IntentDataHelp.getData(dataKey) - textChapter?.let { textChapter -> - nowSpeak = 0 - readAloudNumber = textChapter.getReadLength(pageIndex) - contentList.clear() - if (getPrefBoolean(PreferKey.readAloudByPage)) { - for (index in pageIndex..textChapter.lastIndex) { - textChapter.page(index)?.text?.split("\n")?.let { - contentList.addAll(it) - } + open fun newReadAloud(textChapter: TextChapter?, play: Boolean) { + textChapter?.let { + nowSpeak = 0 + readAloudNumber = textChapter.getReadLength(pageIndex) + contentList.clear() + if (getPrefBoolean(PreferKey.readAloudByPage)) { + for (index in pageIndex..textChapter.lastIndex) { + textChapter.page(index)?.text?.split("\n")?.let { + contentList.addAll(it) } - } else { - textChapter.getUnRead(pageIndex).split("\n").forEach { - if (it.isNotEmpty()) { - contentList.add(it) - } + } + } else { + textChapter.getUnRead(pageIndex).split("\n").forEach { + if (it.isNotEmpty()) { + contentList.add(it) } } - if (play) play() - } ?: stopSelf() - } ?: stopSelf() + } + if (play) play() + } } open fun play() { @@ -307,9 +299,9 @@ abstract class BaseReadAloudService : BaseService(), ) else -> getString(R.string.read_aloud_t) } - nTitle += ": $title" - var nSubtitle = subtitle - if (subtitle.isEmpty()) + nTitle += ": ${ReadBook.book?.name}" + var nSubtitle = ReadBook.curTextChapter?.title + if (nSubtitle.isNullOrBlank()) nSubtitle = getString(R.string.read_aloud_s) val builder = NotificationCompat.Builder(this, AppConst.channelIdReadAloud) .setSmallIcon(R.drawable.ic_volume_up) diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index ce24cf76f..f8672b30c 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -8,6 +8,7 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.ReadAloud import io.legado.app.model.ReadBook import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.utils.* import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive @@ -45,10 +46,10 @@ class HttpReadAloudService : BaseReadAloudService(), player.release() } - override fun newReadAloud(dataKey: String?, play: Boolean) { + override fun newReadAloud(textChapter: TextChapter?, play: Boolean) { player.reset() playingIndex = -1 - super.newReadAloud(dataKey, play) + super.newReadAloud(textChapter, play) } override fun play() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index a8fdfdf74..8e3707092 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -51,12 +51,11 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { private fun initBook(book: Book) { if (ReadBook.book?.bookUrl != book.bookUrl) { ReadBook.resetData(book) - isInitFinish = true if (!book.isLocalBook() && ReadBook.bookSource == null) { autoChangeSource(book.name, book.author) return } - ReadBook.chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl) + isInitFinish = true if (ReadBook.chapterSize == 0) { if (book.tocUrl.isEmpty()) { loadBookInfo(book) @@ -194,11 +193,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { oldTocSize = it.totalChapterNum it.changeTo(newBook) } - ReadBook.book = newBook - ReadBook.bookSource = appDb.bookSourceDao.getBookSource(newBook.origin) - ReadBook.prevTextChapter = null - ReadBook.curTextChapter = null - ReadBook.nextTextChapter = null + ReadBook.resetData(newBook) withContext(Main) { ReadBook.callBack?.upContent() }