Use media button preferences in session demo app

PiperOrigin-RevId: 689738802
This commit is contained in:
tonihei 2024-10-25 04:30:33 -07:00 committed by Copybara-Service
parent aeb9d12a16
commit 0fdc930444

View file

@ -42,7 +42,7 @@ open class DemoMediaLibrarySessionCallback(context: Context) :
} }
@OptIn(UnstableApi::class) // TODO: b/328238954 - Remove once new CommandButton icons are stable. @OptIn(UnstableApi::class) // TODO: b/328238954 - Remove once new CommandButton icons are stable.
private val customLayoutCommandButtons: List<CommandButton> = private val commandButtons: List<CommandButton> =
listOf( listOf(
CommandButton.Builder(CommandButton.ICON_SHUFFLE_OFF) CommandButton.Builder(CommandButton.ICON_SHUFFLE_OFF)
.setDisplayName(context.getString(R.string.exo_controls_shuffle_on_description)) .setDisplayName(context.getString(R.string.exo_controls_shuffle_on_description))
@ -59,7 +59,7 @@ open class DemoMediaLibrarySessionCallback(context: Context) :
MediaSession.ConnectionResult.DEFAULT_SESSION_AND_LIBRARY_COMMANDS.buildUpon() MediaSession.ConnectionResult.DEFAULT_SESSION_AND_LIBRARY_COMMANDS.buildUpon()
.also { builder -> .also { builder ->
// Put all custom session commands in the list that may be used by the notification. // Put all custom session commands in the list that may be used by the notification.
customLayoutCommandButtons.forEach { commandButton -> commandButtons.forEach { commandButton ->
commandButton.sessionCommand?.let { builder.add(it) } commandButton.sessionCommand?.let { builder.add(it) }
} }
} }
@ -78,13 +78,13 @@ open class DemoMediaLibrarySessionCallback(context: Context) :
session.isAutoCompanionController(controller) session.isAutoCompanionController(controller)
) { ) {
// Select the button to display. // Select the button to display.
val customLayout = customLayoutCommandButtons[if (session.player.shuffleModeEnabled) 1 else 0] val customButton = commandButtons[if (session.player.shuffleModeEnabled) 1 else 0]
return MediaSession.ConnectionResult.AcceptedResultBuilder(session) return MediaSession.ConnectionResult.AcceptedResultBuilder(session)
.setAvailableSessionCommands(mediaNotificationSessionCommands) .setAvailableSessionCommands(mediaNotificationSessionCommands)
.setCustomLayout(ImmutableList.of(customLayout)) .setMediaButtonPreferences(ImmutableList.of(customButton))
.build() .build()
} }
// Default commands without custom layout for common controllers. // Default commands without media button preferences for common controllers.
return MediaSession.ConnectionResult.AcceptedResultBuilder(session).build() return MediaSession.ConnectionResult.AcceptedResultBuilder(session).build()
} }
@ -98,19 +98,19 @@ open class DemoMediaLibrarySessionCallback(context: Context) :
if (CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON == customCommand.customAction) { if (CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_ON == customCommand.customAction) {
// Enable shuffling. // Enable shuffling.
session.player.shuffleModeEnabled = true session.player.shuffleModeEnabled = true
// Change the custom layout to contain the `Disable shuffling` command. // Change the media button preferences to contain the `Disable shuffling` button.
session.setCustomLayout( session.setMediaButtonPreferences(
session.mediaNotificationControllerInfo!!, session.mediaNotificationControllerInfo!!,
ImmutableList.of(customLayoutCommandButtons[1]), ImmutableList.of(commandButtons[1]),
) )
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS)) return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
} else if (CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF == customCommand.customAction) { } else if (CUSTOM_COMMAND_TOGGLE_SHUFFLE_MODE_OFF == customCommand.customAction) {
// Disable shuffling. // Disable shuffling.
session.player.shuffleModeEnabled = false session.player.shuffleModeEnabled = false
// Change the custom layout to contain the `Enable shuffling` command. // Change the media button preferences to contain the `Enable shuffling` button.
session.setCustomLayout( session.setMediaButtonPreferences(
session.mediaNotificationControllerInfo!!, session.mediaNotificationControllerInfo!!,
ImmutableList.of(customLayoutCommandButtons[0]), ImmutableList.of(commandButtons[0]),
) )
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS)) return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
} }