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
This commit is contained in:
bachinger 2022-05-09 17:19:09 +01:00 committed by Ian Baker
parent 7a631f4050
commit 4c838043d3
3 changed files with 67 additions and 5 deletions

View File

@ -189,6 +189,16 @@ import org.checkerframework.checker.initialization.qual.Initialized;
}); });
} }
@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 @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);
@ -457,11 +467,6 @@ import org.checkerframework.checker.initialization.qual.Initialized;
}); });
} }
@Override
public void onCustomAction(String action, @Nullable Bundle extras) {
// no-op
}
@Override @Override
public void onSetCaptioningEnabled(boolean enabled) { public void onSetCaptioningEnabled(boolean enabled) {
// no-op // no-op

View File

@ -701,6 +701,58 @@ public class MediaSessionCallbackWithMediaControllerCompatTest {
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
} }
@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 @Test
public void controllerCallback_sessionRejects() throws Exception { public void controllerCallback_sessionRejects() throws Exception {
SessionCallback sessionCallback = SessionCallback sessionCallback =

View File

@ -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
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////