From f06821ccb32d99bf715746647c19c1a222f9d987 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sun, 10 Mar 2024 22:04:53 +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 | 25 +++++++------ .../app/service/BaseReadAloudService.kt | 28 +++++++-------- .../app/service/HttpReadAloudService.kt | 1 + .../app/ui/book/read/ReadBookActivity.kt | 6 ++-- .../ui/book/read/page/entities/TextChapter.kt | 35 ++++++++++++------- .../ui/book/read/page/entities/TextLine.kt | 2 +- .../ui/book/read/page/entities/TextPage.kt | 9 ++++- 8 files changed, 58 insertions(+), 56 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 7b5038751..ebd52cefe 100644 --- a/app/src/main/java/io/legado/app/model/ReadAloud.kt +++ b/app/src/main/java/io/legado/app/model/ReadAloud.kt @@ -125,14 +125,6 @@ object ReadAloud { } } - fun upTtsProgress(context: Context) { - if (BaseReadAloudService.isRun) { - val intent = Intent(context, aloudClass) - intent.action = IntentAction.upTtsProgress - ContextCompat.startForegroundService(context, intent) - } - } - fun setTimer(context: Context, minute: Int) { if (BaseReadAloudService.isRun) { val intent = Intent(context, aloudClass) 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 261e92538..7b16d5c34 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -326,12 +326,14 @@ object ReadBook : CoroutineScope by MainScope() { */ private fun curPageChanged(pageChanged: Boolean = false) { callBack?.pageChanged() - if (BaseReadAloudService.isRun && isLayoutAvailable) { - val scrollPageAnim = pageAnim() == 3 - if (scrollPageAnim && pageChanged) { - ReadAloud.pause(appCtx) - } else { - readAloud(!BaseReadAloudService.pause) + curTextChapter?.let { + if (BaseReadAloudService.isRun && it.isCompleted) { + val scrollPageAnim = pageAnim() == 3 + if (scrollPageAnim && pageChanged) { + ReadAloud.pause(appCtx) + } else { + readAloud(!BaseReadAloudService.pause) + } } } upReadTime() @@ -343,7 +345,8 @@ object ReadBook : CoroutineScope by MainScope() { */ fun readAloud(play: Boolean = true, startPos: Int = 0) { book ?: return - if (isLayoutAvailable) { + val textChapter = curTextChapter ?: return + if (textChapter.isCompleted) { ReadAloud.play(appCtx, play, startPos = startPos) } } @@ -548,8 +551,6 @@ object ReadBook : CoroutineScope by MainScope() { if (upContent) { callBack?.upContent(offset, resetPageOffset) } - curPageChanged() - callBack?.contentLoadFinish() available = true } if (upContent && isScroll) { @@ -560,10 +561,8 @@ object ReadBook : CoroutineScope by MainScope() { callBack?.onLayoutPageCompleted(index, page) } if (upContent) callBack?.upContent(offset, !available && resetPageOffset) - if (!available) { - curPageChanged() - callBack?.contentLoadFinish() - } + curPageChanged() + callBack?.contentLoadFinish() } -1 -> { 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 50cd3da3c..6375909fc 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -36,7 +36,6 @@ import io.legado.app.model.ReadBook import io.legado.app.receiver.MediaButtonReceiver import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.page.entities.TextChapter -import io.legado.app.utils.LogUtils import io.legado.app.utils.activityPendingIntent import io.legado.app.utils.broadcastPendingIntent import io.legado.app.utils.getPrefBoolean @@ -112,7 +111,6 @@ abstract class BaseReadAloudService : BaseService(), private var cover: Bitmap = BitmapFactory.decodeResource(appCtx.resources, R.drawable.icon_read_book) var pageChanged = false - private var ttsProgress = 0 private var toLast = false var paragraphStartPos = 0 private var readAloudByPage = false @@ -186,7 +184,6 @@ abstract class BaseReadAloudService : BaseService(), IntentAction.pause -> pauseReadAloud() IntentAction.resume -> resumeReadAloud() IntentAction.upTtsSpeechRate -> upSpeechRate(true) - IntentAction.upTtsProgress -> upTtsProgress(ttsProgress) IntentAction.prevParagraph -> prevP() IntentAction.nextParagraph -> nextP() IntentAction.addTimer -> addTimer() @@ -201,6 +198,9 @@ abstract class BaseReadAloudService : BaseService(), this@BaseReadAloudService.pageIndex = pageIndex textChapter = ReadBook.curTextChapter val textChapter = textChapter ?: return@execute + if (!textChapter.isCompleted) { + return@execute + } readAloudNumber = textChapter.getReadLength(pageIndex) + startPos readAloudByPage = getPrefBoolean(PreferKey.readAloudByPage) contentList = textChapter.getNeedReadAloud(0, readAloudByPage, 0) @@ -216,13 +216,6 @@ abstract class BaseReadAloudService : BaseService(), } } nowSpeak = textChapter.getParagraphNum(readAloudNumber + 1, readAloudByPage) - 1 - if (nowSpeak < 0) { - LogUtils.d(TAG) { - "nowSpeak:$nowSpeak readAloudNumber:$readAloudNumber isCompleted:${textChapter.isCompleted} " + - "pageSize:${textChapter.pageSize} \np1:${textChapter.paragraphsInternal}\n" + - "p2:${textChapter.pageParagraphsInternal}" - } - } if (!readAloudByPage && startPos == 0 && !toLast) { pos = page.lines.first().chapterPosition - textChapter.paragraphs[nowSpeak].chapterPosition @@ -289,7 +282,6 @@ abstract class BaseReadAloudService : BaseService(), abstract fun upSpeechRate(reset: Boolean = false) fun upTtsProgress(progress: Int) { - ttsProgress = progress postEvent(EventBus.TTS_PROGRESS, progress) } @@ -300,11 +292,7 @@ abstract class BaseReadAloudService : BaseService(), readAloudNumber -= contentList[nowSpeak].length + 1 + paragraphStartPos paragraphStartPos = 0 textChapter?.let { - val paragraphs = if (readAloudByPage) { - it.pageParagraphs - } else { - it.paragraphs - } + val paragraphs = it.getParagraphs(readAloudByPage) if (!paragraphs[nowSpeak].isParagraphEnd) readAloudNumber++ if (readAloudNumber < it.getReadLength(pageIndex)) { pageIndex-- @@ -325,6 +313,14 @@ abstract class BaseReadAloudService : BaseService(), readAloudNumber += contentList[nowSpeak].length.plus(1) - paragraphStartPos paragraphStartPos = 0 nowSpeak++ + textChapter?.let { + val paragraphs = it.getParagraphs(readAloudByPage) + if (!paragraphs[nowSpeak].isParagraphEnd) readAloudNumber-- + if (readAloudNumber >= it.getReadLength(pageIndex + 1)) { + pageIndex++ + ReadBook.moveToNextPage() + } + } upTtsProgress(readAloudNumber + 1) play() } else { 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 d21bfed57..fe300c5c3 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -381,6 +381,7 @@ class HttpReadAloudService : BaseReadAloudService(), exoPlayer.playWhenReady = true exoPlayer.prepare() } else { + exoPlayer.clearMediaItems() updateNextPos() } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 0387e3f24..a8abffda8 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -276,7 +276,7 @@ class ReadBookActivity : BaseReadBookActivity(), override fun onPostCreate(savedInstanceState: Bundle?) { super.onPostCreate(savedInstanceState) Looper.myQueue().addIdleHandler { - viewModel.initData(intent) { upMenu() } + viewModel.initData(intent) false } } @@ -902,6 +902,7 @@ class ReadBookActivity : BaseReadBookActivity(), override fun upMenuView() { handler.post { + upMenu() binding.readMenu.upBookView() } } @@ -919,9 +920,6 @@ class ReadBookActivity : BaseReadBookActivity(), intent.removeExtra("readAloud") ReadBook.readAloud() } - if (BaseReadAloudService.isRun) { - ReadAloud.upTtsProgress(this) - } loadStates = true } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index fecadc9eb..eb8524f5b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -68,9 +68,11 @@ data class TextChapter( val paragraphsInternal: ArrayList get() { val paragraphs = arrayListOf() - pages.forEach { - it.lines.forEach loop@{ line -> - if (line.paragraphNum <= 0) return@loop + for (i in pages.indices) { + val lines = pages[i].lines + for (a in lines.indices) { + val line = lines[a] + if (line.paragraphNum <= 0) continue if (paragraphs.lastIndex < line.paragraphNum - 1) { paragraphs.add(TextParagraph(line.paragraphNum)) } @@ -81,12 +83,15 @@ data class TextChapter( } val pageParagraphsInternal: List - get() = pages.map { - it.paragraphs - }.flatten().also { - it.forEachIndexed { index, textParagraph -> - textParagraph.num = index + 1 + get() { + val paragraphs = arrayListOf() + for (i in pages.indices) { + paragraphs.addAll(pages[i].paragraphs) } + for (i in paragraphs.indices) { + paragraphs[i].num = i + 1 + } + return paragraphs } /** @@ -189,11 +194,7 @@ data class TextChapter( position: Int, pageSplit: Boolean, ): Int { - val paragraphs = if (pageSplit) { - if (isCompleted) pageParagraphs else pageParagraphsInternal - } else { - if (isCompleted) paragraphs else pageParagraphsInternal - } + val paragraphs = getParagraphs(pageSplit) paragraphs.forEach { paragraph -> if (position in paragraph.chapterIndices) { return paragraph.num @@ -202,6 +203,14 @@ data class TextChapter( return -1 } + fun getParagraphs(pageSplit: Boolean): List { + return if (pageSplit) { + if (isCompleted) pageParagraphs else pageParagraphsInternal + } else { + if (isCompleted) paragraphs else paragraphsInternal + } + } + fun getLastParagraphPosition(): Int { return pageParagraphs.last().chapterPosition } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt index 5f3bc881e..49246cf8d 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt @@ -48,7 +48,7 @@ data class TextLine( ) { val columns: List get() = textColumns - val charSize: Int get() = textColumns.size + val charSize: Int get() = text.length val lineStart: Float get() = textColumns.firstOrNull()?.start ?: 0f val lineEnd: Float get() = textColumns.lastOrNull()?.end ?: 0f val chapterIndices: IntRange get() = chapterPosition..chapterPosition + charSize diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index 1e7295cbb..6ba196838 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -250,7 +250,14 @@ data class TextPage( length++ } } - return length + columnIndex + val columns = textLines[maxIndex].columns + for (index in 0 until columnIndex) { + val column = columns[index] + if (column is TextColumn) { + length += column.charData.length + } + } + return length } /**