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

MediaRouteProviderService

public abstract class MediaRouteProviderService extends android.app.Service
Base class for media route provider services.

To implement your own media route provider service, extend this class and override the {@link #onCreateMediaRouteProvider} method to return an instance of your {@link MediaRouteProvider}.

Declare your media route provider service in your application manifest like this:

<service android:name=".MyMediaRouteProviderService"
android:label="@string/my_media_route_provider_service">
<intent-filter>
<action android:name="android.media.MediaRouteProviderService" />
</intent-filter>
</service>

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private final ArrayList
mClients
private final ReceiveHandler
mReceiveHandler
private final android.os.Messenger
mReceiveMessenger
private final PrivateHandler
mPrivateHandler
private final ProviderCallback
mProviderCallback
private MediaRouteProvider
mProvider
private MediaRouteDiscoveryRequest
mCompositeDiscoveryRequest
public static final String
SERVICE_INTERFACE
The {@link Intent} that must be declared as handled by the service. Put this in your manifest.
private static final int
PRIVATE_MSG_CLIENT_DIED
Constructors Summary
public MediaRouteProviderService()
Creates a media route provider service.


               
      
        mReceiveHandler = new ReceiveHandler(this);
        mReceiveMessenger = new Messenger(mReceiveHandler);
        mPrivateHandler = new PrivateHandler();
        mProviderCallback = new ProviderCallback();
    
Methods Summary
private intfindClient(android.os.Messenger messenger)

        final int count = mClients.size();
        for (int i = 0; i < count; i++) {
            ClientRecord client = mClients.get(i);
            if (client.hasMessenger(messenger)) {
                return i;
            }
        }
        return -1;
    
private android.support.v7.media.MediaRouteProviderService$ClientRecordgetClient(android.os.Messenger messenger)

        int index = findClient(messenger);
        return index >= 0 ? mClients.get(index) : null;
    
private static java.lang.StringgetClientId(android.os.Messenger messenger)

        return "Client connection " + messenger.getBinder().toString();
    
public MediaRouteProvidergetMediaRouteProvider()
Gets the media route provider offered by this service.

return
The media route provider offered by this service, or null if it has not yet been created.
see
#onCreateMediaRouteProvider()

        return mProvider;
    
public android.os.IBinderonBind(android.content.Intent intent)

        if (intent.getAction().equals(SERVICE_INTERFACE)) {
            if (mProvider == null) {
                MediaRouteProvider provider = onCreateMediaRouteProvider();
                if (provider != null) {
                    String providerPackage = provider.getMetadata().getPackageName();
                    if (!providerPackage.equals(getPackageName())) {
                        throw new IllegalStateException("onCreateMediaRouteProvider() returned "
                                + "a provider whose package name does not match the package "
                                + "name of the service.  A media route provider service can "
                                + "only export its own media route providers.  "
                                + "Provider package name: " + providerPackage
                                + ".  Service package name: " + getPackageName() + ".");
                    }
                    mProvider = provider;
                    mProvider.setCallback(mProviderCallback);
                }
            }
            if (mProvider != null) {
                return mReceiveMessenger.getBinder();
            }
        }
        return null;
    
private voidonBinderDied(android.os.Messenger messenger)

        int index = findClient(messenger);
        if (index >= 0) {
            ClientRecord client = mClients.remove(index);
            if (DEBUG) {
                Log.d(TAG, client + ": Binder died");
            }
            client.dispose();
        }
    
public abstract MediaRouteProvideronCreateMediaRouteProvider()
Called by the system when it is time to create the media route provider.

return
The media route provider offered by this service, or null if this service has decided not to offer a media route provider.

private booleanonCreateRouteController(android.os.Messenger messenger, int requestId, int controllerId, java.lang.String routeId)

        ClientRecord client = getClient(messenger);
        if (client != null) {
            if (client.createRouteController(routeId, controllerId)) {
                if (DEBUG) {
                    Log.d(TAG, client + ": Route controller created"
                            + ", controllerId=" + controllerId + ", routeId=" + routeId);
                }
                sendGenericSuccess(messenger, requestId);
                return true;
            }
        }
        return false;
    
private booleanonRegisterClient(android.os.Messenger messenger, int requestId, int version)

        if (version >= CLIENT_VERSION_1) {
            int index = findClient(messenger);
            if (index < 0) {
                ClientRecord client = new ClientRecord(messenger, version);
                if (client.register()) {
                    mClients.add(client);
                    if (DEBUG) {
                        Log.d(TAG, client + ": Registered, version=" + version);
                    }
                    if (requestId != 0) {
                        MediaRouteProviderDescriptor descriptor = mProvider.getDescriptor();
                        sendReply(messenger, SERVICE_MSG_REGISTERED,
                                requestId, SERVICE_VERSION_CURRENT,
                                descriptor != null ? descriptor.asBundle() : null, null);
                    }
                    return true;
                }
            }
        }
        return false;
    
private booleanonReleaseRouteController(android.os.Messenger messenger, int requestId, int controllerId)

        ClientRecord client = getClient(messenger);
        if (client != null) {
            if (client.releaseRouteController(controllerId)) {
                if (DEBUG) {
                    Log.d(TAG, client + ": Route controller released"
                            + ", controllerId=" + controllerId);
                }
                sendGenericSuccess(messenger, requestId);
                return true;
            }
        }
        return false;
    
private booleanonRouteControlRequest(android.os.Messenger messenger, int requestId, int controllerId, android.content.Intent intent)

        final ClientRecord client = getClient(messenger);
        if (client != null) {
            MediaRouteProvider.RouteController controller =
                    client.getRouteController(controllerId);
            if (controller != null) {
                MediaRouter.ControlRequestCallback callback = null;
                if (requestId != 0) {
                    callback = new MediaRouter.ControlRequestCallback() {
                        @Override
                        public void onResult(Bundle data) {
                            if (DEBUG) {
                                Log.d(TAG, client + ": Route control request succeeded"
                                        + ", controllerId=" + controllerId
                                        + ", intent=" + intent
                                        + ", data=" + data);
                            }
                            if (findClient(messenger) >= 0) {
                                sendReply(messenger, SERVICE_MSG_CONTROL_REQUEST_SUCCEEDED,
                                        requestId, 0, data, null);
                            }
                        }

                        @Override
                        public void onError(String error, Bundle data) {
                            if (DEBUG) {
                                Log.d(TAG, client + ": Route control request failed"
                                        + ", controllerId=" + controllerId
                                        + ", intent=" + intent
                                        + ", error=" + error + ", data=" + data);
                            }
                            if (findClient(messenger) >= 0) {
                                if (error != null) {
                                    Bundle bundle = new Bundle();
                                    bundle.putString(SERVICE_DATA_ERROR, error);
                                    sendReply(messenger, SERVICE_MSG_CONTROL_REQUEST_FAILED,
                                            requestId, 0, data, bundle);
                                } else {
                                    sendReply(messenger, SERVICE_MSG_CONTROL_REQUEST_FAILED,
                                            requestId, 0, data, null);
                                }
                            }
                        }
                    };
                }
                if (controller.onControlRequest(intent, callback)) {
                    if (DEBUG) {
                        Log.d(TAG, client + ": Route control request delivered"
                                + ", controllerId=" + controllerId + ", intent=" + intent);
                    }
                    return true;
                }
            }
        }
        return false;
    
private booleanonSelectRoute(android.os.Messenger messenger, int requestId, int controllerId)

        ClientRecord client = getClient(messenger);
        if (client != null) {
            MediaRouteProvider.RouteController controller =
                    client.getRouteController(controllerId);
            if (controller != null) {
                controller.onSelect();
                if (DEBUG) {
                    Log.d(TAG, client + ": Route selected"
                            + ", controllerId=" + controllerId);
                }
                sendGenericSuccess(messenger, requestId);
                return true;
            }
        }
        return false;
    
private booleanonSetDiscoveryRequest(android.os.Messenger messenger, int requestId, MediaRouteDiscoveryRequest request)

        ClientRecord client = getClient(messenger);
        if (client != null) {
            boolean actuallyChanged = client.setDiscoveryRequest(request);
            if (DEBUG) {
                Log.d(TAG, client + ": Set discovery request, request=" + request
                        + ", actuallyChanged=" + actuallyChanged
                        + ", compositeDiscoveryRequest=" + mCompositeDiscoveryRequest);
            }
            sendGenericSuccess(messenger, requestId);
            return true;
        }
        return false;
    
private booleanonSetRouteVolume(android.os.Messenger messenger, int requestId, int controllerId, int volume)

        ClientRecord client = getClient(messenger);
        if (client != null) {
            MediaRouteProvider.RouteController controller =
                    client.getRouteController(controllerId);
            if (controller != null) {
                controller.onSetVolume(volume);
                if (DEBUG) {
                    Log.d(TAG, client + ": Route volume changed"
                            + ", controllerId=" + controllerId + ", volume=" + volume);
                }
                sendGenericSuccess(messenger, requestId);
                return true;
            }
        }
        return false;
    
private booleanonUnregisterClient(android.os.Messenger messenger, int requestId)

        int index = findClient(messenger);
        if (index >= 0) {
            ClientRecord client = mClients.remove(index);
            if (DEBUG) {
                Log.d(TAG, client + ": Unregistered");
            }
            client.dispose();
            sendGenericSuccess(messenger, requestId);
            return true;
        }
        return false;
    
private booleanonUnselectRoute(android.os.Messenger messenger, int requestId, int controllerId, int reason)

        ClientRecord client = getClient(messenger);
        if (client != null) {
            MediaRouteProvider.RouteController controller =
                    client.getRouteController(controllerId);
            if (controller != null) {
                controller.onUnselect(reason);
                if (DEBUG) {
                    Log.d(TAG, client + ": Route unselected"
                            + ", controllerId=" + controllerId);
                }
                sendGenericSuccess(messenger, requestId);
                return true;
            }
        }
        return false;
    
private booleanonUpdateRouteVolume(android.os.Messenger messenger, int requestId, int controllerId, int delta)

        ClientRecord client = getClient(messenger);
        if (client != null) {
            MediaRouteProvider.RouteController controller =
                    client.getRouteController(controllerId);
            if (controller != null) {
                controller.onUpdateVolume(delta);
                if (DEBUG) {
                    Log.d(TAG, client + ": Route volume updated"
                            + ", controllerId=" + controllerId + ", delta=" + delta);
                }
                sendGenericSuccess(messenger, requestId);
                return true;
            }
        }
        return false;
    
private voidsendDescriptorChanged(MediaRouteProviderDescriptor descriptor)

        Bundle descriptorBundle = descriptor != null ? descriptor.asBundle() : null;
        final int count = mClients.size();
        for (int i = 0; i < count; i++) {
            ClientRecord client = mClients.get(i);
            sendReply(client.mMessenger, SERVICE_MSG_DESCRIPTOR_CHANGED, 0, 0,
                    descriptorBundle, null);
            if (DEBUG) {
                Log.d(TAG, client + ": Sent descriptor change event, descriptor=" + descriptor);
            }
        }
    
private static voidsendGenericFailure(android.os.Messenger messenger, int requestId)

        if (requestId != 0) {
            sendReply(messenger, SERVICE_MSG_GENERIC_FAILURE, requestId, 0, null, null);
        }
    
private static voidsendGenericSuccess(android.os.Messenger messenger, int requestId)

        if (requestId != 0) {
            sendReply(messenger, SERVICE_MSG_GENERIC_SUCCESS, requestId, 0, null, null);
        }
    
private static voidsendReply(android.os.Messenger messenger, int what, int requestId, int arg, java.lang.Object obj, android.os.Bundle data)

        Message msg = Message.obtain();
        msg.what = what;
        msg.arg1 = requestId;
        msg.arg2 = arg;
        msg.obj = obj;
        msg.setData(data);
        try {
            messenger.send(msg);
        } catch (DeadObjectException ex) {
            // The client died.
        } catch (RemoteException ex) {
            Log.e(TAG, "Could not send message to " + getClientId(messenger), ex);
        }
    
private booleanupdateCompositeDiscoveryRequest()

        MediaRouteDiscoveryRequest composite = null;
        MediaRouteSelector.Builder selectorBuilder = null;
        boolean activeScan = false;
        final int count = mClients.size();
        for (int i = 0; i < count; i++) {
            MediaRouteDiscoveryRequest request = mClients.get(i).mDiscoveryRequest;
            if (request != null
                    && (!request.getSelector().isEmpty() || request.isActiveScan())) {
                activeScan |= request.isActiveScan();
                if (composite == null) {
                    composite = request;
                } else {
                    if (selectorBuilder == null) {
                        selectorBuilder = new MediaRouteSelector.Builder(composite.getSelector());
                    }
                    selectorBuilder.addSelector(request.getSelector());
                }
            }
        }
        if (selectorBuilder != null) {
            composite = new MediaRouteDiscoveryRequest(selectorBuilder.build(), activeScan);
        }
        if (mCompositeDiscoveryRequest != composite
                && (mCompositeDiscoveryRequest == null
                        || !mCompositeDiscoveryRequest.equals(composite))) {
            mCompositeDiscoveryRequest = composite;
            mProvider.setDiscoveryRequest(composite);
            return true;
        }
        return false;