pull/32/head
kunfei 5 years ago
parent f5ad6aba7f
commit 9584a97e8d
  1. 13
      app/src/main/java/io/legado/app/constant/Action.kt
  2. 1
      app/src/main/java/io/legado/app/constant/Status.kt
  3. 27
      app/src/main/java/io/legado/app/service/ReadAloudService.kt
  4. 9
      app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt

@ -0,0 +1,13 @@
package io.legado.app.constant
object Action {
const val play = "play"
const val stop = "stop"
const val resume = "resume"
const val pause = "pause"
const val setTimer = "setTimer"
const val prevParagraph = "prevParagraph"
const val nextParagraph = "nextParagraph"
const val upTtsSpeechRate = "upTtsSpeechRate"
}

@ -3,6 +3,5 @@ package io.legado.app.constant
object Status {
const val STOP = 0
const val PLAY = 1
const val NEXT = 2
const val PAUSE = 3
}

@ -11,6 +11,7 @@ import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.Action
import io.legado.app.constant.Bus
import io.legado.app.constant.Status
import io.legado.app.help.IntentDataHelp
@ -42,7 +43,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
play: Boolean = true
) {
val readAloudIntent = Intent(context, ReadAloudService::class.java)
readAloudIntent.action = "play"
readAloudIntent.action = Action.play
readAloudIntent.putExtra("title", title)
readAloudIntent.putExtra("subtitle", subtitle)
readAloudIntent.putExtra("pageIndex", pageIndex)
@ -54,7 +55,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
fun pause(context: Context) {
if (isRun) {
val intent = Intent(context, ReadAloudService::class.java)
intent.action = "pause"
intent.action = Action.pause
context.startService(intent)
}
}
@ -62,7 +63,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
fun resume(context: Context) {
if (isRun) {
val intent = Intent(context, ReadAloudService::class.java)
intent.action = "resume"
intent.action = Action.resume
context.startService(intent)
}
}
@ -70,7 +71,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
fun stop(context: Context) {
if (isRun) {
val intent = Intent(context, ReadAloudService::class.java)
intent.action = "stop"
intent.action = Action.stop
context.startService(intent)
}
}
@ -78,7 +79,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
fun prevParagraph(context: Context) {
if (isRun) {
val intent = Intent(context, ReadAloudService::class.java)
intent.action = "prevParagraph"
intent.action = Action.prevParagraph
context.startService(intent)
}
}
@ -86,7 +87,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
fun nextParagraph(context: Context) {
if (isRun) {
val intent = Intent(context, ReadAloudService::class.java)
intent.action = "nextParagraph"
intent.action = Action.nextParagraph
context.startService(intent)
}
}
@ -94,7 +95,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
fun upTtsSpeechRate(context: Context) {
if (isRun) {
val intent = Intent(context, ReadAloudService::class.java)
intent.action = "upTtsSpeechRate"
intent.action = Action.upTtsSpeechRate
context.startService(intent)
}
}
@ -148,7 +149,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
intent?.action?.let { action ->
when (action) {
"play" -> {
Action.play -> {
title = intent.getStringExtra("title") ?: ""
subtitle = intent.getStringExtra("subtitle") ?: ""
pageIndex = intent.getIntExtra("pageIndex", 0)
@ -157,11 +158,11 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage
intent.getBooleanExtra("play", true)
)
}
"pause" -> pauseReadAloud(true)
"resume" -> resumeReadAloud()
"upTtsSpeechRate" -> upSpeechRate(true)
"prevParagraph" -> prevP()
"nextParagraph" -> nextP()
Action.pause -> pauseReadAloud(true)
Action.resume -> resumeReadAloud()
Action.upTtsSpeechRate -> upSpeechRate(true)
Action.prevParagraph -> prevP()
Action.nextParagraph -> nextP()
else -> stopSelf()
}
}

@ -3,6 +3,7 @@ package io.legado.app.service.notification
import android.graphics.BitmapFactory
import androidx.core.app.NotificationCompat
import io.legado.app.R
import io.legado.app.constant.Action
import io.legado.app.constant.AppConst
import io.legado.app.help.PendingIntentHelp
import io.legado.app.service.ReadAloudService
@ -33,24 +34,24 @@ object ReadAloudNotification {
builder.addAction(
R.drawable.ic_play_24dp,
service.getString(R.string.resume),
PendingIntentHelp.aloudServicePendingIntent(service, "resume")
PendingIntentHelp.aloudServicePendingIntent(service, Action.resume)
)
} else {
builder.addAction(
R.drawable.ic_pause_24dp,
service.getString(R.string.pause),
PendingIntentHelp.aloudServicePendingIntent(service, "pause")
PendingIntentHelp.aloudServicePendingIntent(service, Action.pause)
)
}
builder.addAction(
R.drawable.ic_stop_black_24dp,
service.getString(R.string.stop),
PendingIntentHelp.aloudServicePendingIntent(service, "stop")
PendingIntentHelp.aloudServicePendingIntent(service, Action.stop)
)
builder.addAction(
R.drawable.ic_time_add_24dp,
service.getString(R.string.set_timer),
PendingIntentHelp.aloudServicePendingIntent(service, "setTimer")
PendingIntentHelp.aloudServicePendingIntent(service, Action.setTimer)
)
builder.setStyle(
androidx.media.app.NotificationCompat.MediaStyle()

Loading…
Cancel
Save