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

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

Introduction

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

Prototype

public boolean isConnected() 

Source Link

Document

Returns whether the browser is connected to the service.

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);
    }/*  w w w  .  j  av a2 s  .c om*/
    if (mMediaControllerProvider.getSupportMediaController() != null) {
        mMediaControllerProvider.getSupportMediaController().unregisterCallback(mMediaControllerCallback);
    }
    this.getActivity().unregisterReceiver(mConnectivityChangeReceiver);
}

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

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

    // fetch browsing information to fill the listview:
    MediaBrowserCompat mediaBrowser = mMediaFragmentListener.getMediaBrowser();

    LogHelper.d(TAG, "fragment.onStart, mediaId=", mMediaId, "  onConnected=" + mediaBrowser.isConnected());

    if (mediaBrowser != null && mediaBrowser.isConnected()) {
        onConnected();//from w w w .java 2s .c o  m
    }

    // Registers BroadcastReceiver to track network connection changes.
    this.getActivity().registerReceiver(mConnectivityChangeReceiver,
            new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}

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

private void updateTitle() {
    if (MediaIDHelper.MEDIA_ID_ROOT.equals(mMediaId)) {
        mMediaFragmentListener.setToolbarTitle(null);
        return;//w ww .  ja  va 2s.co  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);
                }
            });
        }
    }
}