FileDocCategorySizeDatePackage
RegisteredMediaRouteProvider.javaAPI DocAndroid 5.1 API22768Thu Mar 12 22:22:56 GMT 2015android.support.v7.media

RegisteredMediaRouteProvider

public final class RegisteredMediaRouteProvider extends MediaRouteProvider implements android.content.ServiceConnection
Maintains a connection to a particular media route provider service.

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private final android.content.ComponentName
mComponentName
private final PrivateHandler
mPrivateHandler
private final ArrayList
mControllers
private boolean
mStarted
private boolean
mBound
private Connection
mActiveConnection
private boolean
mConnectionReady
Constructors Summary
public RegisteredMediaRouteProvider(android.content.Context context, android.content.ComponentName componentName)


         
        super(context, new ProviderMetadata(componentName));

        mComponentName = componentName;
        mPrivateHandler = new PrivateHandler();
    
Methods Summary
private voidattachControllersToConnection()

        int count = mControllers.size();
        for (int i = 0; i < count; i++) {
            mControllers.get(i).attachConnection(mActiveConnection);
        }
    
private voidbind()

        if (!mBound) {
            if (DEBUG) {
                Log.d(TAG, this + ": Binding");
            }

            Intent service = new Intent(MediaRouteProviderProtocol.SERVICE_INTERFACE);
            service.setComponent(mComponentName);
            try {
                mBound = getContext().bindService(service, this, Context.BIND_AUTO_CREATE);
                if (!mBound && DEBUG) {
                    Log.d(TAG, this + ": Bind failed");
                }
            } catch (SecurityException ex) {
                if (DEBUG) {
                    Log.d(TAG, this + ": Bind failed", ex);
                }
            }
        }
    
private voiddetachControllersFromConnection()

        int count = mControllers.size();
        for (int i = 0; i < count; i++) {
            mControllers.get(i).detachConnection();
        }
    
private voiddisconnect()

        if (mActiveConnection != null) {
            setDescriptor(null);
            mConnectionReady = false;
            detachControllersFromConnection();
            mActiveConnection.dispose();
            mActiveConnection = null;
        }
    
public booleanhasComponentName(java.lang.String packageName, java.lang.String className)

        return mComponentName.getPackageName().equals(packageName)
                && mComponentName.getClassName().equals(className);
    
private voidonConnectionDescriptorChanged(android.support.v7.media.RegisteredMediaRouteProvider$Connection connection, MediaRouteProviderDescriptor descriptor)

        if (mActiveConnection == connection) {
            if (DEBUG) {
                Log.d(TAG, this + ": Descriptor changed, descriptor=" + descriptor);
            }
            setDescriptor(descriptor);
        }
    
private voidonConnectionDied(android.support.v7.media.RegisteredMediaRouteProvider$Connection connection)

        if (mActiveConnection == connection) {
            if (DEBUG) {
                Log.d(TAG, this + ": Service connection died");
            }
            disconnect();
        }
    
private voidonConnectionError(android.support.v7.media.RegisteredMediaRouteProvider$Connection connection, java.lang.String error)

        if (mActiveConnection == connection) {
            if (DEBUG) {
                Log.d(TAG, this + ": Service connection error - " + error);
            }
            unbind();
        }
    
private voidonConnectionReady(android.support.v7.media.RegisteredMediaRouteProvider$Connection connection)

        if (mActiveConnection == connection) {
            mConnectionReady = true;
            attachControllersToConnection();

            MediaRouteDiscoveryRequest request = getDiscoveryRequest();
            if (request != null) {
                mActiveConnection.setDiscoveryRequest(request);
            }
        }
    
private voidonControllerReleased(android.support.v7.media.RegisteredMediaRouteProvider$Controller controller)

        mControllers.remove(controller);
        controller.detachConnection();
        updateBinding();
    
public RouteControlleronCreateRouteController(java.lang.String routeId)

        MediaRouteProviderDescriptor descriptor = getDescriptor();
        if (descriptor != null) {
            List<MediaRouteDescriptor> routes = descriptor.getRoutes();
            final int count = routes.size();
            for (int i = 0; i < count; i++) {
                final MediaRouteDescriptor route = routes.get(i);
                if (route.getId().equals(routeId)) {
                    Controller controller = new Controller(routeId);
                    mControllers.add(controller);
                    if (mConnectionReady) {
                        controller.attachConnection(mActiveConnection);
                    }
                    updateBinding();
                    return controller;
                }
            }
        }
        return null;
    
public voidonDiscoveryRequestChanged(MediaRouteDiscoveryRequest request)

        if (mConnectionReady) {
            mActiveConnection.setDiscoveryRequest(request);
        }
        updateBinding();
    
public voidonServiceConnected(android.content.ComponentName name, android.os.IBinder service)

        if (DEBUG) {
            Log.d(TAG, this + ": Connected");
        }

        if (mBound) {
            disconnect();

            Messenger messenger = (service != null ? new Messenger(service) : null);
            if (isValidRemoteMessenger(messenger)) {
                Connection connection = new Connection(messenger);
                if (connection.register()) {
                    mActiveConnection = connection;
                } else {
                    if (DEBUG) {
                        Log.d(TAG, this + ": Registration failed");
                    }
                }
            } else {
                Log.e(TAG, this + ": Service returned invalid messenger binder");
            }
        }
    
public voidonServiceDisconnected(android.content.ComponentName name)

        if (DEBUG) {
            Log.d(TAG, this + ": Service disconnected");
        }
        disconnect();
    
public voidrebindIfDisconnected()

        if (mActiveConnection == null && shouldBind()) {
            unbind();
            bind();
        }
    
private booleanshouldBind()

        if (mStarted) {
            // Bind whenever there is a discovery request.
            if (getDiscoveryRequest() != null) {
                return true;
            }

            // Bind whenever the application has an active route controller.
            // This means that one of this provider's routes is selected.
            if (!mControllers.isEmpty()) {
                return true;
            }
        }
        return false;
    
public voidstart()

        if (!mStarted) {
            if (DEBUG) {
                Log.d(TAG, this + ": Starting");
            }

            mStarted = true;
            updateBinding();
        }
    
public voidstop()

        if (mStarted) {
            if (DEBUG) {
                Log.d(TAG, this + ": Stopping");
            }

            mStarted = false;
            updateBinding();
        }
    
public java.lang.StringtoString()

        return "Service connection " + mComponentName.flattenToShortString();
    
private voidunbind()

        if (mBound) {
            if (DEBUG) {
                Log.d(TAG, this + ": Unbinding");
            }

            mBound = false;
            disconnect();
            getContext().unbindService(this);
        }
    
private voidupdateBinding()

        if (shouldBind()) {
            bind();
        } else {
            unbind();
        }