pull/32/head
kunfei 6 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 { object Status {
const val STOP = 0 const val STOP = 0
const val PLAY = 1 const val PLAY = 1
const val NEXT = 2
const val PAUSE = 3 const val PAUSE = 3
} }

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

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

Loading…
Cancel
Save