diff --git a/app/src/main/java/com/github/libretube/extensions/Player.kt b/app/src/main/java/com/github/libretube/extensions/Player.kt index ff3df320c..213c467a4 100644 --- a/app/src/main/java/com/github/libretube/extensions/Player.kt +++ b/app/src/main/java/com/github/libretube/extensions/Player.kt @@ -14,6 +14,7 @@ fun Player.togglePlayPauseState() { } !isPlaying -> play() + else -> pause() } } diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index 81a32a016..65c456919 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -27,6 +27,7 @@ import com.github.libretube.constants.IntentData import com.github.libretube.enums.NotificationId import com.github.libretube.extensions.seekBy import com.github.libretube.extensions.toMediaMetadataCompat +import com.github.libretube.extensions.togglePlayPauseState import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.PlayerHelper @@ -66,6 +67,7 @@ class NowPlayingNotification( private fun loadCurrentLargeIcon() { if (DataSaverMode.isEnabled(context)) return + if (notificationBitmap == null) { enqueueThumbnailRequest { createOrUpdateNotification() @@ -84,12 +86,14 @@ class NowPlayingNotification( addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) } } + return PendingIntentCompat .getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false) } private fun createIntent(action: String): PendingIntent? { val intent = Intent(action).setPackage(context.packageName) + return PendingIntentCompat .getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT, false) } @@ -245,15 +249,13 @@ class NowPlayingNotification( private fun updateSessionPlaybackState(isPlaying: Boolean? = null, isLoading: Boolean? = null) { val loading = isLoading == true || (isPlaying == false && player.isLoading) - val newPlaybackState = if (loading) { - createPlaybackState(PlaybackStateCompat.STATE_BUFFERING) - } else if (isPlaying ?: player.isPlaying) { - createPlaybackState(PlaybackStateCompat.STATE_PLAYING) - } else { - createPlaybackState(PlaybackStateCompat.STATE_PAUSED) + val newPlaybackState = when { + loading -> PlaybackStateCompat.STATE_BUFFERING + isPlaying ?: player.isPlaying -> PlaybackStateCompat.STATE_PLAYING + else -> PlaybackStateCompat.STATE_PAUSED } - mediaSession.setPlaybackState(newPlaybackState) + mediaSession.setPlaybackState(createPlaybackState(newPlaybackState)) } private fun createPlaybackState(@PlaybackStateCompat.State state: Int): PlaybackStateCompat { @@ -277,19 +279,15 @@ class NowPlayingNotification( private fun handlePlayerAction(action: String) { when (action) { NEXT -> { - if (PlayingQueue.hasNext()) { - PlayingQueue.onQueueItemSelected( - PlayingQueue.currentIndex() + 1 - ) - } + if (!PlayingQueue.hasNext()) return + + PlayingQueue.onQueueItemSelected(PlayingQueue.currentIndex() + 1) } PREV -> { - if (PlayingQueue.hasPrev()) { - PlayingQueue.onQueueItemSelected( - PlayingQueue.currentIndex() - 1 - ) - } + if (!PlayingQueue.hasPrev()) return + + PlayingQueue.onQueueItemSelected(PlayingQueue.currentIndex() - 1) } REWIND -> { @@ -301,10 +299,7 @@ class NowPlayingNotification( } PLAY_PAUSE -> { - if (player.playerError != null) player.prepare() - if (player.isPlaying) player.pause() - else if (player.playbackState == Player.STATE_ENDED) player.seekTo(0) - else player.play() + player.togglePlayPauseState() } STOP -> {