1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v7.media;
18
19import android.content.Intent;
20import android.os.Messenger;
21
22/**
23 * Defines the communication protocol for media route provider services.
24 * @hide
25 */
26abstract class MediaRouteProviderProtocol {
27    /**
28     * The {@link Intent} that must be declared as handled by the service.
29     * Put this in your manifest.
30     */
31    public static final String SERVICE_INTERFACE =
32            "android.media.MediaRouteProviderService";
33
34    /*
35     * Messages sent from the client to the service.
36     * DO NOT RENUMBER THESE!
37     */
38
39    /** (client v1)
40     * Register client.
41     * - replyTo : client messenger
42     * - arg1    : request id
43     * - arg2    : client version
44     */
45    public static final int CLIENT_MSG_REGISTER = 1;
46
47    /** (client v1)
48     * Unregister client.
49     * - replyTo : client messenger
50     * - arg1    : request id
51     */
52    public static final int CLIENT_MSG_UNREGISTER = 2;
53
54    /** (client v1)
55     * Create route controller.
56     * - replyTo : client messenger
57     * - arg1    : request id
58     * - arg2    : route controller id
59     * - CLIENT_DATA_ROUTE_ID : route id string
60     */
61    public static final int CLIENT_MSG_CREATE_ROUTE_CONTROLLER = 3;
62
63    /** (client v1)
64     * Release route controller.
65     * - replyTo : client messenger
66     * - arg1    : request id
67     * - arg2    : route controller id
68     */
69    public static final int CLIENT_MSG_RELEASE_ROUTE_CONTROLLER = 4;
70
71    /** (client v1)
72     * Select route.
73     * - replyTo : client messenger
74     * - arg1    : request id
75     * - arg2    : route controller id
76     */
77    public static final int CLIENT_MSG_SELECT_ROUTE = 5;
78
79    /** (client v1)
80     * Unselect route.
81     * - replyTo : client messenger
82     * - arg1    : request id
83     * - arg2    : route controller id
84     */
85    public static final int CLIENT_MSG_UNSELECT_ROUTE = 6;
86
87    /** (client v1)
88     * Set route volume.
89     * - replyTo : client messenger
90     * - arg1    : request id
91     * - arg2    : route controller id
92     * - CLIENT_DATA_VOLUME : volume integer
93     */
94    public static final int CLIENT_MSG_SET_ROUTE_VOLUME = 7;
95
96    /** (client v1)
97     * Update route volume.
98     * - replyTo : client messenger
99     * - arg1    : request id
100     * - arg2    : route controller id
101     * - CLIENT_DATA_VOLUME : volume delta integer
102     */
103    public static final int CLIENT_MSG_UPDATE_ROUTE_VOLUME = 8;
104
105    /** (client v1)
106     * Route control request.
107     * - replyTo : client messenger
108     * - arg1    : request id
109     * - arg2    : route controller id
110     * - obj     : media control intent
111     */
112    public static final int CLIENT_MSG_ROUTE_CONTROL_REQUEST = 9;
113
114    /** (client v1)
115     * Sets the discovery request.
116     * - replyTo : client messenger
117     * - arg1    : request id
118     * - obj     : discovery request bundle, or null if none
119     */
120    public static final int CLIENT_MSG_SET_DISCOVERY_REQUEST = 10;
121
122    public static final String CLIENT_DATA_ROUTE_ID = "routeId";
123    public static final String CLIENT_DATA_VOLUME = "volume";
124    public static final String CLIENT_DATA_UNSELECT_REASON = "unselectReason";
125
126    /*
127     * Messages sent from the service to the client.
128     * DO NOT RENUMBER THESE!
129     */
130
131    /** (service v1)
132     * Generic failure sent in response to any unrecognized or malformed request.
133     * - arg1    : request id
134     */
135    public static final int SERVICE_MSG_GENERIC_FAILURE = 0;
136
137    /** (service v1)
138     * Generic failure sent in response to a successful message.
139     * - arg1    : request id
140     */
141    public static final int SERVICE_MSG_GENERIC_SUCCESS = 1;
142
143    /** (service v1)
144     * Registration succeeded.
145     * - arg1    : request id
146     * - arg2    : server version
147     * - obj     : route provider descriptor bundle, or null
148     */
149    public static final int SERVICE_MSG_REGISTERED = 2;
150
151    /** (service v1)
152     * Route control request success result.
153     * - arg1    : request id
154     * - obj     : result data bundle, or null
155     */
156    public static final int SERVICE_MSG_CONTROL_REQUEST_SUCCEEDED = 3;
157
158    /** (service v1)
159     * Route control request failure result.
160     * - arg1    : request id
161     * - obj     : result data bundle, or null
162     * - SERVICE_DATA_ERROR: error message
163     */
164    public static final int SERVICE_MSG_CONTROL_REQUEST_FAILED = 4;
165
166    /** (service v1)
167     * Route provider descriptor changed.  (unsolicited event)
168     * - arg1    : reserved (0)
169     * - obj     : route provider descriptor bundle, or null
170     */
171    public static final int SERVICE_MSG_DESCRIPTOR_CHANGED = 5;
172
173    public static final String SERVICE_DATA_ERROR = "error";
174
175    /*
176     * Recognized client version numbers.  (Reserved for future use.)
177     * DO NOT RENUMBER THESE!
178     */
179
180    public static final int CLIENT_VERSION_1 = 1;
181    public static final int CLIENT_VERSION_CURRENT = CLIENT_VERSION_1;
182
183    /*
184     * Recognized server version numbers.  (Reserved for future use.)
185     * DO NOT RENUMBER THESE!
186     */
187
188    public static final int SERVICE_VERSION_1 = 1;
189    public static final int SERVICE_VERSION_CURRENT = SERVICE_VERSION_1;
190
191    /**
192     * Returns true if the messenger object is valid.
193     * <p>
194     * The messenger constructor and unparceling code does not check whether the
195     * provided IBinder is a valid IMessenger object.  As a result, it's possible
196     * for a peer to send an invalid IBinder that will result in crashes downstream.
197     * This method checks that the messenger is in a valid state.
198     * </p>
199     */
200    public static boolean isValidRemoteMessenger(Messenger messenger) {
201        try {
202            return messenger != null && messenger.getBinder() != null;
203        } catch (NullPointerException ex) {
204            // If the messenger was constructed with a binder interface other than
205            // IMessenger then the call to getBinder() will crash with an NPE.
206            return false;
207        }
208    }
209}
210