Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto: Showing the progress while doing a background call

We have an audio app and we want to add Android Auto capability to it. The app basically has a list of radio streams and the user can select which one to play.

In order to obtain the streams, I need to call a web service and obtain the streams, while that is occurring I wish to show an indeterminate progress at least on the hamburger menu like Spotify does when it's loading content.

At this moment if I have already downloaded the streams, on my MediaBrowserServiceCompat when Auto calls onLoadChildren my Result<List<MediaBrowserCompat.MediaItem>> result object is populated with the MediaItems, if I don't have them I just send an empty array of MediaItems and call the API, once it has finished I call notifyChildrenChanged on the Service and the list of streams appear, but until then I have a "No items" message.

I could not find on Android Developer site a way to set the children menu to show a loading and I could not find a sample app that does that. Does anyone know if there's way, maybe when onGetRoot is called to let Auto know it has to wait before calling onGetRoot and onLoadChildren? I also tried calling the API then calling myself onGetRoot but it did not work.

Thanks in advance.

like image 881
e_ori Avatar asked Nov 08 '18 16:11

e_ori


1 Answers

OnGetRoot should return immediately, because it actually carries no content.

onLoadChildren though is where the content is loaded from, and it supports async loading on any content level. When your fetching the streams, don’t send an empty array of MediaItems before you call the API. Instead, fetch the streams from within onLoadChildren(). Call results.detach(), fetch the streams in another thread, then call results.sendResult when the content is available. The media content browser will show a progress spinner while waiting for the async sendResult call

Something like this will do the trick.

@Override
public void onLoadChildren(@NonNull final String parentMediaId, @NonNull final Result<List<MediaItem>> result) {
    if (/* if music library is ready, return immediately */) {
        result.sendResult(getChildren(parentMediaId, getResources()));
    } else {
        // otherwise, return results when the streams are ready
        result.detach();
        someMusicProvider.retrieveMediaAsync(new MusicProvider.Callback() {
            @Override
            public void onStreamsReady(boolean success) {
                result.sendResult(...);
            }
        });
    }
}
like image 192
salminnella Avatar answered Nov 20 '22 03:11

salminnella