pull/1319/head
gedoor 3 years ago
parent 27e4d1acbf
commit 8f7d7937fa
  1. 8
      app/src/main/java/io/legado/app/model/ReadAloud.kt
  2. 15
      app/src/main/java/io/legado/app/model/ReadBook.kt
  3. 20
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt
  4. 52
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  5. 5
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  6. 9
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -33,18 +33,10 @@ object ReadAloud {
fun play( fun play(
context: Context, context: Context,
title: String,
subtitle: String,
pageIndex: Int,
dataKey: String,
play: Boolean = true play: Boolean = true
) { ) {
val intent = Intent(context, aloudClass) val intent = Intent(context, aloudClass)
intent.action = IntentAction.play intent.action = IntentAction.play
intent.putExtra("title", title)
intent.putExtra("subtitle", subtitle)
intent.putExtra("pageIndex", pageIndex)
intent.putExtra("dataKey", dataKey)
intent.putExtra("play", play) intent.putExtra("play", play)
context.startService(intent) context.startService(intent)
} }

@ -5,7 +5,10 @@ import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.* 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.coroutine.Coroutine
import io.legado.app.help.storage.AppWebDav import io.legado.app.help.storage.AppWebDav
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
@ -46,6 +49,7 @@ object ReadBook : CoroutineScope by MainScope() {
ReadBook.book = book ReadBook.book = book
readRecord.bookName = book.name readRecord.bookName = book.name
readRecord.readTime = appDb.readRecordDao.getReadTime(book.name) ?: 0 readRecord.readTime = appDb.readRecordDao.getReadTime(book.name) ?: 0
chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl)
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
durChapterPos = book.durChapterPos durChapterPos = book.durChapterPos
isLocalBook = book.origin == BookType.local isLocalBook = book.origin == BookType.local
@ -200,13 +204,8 @@ object ReadBook : CoroutineScope by MainScope() {
* 朗读 * 朗读
*/ */
fun readAloud(play: Boolean = true) { fun readAloud(play: Boolean = true) {
val book = book book?.let {
val textChapter = curTextChapter ReadAloud.play(appCtx, play)
if (book != null && textChapter != null) {
val key = IntentDataHelp.putData(textChapter)
ReadAloud.play(
appCtx, book.name, textChapter.title, durPageIndex(), key, play
)
} }
} }

@ -15,10 +15,8 @@ import io.legado.app.service.AudioPlayService
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.ui.book.audio.AudioPlayActivity import io.legado.app.ui.book.audio.AudioPlayActivity
import io.legado.app.ui.book.read.ReadBookActivity 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.getPrefBoolean
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.startActivity
/** /**
@ -87,16 +85,14 @@ class MediaButtonReceiver : BroadcastReceiver() {
postEvent(EventBus.MEDIA_BUTTON, true) postEvent(EventBus.MEDIA_BUTTON, true)
LifecycleHelp.isExistActivity(AudioPlayActivity::class.java) -> LifecycleHelp.isExistActivity(AudioPlayActivity::class.java) ->
postEvent(EventBus.MEDIA_BUTTON, true) postEvent(EventBus.MEDIA_BUTTON, true)
else -> if (AppConfig.mediaButtonOnExit || !isMediaKey || LifecycleHelp.activitySize() > 0) { else -> if (AppConfig.mediaButtonOnExit || LifecycleHelp.activitySize() > 0 || !isMediaKey) {
appDb.bookDao.lastReadBook?.let { if (ReadBook.book != null) {
if (!LifecycleHelp.isExistActivity(MainActivity::class.java)) { ReadBook.readAloud()
context.startActivity<MainActivity> { } else {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) appDb.bookDao.lastReadBook?.let {
} ReadBook.resetData(it)
} ReadBook.curTextChapter ?: ReadBook.loadContent(false)
context.startActivity<ReadBookActivity> { ReadBook.readAloud()
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra("readAloud", true)
} }
} }
} }

@ -16,7 +16,6 @@ import androidx.media.AudioFocusRequestCompat
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.* import io.legado.app.constant.*
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.MediaHelp import io.legado.app.help.MediaHelp
import io.legado.app.model.ReadAloud import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
@ -46,8 +45,6 @@ abstract class BaseReadAloudService : BaseService(),
private var mFocusRequest: AudioFocusRequestCompat? = null private var mFocusRequest: AudioFocusRequestCompat? = null
private var broadcastReceiver: BroadcastReceiver? = null private var broadcastReceiver: BroadcastReceiver? = null
private lateinit var mediaSessionCompat: MediaSessionCompat private lateinit var mediaSessionCompat: MediaSessionCompat
private var title: String = ""
private var subtitle: String = ""
internal val contentList = arrayListOf<String>() internal val contentList = arrayListOf<String>()
internal var nowSpeak: Int = 0 internal var nowSpeak: Int = 0
internal var readAloudNumber: Int = 0 internal var readAloudNumber: Int = 0
@ -84,11 +81,9 @@ abstract class BaseReadAloudService : BaseService(),
intent?.action?.let { action -> intent?.action?.let { action ->
when (action) { when (action) {
IntentAction.play -> { IntentAction.play -> {
title = intent.getStringExtra("title") ?: "" pageIndex = ReadBook.durPageIndex()
subtitle = intent.getStringExtra("subtitle") ?: ""
pageIndex = intent.getIntExtra("pageIndex", 0)
newReadAloud( newReadAloud(
intent.getStringExtra("dataKey"), ReadBook.curTextChapter,
intent.getBooleanExtra("play", true) intent.getBooleanExtra("play", true)
) )
} }
@ -106,29 +101,26 @@ abstract class BaseReadAloudService : BaseService(),
} }
@CallSuper @CallSuper
open fun newReadAloud(dataKey: String?, play: Boolean) { open fun newReadAloud(textChapter: TextChapter?, play: Boolean) {
dataKey?.let { textChapter?.let {
textChapter = IntentDataHelp.getData<TextChapter>(dataKey) nowSpeak = 0
textChapter?.let { textChapter -> readAloudNumber = textChapter.getReadLength(pageIndex)
nowSpeak = 0 contentList.clear()
readAloudNumber = textChapter.getReadLength(pageIndex) if (getPrefBoolean(PreferKey.readAloudByPage)) {
contentList.clear() for (index in pageIndex..textChapter.lastIndex) {
if (getPrefBoolean(PreferKey.readAloudByPage)) { textChapter.page(index)?.text?.split("\n")?.let {
for (index in pageIndex..textChapter.lastIndex) { contentList.addAll(it)
textChapter.page(index)?.text?.split("\n")?.let {
contentList.addAll(it)
}
} }
} else { }
textChapter.getUnRead(pageIndex).split("\n").forEach { } else {
if (it.isNotEmpty()) { textChapter.getUnRead(pageIndex).split("\n").forEach {
contentList.add(it) if (it.isNotEmpty()) {
} contentList.add(it)
} }
} }
if (play) play() }
} ?: stopSelf() if (play) play()
} ?: stopSelf() }
} }
open fun play() { open fun play() {
@ -307,9 +299,9 @@ abstract class BaseReadAloudService : BaseService(),
) )
else -> getString(R.string.read_aloud_t) else -> getString(R.string.read_aloud_t)
} }
nTitle += ": $title" nTitle += ": ${ReadBook.book?.name}"
var nSubtitle = subtitle var nSubtitle = ReadBook.curTextChapter?.title
if (subtitle.isEmpty()) if (nSubtitle.isNullOrBlank())
nSubtitle = getString(R.string.read_aloud_s) nSubtitle = getString(R.string.read_aloud_s)
val builder = NotificationCompat.Builder(this, AppConst.channelIdReadAloud) val builder = NotificationCompat.Builder(this, AppConst.channelIdReadAloud)
.setSmallIcon(R.drawable.ic_volume_up) .setSmallIcon(R.drawable.ic_volume_up)

@ -8,6 +8,7 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.ReadAloud import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
@ -45,10 +46,10 @@ class HttpReadAloudService : BaseReadAloudService(),
player.release() player.release()
} }
override fun newReadAloud(dataKey: String?, play: Boolean) { override fun newReadAloud(textChapter: TextChapter?, play: Boolean) {
player.reset() player.reset()
playingIndex = -1 playingIndex = -1
super.newReadAloud(dataKey, play) super.newReadAloud(textChapter, play)
} }
override fun play() { override fun play() {

@ -51,12 +51,11 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun initBook(book: Book) { private fun initBook(book: Book) {
if (ReadBook.book?.bookUrl != book.bookUrl) { if (ReadBook.book?.bookUrl != book.bookUrl) {
ReadBook.resetData(book) ReadBook.resetData(book)
isInitFinish = true
if (!book.isLocalBook() && ReadBook.bookSource == null) { if (!book.isLocalBook() && ReadBook.bookSource == null) {
autoChangeSource(book.name, book.author) autoChangeSource(book.name, book.author)
return return
} }
ReadBook.chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl) isInitFinish = true
if (ReadBook.chapterSize == 0) { if (ReadBook.chapterSize == 0) {
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
loadBookInfo(book) loadBookInfo(book)
@ -194,11 +193,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
oldTocSize = it.totalChapterNum oldTocSize = it.totalChapterNum
it.changeTo(newBook) it.changeTo(newBook)
} }
ReadBook.book = newBook ReadBook.resetData(newBook)
ReadBook.bookSource = appDb.bookSourceDao.getBookSource(newBook.origin)
ReadBook.prevTextChapter = null
ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null
withContext(Main) { withContext(Main) {
ReadBook.callBack?.upContent() ReadBook.callBack?.upContent()
} }

Loading…
Cancel
Save