优化
This commit is contained in:
@@ -4,6 +4,7 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import io.legado.app.constant.AppLog
|
||||||
import io.legado.app.constant.EventBus
|
import io.legado.app.constant.EventBus
|
||||||
import io.legado.app.constant.IntentAction
|
import io.legado.app.constant.IntentAction
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
@@ -14,6 +15,7 @@ import io.legado.app.service.HttpReadAloudService
|
|||||||
import io.legado.app.service.TTSReadAloudService
|
import io.legado.app.service.TTSReadAloudService
|
||||||
import io.legado.app.utils.StringUtils
|
import io.legado.app.utils.StringUtils
|
||||||
import io.legado.app.utils.postEvent
|
import io.legado.app.utils.postEvent
|
||||||
|
import io.legado.app.utils.toastOnUi
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
|
|
||||||
object ReadAloud {
|
object ReadAloud {
|
||||||
@@ -51,7 +53,13 @@ object ReadAloud {
|
|||||||
intent.putExtra("play", play)
|
intent.putExtra("play", play)
|
||||||
intent.putExtra("pageIndex", pageIndex)
|
intent.putExtra("pageIndex", pageIndex)
|
||||||
intent.putExtra("startPos", startPos)
|
intent.putExtra("startPos", startPos)
|
||||||
ContextCompat.startForegroundService(context, intent)
|
try {
|
||||||
|
ContextCompat.startForegroundService(context, intent)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
val msg = "启动朗读服务出错\n${e.localizedMessage}"
|
||||||
|
AppLog.put(msg, e)
|
||||||
|
context.toastOnUi(msg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun playByEventBus(
|
fun playByEventBus(
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import kotlinx.coroutines.Dispatchers.IO
|
|||||||
import kotlinx.coroutines.Dispatchers.Main
|
import kotlinx.coroutines.Dispatchers.Main
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import splitties.systemservices.audioManager
|
import splitties.systemservices.audioManager
|
||||||
|
import splitties.systemservices.notificationManager
|
||||||
import splitties.systemservices.powerManager
|
import splitties.systemservices.powerManager
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,7 +120,7 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
.get()
|
.get()
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
cover = it
|
cover = it
|
||||||
upNotification()
|
upReadAloudNotification()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,7 +214,7 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
isRun = true
|
isRun = true
|
||||||
pause = false
|
pause = false
|
||||||
needResumeOnAudioFocusGain = false
|
needResumeOnAudioFocusGain = false
|
||||||
upNotification()
|
upReadAloudNotification()
|
||||||
postEvent(EventBus.ALOUD_STATE, Status.PLAY)
|
postEvent(EventBus.ALOUD_STATE, Status.PLAY)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +227,7 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
if (abandonFocus) {
|
if (abandonFocus) {
|
||||||
abandonFocus()
|
abandonFocus()
|
||||||
}
|
}
|
||||||
upNotification()
|
upReadAloudNotification()
|
||||||
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
|
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
|
||||||
postEvent(EventBus.ALOUD_STATE, Status.PAUSE)
|
postEvent(EventBus.ALOUD_STATE, Status.PAUSE)
|
||||||
ReadBook.uploadProgress()
|
ReadBook.uploadProgress()
|
||||||
@@ -237,7 +238,7 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
@CallSuper
|
@CallSuper
|
||||||
open fun resumeReadAloud() {
|
open fun resumeReadAloud() {
|
||||||
pause = false
|
pause = false
|
||||||
upNotification()
|
upReadAloudNotification()
|
||||||
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
|
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
|
||||||
postEvent(EventBus.ALOUD_STATE, Status.PLAY)
|
postEvent(EventBus.ALOUD_STATE, Status.PLAY)
|
||||||
}
|
}
|
||||||
@@ -309,7 +310,7 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
@Synchronized
|
@Synchronized
|
||||||
private fun doDs() {
|
private fun doDs() {
|
||||||
postEvent(EventBus.READ_ALOUD_DS, timeMinute)
|
postEvent(EventBus.READ_ALOUD_DS, timeMinute)
|
||||||
upNotification()
|
upReadAloudNotification()
|
||||||
dsJob?.cancel()
|
dsJob?.cancel()
|
||||||
dsJob = lifecycleScope.launch {
|
dsJob = lifecycleScope.launch {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
@@ -323,7 +324,7 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
postEvent(EventBus.READ_ALOUD_DS, timeMinute)
|
postEvent(EventBus.READ_ALOUD_DS, timeMinute)
|
||||||
upNotification()
|
upReadAloudNotification()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -425,67 +426,80 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun upReadAloudNotification() {
|
||||||
|
execute {
|
||||||
|
createNotification()
|
||||||
|
}.onSuccess {
|
||||||
|
notificationManager.notify(NotificationId.ReadAloudService, it.build())
|
||||||
|
}.onError {
|
||||||
|
AppLog.put("创建朗读通知出错,${it.localizedMessage}", it, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createNotification(): NotificationCompat.Builder {
|
||||||
|
var nTitle: String = when {
|
||||||
|
pause -> getString(R.string.read_aloud_pause)
|
||||||
|
timeMinute > 0 -> getString(
|
||||||
|
R.string.read_aloud_timer,
|
||||||
|
timeMinute
|
||||||
|
)
|
||||||
|
|
||||||
|
else -> getString(R.string.read_aloud_t)
|
||||||
|
}
|
||||||
|
nTitle += ": ${ReadBook.book?.name}"
|
||||||
|
var nSubtitle = ReadBook.curTextChapter?.title
|
||||||
|
if (nSubtitle.isNullOrBlank())
|
||||||
|
nSubtitle = getString(R.string.read_aloud_s)
|
||||||
|
val builder = NotificationCompat
|
||||||
|
.Builder(this@BaseReadAloudService, AppConst.channelIdReadAloud)
|
||||||
|
.setSmallIcon(R.drawable.ic_volume_up)
|
||||||
|
.setSubText(getString(R.string.read_aloud))
|
||||||
|
.setOngoing(true)
|
||||||
|
.setContentTitle(nTitle)
|
||||||
|
.setContentText(nSubtitle)
|
||||||
|
.setContentIntent(
|
||||||
|
activityPendingIntent<ReadBookActivity>("activity")
|
||||||
|
)
|
||||||
|
.setVibrate(null)
|
||||||
|
.setSound(null)
|
||||||
|
.setLights(0, 0, 0)
|
||||||
|
builder.setLargeIcon(cover)
|
||||||
|
if (pause) {
|
||||||
|
builder.addAction(
|
||||||
|
R.drawable.ic_play_24dp,
|
||||||
|
getString(R.string.resume),
|
||||||
|
aloudServicePendingIntent(IntentAction.resume)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
builder.addAction(
|
||||||
|
R.drawable.ic_pause_24dp,
|
||||||
|
getString(R.string.pause),
|
||||||
|
aloudServicePendingIntent(IntentAction.pause)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
builder.addAction(
|
||||||
|
R.drawable.ic_stop_black_24dp,
|
||||||
|
getString(R.string.stop),
|
||||||
|
aloudServicePendingIntent(IntentAction.stop)
|
||||||
|
)
|
||||||
|
builder.addAction(
|
||||||
|
R.drawable.ic_time_add_24dp,
|
||||||
|
getString(R.string.set_timer),
|
||||||
|
aloudServicePendingIntent(IntentAction.addTimer)
|
||||||
|
)
|
||||||
|
builder.setStyle(
|
||||||
|
androidx.media.app.NotificationCompat.MediaStyle()
|
||||||
|
.setShowActionsInCompactView(0, 1, 2)
|
||||||
|
)
|
||||||
|
return builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新通知
|
* 更新通知
|
||||||
*/
|
*/
|
||||||
override fun upNotification() {
|
override fun upNotification() {
|
||||||
execute {
|
execute {
|
||||||
var nTitle: String = when {
|
createNotification()
|
||||||
pause -> getString(R.string.read_aloud_pause)
|
|
||||||
timeMinute > 0 -> getString(
|
|
||||||
R.string.read_aloud_timer,
|
|
||||||
timeMinute
|
|
||||||
)
|
|
||||||
|
|
||||||
else -> getString(R.string.read_aloud_t)
|
|
||||||
}
|
|
||||||
nTitle += ": ${ReadBook.book?.name}"
|
|
||||||
var nSubtitle = ReadBook.curTextChapter?.title
|
|
||||||
if (nSubtitle.isNullOrBlank())
|
|
||||||
nSubtitle = getString(R.string.read_aloud_s)
|
|
||||||
val builder = NotificationCompat
|
|
||||||
.Builder(this@BaseReadAloudService, AppConst.channelIdReadAloud)
|
|
||||||
.setSmallIcon(R.drawable.ic_volume_up)
|
|
||||||
.setSubText(getString(R.string.read_aloud))
|
|
||||||
.setOngoing(true)
|
|
||||||
.setContentTitle(nTitle)
|
|
||||||
.setContentText(nSubtitle)
|
|
||||||
.setContentIntent(
|
|
||||||
activityPendingIntent<ReadBookActivity>("activity")
|
|
||||||
)
|
|
||||||
.setVibrate(null)
|
|
||||||
.setSound(null)
|
|
||||||
.setLights(0, 0, 0)
|
|
||||||
builder.setLargeIcon(cover)
|
|
||||||
if (pause) {
|
|
||||||
builder.addAction(
|
|
||||||
R.drawable.ic_play_24dp,
|
|
||||||
getString(R.string.resume),
|
|
||||||
aloudServicePendingIntent(IntentAction.resume)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
builder.addAction(
|
|
||||||
R.drawable.ic_pause_24dp,
|
|
||||||
getString(R.string.pause),
|
|
||||||
aloudServicePendingIntent(IntentAction.pause)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
builder.addAction(
|
|
||||||
R.drawable.ic_stop_black_24dp,
|
|
||||||
getString(R.string.stop),
|
|
||||||
aloudServicePendingIntent(IntentAction.stop)
|
|
||||||
)
|
|
||||||
builder.addAction(
|
|
||||||
R.drawable.ic_time_add_24dp,
|
|
||||||
getString(R.string.set_timer),
|
|
||||||
aloudServicePendingIntent(IntentAction.addTimer)
|
|
||||||
)
|
|
||||||
builder.setStyle(
|
|
||||||
androidx.media.app.NotificationCompat.MediaStyle()
|
|
||||||
.setShowActionsInCompactView(0, 1, 2)
|
|
||||||
)
|
|
||||||
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
||||||
builder
|
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
startForeground(NotificationId.ReadAloudService, it.build())
|
startForeground(NotificationId.ReadAloudService, it.build())
|
||||||
}.onError {
|
}.onError {
|
||||||
|
|||||||
Reference in New Issue
Block a user