mirror of
https://github.com/gedoor/legado.git
synced 2024-07-04 23:36:56 +08:00
优化
This commit is contained in:
parent
f128df2d96
commit
f06821ccb3
|
@ -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) {
|
fun setTimer(context: Context, minute: Int) {
|
||||||
if (BaseReadAloudService.isRun) {
|
if (BaseReadAloudService.isRun) {
|
||||||
val intent = Intent(context, aloudClass)
|
val intent = Intent(context, aloudClass)
|
||||||
|
|
|
@ -326,12 +326,14 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||||
*/
|
*/
|
||||||
private fun curPageChanged(pageChanged: Boolean = false) {
|
private fun curPageChanged(pageChanged: Boolean = false) {
|
||||||
callBack?.pageChanged()
|
callBack?.pageChanged()
|
||||||
if (BaseReadAloudService.isRun && isLayoutAvailable) {
|
curTextChapter?.let {
|
||||||
val scrollPageAnim = pageAnim() == 3
|
if (BaseReadAloudService.isRun && it.isCompleted) {
|
||||||
if (scrollPageAnim && pageChanged) {
|
val scrollPageAnim = pageAnim() == 3
|
||||||
ReadAloud.pause(appCtx)
|
if (scrollPageAnim && pageChanged) {
|
||||||
} else {
|
ReadAloud.pause(appCtx)
|
||||||
readAloud(!BaseReadAloudService.pause)
|
} else {
|
||||||
|
readAloud(!BaseReadAloudService.pause)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
upReadTime()
|
upReadTime()
|
||||||
|
@ -343,7 +345,8 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||||
*/
|
*/
|
||||||
fun readAloud(play: Boolean = true, startPos: Int = 0) {
|
fun readAloud(play: Boolean = true, startPos: Int = 0) {
|
||||||
book ?: return
|
book ?: return
|
||||||
if (isLayoutAvailable) {
|
val textChapter = curTextChapter ?: return
|
||||||
|
if (textChapter.isCompleted) {
|
||||||
ReadAloud.play(appCtx, play, startPos = startPos)
|
ReadAloud.play(appCtx, play, startPos = startPos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -548,8 +551,6 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||||
if (upContent) {
|
if (upContent) {
|
||||||
callBack?.upContent(offset, resetPageOffset)
|
callBack?.upContent(offset, resetPageOffset)
|
||||||
}
|
}
|
||||||
curPageChanged()
|
|
||||||
callBack?.contentLoadFinish()
|
|
||||||
available = true
|
available = true
|
||||||
}
|
}
|
||||||
if (upContent && isScroll) {
|
if (upContent && isScroll) {
|
||||||
|
@ -560,10 +561,8 @@ object ReadBook : CoroutineScope by MainScope() {
|
||||||
callBack?.onLayoutPageCompleted(index, page)
|
callBack?.onLayoutPageCompleted(index, page)
|
||||||
}
|
}
|
||||||
if (upContent) callBack?.upContent(offset, !available && resetPageOffset)
|
if (upContent) callBack?.upContent(offset, !available && resetPageOffset)
|
||||||
if (!available) {
|
curPageChanged()
|
||||||
curPageChanged()
|
callBack?.contentLoadFinish()
|
||||||
callBack?.contentLoadFinish()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
-1 -> {
|
-1 -> {
|
||||||
|
|
|
@ -36,7 +36,6 @@ import io.legado.app.model.ReadBook
|
||||||
import io.legado.app.receiver.MediaButtonReceiver
|
import io.legado.app.receiver.MediaButtonReceiver
|
||||||
import io.legado.app.ui.book.read.ReadBookActivity
|
import io.legado.app.ui.book.read.ReadBookActivity
|
||||||
import io.legado.app.ui.book.read.page.entities.TextChapter
|
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.activityPendingIntent
|
||||||
import io.legado.app.utils.broadcastPendingIntent
|
import io.legado.app.utils.broadcastPendingIntent
|
||||||
import io.legado.app.utils.getPrefBoolean
|
import io.legado.app.utils.getPrefBoolean
|
||||||
|
@ -112,7 +111,6 @@ abstract class BaseReadAloudService : BaseService(),
|
||||||
private var cover: Bitmap =
|
private var cover: Bitmap =
|
||||||
BitmapFactory.decodeResource(appCtx.resources, R.drawable.icon_read_book)
|
BitmapFactory.decodeResource(appCtx.resources, R.drawable.icon_read_book)
|
||||||
var pageChanged = false
|
var pageChanged = false
|
||||||
private var ttsProgress = 0
|
|
||||||
private var toLast = false
|
private var toLast = false
|
||||||
var paragraphStartPos = 0
|
var paragraphStartPos = 0
|
||||||
private var readAloudByPage = false
|
private var readAloudByPage = false
|
||||||
|
@ -186,7 +184,6 @@ abstract class BaseReadAloudService : BaseService(),
|
||||||
IntentAction.pause -> pauseReadAloud()
|
IntentAction.pause -> pauseReadAloud()
|
||||||
IntentAction.resume -> resumeReadAloud()
|
IntentAction.resume -> resumeReadAloud()
|
||||||
IntentAction.upTtsSpeechRate -> upSpeechRate(true)
|
IntentAction.upTtsSpeechRate -> upSpeechRate(true)
|
||||||
IntentAction.upTtsProgress -> upTtsProgress(ttsProgress)
|
|
||||||
IntentAction.prevParagraph -> prevP()
|
IntentAction.prevParagraph -> prevP()
|
||||||
IntentAction.nextParagraph -> nextP()
|
IntentAction.nextParagraph -> nextP()
|
||||||
IntentAction.addTimer -> addTimer()
|
IntentAction.addTimer -> addTimer()
|
||||||
|
@ -201,6 +198,9 @@ abstract class BaseReadAloudService : BaseService(),
|
||||||
this@BaseReadAloudService.pageIndex = pageIndex
|
this@BaseReadAloudService.pageIndex = pageIndex
|
||||||
textChapter = ReadBook.curTextChapter
|
textChapter = ReadBook.curTextChapter
|
||||||
val textChapter = textChapter ?: return@execute
|
val textChapter = textChapter ?: return@execute
|
||||||
|
if (!textChapter.isCompleted) {
|
||||||
|
return@execute
|
||||||
|
}
|
||||||
readAloudNumber = textChapter.getReadLength(pageIndex) + startPos
|
readAloudNumber = textChapter.getReadLength(pageIndex) + startPos
|
||||||
readAloudByPage = getPrefBoolean(PreferKey.readAloudByPage)
|
readAloudByPage = getPrefBoolean(PreferKey.readAloudByPage)
|
||||||
contentList = textChapter.getNeedReadAloud(0, readAloudByPage, 0)
|
contentList = textChapter.getNeedReadAloud(0, readAloudByPage, 0)
|
||||||
|
@ -216,13 +216,6 @@ abstract class BaseReadAloudService : BaseService(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nowSpeak = textChapter.getParagraphNum(readAloudNumber + 1, readAloudByPage) - 1
|
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) {
|
if (!readAloudByPage && startPos == 0 && !toLast) {
|
||||||
pos = page.lines.first().chapterPosition -
|
pos = page.lines.first().chapterPosition -
|
||||||
textChapter.paragraphs[nowSpeak].chapterPosition
|
textChapter.paragraphs[nowSpeak].chapterPosition
|
||||||
|
@ -289,7 +282,6 @@ abstract class BaseReadAloudService : BaseService(),
|
||||||
abstract fun upSpeechRate(reset: Boolean = false)
|
abstract fun upSpeechRate(reset: Boolean = false)
|
||||||
|
|
||||||
fun upTtsProgress(progress: Int) {
|
fun upTtsProgress(progress: Int) {
|
||||||
ttsProgress = progress
|
|
||||||
postEvent(EventBus.TTS_PROGRESS, progress)
|
postEvent(EventBus.TTS_PROGRESS, progress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,11 +292,7 @@ abstract class BaseReadAloudService : BaseService(),
|
||||||
readAloudNumber -= contentList[nowSpeak].length + 1 + paragraphStartPos
|
readAloudNumber -= contentList[nowSpeak].length + 1 + paragraphStartPos
|
||||||
paragraphStartPos = 0
|
paragraphStartPos = 0
|
||||||
textChapter?.let {
|
textChapter?.let {
|
||||||
val paragraphs = if (readAloudByPage) {
|
val paragraphs = it.getParagraphs(readAloudByPage)
|
||||||
it.pageParagraphs
|
|
||||||
} else {
|
|
||||||
it.paragraphs
|
|
||||||
}
|
|
||||||
if (!paragraphs[nowSpeak].isParagraphEnd) readAloudNumber++
|
if (!paragraphs[nowSpeak].isParagraphEnd) readAloudNumber++
|
||||||
if (readAloudNumber < it.getReadLength(pageIndex)) {
|
if (readAloudNumber < it.getReadLength(pageIndex)) {
|
||||||
pageIndex--
|
pageIndex--
|
||||||
|
@ -325,6 +313,14 @@ abstract class BaseReadAloudService : BaseService(),
|
||||||
readAloudNumber += contentList[nowSpeak].length.plus(1) - paragraphStartPos
|
readAloudNumber += contentList[nowSpeak].length.plus(1) - paragraphStartPos
|
||||||
paragraphStartPos = 0
|
paragraphStartPos = 0
|
||||||
nowSpeak++
|
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)
|
upTtsProgress(readAloudNumber + 1)
|
||||||
play()
|
play()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -381,6 +381,7 @@ class HttpReadAloudService : BaseReadAloudService(),
|
||||||
exoPlayer.playWhenReady = true
|
exoPlayer.playWhenReady = true
|
||||||
exoPlayer.prepare()
|
exoPlayer.prepare()
|
||||||
} else {
|
} else {
|
||||||
|
exoPlayer.clearMediaItems()
|
||||||
updateNextPos()
|
updateNextPos()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,7 +276,7 @@ class ReadBookActivity : BaseReadBookActivity(),
|
||||||
override fun onPostCreate(savedInstanceState: Bundle?) {
|
override fun onPostCreate(savedInstanceState: Bundle?) {
|
||||||
super.onPostCreate(savedInstanceState)
|
super.onPostCreate(savedInstanceState)
|
||||||
Looper.myQueue().addIdleHandler {
|
Looper.myQueue().addIdleHandler {
|
||||||
viewModel.initData(intent) { upMenu() }
|
viewModel.initData(intent)
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -902,6 +902,7 @@ class ReadBookActivity : BaseReadBookActivity(),
|
||||||
|
|
||||||
override fun upMenuView() {
|
override fun upMenuView() {
|
||||||
handler.post {
|
handler.post {
|
||||||
|
upMenu()
|
||||||
binding.readMenu.upBookView()
|
binding.readMenu.upBookView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -919,9 +920,6 @@ class ReadBookActivity : BaseReadBookActivity(),
|
||||||
intent.removeExtra("readAloud")
|
intent.removeExtra("readAloud")
|
||||||
ReadBook.readAloud()
|
ReadBook.readAloud()
|
||||||
}
|
}
|
||||||
if (BaseReadAloudService.isRun) {
|
|
||||||
ReadAloud.upTtsProgress(this)
|
|
||||||
}
|
|
||||||
loadStates = true
|
loadStates = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,9 +68,11 @@ data class TextChapter(
|
||||||
val paragraphsInternal: ArrayList<TextParagraph>
|
val paragraphsInternal: ArrayList<TextParagraph>
|
||||||
get() {
|
get() {
|
||||||
val paragraphs = arrayListOf<TextParagraph>()
|
val paragraphs = arrayListOf<TextParagraph>()
|
||||||
pages.forEach {
|
for (i in pages.indices) {
|
||||||
it.lines.forEach loop@{ line ->
|
val lines = pages[i].lines
|
||||||
if (line.paragraphNum <= 0) return@loop
|
for (a in lines.indices) {
|
||||||
|
val line = lines[a]
|
||||||
|
if (line.paragraphNum <= 0) continue
|
||||||
if (paragraphs.lastIndex < line.paragraphNum - 1) {
|
if (paragraphs.lastIndex < line.paragraphNum - 1) {
|
||||||
paragraphs.add(TextParagraph(line.paragraphNum))
|
paragraphs.add(TextParagraph(line.paragraphNum))
|
||||||
}
|
}
|
||||||
|
@ -81,12 +83,15 @@ data class TextChapter(
|
||||||
}
|
}
|
||||||
|
|
||||||
val pageParagraphsInternal: List<TextParagraph>
|
val pageParagraphsInternal: List<TextParagraph>
|
||||||
get() = pages.map {
|
get() {
|
||||||
it.paragraphs
|
val paragraphs = arrayListOf<TextParagraph>()
|
||||||
}.flatten().also {
|
for (i in pages.indices) {
|
||||||
it.forEachIndexed { index, textParagraph ->
|
paragraphs.addAll(pages[i].paragraphs)
|
||||||
textParagraph.num = index + 1
|
|
||||||
}
|
}
|
||||||
|
for (i in paragraphs.indices) {
|
||||||
|
paragraphs[i].num = i + 1
|
||||||
|
}
|
||||||
|
return paragraphs
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -189,11 +194,7 @@ data class TextChapter(
|
||||||
position: Int,
|
position: Int,
|
||||||
pageSplit: Boolean,
|
pageSplit: Boolean,
|
||||||
): Int {
|
): Int {
|
||||||
val paragraphs = if (pageSplit) {
|
val paragraphs = getParagraphs(pageSplit)
|
||||||
if (isCompleted) pageParagraphs else pageParagraphsInternal
|
|
||||||
} else {
|
|
||||||
if (isCompleted) paragraphs else pageParagraphsInternal
|
|
||||||
}
|
|
||||||
paragraphs.forEach { paragraph ->
|
paragraphs.forEach { paragraph ->
|
||||||
if (position in paragraph.chapterIndices) {
|
if (position in paragraph.chapterIndices) {
|
||||||
return paragraph.num
|
return paragraph.num
|
||||||
|
@ -202,6 +203,14 @@ data class TextChapter(
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getParagraphs(pageSplit: Boolean): List<TextParagraph> {
|
||||||
|
return if (pageSplit) {
|
||||||
|
if (isCompleted) pageParagraphs else pageParagraphsInternal
|
||||||
|
} else {
|
||||||
|
if (isCompleted) paragraphs else paragraphsInternal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun getLastParagraphPosition(): Int {
|
fun getLastParagraphPosition(): Int {
|
||||||
return pageParagraphs.last().chapterPosition
|
return pageParagraphs.last().chapterPosition
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ data class TextLine(
|
||||||
) {
|
) {
|
||||||
|
|
||||||
val columns: List<BaseColumn> get() = textColumns
|
val columns: List<BaseColumn> get() = textColumns
|
||||||
val charSize: Int get() = textColumns.size
|
val charSize: Int get() = text.length
|
||||||
val lineStart: Float get() = textColumns.firstOrNull()?.start ?: 0f
|
val lineStart: Float get() = textColumns.firstOrNull()?.start ?: 0f
|
||||||
val lineEnd: Float get() = textColumns.lastOrNull()?.end ?: 0f
|
val lineEnd: Float get() = textColumns.lastOrNull()?.end ?: 0f
|
||||||
val chapterIndices: IntRange get() = chapterPosition..chapterPosition + charSize
|
val chapterIndices: IntRange get() = chapterPosition..chapterPosition + charSize
|
||||||
|
|
|
@ -250,7 +250,14 @@ data class TextPage(
|
||||||
length++
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user