pull/32/head
kunfei 5 years ago
parent aeb210bf4b
commit e274d2c3e4
  1. 16
      app/src/main/java/io/legado/app/help/IntentDataHelp.kt
  2. 55
      app/src/main/java/io/legado/app/service/ReadAloudService.kt
  3. 4
      app/src/main/java/io/legado/app/utils/GsonExtensions.kt

@ -0,0 +1,16 @@
package io.legado.app.help
object IntentDataHelp {
private val bigData: MutableMap<String, Any> = mutableMapOf()
fun putData(key: String, data: Any) {
bigData[key] = data
}
fun getData(key: String): Any? {
val data = bigData[key]
bigData.remove(key)
return data
}
}

@ -13,10 +13,12 @@ import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.Bus
import io.legado.app.constant.Status
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.IntentHelp
import io.legado.app.help.MediaHelp
import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.service.notification.ReadAloudNotification
import io.legado.app.ui.widget.page.TextChapter
import io.legado.app.utils.postEvent
import io.legado.app.utils.toast
import kotlinx.coroutines.launch
@ -28,13 +30,19 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
val tag: String = ReadAloudService::class.java.simpleName
var isRun = false
fun play(context: Context, title: String, subtitle: String, readLength: Int, body: String) {
fun play(
context: Context,
title: String,
subtitle: String,
pageIndex: Int,
dataKey: String
) {
val readAloudIntent = Intent(context, ReadAloudService::class.java)
readAloudIntent.action = "play"
readAloudIntent.putExtra("title", title)
readAloudIntent.putExtra("subtitle", subtitle)
readAloudIntent.putExtra("readLength", readLength)
readAloudIntent.putExtra("body", body)
readAloudIntent.putExtra("pageIndex", pageIndex)
readAloudIntent.putExtra("dataKey", dataKey)
context.startService(readAloudIntent)
}
@ -67,16 +75,18 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
private var ttsIsSuccess: Boolean = false
private lateinit var audioManager: AudioManager
private lateinit var mFocusRequest: AudioFocusRequest
var mediaSessionCompat: MediaSessionCompat? = null
private var broadcastReceiver: BroadcastReceiver? = null
private var speak: Boolean = true
private var nowSpeak: Int = 0
private val contentList = arrayListOf<String>()
private var readAloudNumber: Int = 0
private var textChapter: TextChapter? = null
private var pageIndex = 0
var mediaSessionCompat: MediaSessionCompat? = null
var pause = false
var title: String = ""
var subtitle: String = ""
var timeMinute: Int = 0
private var readAloudNumber: Int = 0
override fun onCreate() {
super.onCreate()
@ -104,18 +114,12 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
"play" -> {
title = intent.getStringExtra("title") ?: ""
subtitle = intent.getStringExtra("subtitle") ?: ""
readAloudNumber = intent.getIntExtra("readLength", 0)
newReadAloud(intent.getStringExtra("body"))
}
"pause" -> {
pauseReadAloud(true)
}
"resume" -> {
resumeReadAloud()
}
"stop" -> {
stopSelf()
pageIndex = intent.getIntExtra("pageIndex", 0)
newReadAloud(intent.getStringExtra("dataKey"))
}
"pause" -> pauseReadAloud(true)
"resume" -> resumeReadAloud()
"stop" -> stopSelf()
}
}
return super.onStartCommand(intent, flags, startId)
@ -139,15 +143,16 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
}
}
private fun newReadAloud(body: String?) {
if (body.isNullOrEmpty()) {
stopSelf()
} else {
private fun newReadAloud(dataKey: String?) {
dataKey?.let {
textChapter = IntentDataHelp.getData(dataKey) as? TextChapter
textChapter?.let {
nowSpeak = 0
readAloudNumber = 0
readAloudNumber = it.getReadLength(pageIndex)
contentList.clear()
contentList.addAll(body.split("\n"))
}
contentList.addAll(it.getUnRead(pageIndex).split("\n"))
} ?: stopSelf()
} ?: stopSelf()
}
@Suppress("DEPRECATION")
@ -288,7 +293,9 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
}
override fun onError(s: String) {
launch {
toast(s)
}
}
}

@ -30,10 +30,10 @@ inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? {
}.value
}
class ParameterizedTypeImpl(val clz: Class<*>) : ParameterizedType {
class ParameterizedTypeImpl(private val clazz: Class<*>) : ParameterizedType {
override fun getRawType(): Type = List::class.java
override fun getOwnerType(): Type? = null
override fun getActualTypeArguments(): Array<Type> = arrayOf(clz)
override fun getActualTypeArguments(): Array<Type> = arrayOf(clazz)
}

Loading…
Cancel
Save