Skip to content

Commit

Permalink
Make the media notification controller marker key visible
Browse files Browse the repository at this point in the history
An app that uses the `MediaSession` without a `MediaSessionService` may
want to connect a media notification controller on it own. To avoid apps
using a string literal, the key should be exposed as public.

PiperOrigin-RevId: 589875603
  • Loading branch information
marcbaechinger authored and copybara-github committed Dec 11, 2023
1 parent 3e5ef51 commit 7f9b020
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ public class MediaController implements Player {
*/
@UnstableApi public static final long RELEASE_UNBIND_TIMEOUT_MS = 30_000;

/**
* Key to mark the connection hints of the media notification controller.
*
* <p>For a controller to be {@linkplain
* MediaSession#isMediaNotificationController(MediaSession.ControllerInfo) recognized by the
* session as the media notification controller}, this key needs to be used to {@linkplain
* Bundle#putBoolean(String, boolean) set a boolean flag} in the connection hints to true. Only an
* internal controller that has the same package name as the session can be used as a media
* notification controller.
*
* <p>When using a session within a {@link MediaSessionService} or {@link MediaLibraryService},
* the service connects a media notification controller automatically. Apps can do this for
* standalone session to configure the platform session in the same way.
*/
@UnstableApi
public static final String KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG =
"androidx.media3.session.MediaNotificationManager";

private static final String TAG = "MediaController";

private static final String WRONG_THREAD_ERROR_MESSAGE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@
*/
/* package */ final class MediaNotificationManager {

/* package */ static final String KEY_MEDIA_NOTIFICATION_MANAGER =
"androidx.media3.session.MediaNotificationManager";
private static final String TAG = "MediaNtfMng";

private final MediaSessionService mediaSessionService;
Expand Down Expand Up @@ -92,7 +90,7 @@ public void addSession(MediaSession session) {
}
MediaControllerListener listener = new MediaControllerListener(mediaSessionService, session);
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
ListenableFuture<MediaController> controllerFuture =
new MediaController.Builder(mediaSessionService, session.getToken())
.setConnectionHints(connectionHints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public boolean isMediaNotificationController(MediaSession.ControllerInfo control
&& controllerInfo
.getConnectionHints()
.getBoolean(
MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, /* defaultValue= */ false);
MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, /* defaultValue= */ false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void isMediaNotificationController_applicationPackage_returnsTrue() {
/* pid= */ MediaSessionManager.RemoteUserInfo.UNKNOWN_PID,
/* uid= */ MediaSessionManager.RemoteUserInfo.UNKNOWN_UID);
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
MediaSession.ControllerInfo controllerInfo =
new MediaSession.ControllerInfo(
remoteUserInfo,
Expand All @@ -174,7 +174,7 @@ public void isMediaNotificationController_randomPackage_returnsFalse() {
/* pid= */ MediaSessionManager.RemoteUserInfo.UNKNOWN_PID,
/* uid= */ MediaSessionManager.RemoteUserInfo.UNKNOWN_UID);
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
MediaSession.ControllerInfo controllerInfo =
new MediaSession.ControllerInfo(
remoteUserInfo,
Expand Down Expand Up @@ -214,7 +214,7 @@ public void isMediaNotificationController_applicationPackageLegacyVersion_return
/* pid= */ MediaSessionManager.RemoteUserInfo.UNKNOWN_PID,
/* uid= */ MediaSessionManager.RemoteUserInfo.UNKNOWN_UID);
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
MediaSession.ControllerInfo controllerInfo =
new MediaSession.ControllerInfo(
remoteUserInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ private void connectMediaNotificationController(MediaSession mediaSession)
throws InterruptedException {
CountDownLatch connectionLatch = new CountDownLatch(1);
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
ListenableFuture<MediaController> mediaNotificationControllerFuture =
new MediaController.Builder(
ApplicationProvider.getApplicationContext(), mediaSession.getToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ public ListenableFuture<MediaSession.MediaItemsWithStartPosition> onPlaybackResu
/* waitForConnection= */ true);
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(
ApplicationProvider.getApplicationContext(), session.get().getToken())
.setConnectionHints(connectionHints)
Expand Down Expand Up @@ -889,7 +889,7 @@ public void dispatchMediaButtonEvent_playWithNonEmptyTimeline_callsHandlePlayBut
.setId("dispatchMediaButtonEvent")
.build()));
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(
ApplicationProvider.getApplicationContext(), session.get().getToken())
.setConnectionHints(connectionHints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void previousKeyEvent() throws Exception {
.get(0)
.getConnectionHints()
.getBoolean(
MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER,
MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG,
/* defaultValue= */ false))
.isTrue();
threadTestRule.getHandler().postAndSync(controller::release);
Expand Down Expand Up @@ -234,7 +234,7 @@ public void rewindKeyEvent_mediaNotificationControllerConnected_callFromNotifica
.get(0)
.getConnectionHints()
.getBoolean(
MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER,
MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG,
/* defaultValue= */ false))
.isTrue();
threadTestRule.getHandler().postAndSync(controller::release);
Expand Down Expand Up @@ -351,7 +351,7 @@ private MediaController connectMediaNotificationController() throws Exception {
() -> {
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(
MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, /* value= */ true);
MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, /* value= */ true);
return new MediaController.Builder(
ApplicationProvider.getApplicationContext(), session.getToken())
.setConnectionHints(connectionHints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public MediaSession.ConnectionResult onConnect(
.get(0)
.getConnectionHints()
.getBoolean(
MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER,
MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG,
/* defaultValue= */ false))
.isTrue();
assertThat(TestUtils.equals(controllerInfoList.get(1).getConnectionHints(), testHints))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ public MediaSession.ConnectionResult onConnect(
})
.build()));
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(
ApplicationProvider.getApplicationContext(), session.get().getToken())
.setConnectionHints(connectionHints)
Expand Down Expand Up @@ -740,7 +740,7 @@ public boolean onMediaButtonEvent(
})
.build());
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(ApplicationProvider.getApplicationContext(), session.getToken())
.setConnectionHints(connectionHints)
.buildAsync()
Expand Down Expand Up @@ -779,7 +779,7 @@ public void onMediaButtonEvent_noKeyEvent_returnsFalse() {
public void onMediaButtonEvent_noKeyEvent_mediaNotificationControllerConnected_returnsFalse()
throws Exception {
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(ApplicationProvider.getApplicationContext(), session.getToken())
.setConnectionHints(connectionHints)
.buildAsync()
Expand Down Expand Up @@ -809,7 +809,7 @@ public void onMediaButtonEvent_invalidKeyEvent_returnsFalse() {
public void onMediaButtonEvent_invalidKeyEvent_mediaNotificationControllerConnected_returnsFalse()
throws Exception {
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(ApplicationProvider.getApplicationContext(), session.getToken())
.setConnectionHints(connectionHints)
.buildAsync()
Expand Down Expand Up @@ -839,7 +839,7 @@ public void onMediaButtonEvent_invalidAction_returnsFalse() {
public void onMediaButtonEvent_invalidAction_mediaNotificationControllerConnected_returnsFalse()
throws Exception {
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(ApplicationProvider.getApplicationContext(), session.getToken())
.setConnectionHints(connectionHints)
.buildAsync()
Expand Down Expand Up @@ -869,7 +869,7 @@ public void onMediaButtonEvent_invalidComponent_returnsFalse() {
onMediaButtonEvent_invalidComponent_mediaNotificationControllerConnected_returnsFalse()
throws Exception {
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(ApplicationProvider.getApplicationContext(), session.getToken())
.setConnectionHints(connectionHints)
.buildAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public MediaSession.ConnectionResult onConnect(
sessionTestRule.ensureReleaseAfterTest(
new MediaSession.Builder(context, player).setId(TAG).setCallback(callback).build());
Bundle connectionHints = new Bundle();
connectionHints.putBoolean(MediaNotificationManager.KEY_MEDIA_NOTIFICATION_MANAGER, true);
connectionHints.putBoolean(MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
new MediaController.Builder(context.getApplicationContext(), session.getToken())
.setConnectionHints(connectionHints)
.buildAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ public MediaSession.ConnectionResult onConnect(
session.setSessionPositionUpdateDelayMs(0L);
if (useFakeMediaNotificationManagerController) {
Bundle connectionHints = new Bundle();
connectionHints.putBoolean("androidx.media3.session.MediaNotificationManager", true);
connectionHints.putBoolean(
MediaController.KEY_MEDIA_NOTIFICATION_CONTROLLER_FLAG, true);
//noinspection unused
ListenableFuture<MediaController> unusedFuture =
new MediaController.Builder(getApplicationContext(), session.getToken())
Expand Down

0 comments on commit 7f9b020

Please sign in to comment.