pull/32/head
kunfei 5 years ago
parent 7334ae43d6
commit 37a039e794
  1. 2
      app/src/main/java/io/legado/app/constant/Bus.kt
  2. 11
      app/src/main/java/io/legado/app/service/ReadAloudService.kt
  3. 46
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/readbook/ReadMenu.kt
  5. 2
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt

@ -5,7 +5,7 @@ object Bus {
const val UP_BOOK = "sourceDebugLog" const val UP_BOOK = "sourceDebugLog"
const val ALOUD_STATE = "aloud_state" const val ALOUD_STATE = "aloud_state"
const val TTS_START = "ttsStart" const val TTS_START = "ttsStart"
const val TTS_RANGE_START = "readAloudNumber" const val TTS_NEXT = "ttsNextPage"
const val BATTERY_CHANGED = "batteryChanged" const val BATTERY_CHANGED = "batteryChanged"
const val TIME_CHANGED = "timeChanged" const val TIME_CHANGED = "timeChanged"
const val READ_ALOUD = "readAloud" const val READ_ALOUD = "readAloud"

@ -282,20 +282,25 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
override fun onStart(s: String) { override fun onStart(s: String) {
postEvent(Bus.TTS_START, readAloudNumber + 1) postEvent(Bus.TTS_START, readAloudNumber + 1)
postEvent(Bus.TTS_RANGE_START, readAloudNumber + 1)
} }
override fun onDone(s: String) { override fun onDone(s: String) {
readAloudNumber += contentList[nowSpeak].length + 1 readAloudNumber += contentList[nowSpeak].length + 1
nowSpeak += 1 nowSpeak += 1
if (nowSpeak >= contentList.size) { if (nowSpeak >= contentList.size) {
postEvent(Bus.ALOUD_STATE, Status.NEXT) postEvent(Bus.TTS_NEXT, true)
} }
} }
override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) { override fun onRangeStart(utteranceId: String?, start: Int, end: Int, frame: Int) {
super.onRangeStart(utteranceId, start, end, frame) super.onRangeStart(utteranceId, start, end, frame)
postEvent(Bus.TTS_RANGE_START, readAloudNumber + start) textChapter?.let {
if (readAloudNumber + start > it.getReadLength(pageIndex + 1)) {
pageIndex++
postEvent(Bus.TTS_NEXT, false)
postEvent(Bus.TTS_START, readAloudNumber + start)
}
}
} }
override fun onError(s: String) { override fun onError(s: String) {

@ -158,6 +158,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
return super.onCompatCreateOptionsMenu(menu) return super.onCompatCreateOptionsMenu(menu)
} }
/**
* 菜单
*/
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_change_source -> { R.id.menu_change_source -> {
@ -173,26 +176,30 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }
/**
* 按键拦截,显示菜单
*/
override fun dispatchKeyEvent(event: KeyEvent?): Boolean { override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
val keyCode = event?.keyCode val keyCode = event?.keyCode
val action = event?.action val action = event?.action
val isDown = action == 0 val isDown = action == 0
if (keyCode == KeyEvent.KEYCODE_MENU) { if (keyCode == KeyEvent.KEYCODE_MENU) {
if (isDown && !read_menu.menuBarShow) { if (isDown && !read_menu.cnaShowMenu) {
read_menu.runMenuIn() read_menu.runMenuIn()
return true return true
} }
if (!isDown && !read_menu.menuBarShow) { if (!isDown && !read_menu.cnaShowMenu) {
read_menu.menuBarShow = true read_menu.cnaShowMenu = true
return true return true
} }
} }
return super.dispatchKeyEvent(event) return super.dispatchKeyEvent(event)
} }
/**
* 书籍加载完成,开始加载章节内容
*/
private fun bookLoadFinish() { private fun bookLoadFinish() {
viewModel.bookData.value?.let { viewModel.bookData.value?.let {
viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(it, viewModel.durChapterIndex)
@ -201,12 +208,18 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
override fun loadChapter(index: Int) { /**
* 加载章节内容
*/
override fun loadContent(index: Int) {
viewModel.bookData.value?.let { viewModel.bookData.value?.let {
viewModel.loadContent(it, index) viewModel.loadContent(it, index)
} }
} }
/**
* 内容加载完成
*/
override fun contentLoadFinish(bookChapter: BookChapter, content: String) { override fun contentLoadFinish(bookChapter: BookChapter, content: String) {
launch { launch {
when (bookChapter.index) { when (bookChapter.index) {
@ -291,6 +304,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
/**
* 朗读按钮
*/
private fun onClickReadAloud() { private fun onClickReadAloud() {
if (!ReadAloudService.isRun) { if (!ReadAloudService.isRun) {
readAloudStatus = Status.STOP readAloudStatus = Status.STOP
@ -303,6 +319,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
/**
* 朗读
*/
private fun readAloud() { private fun readAloud() {
val book = viewModel.bookData.value val book = viewModel.bookData.value
val textChapter = viewModel.curTextChapter val textChapter = viewModel.curTextChapter
@ -319,6 +338,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
/**
* 设置屏幕亮度
*/
private fun setScreenBrightness(value: Int) { private fun setScreenBrightness(value: Int) {
var brightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE var brightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
if (this.getPrefBoolean("brightnessAuto").not()) { if (this.getPrefBoolean("brightnessAuto").not()) {
@ -364,8 +386,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
override fun observeLiveBus() { override fun observeLiveBus() {
super.observeLiveBus() super.observeLiveBus()
observeEvent<Int>(Bus.ALOUD_STATE) { readAloudStatus = it } observeEvent<Int>(Bus.ALOUD_STATE) { readAloudStatus = it }
observeEvent<Int>(Bus.TTS_START) {}
observeEvent<Int>(Bus.TTS_RANGE_START) {}
observeEvent<String>(Bus.TIME_CHANGED) { page_view.upTime() } observeEvent<String>(Bus.TIME_CHANGED) { page_view.upTime() }
observeEvent<Int>(Bus.BATTERY_CHANGED) { page_view.upBattery(it) } observeEvent<Int>(Bus.BATTERY_CHANGED) { page_view.upBattery(it) }
observeEvent<BookChapter>(Bus.OPEN_CHAPTER) { viewModel.openChapter(it) } observeEvent<BookChapter>(Bus.OPEN_CHAPTER) { viewModel.openChapter(it) }
@ -376,6 +396,16 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
content_view.upStyle() content_view.upStyle()
page_view.upStyle() page_view.upStyle()
} }
observeEvent<Int>(Bus.TTS_START) {
}
observeEvent<Boolean>(Bus.TTS_NEXT) {
if (it) {
} else {
}
}
} }
} }

@ -19,7 +19,7 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ReadMenu : FrameLayout { class ReadMenu : FrameLayout {
private var callback: Callback? = null private var callback: Callback? = null
var menuBarShow: Boolean = false var cnaShowMenu: Boolean = false
private lateinit var menuTopIn: Animation private lateinit var menuTopIn: Animation
private lateinit var menuTopOut: Animation private lateinit var menuTopOut: Animation
private lateinit var menuBottomIn: Animation private lateinit var menuBottomIn: Animation
@ -209,7 +209,7 @@ class ReadMenu : FrameLayout {
this@ReadMenu.invisible() this@ReadMenu.invisible()
title_bar.invisible() title_bar.invisible()
bottom_menu.invisible() bottom_menu.invisible()
menuBarShow = false cnaShowMenu = false
onMenuOutEnd?.invoke() onMenuOutEnd?.invoke()
callback?.menuHide() callback?.menuHide()
} }

@ -220,7 +220,7 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
fun durChapterIndex(): Int fun durChapterIndex(): Int
fun durChapterPos(): Int fun durChapterPos(): Int
fun textChapter(chapterOnDur: Int = 0): TextChapter? fun textChapter(chapterOnDur: Int = 0): TextChapter?
fun loadChapter(index: Int) fun loadContent(index: Int)
fun moveToNextChapter() fun moveToNextChapter()
fun moveToPrevChapter() fun moveToPrevChapter()
fun setPageIndex(pageIndex: Int) fun setPageIndex(pageIndex: Int)

Loading…
Cancel
Save