diff --git a/app/src/main/java/org/y20k/transistor/Keys.kt b/app/src/main/java/org/y20k/transistor/Keys.kt index 890b5eb7..49babd0e 100644 --- a/app/src/main/java/org/y20k/transistor/Keys.kt +++ b/app/src/main/java/org/y20k/transistor/Keys.kt @@ -71,6 +71,7 @@ object Keys { const val CMD_START_SLEEP_TIMER: String = "START_SLEEP_TIMER" const val CMD_CANCEL_SLEEP_TIMER: String = "CANCEL_SLEEP_TIMER" const val CMD_PLAY_STREAM: String = "PLAY_STREAM" + const val CMD_DISMISS_NOTIFICATION: String = "DISMISS_NOTIFICATION" // preferences const val PREF_RADIO_BROWSER_API: String = "RADIO_BROWSER_API" diff --git a/app/src/main/java/org/y20k/transistor/helpers/NotificationHelper.kt b/app/src/main/java/org/y20k/transistor/helpers/NotificationHelper.kt index 7ec28a6b..7ae2d4b4 100644 --- a/app/src/main/java/org/y20k/transistor/helpers/NotificationHelper.kt +++ b/app/src/main/java/org/y20k/transistor/helpers/NotificationHelper.kt @@ -47,11 +47,11 @@ class NotificationHelper(private val context: Context, sessionToken: MediaSessio private val serviceJob = SupervisorJob() private val serviceScope = CoroutineScope(Main + serviceJob) private val notificationManager: PlayerNotificationManager + private val mediaController: MediaControllerCompat = MediaControllerCompat(context, sessionToken) /* Constructor */ init { - val mediaController = MediaControllerCompat(context, sessionToken) notificationManager = PlayerNotificationManager.createWithNotificationChannel( context, Keys.NOW_PLAYING_NOTIFICATION_CHANNEL_ID, @@ -65,7 +65,7 @@ class NotificationHelper(private val context: Context, sessionToken: MediaSessio setMediaSessionToken(sessionToken) setSmallIcon(R.drawable.ic_notification_app_icon_white_24dp) setUsePlayPauseActions(true) - setControlDispatcher(DefaultControlDispatcher(0L, 0L)) // hide fastforward and reweind + setControlDispatcher(Dispatcher()) setUseStopAction(true) // set true to display the dismiss button setUsePreviousAction(false) setUsePreviousActionInCompactView(false) @@ -94,6 +94,21 @@ class NotificationHelper(private val context: Context, sessionToken: MediaSessio } + /* + * Inner class: Intercept stop button tap + */ + private inner class Dispatcher: DefaultControlDispatcher(0L, 0L /* hide fast forward and rewind */) { + override fun dispatchStop(player: Player, reset: Boolean): Boolean { + // Default implementation see: https://github.com/google/ExoPlayer/blob/b1000940eaec9e1202d9abf341a48a58b728053f/library/core/src/main/java/com/google/android/exoplayer2/DefaultControlDispatcher.java#L137 + mediaController.sendCommand(Keys.CMD_DISMISS_NOTIFICATION, null, null) + return true + } + } + /* + * End of inner class + */ + + /* * Inner class: Create content of notification from metaddata */ diff --git a/app/src/main/java/org/y20k/transistor/playback/PlayerService.kt b/app/src/main/java/org/y20k/transistor/playback/PlayerService.kt index 7554527b..ef3e0ec3 100644 --- a/app/src/main/java/org/y20k/transistor/playback/PlayerService.kt +++ b/app/src/main/java/org/y20k/transistor/playback/PlayerService.kt @@ -302,9 +302,8 @@ class PlayerService(): MediaBrowserServiceCompat() { // save collection state and player state collection = CollectionHelper.savePlaybackState(this, collection, station, playbackState) updatePlayerState(station, playbackState) - // update media session - mediaSession.setPlaybackState(createPlaybackState(playbackState, 0)) - mediaSession.isActive = playbackState != PlaybackStateCompat.STATE_STOPPED + // display notification (notification is hidden via CMD_DISMISS_NOTIFICATION) + if (player.isPlaying) notificationHelper.showNotificationForPlayer(player) } @@ -395,32 +394,6 @@ class PlayerService(): MediaBrowserServiceCompat() { } - /* Creates playback state - actions for playback state to be used in media session callback */ - private fun createPlaybackState(state: Int, position: Long): PlaybackStateCompat { - val skipActions: Long = PlaybackStateCompat.ACTION_FAST_FORWARD or PlaybackStateCompat.ACTION_REWIND or PlaybackStateCompat.ACTION_SKIP_TO_NEXT or PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS - when(state) { - PlaybackStateCompat.STATE_PLAYING -> { - return PlaybackStateCompat.Builder() - .setState(PlaybackStateCompat.STATE_PLAYING, position, 1f) - .setActions(PlaybackStateCompat.ACTION_STOP or PlaybackStateCompat.ACTION_PAUSE or PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID or skipActions) - .build() - } - PlaybackStateCompat.STATE_PAUSED -> { - return PlaybackStateCompat.Builder() - .setState(PlaybackStateCompat.STATE_PAUSED, position, 0f) - .setActions(PlaybackStateCompat.ACTION_PLAY) - .build() - } - else -> { - return PlaybackStateCompat.Builder() - .setState(PlaybackStateCompat.STATE_STOPPED, position, 0f) - .setActions(PlaybackStateCompat.ACTION_PLAY) - .build() - } - } - } - - /* Starts sleep timer / adds default duration to running sleeptimer */ private fun startSleepTimer() { // stop running timer @@ -740,7 +713,12 @@ class PlayerService(): MediaBrowserServiceCompat() { preparePlayer(true) return true } - + Keys.CMD_DISMISS_NOTIFICATION -> { + // pause playback and hide notification + player.pause() + notificationHelper.hideNotification() + return true + } else -> { return false }