From 503d03f609fbef8305eda048e6667a9c48acbf8b Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 29 Mar 2022 15:25:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/service/AudioPlayService.kt | 113 ++++++++++-------- .../app/service/BaseReadAloudService.kt | 111 +++++++++-------- 2 files changed, 119 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 4318f368f..88b012f38 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -445,64 +445,71 @@ class AudioPlayService : BaseService(), * 更新通知 */ private fun upNotification() { - var nTitle: String = when { - pause -> getString(R.string.audio_pause) - timeMinute in 1..60 -> getString( - R.string.playing_timer, - timeMinute - ) - else -> getString(R.string.audio_play_t) - } - nTitle += ": $title" - var nSubtitle = subtitle - if (subtitle.isEmpty()) { - nSubtitle = getString(R.string.audio_play_s) - } - val builder = NotificationCompat.Builder(this, AppConst.channelIdReadAloud) - .setSmallIcon(R.drawable.ic_volume_up) - .setOngoing(true) - .setContentTitle(nTitle) - .setContentText(nSubtitle) - .setContentIntent( - activityPendingIntent("activity") - ) - kotlin.runCatching { - ImageLoader.loadBitmap(this, AudioPlay.book?.getDisplayCover()).submit().get() - }.getOrElse { - BitmapFactory.decodeResource(resources, R.drawable.icon_read_book) - }.let { - builder.setLargeIcon(it) - } - if (pause) { + execute { + var nTitle: String = when { + pause -> getString(R.string.audio_pause) + timeMinute in 1..60 -> getString( + R.string.playing_timer, + timeMinute + ) + else -> getString(R.string.audio_play_t) + } + nTitle += ": $title" + var nSubtitle = subtitle + if (subtitle.isEmpty()) { + nSubtitle = getString(R.string.audio_play_s) + } + val builder = NotificationCompat + .Builder(this@AudioPlayService, AppConst.channelIdReadAloud) + .setSmallIcon(R.drawable.ic_volume_up) + .setOngoing(true) + .setContentTitle(nTitle) + .setContentText(nSubtitle) + .setContentIntent( + activityPendingIntent("activity") + ) + kotlin.runCatching { + ImageLoader + .loadBitmap(this@AudioPlayService, AudioPlay.book?.getDisplayCover()) + .submit() + .get() + }.getOrElse { + BitmapFactory.decodeResource(resources, R.drawable.icon_read_book) + }.let { + builder.setLargeIcon(it) + } + if (pause) { + builder.addAction( + R.drawable.ic_play_24dp, + getString(R.string.resume), + servicePendingIntent(IntentAction.resume) + ) + } else { + builder.addAction( + R.drawable.ic_pause_24dp, + getString(R.string.pause), + servicePendingIntent(IntentAction.pause) + ) + } builder.addAction( - R.drawable.ic_play_24dp, - getString(R.string.resume), - servicePendingIntent(IntentAction.resume) + R.drawable.ic_stop_black_24dp, + getString(R.string.stop), + servicePendingIntent(IntentAction.stop) ) - } else { builder.addAction( - R.drawable.ic_pause_24dp, - getString(R.string.pause), - servicePendingIntent(IntentAction.pause) + R.drawable.ic_time_add_24dp, + getString(R.string.set_timer), + servicePendingIntent(IntentAction.addTimer) + ) + builder.setStyle( + androidx.media.app.NotificationCompat.MediaStyle() + .setShowActionsInCompactView(0, 1, 2) ) + builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + builder + }.onSuccess { + startForeground(AppConst.notificationIdAudio, it.build()) } - builder.addAction( - R.drawable.ic_stop_black_24dp, - getString(R.string.stop), - servicePendingIntent(IntentAction.stop) - ) - builder.addAction( - R.drawable.ic_time_add_24dp, - getString(R.string.set_timer), - servicePendingIntent(IntentAction.addTimer) - ) - builder.setStyle( - androidx.media.app.NotificationCompat.MediaStyle() - .setShowActionsInCompactView(0, 1, 2) - ) - builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - val notification = builder.build() - startForeground(AppConst.notificationIdAudio, notification) } /** diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 4674faea8..3f96a338a 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -301,63 +301,70 @@ abstract class BaseReadAloudService : BaseService(), * 更新通知 */ private fun upNotification() { - 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, AppConst.channelIdReadAloud) - .setSmallIcon(R.drawable.ic_volume_up) - .setOngoing(true) - .setContentTitle(nTitle) - .setContentText(nSubtitle) - .setContentIntent( - activityPendingIntent("activity") - ) - kotlin.runCatching { - ImageLoader.loadBitmap(this, ReadBook.book?.getDisplayCover()).submit().get() - }.getOrElse { - BitmapFactory.decodeResource(resources, R.drawable.icon_read_book) - }.let { - builder.setLargeIcon(it) - } - if (pause) { + execute { + 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) + .setOngoing(true) + .setContentTitle(nTitle) + .setContentText(nSubtitle) + .setContentIntent( + activityPendingIntent("activity") + ) + kotlin.runCatching { + ImageLoader + .loadBitmap(this@BaseReadAloudService, ReadBook.book?.getDisplayCover()) + .submit() + .get() + }.getOrElse { + BitmapFactory.decodeResource(resources, R.drawable.icon_read_book) + }.let { + builder.setLargeIcon(it) + } + 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_play_24dp, - getString(R.string.resume), - aloudServicePendingIntent(IntentAction.resume) + R.drawable.ic_stop_black_24dp, + getString(R.string.stop), + aloudServicePendingIntent(IntentAction.stop) ) - } else { builder.addAction( - R.drawable.ic_pause_24dp, - getString(R.string.pause), - aloudServicePendingIntent(IntentAction.pause) + 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 { + startForeground(AppConst.notificationIdRead, it.build()) } - 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) - val notification = builder.build() - startForeground(AppConst.notificationIdRead, notification) } abstract fun aloudServicePendingIntent(actionStr: String): PendingIntent?