Ensure Media3 play calls get FGS exemption

When a Media3 controller calls play on a Media3 session, the call
is currently not routed through the platform session at all.
This means the usual exemption to start a FGS for media controller
interactions is not triggered.

We can manually ensure this exemption is given by sending a custom
platform command to the session. The Media3 session will never
receive this command as it's not a known Media3 custom command.
Sessions will see a single onConnect call with a platform controller
from the sender app though. We can prevent this on newer versions of
the code by dropping the onCommand call early.

PiperOrigin-RevId: 679115247
This commit is contained in:
tonihei 2024-09-26 05:54:37 -07:00 committed by Copybara-Service
parent 50c879ee21
commit f4eef88089
4 changed files with 20 additions and 2 deletions

View File

@ -94,6 +94,8 @@
items are available for both, `MediaBrowser` and `MediaController`. See
<a href="https://developer.android.com/training/cars/media#custom_browse_actions">Custom
Browse actions of AAOS</a>.
* Fix bug where a Media3 controller was sometimes unable to let a session
app start a foreground service after requesting `play()`.
* UI:
* Make the stretched/cropped video in
`PlayerView`-in-Compose-`AndroidView` workaround opt-in, due to issues

View File

@ -497,6 +497,8 @@ public final class MediaConstants {
"androidx.media3.session.SESSION_COMMAND_ON_CAPTIONING_ENABLED_CHANGED";
/* package */ static final String SESSION_COMMAND_REQUEST_SESSION3_TOKEN =
"androidx.media3.session.SESSION_COMMAND_REQUEST_SESSION3_TOKEN";
/* package */ static final String SESSION_COMMAND_MEDIA3_PLAY_REQUEST =
"androidx.media3.session.SESSION_COMMAND_MEDIA3_PLAY_REQUEST";
/* package */ static final String ARGUMENT_CAPTIONING_ENABLED =
"androidx.media3.session.ARGUMENT_CAPTIONING_ENABLED";

View File

@ -137,6 +137,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
@Nullable private TextureView videoTextureView;
private Size surfaceSize;
@Nullable private IMediaSession iSession;
@Nullable private android.media.session.MediaController platformController;
private long currentPositionMs;
private long lastSetPlayWhenReadyCalledTimeMs;
@Nullable private PlayerInfo pendingPlayerInfo;
@ -405,6 +406,13 @@ import org.checkerframework.checker.nullness.qual.NonNull;
return;
}
if (Util.SDK_INT >= 31 && platformController != null) {
// Ensure the platform session gets allow-listed to start a foreground service after receiving
// the play command.
platformController.sendCommand(
MediaConstants.SESSION_COMMAND_MEDIA3_PLAY_REQUEST, /* args= */ null, /* cb= */ null);
}
dispatchRemoteSessionTaskWithPlayerCommand(
(iSession, seq) -> iSession.play(controllerStub, seq));
@ -2644,6 +2652,9 @@ import org.checkerframework.checker.nullness.qual.NonNull;
playerInfo = result.playerInfo;
MediaSession.Token platformToken =
result.platformToken == null ? token.getPlatformToken() : result.platformToken;
if (platformToken != null) {
platformController = new android.media.session.MediaController(context, platformToken);
}
try {
// Implementation for the local binder is no-op,
// so can be used without worrying about deadlock.

View File

@ -280,8 +280,11 @@ import org.checkerframework.checker.initialization.qual.Initialized;
@Override
public void onCommand(String commandName, @Nullable Bundle args, @Nullable ResultReceiver cb) {
checkStateNotNull(commandName);
if (TextUtils.equals(MediaConstants.SESSION_COMMAND_REQUEST_SESSION3_TOKEN, commandName)
&& cb != null) {
if (commandName.equals(MediaConstants.SESSION_COMMAND_MEDIA3_PLAY_REQUEST)) {
// Ignore, no need to handle this command here.
return;
}
if (commandName.equals(MediaConstants.SESSION_COMMAND_REQUEST_SESSION3_TOKEN) && cb != null) {
cb.send(RESULT_SUCCESS, sessionImpl.getToken().toBundle());
return;
}