FileDocCategorySizeDatePackage
MediaRouteActionProvider.javaAPI DocAndroid 5.1 API6641Thu Mar 12 22:22:10 GMT 2015android.app

MediaRouteActionProvider

public class MediaRouteActionProvider extends android.view.ActionProvider
The media route action provider displays a {@link MediaRouteButton media route button} in the application's {@link ActionBar} to allow the user to select routes and to control the currently selected route.

The application must specify the kinds of routes that the user should be allowed to select by specifying the route types with the {@link #setRouteTypes} method.

Refer to {@link MediaRouteButton} for a description of the button that will appear in the action bar menu. Note that instead of disabling the button when no routes are available, the action provider will instead make the menu item invisible. In this way, the button will only be visible when it is possible for the user to discover and select a matching route.

Fields Summary
private static final String
TAG
private final android.content.Context
mContext
private final android.media.MediaRouter
mRouter
private final MediaRouterCallback
mCallback
private int
mRouteTypes
private MediaRouteButton
mButton
private View.OnClickListener
mExtendedSettingsListener
Constructors Summary
public MediaRouteActionProvider(android.content.Context context)


       
        super(context);

        mContext = context;
        mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
        mCallback = new MediaRouterCallback(this);

        // Start with live audio by default.
        // TODO Update this when new route types are added; segment by API level
        // when different route types were added.
        setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_AUDIO);
    
Methods Summary
public booleanisVisible()

        return mRouter.isRouteAvailable(mRouteTypes,
                MediaRouter.AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE);
    
public android.view.ViewonCreateActionView()

        throw new UnsupportedOperationException("Use onCreateActionView(MenuItem) instead.");
    
public android.view.ViewonCreateActionView(android.view.MenuItem item)

        if (mButton != null) {
            Log.e(TAG, "onCreateActionView: this ActionProvider is already associated " +
                    "with a menu item. Don't reuse MediaRouteActionProvider instances! " +
                    "Abandoning the old one...");
        }

        mButton = new MediaRouteButton(mContext);
        mButton.setCheatSheetEnabled(true);
        mButton.setRouteTypes(mRouteTypes);
        mButton.setExtendedSettingsClickListener(mExtendedSettingsListener);
        mButton.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        return mButton;
    
public booleanonPerformDefaultAction()

        if (mButton != null) {
            return mButton.showDialogInternal();
        }
        return false;
    
public booleanoverridesItemVisibility()

        return true;
    
private voidrefreshRoute()

        refreshVisibility();
    
public voidsetExtendedSettingsClickListener(View.OnClickListener listener)

        mExtendedSettingsListener = listener;
        if (mButton != null) {
            mButton.setExtendedSettingsClickListener(listener);
        }
    
public voidsetRouteTypes(int types)
Sets the types of routes that will be shown in the media route chooser dialog launched by this button.

param
types The route types to match.

        if (mRouteTypes != types) {
            // FIXME: We currently have no way of knowing whether the action provider
            // is still needed by the UI.  Unfortunately this means the action provider
            // may leak callbacks until garbage collection occurs.  This may result in
            // media route providers doing more work than necessary in the short term
            // while trying to discover routes that are no longer of interest to the
            // application.  To solve this problem, the action provider will need some
            // indication from the framework that it is being destroyed.
            if (mRouteTypes != 0) {
                mRouter.removeCallback(mCallback);
            }
            mRouteTypes = types;
            if (types != 0) {
                mRouter.addCallback(types, mCallback,
                        MediaRouter.CALLBACK_FLAG_PASSIVE_DISCOVERY);
            }
            refreshRoute();

            if (mButton != null) {
                mButton.setRouteTypes(mRouteTypes);
            }
        }