Commit 4c838043 by bachinger Committed by Ian Baker

Dispatch custom action in MediaSessionLegacyStub

A custom action sent by legacy controllers as for instance the System UI mini player arrive at MediaSessionCompat.Callback.onCustomAction() and need to be dispatched to the session callback as a such.

PiperOrigin-RevId: 447486859
parent 7a631f40
...@@ -190,6 +190,16 @@ import org.checkerframework.checker.initialization.qual.Initialized; ...@@ -190,6 +190,16 @@ import org.checkerframework.checker.initialization.qual.Initialized;
} }
@Override @Override
public void onCustomAction(String action, @Nullable Bundle extras) {
Bundle args = extras == null ? Bundle.EMPTY : extras;
SessionCommand command = new SessionCommand(action, args);
dispatchSessionTaskWithSessionCommand(
command,
controller ->
ignoreFuture(sessionImpl.onCustomCommandOnHandler(controller, command, args)));
}
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) { public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
@Nullable KeyEvent keyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); @Nullable KeyEvent keyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (keyEvent == null || keyEvent.getAction() != KeyEvent.ACTION_DOWN) { if (keyEvent == null || keyEvent.getAction() != KeyEvent.ACTION_DOWN) {
...@@ -458,11 +468,6 @@ import org.checkerframework.checker.initialization.qual.Initialized; ...@@ -458,11 +468,6 @@ import org.checkerframework.checker.initialization.qual.Initialized;
} }
@Override @Override
public void onCustomAction(String action, @Nullable Bundle extras) {
// no-op
}
@Override
public void onSetCaptioningEnabled(boolean enabled) { public void onSetCaptioningEnabled(boolean enabled) {
// no-op // no-op
} }
......
...@@ -702,6 +702,58 @@ public class MediaSessionCallbackWithMediaControllerCompatTest { ...@@ -702,6 +702,58 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
} }
@Test @Test
public void sendCustomCommand() throws Exception {
String testCommand = "test_custom_command";
Bundle testArgs = new Bundle();
testArgs.putString("args", "test_custom_args");
SessionCommand customCommand = new SessionCommand(testCommand, /* extras= */ Bundle.EMPTY);
CountDownLatch latch = new CountDownLatch(1);
SessionCallback callback =
new SessionCallback() {
@Override
public MediaSession.ConnectionResult onConnect(
MediaSession session, ControllerInfo controller) {
if (EXPECTED_CONTROLLER_PACKAGE_NAME.equals(controller.getPackageName())) {
MediaSession.ConnectionResult connectionResult =
SessionCallback.super.onConnect(session, controller);
SessionCommands.Builder builder =
connectionResult.availableSessionCommands.buildUpon().add(customCommand);
return MediaSession.ConnectionResult.accept(
/* availableSessionCommands= */ builder.build(),
connectionResult.availablePlayerCommands);
} else {
return MediaSession.ConnectionResult.reject();
}
}
@Override
public ListenableFuture<SessionResult> onCustomCommand(
MediaSession session,
ControllerInfo controller,
SessionCommand sessionCommand,
Bundle args) {
if (sessionCommand.customAction.equals(testCommand)
&& TestUtils.equals(testArgs, args)) {
latch.countDown();
}
return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
}
};
session =
new MediaSession.Builder(context, player)
.setId("sendCommand")
.setSessionCallback(callback)
.build();
controller =
new RemoteMediaControllerCompat(
context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */ true);
controller.sendCustomCommand(customCommand, testArgs);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
@Test
public void controllerCallback_sessionRejects() throws Exception { public void controllerCallback_sessionRejects() throws Exception {
SessionCallback sessionCallback = SessionCallback sessionCallback =
new SessionCallback() { new SessionCallback() {
......
...@@ -120,6 +120,11 @@ public class RemoteMediaControllerCompat { ...@@ -120,6 +120,11 @@ public class RemoteMediaControllerCompat {
binder.sendCommand(controllerId, command, params, cb); binder.sendCommand(controllerId, command, params, cb);
} }
public void sendCustomCommand(SessionCommand customCommand, Bundle params)
throws RemoteException {
binder.sendCustomActionWithName(controllerId, customCommand.customAction, params);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// MediaControllerCompat.TransportControls methods // MediaControllerCompat.TransportControls methods
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment