Add the MediaSession as an argument to getMediaButtons()
Issue: androidx/media#216 #minor-release PiperOrigin-RevId: 503406474
This commit is contained in:
parent
4b4904899c
commit
e690802e9e
@ -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.
|
||||
|
@ -54,7 +54,6 @@ import com.google.common.util.concurrent.FutureCallback;
|
||||
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;
|
||||
@ -310,6 +309,7 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
|
||||
addNotificationActions(
|
||||
mediaSession,
|
||||
getMediaButtons(
|
||||
mediaSession,
|
||||
player.getAvailableCommands(),
|
||||
customLayout,
|
||||
/* showPauseButton= */ player.getPlayWhenReady()
|
||||
@ -418,6 +418,7 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
|
||||
* 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}.
|
||||
@ -425,10 +426,13 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
|
||||
* 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);
|
||||
@ -477,14 +481,14 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
|
||||
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
|
||||
@ -505,7 +509,7 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
|
||||
*/
|
||||
protected int[] addNotificationActions(
|
||||
MediaSession mediaSession,
|
||||
List<CommandButton> mediaButtons,
|
||||
ImmutableList<CommandButton> mediaButtons,
|
||||
NotificationCompat.Builder builder,
|
||||
MediaNotification.ActionFactory actionFactory) {
|
||||
int[] compactViewIndices = new int[3];
|
||||
|
@ -68,13 +68,20 @@ public class DefaultMediaNotificationProviderTest {
|
||||
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);
|
||||
@ -93,6 +100,7 @@ public class DefaultMediaNotificationProviderTest {
|
||||
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 =
|
||||
@ -104,7 +112,10 @@ public class DefaultMediaNotificationProviderTest {
|
||||
|
||||
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)
|
||||
@ -119,6 +130,7 @@ public class DefaultMediaNotificationProviderTest {
|
||||
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 =
|
||||
@ -130,7 +142,10 @@ public class DefaultMediaNotificationProviderTest {
|
||||
|
||||
List<CommandButton> mediaButtons =
|
||||
defaultMediaNotificationProvider.getMediaButtons(
|
||||
commands, ImmutableList.of(customCommandButton), /* showPauseButton= */ true);
|
||||
mockMediaSession,
|
||||
commands,
|
||||
ImmutableList.of(customCommandButton),
|
||||
/* showPauseButton= */ true);
|
||||
|
||||
assertThat(mediaButtons).containsExactly(customCommandButton);
|
||||
}
|
||||
@ -703,17 +718,19 @@ public class DefaultMediaNotificationProviderTest {
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user