Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
customize Cancel button behavior (pause & remove notification)
Browse files Browse the repository at this point in the history
  • Loading branch information
y20k committed Feb 21, 2021
1 parent 375515e commit 1b6d63a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/org/y20k/transistor/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
*/
Expand Down
38 changes: 8 additions & 30 deletions app/src/main/java/org/y20k/transistor/playback/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1b6d63a

Please sign in to comment.