Skip to content

Commit

Permalink
Add the MediaSession as an argument to getMediaButtons()
Browse files Browse the repository at this point in the history
Issue: #216
#minor-release
PiperOrigin-RevId: 503406474
  • Loading branch information
marcbaechinger authored and christosts committed Jan 23, 2023
1 parent 4b49048 commit e690802
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Release notes
`SessionToken` ([#171](https://github.com/androidx/media/issues/171)).
* Use `onMediaMetadataChanged` to trigger updates of the platform media
session ([#219](https://github.com/androidx/media/issues/219)).
* Add the media session as an argument of `getMediaButtons()` of the
`DefaultMediaNotificationProvider` and use immutable lists for clarity
([#216](https://github.com/androidx/media/issues/216)).
* Metadata:
* Parse multiple null-separated values from ID3 frames, as permitted by
ID3 v2.4.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -310,6 +309,7 @@ public final MediaNotification createNotification(
addNotificationActions(
mediaSession,
getMediaButtons(
mediaSession,
player.getAvailableCommands(),
customLayout,
/* showPauseButton= */ player.getPlayWhenReady()
Expand Down Expand Up @@ -418,17 +418,21 @@ public final void setSmallIcon(@DrawableRes int smallIconResourceId) {
* need the custom command set in {@link MediaSession.Callback#onPostConnect(MediaSession,
* MediaSession.ControllerInfo)} also.
*
* @param session The media session.
* @param playerCommands The available player commands.
* @param customLayout The {@linkplain MediaSession#setCustomLayout(List) custom layout of
* commands}.
* @param showPauseButton Whether the notification should show a pause button (e.g., because the
* player is currently playing content), otherwise show a play button to start playback.
* @return The ordered list of command buttons to be placed on the notification.
*/
protected List<CommandButton> getMediaButtons(
Player.Commands playerCommands, List<CommandButton> customLayout, boolean showPauseButton) {
protected ImmutableList<CommandButton> getMediaButtons(
MediaSession session,
Player.Commands playerCommands,
ImmutableList<CommandButton> customLayout,
boolean showPauseButton) {
// Skip to previous action.
List<CommandButton> commandButtons = new ArrayList<>();
ImmutableList.Builder<CommandButton> commandButtons = new ImmutableList.Builder<>();
if (playerCommands.containsAny(COMMAND_SEEK_TO_PREVIOUS, COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM)) {
Bundle commandButtonExtras = new Bundle();
commandButtonExtras.putInt(COMMAND_KEY_COMPACT_VIEW_INDEX, INDEX_UNSET);
Expand Down Expand Up @@ -477,14 +481,14 @@ protected List<CommandButton> getMediaButtons(
commandButtons.add(button);
}
}
return commandButtons;
return commandButtons.build();
}

/**
* Adds the media buttons to the notification builder for the given action factory.
*
* <p>The list of {@code mediaButtons} is the list resulting from {@link #getMediaButtons(
* Player.Commands, List, boolean)}.
* MediaSession, Player.Commands, ImmutableList, boolean)}.
*
* <p>Override this method to customize how the media buttons {@linkplain
* NotificationCompat.Builder#addAction(NotificationCompat.Action) are added} to the notification
Expand All @@ -505,7 +509,7 @@ protected List<CommandButton> getMediaButtons(
*/
protected int[] addNotificationActions(
MediaSession mediaSession,
List<CommandButton> mediaButtons,
ImmutableList<CommandButton> mediaButtons,
NotificationCompat.Builder builder,
MediaNotification.ActionFactory actionFactory) {
int[] compactViewIndices = new int[3];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,20 @@ public void getMediaButtons_playWhenReadyTrueOrFalse_correctPlayPauseResources()
new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext())
.build();
Commands commands = new Commands.Builder().addAllCommands().build();
MediaSession mockMediaSession = mock(MediaSession.class);

List<CommandButton> mediaButtonsWhenPlaying =
defaultMediaNotificationProvider.getMediaButtons(
commands, /* customLayout= */ ImmutableList.of(), /* showPauseButton= */ true);
mockMediaSession,
commands,
/* customLayout= */ ImmutableList.of(),
/* showPauseButton= */ true);
List<CommandButton> mediaButtonWhenPaused =
defaultMediaNotificationProvider.getMediaButtons(
commands, /* customLayout= */ ImmutableList.of(), /* showPauseButton= */ false);
mockMediaSession,
commands,
/* customLayout= */ ImmutableList.of(),
/* showPauseButton= */ false);

assertThat(mediaButtonsWhenPlaying).hasSize(3);
assertThat(mediaButtonsWhenPlaying.get(1).playerCommand).isEqualTo(Player.COMMAND_PLAY_PAUSE);
Expand All @@ -93,6 +100,7 @@ public void getMediaButtons_allCommandsAvailable_createsPauseSkipNextSkipPreviou
DefaultMediaNotificationProvider defaultMediaNotificationProvider =
new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext())
.build();
MediaSession mockMediaSession = mock(MediaSession.class);
Commands commands = new Commands.Builder().addAllCommands().build();
SessionCommand customSessionCommand = new SessionCommand("", Bundle.EMPTY);
CommandButton customCommandButton =
Expand All @@ -104,7 +112,10 @@ public void getMediaButtons_allCommandsAvailable_createsPauseSkipNextSkipPreviou

List<CommandButton> mediaButtons =
defaultMediaNotificationProvider.getMediaButtons(
commands, ImmutableList.of(customCommandButton), /* showPauseButton= */ true);
mockMediaSession,
commands,
ImmutableList.of(customCommandButton),
/* showPauseButton= */ true);

assertThat(mediaButtons).hasSize(4);
assertThat(mediaButtons.get(0).playerCommand)
Expand All @@ -119,6 +130,7 @@ public void getMediaButtons_noPlayerCommandsAvailable_onlyCustomLayoutButtons()
DefaultMediaNotificationProvider defaultMediaNotificationProvider =
new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext())
.build();
MediaSession mockMediaSession = mock(MediaSession.class);
Commands commands = new Commands.Builder().build();
SessionCommand customSessionCommand = new SessionCommand("action1", Bundle.EMPTY);
CommandButton customCommandButton =
Expand All @@ -130,7 +142,10 @@ public void getMediaButtons_noPlayerCommandsAvailable_onlyCustomLayoutButtons()

List<CommandButton> mediaButtons =
defaultMediaNotificationProvider.getMediaButtons(
commands, ImmutableList.of(customCommandButton), /* showPauseButton= */ true);
mockMediaSession,
commands,
ImmutableList.of(customCommandButton),
/* showPauseButton= */ true);

assertThat(mediaButtons).containsExactly(customCommandButton);
}
Expand Down Expand Up @@ -703,17 +718,19 @@ public void overridesProviderDefinition_compilesSuccessfully() {
DefaultMediaNotificationProvider unused =
new DefaultMediaNotificationProvider(context) {
@Override
public List<CommandButton> getMediaButtons(
public ImmutableList<CommandButton> getMediaButtons(
MediaSession mediaSession,
Player.Commands playerCommands,
List<CommandButton> customLayout,
ImmutableList<CommandButton> customLayout,
boolean showPauseButton) {
return super.getMediaButtons(playerCommands, customLayout, showPauseButton);
return super.getMediaButtons(
mediaSession, playerCommands, customLayout, showPauseButton);
}

@Override
public int[] addNotificationActions(
MediaSession mediaSession,
List<CommandButton> mediaButtons,
ImmutableList<CommandButton> mediaButtons,
NotificationCompat.Builder builder,
MediaNotification.ActionFactory actionFactory) {
return super.addNotificationActions(mediaSession, mediaButtons, builder, actionFactory);
Expand Down

0 comments on commit e690802

Please sign in to comment.