Skip to content

Allow to opt into System UI and bluetooth playback resumption seperately #2671

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 1 commit 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 @@ -606,6 +606,24 @@ public Builder setMediaButtonPreferences(List<CommandButton> mediaButtonPreferen
return super.setMediaButtonPreferences(mediaButtonPreferences);
}

/**
* Sets whether to opt into the System UI playback resumption. If this is set to {@code true},
* {@link MediaSession.Callback#onPlaybackResumption(MediaSession, ControllerInfo, boolean)}
* must be implemented.
*
* <p>The default is {@code null}.
*
* @param systemUiPlaybackResumptionOptIn Whether to opt into System UI playback resumption
* notification, or {@code null} to determine based on the presence of a {@link
* MediaButtonReceiver} in the manifest.
*/
@UnstableApi
@Override
public Builder setSystemUiPlaybackResumptionOptIn(
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
return super.setSystemUiPlaybackResumptionOptIn(systemUiPlaybackResumptionOptIn);
}

/**
* Sets whether a play button is shown if playback is {@linkplain
* Player#getPlaybackSuppressionReason() suppressed}.
Expand Down Expand Up @@ -705,7 +723,8 @@ public MediaLibrarySession build() {
checkNotNull(bitmapLoader),
playIfSuppressed,
isPeriodicPositionUpdateEnabled,
libraryErrorReplicationMode);
libraryErrorReplicationMode,
systemUiPlaybackResumptionOptIn);
}
}

Expand All @@ -723,7 +742,8 @@ public MediaLibrarySession build() {
BitmapLoader bitmapLoader,
boolean playIfSuppressed,
boolean isPeriodicPositionUpdateEnabled,
@LibraryErrorReplicationMode int libraryErrorReplicationMode) {
@LibraryErrorReplicationMode int libraryErrorReplicationMode,
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
super(
context,
id,
Expand All @@ -738,7 +758,8 @@ public MediaLibrarySession build() {
bitmapLoader,
playIfSuppressed,
isPeriodicPositionUpdateEnabled,
libraryErrorReplicationMode);
libraryErrorReplicationMode,
systemUiPlaybackResumptionOptIn);
}

@Override
Expand All @@ -756,7 +777,8 @@ public MediaLibrarySession build() {
BitmapLoader bitmapLoader,
boolean playIfSuppressed,
boolean isPeriodicPositionUpdateEnabled,
@LibraryErrorReplicationMode int libraryErrorReplicationMode) {
@LibraryErrorReplicationMode int libraryErrorReplicationMode,
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
return new MediaLibrarySessionImpl(
this,
context,
Expand All @@ -772,7 +794,8 @@ public MediaLibrarySession build() {
bitmapLoader,
playIfSuppressed,
isPeriodicPositionUpdateEnabled,
libraryErrorReplicationMode);
libraryErrorReplicationMode,
systemUiPlaybackResumptionOptIn);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public MediaLibrarySessionImpl(
BitmapLoader bitmapLoader,
boolean playIfSuppressed,
boolean isPeriodicPositionUpdateEnabled,
@MediaLibrarySession.LibraryErrorReplicationMode int libraryErrorReplicationMode) {
@MediaLibrarySession.LibraryErrorReplicationMode int libraryErrorReplicationMode,
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
super(
instance,
context,
Expand All @@ -96,7 +97,8 @@ public MediaLibrarySessionImpl(
sessionExtras,
bitmapLoader,
playIfSuppressed,
isPeriodicPositionUpdateEnabled);
isPeriodicPositionUpdateEnabled,
systemUiPlaybackResumptionOptIn);
this.instance = instance;
this.callback = callback;
this.libraryErrorReplicationMode = libraryErrorReplicationMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,24 @@ public Builder setShowPlayButtonIfPlaybackIsSuppressed(
return super.setShowPlayButtonIfPlaybackIsSuppressed(showPlayButtonIfPlaybackIsSuppressed);
}

/**
* Sets whether to opt into the System UI playback resumption. If this is set to {@code true},
* {@link MediaSession.Callback#onPlaybackResumption(MediaSession, ControllerInfo, boolean)}
* must be implemented.
*
* <p>The default is {@code null}.
*
* @param systemUiPlaybackResumptionOptIn Whether to opt into System UI playback resumption
* notification, or {@code null} to determine based on the presence of a {@link
* MediaButtonReceiver} in the manifest.
*/
@UnstableApi
@Override
public Builder setSystemUiPlaybackResumptionOptIn(
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
return super.setSystemUiPlaybackResumptionOptIn(systemUiPlaybackResumptionOptIn);
}

/**
* Sets {@link CommandButton command buttons} that can be added as {@linkplain
* MediaMetadata.Builder#setSupportedCommands(List) supported media item commands}.
Expand Down Expand Up @@ -499,7 +517,8 @@ public MediaSession build() {
checkNotNull(bitmapLoader),
playIfSuppressed,
isPeriodicPositionUpdateEnabled,
MediaLibrarySession.LIBRARY_ERROR_REPLICATION_MODE_NONE);
MediaLibrarySession.LIBRARY_ERROR_REPLICATION_MODE_NONE,
systemUiPlaybackResumptionOptIn);
}
}

Expand Down Expand Up @@ -730,7 +749,8 @@ public static ControllerInfo createTestOnlyControllerInfo(
BitmapLoader bitmapLoader,
boolean playIfSuppressed,
boolean isPeriodicPositionUpdateEnabled,
@MediaLibrarySession.LibraryErrorReplicationMode int libraryErrorReplicationMode) {
@MediaLibrarySession.LibraryErrorReplicationMode int libraryErrorReplicationMode,
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
synchronized (STATIC_LOCK) {
if (SESSION_ID_TO_SESSION_MAP.containsKey(id)) {
throw new IllegalStateException("Session ID must be unique. ID=" + id);
Expand All @@ -752,7 +772,8 @@ public static ControllerInfo createTestOnlyControllerInfo(
bitmapLoader,
playIfSuppressed,
isPeriodicPositionUpdateEnabled,
libraryErrorReplicationMode);
libraryErrorReplicationMode,
systemUiPlaybackResumptionOptIn);
}

/* package */ MediaSessionImpl createImpl(
Expand All @@ -769,7 +790,8 @@ public static ControllerInfo createTestOnlyControllerInfo(
BitmapLoader bitmapLoader,
boolean playIfSuppressed,
boolean isPeriodicPositionUpdateEnabled,
@MediaLibrarySession.LibraryErrorReplicationMode int libraryErrorReplicationMode) {
@MediaLibrarySession.LibraryErrorReplicationMode int libraryErrorReplicationMode,
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
return new MediaSessionImpl(
this,
context,
Expand All @@ -784,7 +806,8 @@ public static ControllerInfo createTestOnlyControllerInfo(
sessionExtras,
bitmapLoader,
playIfSuppressed,
isPeriodicPositionUpdateEnabled);
isPeriodicPositionUpdateEnabled,
systemUiPlaybackResumptionOptIn);
}

/* package */ MediaSessionImpl getImpl() {
Expand Down Expand Up @@ -2421,6 +2444,7 @@ default void onError(int seq, SessionError sessionError) throws RemoteException
/* package */ ImmutableList<CommandButton> mediaButtonPreferences;
/* package */ ImmutableList<CommandButton> commandButtonsForMediaItems;
/* package */ boolean isPeriodicPositionUpdateEnabled;
/* package */ @Nullable Boolean systemUiPlaybackResumptionOptIn;

public BuilderBase(Context context, Player player, CallbackT callback) {
this.context = checkNotNull(context);
Expand Down Expand Up @@ -2496,6 +2520,14 @@ public BuilderT setMediaButtonPreferences(List<CommandButton> mediaButtonPrefere
return (BuilderT) this;
}

@CanIgnoreReturnValue
@SuppressWarnings("unchecked")
public BuilderT setSystemUiPlaybackResumptionOptIn(
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
this.systemUiPlaybackResumptionOptIn = systemUiPlaybackResumptionOptIn;
return (BuilderT) this;
}

@CanIgnoreReturnValue
@SuppressWarnings("unchecked")
public BuilderT setShowPlayButtonIfPlaybackIsSuppressed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
private ImmutableList<CommandButton> mediaButtonPreferences;
private Bundle sessionExtras;
@Nullable private PlaybackException playbackException;
private final @Nullable Boolean systemUiPlaybackResumptionOptIn;

@SuppressWarnings("argument.type.incompatible") // Using this in System.identityHashCode
public MediaSessionImpl(
Expand All @@ -169,7 +170,8 @@ public MediaSessionImpl(
Bundle sessionExtras,
BitmapLoader bitmapLoader,
boolean playIfSuppressed,
boolean isPeriodicPositionUpdateEnabled) {
boolean isPeriodicPositionUpdateEnabled,
@Nullable Boolean systemUiPlaybackResumptionOptIn) {
Log.i(
TAG,
"Init "
Expand All @@ -191,6 +193,7 @@ public MediaSessionImpl(
this.bitmapLoader = bitmapLoader;
this.playIfSuppressed = playIfSuppressed;
this.isPeriodicPositionUpdateEnabled = isPeriodicPositionUpdateEnabled;
this.systemUiPlaybackResumptionOptIn = systemUiPlaybackResumptionOptIn;

@SuppressWarnings("nullness:assignment")
@Initialized
Expand Down Expand Up @@ -1079,7 +1082,9 @@ protected MediaSessionServiceLegacyStub getLegacyBrowserService() {
}

/* package */ boolean canResumePlaybackOnStart() {
return sessionLegacyStub.canResumePlaybackOnStart();
return systemUiPlaybackResumptionOptIn != null
? systemUiPlaybackResumptionOptIn
: sessionLegacyStub.canResumePlaybackOnStart();
}

/* package */ void setMediaSessionListener(MediaSession.Listener listener) {
Expand Down