From 107a481356ce80bbb439b6ee44f1f37f8cc9543a Mon Sep 17 00:00:00 2001 From: bachinger Date: Fri, 20 Jan 2023 12:03:58 +0000 Subject: [PATCH] Add the MediaSession as an argument to `getMediaButtons()` Issue: androidx/media#216 #minor-release PiperOrigin-RevId: 503406474 (cherry picked from commit e690802e9ecf96dfbb972864819a45ae92c47c90) --- RELEASENOTES.md | 3 ++ .../DefaultMediaNotificationProvider.java | 18 ++++++---- .../DefaultMediaNotificationProviderTest.java | 33 ++++++++++++++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 69a5f177a3..47170725be 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -43,6 +43,9 @@ `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. diff --git a/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java b/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java index 6b2368df7d..b6c487fcd2 100644 --- a/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java +++ b/libraries/session/src/main/java/androidx/media3/session/DefaultMediaNotificationProvider.java @@ -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 getMediaButtons( - Player.Commands playerCommands, List customLayout, boolean showPauseButton) { + protected ImmutableList getMediaButtons( + MediaSession session, + Player.Commands playerCommands, + ImmutableList customLayout, + boolean showPauseButton) { // Skip to previous action. - List commandButtons = new ArrayList<>(); + ImmutableList.Builder 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. * *

The list of {@code mediaButtons} is the list resulting from {@link #getMediaButtons( - * Player.Commands, List, boolean)}. + * MediaSession, Player.Commands, ImmutableList, boolean)}. * *

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 mediaButtons, + ImmutableList mediaButtons, NotificationCompat.Builder builder, MediaNotification.ActionFactory actionFactory) { int[] compactViewIndices = new int[3]; diff --git a/libraries/session/src/test/java/androidx/media3/session/DefaultMediaNotificationProviderTest.java b/libraries/session/src/test/java/androidx/media3/session/DefaultMediaNotificationProviderTest.java index e696979c6c..cd159ea0be 100644 --- a/libraries/session/src/test/java/androidx/media3/session/DefaultMediaNotificationProviderTest.java +++ b/libraries/session/src/test/java/androidx/media3/session/DefaultMediaNotificationProviderTest.java @@ -67,13 +67,20 @@ public class DefaultMediaNotificationProviderTest { new DefaultMediaNotificationProvider.Builder(ApplicationProvider.getApplicationContext()) .build(); Commands commands = new Commands.Builder().addAllCommands().build(); + MediaSession mockMediaSession = mock(MediaSession.class); List mediaButtonsWhenPlaying = defaultMediaNotificationProvider.getMediaButtons( - commands, /* customLayout= */ ImmutableList.of(), /* showPauseButton= */ true); + mockMediaSession, + commands, + /* customLayout= */ ImmutableList.of(), + /* showPauseButton= */ true); List 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); @@ -92,6 +99,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 = @@ -103,7 +111,10 @@ public class DefaultMediaNotificationProviderTest { List 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) @@ -118,6 +129,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 = @@ -129,7 +141,10 @@ public class DefaultMediaNotificationProviderTest { List mediaButtons = defaultMediaNotificationProvider.getMediaButtons( - commands, ImmutableList.of(customCommandButton), /* showPauseButton= */ true); + mockMediaSession, + commands, + ImmutableList.of(customCommandButton), + /* showPauseButton= */ true); assertThat(mediaButtons).containsExactly(customCommandButton); } @@ -702,17 +717,19 @@ public class DefaultMediaNotificationProviderTest { DefaultMediaNotificationProvider unused = new DefaultMediaNotificationProvider(context) { @Override - public List getMediaButtons( + public ImmutableList getMediaButtons( + MediaSession mediaSession, Player.Commands playerCommands, - List customLayout, + ImmutableList customLayout, boolean showPauseButton) { - return super.getMediaButtons(playerCommands, customLayout, showPauseButton); + return super.getMediaButtons( + mediaSession, playerCommands, customLayout, showPauseButton); } @Override public int[] addNotificationActions( MediaSession mediaSession, - List mediaButtons, + ImmutableList mediaButtons, NotificationCompat.Builder builder, MediaNotification.ActionFactory actionFactory) { return super.addNotificationActions(mediaSession, mediaButtons, builder, actionFactory);