MediaRouteProviderServicepublic 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_INTERFACEThe {@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 int | findClient(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$ClientRecord | getClient(android.os.Messenger messenger)
int index = findClient(messenger);
return index >= 0 ? mClients.get(index) : null;
| private static java.lang.String | getClientId(android.os.Messenger messenger)
return "Client connection " + messenger.getBinder().toString();
| public MediaRouteProvider | getMediaRouteProvider()Gets the media route provider offered by this service.
return mProvider;
| public android.os.IBinder | onBind(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 void | onBinderDied(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 MediaRouteProvider | onCreateMediaRouteProvider()Called by the system when it is time to create the media route provider.
| private boolean | onCreateRouteController(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 boolean | onRegisterClient(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 boolean | onReleaseRouteController(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 boolean | onRouteControlRequest(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 boolean | onSelectRoute(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 boolean | onSetDiscoveryRequest(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 boolean | onSetRouteVolume(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 boolean | onUnregisterClient(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 boolean | onUnselectRoute(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 boolean | onUpdateRouteVolume(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 void | sendDescriptorChanged(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 void | sendGenericFailure(android.os.Messenger messenger, int requestId)
if (requestId != 0) {
sendReply(messenger, SERVICE_MSG_GENERIC_FAILURE, requestId, 0, null, null);
}
| private static void | sendGenericSuccess(android.os.Messenger messenger, int requestId)
if (requestId != 0) {
sendReply(messenger, SERVICE_MSG_GENERIC_SUCCESS, requestId, 0, null, null);
}
| private static void | sendReply(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 boolean | updateCompositeDiscoveryRequest()
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;
|
|