Allow MediaLibraryService to reject the resumption notification

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
(cherry picked from commit 7938978b5165a9cbb63a6ee1fe5209934e996c6e)
This commit is contained in:
bachinger 2023-04-26 16:42:28 +01:00 committed by Ian Baker
parent 31492031c1
commit 3406334ee8
3 changed files with 16 additions and 2 deletions

View File

@ -16,6 +16,10 @@
* Fix issue where `MediaController` doesn't update its available commands
when connected to a legacy `MediaSessionCompat` that updates its
actions.
* 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)).
### 1.0.1 (2023-04-18)

View File

@ -29,6 +29,7 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.util.Util
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
@ -142,6 +143,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))
}

View File

@ -126,8 +126,11 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
.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