Skip to content

Commit

Permalink
Allow MediaLibraryService to reject the resumption notification
Browse files Browse the repository at this point in the history
To reliably reject the System UI playback resumption notification on
all API levels (specifically API 30), the backward compatibility layer
needs to return `null` for the library root.

This is not possible in the Media3 implementation. This change allows
an app to return a `LibraryResult.ofError(RESULT_ERROR_NOT_SUPPORTED)`
that then is translated to return null by the backwards compatibility
layer.

Issue: androidx/media#355
Issue: androidx/media#167
Issue: androidx/media#27

See https://developer.android.com/guide/topics/media/media-controls#mediabrowserservice_implementation

PiperOrigin-RevId: 527276529
  • Loading branch information
marcbaechinger authored and icbaker committed Apr 27, 2023
1 parent 178a323 commit 7938978
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
* Add helper method `MediaSession.getControllerForCurrentRequest` to
obtain information about the controller that is currently calling
a`Player` method.
* Fix bug that prevented the `MediaLibraryService` from returning null for
a call from System UI to `Callback.onGetLibraryRoot` with
`params.isRecent == true` on API 30
([#355](https://github.com/androidx/media/issues/355)).
* UI:

* Add Util methods `shouldShowPlayButton` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.media3.common.util.Util
import androidx.media3.datasource.DataSourceBitmapLoader
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.*
import androidx.media3.session.LibraryResult.RESULT_ERROR_NOT_SUPPORTED
import androidx.media3.session.MediaSession.ControllerInfo
import com.google.common.collect.ImmutableList
import com.google.common.util.concurrent.Futures
Expand Down Expand Up @@ -143,6 +144,12 @@ class PlaybackService : MediaLibraryService() {
browser: ControllerInfo,
params: LibraryParams?
): ListenableFuture<LibraryResult<MediaItem>> {
if (params != null && params.isRecent) {
// The service currently does not support playback resumption. Tell System UI by returning
// an error of type 'RESULT_ERROR_NOT_SUPPORTED' for a `params.isRecent` request. See
// https://github.com/androidx/media/issues/355
return Futures.immediateFuture(LibraryResult.ofError(RESULT_ERROR_NOT_SUPPORTED))
}
return Futures.immediateFuture(LibraryResult.ofItem(MediaItemTree.getRootItem(), params))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ public BrowserRoot onGetRoot(
.putBoolean(BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, isSearchSessionCommandAvailable);
return new BrowserRoot(result.value.mediaId, extras);
}
// No library root, but keep browser compat connected to allow getting session.
return MediaUtils.defaultBrowserRoot;
// No library root, but keep browser compat connected to allow getting session unless the
// `Callback` implementation has not returned a `RESULT_SUCCESS`.
return result != null && result.resultCode != RESULT_SUCCESS
? null
: MediaUtils.defaultBrowserRoot;
}

// TODO(b/192455639): Optimize potential multiple calls of
Expand Down

0 comments on commit 7938978

Please sign in to comment.