From f5c2cf40a5479e47d3b7fca58b376d26ceca2562 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 16 Aug 2019 10:15:02 +0800 Subject: [PATCH] up --- .../io/legado/app/service/ReadAloudService.kt | 59 ++++++++++--------- .../notification/ReadAloudNotification.kt | 54 ++++++++--------- 2 files changed, 54 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/ReadAloudService.kt b/app/src/main/java/io/legado/app/service/ReadAloudService.kt index 1abfe74bf..ba8a124db 100644 --- a/app/src/main/java/io/legado/app/service/ReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/ReadAloudService.kt @@ -11,13 +11,10 @@ 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.Bus -import io.legado.app.constant.Status 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.utils.postEvent import io.legado.app.utils.toast import kotlinx.coroutines.launch import java.util.* @@ -66,15 +63,16 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage private var ttsIsSuccess: Boolean = false private lateinit var audioManager: AudioManager private lateinit var mFocusRequest: AudioFocusRequest - private var mediaSessionCompat: MediaSessionCompat? = null + var mediaSessionCompat: MediaSessionCompat? = null private var broadcastReceiver: BroadcastReceiver? = null private var speak: Boolean = true private var nowSpeak: Int = 0 private val contentList = arrayListOf() - private var pause = false - private var title: String = "" - private var subtitle: String = "" - private var timeMinute: Int = 0 + var pause = false + var title: String = "" + var subtitle: String = "" + private var readAloudNumber: Int = 0 + var timeMinute: Int = 0 override fun onCreate() { super.onCreate() @@ -87,13 +85,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage initMediaSession() initBroadcastReceiver() upMediaSessionPlaybackState() - ReadAloudNotification.upNotification( - this, - mediaSessionCompat, - pause, - title, - subtitle - ) + ReadAloudNotification.upNotification(this) } override fun onDestroy() { @@ -106,9 +98,9 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage intent?.action?.let { action -> when (action) { "play" -> { - title = intent.getStringExtra("title") - subtitle = intent.getStringExtra("subtitle") - + title = intent.getStringExtra("title") ?: "" + subtitle = intent.getStringExtra("subtitle") ?: "" + newReadAloud(intent.getStringExtra("body")) } "pause" -> { pauseReadAloud(true) @@ -134,6 +126,7 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage } else { textToSpeech?.setOnUtteranceProgressListener(TTSUtteranceListener()) ttsIsSuccess = true + playTTS() } } else { toast(R.string.tts_init_failed) @@ -141,6 +134,26 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage } } + private fun newReadAloud(body: String?) { + if (body.isNullOrEmpty()) { + stopSelf() + } else { + nowSpeak = 0 + readAloudNumber = 0 + contentList.clear() + contentList.addAll(body.split("\n")) + } + } + + private fun playTTS() { + if (contentList.size < 1 || !ttsIsSuccess) { + return + } + if (!pause && requestFocus()) { + ReadAloudNotification.upNotification(this) + } + } + /** * 初始化MediaSession */ @@ -183,16 +196,6 @@ class ReadAloudService : BaseService(), TextToSpeech.OnInitListener, AudioManage playTTS() } - private fun playTTS() { - if (contentList.size < 1) { - postEvent(Bus.ALOUD_STATE, Status.NEXT) - return - } - if (ttsIsSuccess && !speak && requestFocus()) { - - } - } - /** * @return 音频焦点 */ diff --git a/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt b/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt index 3fab8f239..5d980ad4f 100644 --- a/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt +++ b/app/src/main/java/io/legado/app/service/notification/ReadAloudNotification.kt @@ -1,72 +1,64 @@ package io.legado.app.service.notification -import android.app.Service import android.graphics.BitmapFactory -import android.support.v4.media.session.MediaSessionCompat import androidx.core.app.NotificationCompat import io.legado.app.R import io.legado.app.constant.AppConst import io.legado.app.help.PendingIntentHelp +import io.legado.app.service.ReadAloudService object ReadAloudNotification { /** * 更新通知 */ - fun upNotification( - context: Service, - mediaSessionCompat: MediaSessionCompat?, - pause: Boolean, - title: String, - subtitle: String, - timeMinute: Int = 0 - ) { + fun upNotification(service: ReadAloudService) { var nTitle: String = when { - pause -> context.getString(R.string.read_aloud_pause) - timeMinute in 1..60 -> context.getString(R.string.read_aloud_timer, timeMinute) - else -> context.getString(R.string.read_aloud_t) + service.pause -> service.getString(R.string.read_aloud_pause) + service.timeMinute in 1..60 -> service.getString(R.string.read_aloud_timer, service.timeMinute) + else -> service.getString(R.string.read_aloud_t) } - nTitle += ": $title" - var nSubtitle = subtitle - if (subtitle.isEmpty()) - nSubtitle = context.getString(R.string.read_aloud_s) - val builder = NotificationCompat.Builder(context, AppConst.channelIdReadAloud) + nTitle += ": ${service.title}" + var nSubtitle = service.subtitle + if (service.subtitle.isEmpty()) + nSubtitle = service.getString(R.string.read_aloud_s) + val builder = NotificationCompat.Builder(service, AppConst.channelIdReadAloud) .setSmallIcon(R.drawable.ic_volume_up) - .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.icon_read_book)) + .setLargeIcon(BitmapFactory.decodeResource(service.resources, R.drawable.icon_read_book)) .setOngoing(true) .setContentTitle(nTitle) .setContentText(nSubtitle) - .setContentIntent(PendingIntentHelp.readBookActivityPendingIntent(context)) - if (pause) { + .setContentIntent(PendingIntentHelp.readBookActivityPendingIntent(service)) + if (service.pause) { builder.addAction( R.drawable.ic_play_24dp, - context.getString(R.string.resume), - PendingIntentHelp.aloudServicePendingIntent(context, "resume") + service.getString(R.string.resume), + PendingIntentHelp.aloudServicePendingIntent(service, "resume") ) } else { builder.addAction( R.drawable.ic_pause_24dp, - context.getString(R.string.pause), - PendingIntentHelp.aloudServicePendingIntent(context, "pause") + service.getString(R.string.pause), + PendingIntentHelp.aloudServicePendingIntent(service, "pause") ) } builder.addAction( R.drawable.ic_stop_black_24dp, - context.getString(R.string.stop), - PendingIntentHelp.aloudServicePendingIntent(context, "stop") + service.getString(R.string.stop), + PendingIntentHelp.aloudServicePendingIntent(service, "stop") ) builder.addAction( R.drawable.ic_time_add_24dp, - context.getString(R.string.set_timer), - PendingIntentHelp.aloudServicePendingIntent(context, "setTimer") + service.getString(R.string.set_timer), + PendingIntentHelp.aloudServicePendingIntent(service, "setTimer") ) builder.setStyle( androidx.media.app.NotificationCompat.MediaStyle() - .setMediaSession(mediaSessionCompat?.sessionToken) + .setMediaSession(service.mediaSessionCompat?.sessionToken) .setShowActionsInCompactView(0, 1, 2) ) builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) val notification = builder.build() - context.startForeground(112201, notification) + service.startForeground(112201, notification) } } \ No newline at end of file