Reputation: 61
Recently, I've been trying to implement pagination on the Android Auto part of my application. To do so, I took inspiration from this Medium post. This question is also related. However, I am still stuck: while I can make the code from the Medium post working on the regular application, the Android Auto integration does not work quite well.
Long story short, the technique used in the post is to subscribe to the MediaBrowser and pass pagination intents. Then, the onLoadChildren method with the options argument is called:
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result, @NonNull Bundle options) {
This works from the regular application. However, when calling the application from Android Auto, the regular method without the options argument is called:
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
As a result, it is impossible to paginate the results from Android Auto.
I did some experimentations with this project, that is a very basic boilerplate to show how the MediaBrowser and the MediaBrowserService work. I applied the following patch:
===================================================================
--- final/src/main/java/com/example/android/musicplayercodelab/MusicPlayerActivity.java (revision d322b5a62b0da5afba209553bcccb660c12529d7)
+++ final/src/main/java/com/example/android/musicplayercodelab/MusicPlayerActivity.java (date 1551653654000)
@@ -57,7 +58,10 @@
new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
- mMediaBrowser.subscribe(mMediaBrowser.getRoot(), mSubscriptionCallback);
+ Bundle extra = new Bundle();
+ extra.putInt(MediaBrowserCompat.EXTRA_PAGE, 0);
+ extra.putInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, 10);
+ mMediaBrowser.subscribe(mMediaBrowser.getRoot(), extra, mSubscriptionCallback);
try {
MediaControllerCompat mediaController =
new MediaControllerCompat(
@@ -100,7 +104,7 @@
new MediaBrowserCompat.SubscriptionCallback() {
@Override
public void onChildrenLoaded(
- String parentId, List<MediaBrowserCompat.MediaItem> children) {
+ String parentId, List<MediaBrowserCompat.MediaItem> children, @NonNull Bundle options) {
onMediaLoaded(children);
}
};
===================================================================
--- final/src/main/java/com/example/android/musicplayercodelab/MusicService.java (revision d322b5a62b0da5afba209553bcccb660c12529d7)
+++ final/src/main/java/com/example/android/musicplayercodelab/MusicService.java (date 1551653364000)
@@ -116,4 +117,10 @@
final String parentMediaId, final Result<List<MediaBrowserCompat.MediaItem>> result) {
result.sendResult(MusicLibrary.getMediaItems());
}
+
+ @Override
+ public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result, @NonNull Bundle options) {
+ result.sendResult(MusicLibrary.getMediaItems());
+ }
+
}
I observed the following:
the new onLoadChildren method with the options Bundle is called when starting the application normally. Interestingly, Android Auto calls the MusicPlayerActivity when it is launched, and calls connect() every time, so I definitely need to keep the call to connect(). This is important because it means that we do not have to try to get the MediaBrowser instance used by Android Auto unlike what we thought previously.
When opening the application it goes like this:
When opening Android Auto, it goes like this:
Thus, I am not sure what to do next. Is there anything I can do on my side, or could it be an Android Auto bug?
Thank you!
Upvotes: 2
Views: 804
Reputation: 754
Even though the MediaBrowserService can handle custom pagination, the browsing is handled for you in Android Auto. See this documentation
What you do have control over is the content hierarchy that is provided to the drawer for the browser tree as well as a bit of the contents style. Otherwise, there isn't much else you can do to customize the pagination when targeting Android Auto.
Upvotes: 1