Skip to content

Move legacy session calls to a new thread #2648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import androidx.annotation.Nullable;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
Expand Down Expand Up @@ -359,19 +358,6 @@ protected MediaSessionServiceLegacyStub createLegacyBrowserService(
return stub;
}

@Override
protected void dispatchRemoteControllerTaskWithoutReturn(RemoteControllerTask task) {
super.dispatchRemoteControllerTaskWithoutReturn(task);
@Nullable MediaLibraryServiceLegacyStub legacyStub = getLegacyBrowserService();
if (legacyStub != null) {
try {
task.run(legacyStub.getBrowserLegacyCbForBroadcast(), /* seq= */ 0);
} catch (RemoteException e) {
Log.e(TAG, "Exception in using media1 API", e);
}
}
}

private void maybeUpdateLegacyErrorState(ControllerInfo browser, LibraryResult<?> result) {
if (libraryErrorReplicationMode == MediaLibrarySession.LIBRARY_ERROR_REPLICATION_MODE_NONE
|| browser.getControllerVersion() != ControllerInfo.LEGACY_CONTROLLER_VERSION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,20 +406,6 @@ public void onConnected(boolean shouldShowNotification) {
}
}

@Override
public void onMediaButtonPreferencesChanged(
MediaController controller, List<CommandButton> mediaButtonPreferences) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
}

@Override
public void onAvailableSessionCommandsChanged(
MediaController controller, SessionCommands commands) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
}

@Override
public ListenableFuture<SessionResult> onCustomCommand(
MediaController controller, SessionCommand command, Bundle args) {
Expand All @@ -440,20 +426,6 @@ public void onDisconnected(MediaController controller) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
}

@Override
public void onEvents(Player player, Player.Events events) {
// We must limit the frequency of notification updates, otherwise the system may suppress
// them.
if (events.containsAny(
Player.EVENT_PLAYBACK_STATE_CHANGED,
Player.EVENT_PLAY_WHEN_READY_CHANGED,
Player.EVENT_MEDIA_METADATA_CHANGED,
Player.EVENT_TIMELINE_CHANGED)) {
mediaSessionService.onUpdateNotificationInternal(
session, /* startInForegroundWhenPaused= */ false);
}
}
}

@SuppressLint("InlinedApi") // Using compile time constant FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ public MediaSessionImpl(
new MediaSessionLegacyStub(
/* session= */ thisRef,
sessionUri,
applicationHandler,
tokenExtras,
playIfSuppressed,
customLayout,
Expand All @@ -240,7 +239,7 @@ public MediaSessionImpl(
connectionResult.availablePlayerCommands,
sessionExtras);

Token platformToken = sessionLegacyStub.getSessionCompat().getSessionToken().getToken();
Token platformToken = sessionLegacyStub.getSessionToken().getToken();
sessionToken =
new SessionToken(
Process.myUid(),
Expand Down Expand Up @@ -504,7 +503,7 @@ public ListenableFuture<SessionResult> setCustomLayout(
ControllerInfo controller, ImmutableList<CommandButton> customLayout) {
if (isMediaNotificationController(controller)) {
sessionLegacyStub.setPlatformCustomLayout(customLayout);
sessionLegacyStub.updateLegacySessionPlaybackState(playerWrapper);
sessionLegacyStub.updateLegacySessionPlaybackState(playerWrapper, true);
}
return dispatchRemoteControllerTask(
controller, (controller1, seq) -> controller1.setCustomLayout(seq, customLayout));
Expand All @@ -529,7 +528,7 @@ public ListenableFuture<SessionResult> setMediaButtonPreferences(
ControllerInfo controller, ImmutableList<CommandButton> mediaButtonPreferences) {
if (isMediaNotificationController(controller)) {
sessionLegacyStub.setPlatformMediaButtonPreferences(mediaButtonPreferences);
sessionLegacyStub.updateLegacySessionPlaybackState(playerWrapper);
sessionLegacyStub.updateLegacySessionPlaybackState(playerWrapper, true);
}
return dispatchRemoteControllerTask(
controller,
Expand Down Expand Up @@ -980,7 +979,7 @@ public void connectFromService(IMediaController caller, ControllerInfo controlle

@SuppressWarnings("UnnecessarilyFullyQualified") // Avoiding confusion by just using "Token"
public android.media.session.MediaSession.Token getPlatformToken() {
return sessionLegacyStub.getSessionCompat().getSessionToken().getToken();
return sessionLegacyStub.getSessionToken().getToken();
}

public void setLegacyControllerConnectionTimeoutMs(long timeoutMs) {
Expand Down Expand Up @@ -1049,8 +1048,7 @@ protected IBinder getLegacyBrowserServiceBinder() {
MediaSessionServiceLegacyStub legacyStub;
synchronized (lock) {
if (browserServiceLegacyStub == null) {
browserServiceLegacyStub =
createLegacyBrowserService(sessionLegacyStub.getSessionCompat().getSessionToken());
browserServiceLegacyStub = createLegacyBrowserService(sessionLegacyStub.getSessionToken());
}
legacyStub = browserServiceLegacyStub;
}
Expand Down Expand Up @@ -1460,7 +1458,7 @@ private void handleAvailablePlayerCommandsChanged(Player.Commands availableComma
sessionLegacyStub.onSkipToNext();
return true;
} else if (callerInfo.getControllerVersion() != ControllerInfo.LEGACY_CONTROLLER_VERSION) {
sessionLegacyStub.getSessionCompat().getController().dispatchMediaButtonEvent(keyEvent);
sessionLegacyStub.getControllerCompat().dispatchMediaButtonEvent(keyEvent);
return true;
}
// This is an unhandled framework event. Return false to let the framework resolve by calling
Expand Down
Loading