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

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

Introduction

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

Prototype

public void unsubscribe(@NonNull String parentId) 

Source Link

Document

Unsubscribes for changes to the children of the specified media id.

Usage

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

@Override
public void onStop() {
    super.onStop();

    MediaBrowserCompat mediaBrowser = mMediaFragmentListener.getMediaBrowser();
    if (mediaBrowser != null && mediaBrowser.isConnected() && mMediaId != null) {
        mediaBrowser.unsubscribe(mMediaId);
    }/*from   www. j  a  v a 2s  .  c  o  m*/
    if (mMediaControllerProvider.getSupportMediaController() != null) {
        mMediaControllerProvider.getSupportMediaController().unregisterCallback(mMediaControllerCallback);
    }
    this.getActivity().unregisterReceiver(mConnectivityChangeReceiver);
}

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

private void updateTitle() {
    if (MediaIDHelper.MEDIA_ID_ROOT.equals(mMediaId)) {
        mMediaFragmentListener.setToolbarTitle(null);
        return;/*from   ww w .  j  a v  a2  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);
                }
            });
        }
    }
}