FileDocCategorySizeDatePackage
MediaRouteDiscoveryFragment.javaAPI DocAndroid 5.1 API4909Thu Mar 12 22:22:56 GMT 2015android.support.v7.app

MediaRouteDiscoveryFragment

public class MediaRouteDiscoveryFragment extends android.support.v4.app.Fragment
Media route discovery fragment.

This fragment takes care of registering a callback for media route discovery during the activity's {@link android.app.Activity#onStart onStart()} phase and removing it during the {@link android.app.Activity#onStart onStop()} phase.

The application must supply a route selector to specify the kinds of routes to discover. The application may also override {@link #onCreateCallback} to provide the {@link MediaRouter} callback to register.

Fields Summary
private final String
ARGUMENT_SELECTOR
private android.support.v7.media.MediaRouter
mRouter
private android.support.v7.media.MediaRouteSelector
mSelector
private MediaRouter.Callback
mCallback
Constructors Summary
public MediaRouteDiscoveryFragment()


      
    
Methods Summary
private voidensureRouteSelector()

        if (mSelector == null) {
            Bundle args = getArguments();
            if (args != null) {
                mSelector = MediaRouteSelector.fromBundle(args.getBundle(ARGUMENT_SELECTOR));
            }
            if (mSelector == null) {
                mSelector = MediaRouteSelector.EMPTY;
            }
        }
    
private voidensureRouter()

        if (mRouter == null) {
            mRouter = MediaRouter.getInstance(getActivity());
        }
    
public android.support.v7.media.MediaRoutergetMediaRouter()
Gets the media router instance.

        ensureRouter();
        return mRouter;
    
public android.support.v7.media.MediaRouteSelectorgetRouteSelector()
Gets the media route selector for filtering the routes to be discovered.

return
The selector, never null.

        ensureRouteSelector();
        return mSelector;
    
public MediaRouter.CallbackonCreateCallback()
Called to create the {@link android.support.v7.media.MediaRouter.Callback callback} that will be registered.

The default callback does nothing. The application may override this method to supply its own callback.

return
The new callback, or null if no callback should be registered.

        return new MediaRouter.Callback() { };
    
public intonPrepareCallbackFlags()
Called to prepare the callback flags that will be used when the {@link android.support.v7.media.MediaRouter.Callback callback} is registered.

The default implementation returns {@link MediaRouter#CALLBACK_FLAG_REQUEST_DISCOVERY}.

return
The desired callback flags.

        return MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY;
    
public voidonStart()

        super.onStart();

        ensureRouteSelector();
        ensureRouter();
        mCallback = onCreateCallback();
        if (mCallback != null) {
            mRouter.addCallback(mSelector, mCallback, onPrepareCallbackFlags());
        }
    
public voidonStop()

        if (mCallback != null) {
            mRouter.removeCallback(mCallback);
            mCallback = null;
        }

        super.onStop();
    
public voidsetRouteSelector(android.support.v7.media.MediaRouteSelector selector)
Sets the media route selector for filtering the routes to be discovered. This method must be called before the fragment is added.

param
selector The selector to set.

        if (selector == null) {
            throw new IllegalArgumentException("selector must not be null");
        }

        ensureRouteSelector();
        if (!mSelector.equals(selector)) {
            mSelector = selector;

            Bundle args = getArguments();
            if (args == null) {
                args = new Bundle();
            }
            args.putBundle(ARGUMENT_SELECTOR, selector.asBundle());
            setArguments(args);

            if (mCallback != null) {
                mRouter.removeCallback(mCallback);
                mRouter.addCallback(mSelector, mCallback, onPrepareCallbackFlags());
            }
        }