Example usage for android.support.v4.media.browse MediaBrowserCompat subscribe

List of usage examples for android.support.v4.media.browse MediaBrowserCompat subscribe

Introduction

In this page you can find the example usage for android.support.v4.media.browse MediaBrowserCompat subscribe.

Prototype

public void subscribe(String parentId, SubscriptionCallback callback) 

Source Link

Document

Queries for information about the media items that are contained within the specified id and subscribes to receive updates when they change.

Usage

From source file:com.torrenttunes.android.ui.MediaBrowserFragment.java

private void updateTitle() {
    if (MediaIDHelper.MEDIA_ID_ROOT.equals(mMediaId)) {
        mMediaFragmentListener.setToolbarTitle(null);
        return;//from  www . jav a 2  s  .  c  o m
    }

    final String parentId = MediaIDHelper.getParentMediaID(mMediaId);

    // MediaBrowserCompat doesn't provide metadata for a given mediaID, only for its children. Since
    // the mediaId contains the item's hierarchy, we know the item's parent mediaId and we can
    // fetch and iterate over it and find the proper MediaItem, from which we get the title,
    // This is temporary - a better solution (a method to get a mediaItem by its mediaID)
    // is being worked out in the platform and should be available soon.
    LogHelper.d(TAG, "on updateTitle: mediaId=", mMediaId, " parentID=", parentId);
    if (parentId != null) {
        MediaBrowserCompat mediaBrowser = mMediaFragmentListener.getMediaBrowser();
        LogHelper.d(TAG, "on updateTitle: mediaBrowser is ",
                mediaBrowser == null ? "null" : ("not null, connected=" + mediaBrowser.isConnected()));
        if (mediaBrowser != null && mediaBrowser.isConnected()) {
            // Unsubscribing is required to guarantee that we will get the initial values.
            // Otherwise, if there is another callback subscribed to this mediaID, mediaBrowser
            // will only call this callback when the media content change.
            mediaBrowser.unsubscribe(parentId);
            mediaBrowser.subscribe(parentId, new MediaBrowserCompat.SubscriptionCallback() {
                @Override
                public void onChildrenLoaded(String parentId, List<MediaBrowserCompat.MediaItem> children) {
                    LogHelper.d(TAG, "Got ", children.size(), " children for ", parentId, ". Looking for ",
                            mMediaId);
                    for (MediaBrowserCompat.MediaItem item : children) {
                        LogHelper.d(TAG, "child ", item.getMediaId());
                        if (item.getMediaId().equals(mMediaId)) {
                            if (mMediaFragmentListener != null) {
                                mMediaFragmentListener.setToolbarTitle(item.getDescription().getTitle());
                            }
                            return;
                        }
                    }
                    mMediaFragmentListener.getMediaBrowser().unsubscribe(parentId);
                }

                @Override
                public void onError(String id) {
                    super.onError(id);
                    LogHelper.d(TAG, "subscribe error: id=", id);
                }
            });
        }
    }
}