Add the MediaSession as an argument to getMediaButtons()

Issue: androidx/media#216
#minor-release
PiperOrigin-RevId: 503406474
(cherry picked from commit e690802e9ecf96dfbb972864819a45ae92c47c90)
This commit is contained in:
bachinger 2023-01-20 12:03:58 +00:00 committed by christosts
parent 967224c1aa
commit 107a481356
3 changed files with 39 additions and 15 deletions

View File

@ -43,6 +43,9 @@
`SessionToken` ([#171](https://github.com/androidx/media/issues/171)). `SessionToken` ([#171](https://github.com/androidx/media/issues/171)).
* Use `onMediaMetadataChanged` to trigger updates of the platform media * Use `onMediaMetadataChanged` to trigger updates of the platform media
session ([#219](https://github.com/androidx/media/issues/219)). 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: * Metadata:
* Parse multiple null-separated values from ID3 frames, as permitted by * Parse multiple null-separated values from ID3 frames, as permitted by
ID3 v2.4. ID3 v2.4.

View File

@ -54,7 +54,6 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import com.google.errorprone.annotations.CanIgnoreReturnValue; import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -310,6 +309,7 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
addNotificationActions( addNotificationActions(
mediaSession, mediaSession,
getMediaButtons( getMediaButtons(
mediaSession,
player.getAvailableCommands(), player.getAvailableCommands(),
customLayout, customLayout,
/* showPauseButton= */ player.getPlayWhenReady() /* showPauseButton= */ player.getPlayWhenReady()
@ -418,6 +418,7 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
* need the custom command set in {@link MediaSession.Callback#onPostConnect(MediaSession, * need the custom command set in {@link MediaSession.Callback#onPostConnect(MediaSession,
* MediaSession.ControllerInfo)} also. * MediaSession.ControllerInfo)} also.
* *
* @param session The media session.
* @param playerCommands The available player commands. * @param playerCommands The available player commands.
* @param customLayout The {@linkplain MediaSession#setCustomLayout(List) custom layout of * @param customLayout The {@linkplain MediaSession#setCustomLayout(List) custom layout of
* commands}. * commands}.
@ -425,10 +426,13 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
* player is currently playing content), otherwise show a play button to start playback. * 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. * @return The ordered list of command buttons to be placed on the notification.
*/ */
protected List<CommandButton> getMediaButtons( protected ImmutableList<CommandButton> getMediaButtons(
Player.Commands playerCommands, List<CommandButton> customLayout, boolean showPauseButton) { MediaSession session,
Player.Commands playerCommands,
ImmutableList<CommandButton> customLayout,
boolean showPauseButton) {
// Skip to previous action. // 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)) { if (playerCommands.containsAny(COMMAND_SEEK_TO_PREVIOUS, COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM)) {
Bundle commandButtonExtras = new Bundle(); Bundle commandButtonExtras = new Bundle();
commandButtonExtras.putInt(COMMAND_KEY_COMPACT_VIEW_INDEX, INDEX_UNSET); commandButtonExtras.putInt(COMMAND_KEY_COMPACT_VIEW_INDEX, INDEX_UNSET);
@ -477,14 +481,14 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
commandButtons.add(button); commandButtons.add(button);
} }
} }
return commandButtons; return commandButtons.build();
} }
/** /**
* Adds the media buttons to the notification builder for the given action factory. * 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( * <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 * <p>Override this method to customize how the media buttons {@linkplain
* NotificationCompat.Builder#addAction(NotificationCompat.Action) are added} to the notification * NotificationCompat.Builder#addAction(NotificationCompat.Action) are added} to the notification
@ -505,7 +509,7 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
*/ */
protected int[] addNotificationActions( protected int[] addNotificationActions(
MediaSession mediaSession, MediaSession mediaSession,
List<CommandButton> mediaButtons, ImmutableList<CommandButton> mediaButtons,
NotificationCompat.Builder builder, NotificationCompat.Builder builder,
MediaNotification.ActionFactory actionFactory) { MediaNotification.ActionFactory actionFactory) {
int[] compactViewIndices = new int[3]; int[] compactViewIndices = new int[3];

View File

@ -67,13 +67,20 @@ public class DefaultMediaNotificationProviderTest {
new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext()) new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext())
.build(); .build();
Commands commands = new Commands.Builder().addAllCommands().build(); Commands commands = new Commands.Builder().addAllCommands().build();
MediaSession mockMediaSession = mock(MediaSession.class);
List<CommandButton> mediaButtonsWhenPlaying = List<CommandButton> mediaButtonsWhenPlaying =
defaultMediaNotificationProvider.getMediaButtons( defaultMediaNotificationProvider.getMediaButtons(
commands, /* customLayout= */ ImmutableList.of(), /* showPauseButton= */ true); mockMediaSession,
commands,
/* customLayout= */ ImmutableList.of(),
/* showPauseButton= */ true);
List<CommandButton> mediaButtonWhenPaused = List<CommandButton> mediaButtonWhenPaused =
defaultMediaNotificationProvider.getMediaButtons( defaultMediaNotificationProvider.getMediaButtons(
commands, /* customLayout= */ ImmutableList.of(), /* showPauseButton= */ false); mockMediaSession,
commands,
/* customLayout= */ ImmutableList.of(),
/* showPauseButton= */ false);
assertThat(mediaButtonsWhenPlaying).hasSize(3); assertThat(mediaButtonsWhenPlaying).hasSize(3);
assertThat(mediaButtonsWhenPlaying.get(1).playerCommand).isEqualTo(Player.COMMAND_PLAY_PAUSE); assertThat(mediaButtonsWhenPlaying.get(1).playerCommand).isEqualTo(Player.COMMAND_PLAY_PAUSE);
@ -92,6 +99,7 @@ public class DefaultMediaNotificationProviderTest {
DefaultMediaNotificationProvider defaultMediaNotificationProvider = DefaultMediaNotificationProvider defaultMediaNotificationProvider =
new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext()) new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext())
.build(); .build();
MediaSession mockMediaSession = mock(MediaSession.class);
Commands commands = new Commands.Builder().addAllCommands().build(); Commands commands = new Commands.Builder().addAllCommands().build();
SessionCommand customSessionCommand = new SessionCommand("", Bundle.EMPTY); SessionCommand customSessionCommand = new SessionCommand("", Bundle.EMPTY);
CommandButton customCommandButton = CommandButton customCommandButton =
@ -103,7 +111,10 @@ public class DefaultMediaNotificationProviderTest {
List<CommandButton> mediaButtons = List<CommandButton> mediaButtons =
defaultMediaNotificationProvider.getMediaButtons( defaultMediaNotificationProvider.getMediaButtons(
commands, ImmutableList.of(customCommandButton), /* showPauseButton= */ true); mockMediaSession,
commands,
ImmutableList.of(customCommandButton),
/* showPauseButton= */ true);
assertThat(mediaButtons).hasSize(4); assertThat(mediaButtons).hasSize(4);
assertThat(mediaButtons.get(0).playerCommand) assertThat(mediaButtons.get(0).playerCommand)
@ -118,6 +129,7 @@ public class DefaultMediaNotificationProviderTest {
DefaultMediaNotificationProvider defaultMediaNotificationProvider = DefaultMediaNotificationProvider defaultMediaNotificationProvider =
new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext()) new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext())
.build(); .build();
MediaSession mockMediaSession = mock(MediaSession.class);
Commands commands = new Commands.Builder().build(); Commands commands = new Commands.Builder().build();
SessionCommand customSessionCommand = new SessionCommand("action1", Bundle.EMPTY); SessionCommand customSessionCommand = new SessionCommand("action1", Bundle.EMPTY);
CommandButton customCommandButton = CommandButton customCommandButton =
@ -129,7 +141,10 @@ public class DefaultMediaNotificationProviderTest {
List<CommandButton> mediaButtons = List<CommandButton> mediaButtons =
defaultMediaNotificationProvider.getMediaButtons( defaultMediaNotificationProvider.getMediaButtons(
commands, ImmutableList.of(customCommandButton), /* showPauseButton= */ true); mockMediaSession,
commands,
ImmutableList.of(customCommandButton),
/* showPauseButton= */ true);
assertThat(mediaButtons).containsExactly(customCommandButton); assertThat(mediaButtons).containsExactly(customCommandButton);
} }
@ -702,17 +717,19 @@ public class DefaultMediaNotificationProviderTest {
DefaultMediaNotificationProvider unused = DefaultMediaNotificationProvider unused =
new DefaultMediaNotificationProvider(context) { new DefaultMediaNotificationProvider(context) {
@Override @Override
public List<CommandButton> getMediaButtons( public ImmutableList<CommandButton> getMediaButtons(
MediaSession mediaSession,
Player.Commands playerCommands, Player.Commands playerCommands,
List<CommandButton> customLayout, ImmutableList<CommandButton> customLayout,
boolean showPauseButton) { boolean showPauseButton) {
return super.getMediaButtons(playerCommands, customLayout, showPauseButton); return super.getMediaButtons(
mediaSession, playerCommands, customLayout, showPauseButton);
} }
@Override @Override
public int[] addNotificationActions( public int[] addNotificationActions(
MediaSession mediaSession, MediaSession mediaSession,
List<CommandButton> mediaButtons, ImmutableList<CommandButton> mediaButtons,
NotificationCompat.Builder builder, NotificationCompat.Builder builder,
MediaNotification.ActionFactory actionFactory) { MediaNotification.ActionFactory actionFactory) {
return super.addNotificationActions(mediaSession, mediaButtons, builder, actionFactory); return super.addNotificationActions(mediaSession, mediaButtons, builder, actionFactory);