1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.media;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.IRemoteDisplayCallback;
import android.media.IRemoteDisplayProvider;
import android.media.RemoteDisplayState;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.IBinder.DeathRecipient;
import android.os.UserHandle;
import android.util.Log;
import android.util.Slog;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.Objects;
/**
* Maintains a connection to a particular remote display provider service.
*/
final class RemoteDisplayProviderProxy implements ServiceConnection {
private static final String TAG = "RemoteDisplayProvider"; // max. 23 chars
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private final Context mContext;
private final ComponentName mComponentName;
private final int mUserId;
private final Handler mHandler;
private Callback mDisplayStateCallback;
// Connection state
private boolean mRunning;
private boolean mBound;
private Connection mActiveConnection;
private boolean mConnectionReady;
// Logical state
private int mDiscoveryMode;
private String mSelectedDisplayId;
private RemoteDisplayState mDisplayState;
private boolean mScheduledDisplayStateChangedCallback;
public RemoteDisplayProviderProxy(Context context, ComponentName componentName,
int userId) {
mContext = context;
mComponentName = componentName;
mUserId = userId;
mHandler = new Handler();
}
public void dump(PrintWriter pw, String prefix) {
pw.println(prefix + "Proxy");
pw.println(prefix + " mUserId=" + mUserId);
pw.println(prefix + " mRunning=" + mRunning);
pw.println(prefix + " mBound=" + mBound);
pw.println(prefix + " mActiveConnection=" + mActiveConnection);
pw.println(prefix + " mConnectionReady=" + mConnectionReady);
pw.println(prefix + " mDiscoveryMode=" + mDiscoveryMode);
pw.println(prefix + " mSelectedDisplayId=" + mSelectedDisplayId);
pw.println(prefix + " mDisplayState=" + mDisplayState);
}
public void setCallback(Callback callback) {
mDisplayStateCallback = callback;
}
public RemoteDisplayState getDisplayState() {
return mDisplayState;
}
public void setDiscoveryMode(int mode) {
if (mDiscoveryMode != mode) {
mDiscoveryMode = mode;
if (mConnectionReady) {
mActiveConnection.setDiscoveryMode(mode);
}
updateBinding();
}
}
public void setSelectedDisplay(String id) {
if (!Objects.equals(mSelectedDisplayId, id)) {
if (mConnectionReady && mSelectedDisplayId != null) {
mActiveConnection.disconnect(mSelectedDisplayId);
}
mSelectedDisplayId = id;
if (mConnectionReady && id != null) {
mActiveConnection.connect(id);
}
updateBinding();
}
}
public void setDisplayVolume(int volume) {
if (mConnectionReady && mSelectedDisplayId != null) {
mActiveConnection.setVolume(mSelectedDisplayId, volume);
}
}
public void adjustDisplayVolume(int delta) {
if (mConnectionReady && mSelectedDisplayId != null) {
mActiveConnection.adjustVolume(mSelectedDisplayId, delta);
}
}
public boolean hasComponentName(String packageName, String className) {
return mComponentName.getPackageName().equals(packageName)
&& mComponentName.getClassName().equals(className);
}
public String getFlattenedComponentName() {
return mComponentName.flattenToShortString();
}
public void start() {
if (!mRunning) {
if (DEBUG) {
Slog.d(TAG, this + ": Starting");
}
mRunning = true;
updateBinding();
}
}
public void stop() {
if (mRunning) {
if (DEBUG) {
Slog.d(TAG, this + ": Stopping");
}
mRunning = false;
updateBinding();
}
}
public void rebindIfDisconnected() {
if (mActiveConnection == null && shouldBind()) {
unbind();
bind();
}
}
private void updateBinding() {
if (shouldBind()) {
bind();
} else {
unbind();
}
}
private boolean shouldBind() {
if (mRunning) {
// Bind whenever there is a discovery request or selected display.
if (mDiscoveryMode != RemoteDisplayState.DISCOVERY_MODE_NONE
|| mSelectedDisplayId != null) {
return true;
}
}
return false;
}
private void bind() {
if (!mBound) {
if (DEBUG) {
Slog.d(TAG, this + ": Binding");
}
Intent service = new Intent(RemoteDisplayState.SERVICE_INTERFACE);
service.setComponent(mComponentName);
try {
mBound = mContext.bindServiceAsUser(service, this, Context.BIND_AUTO_CREATE,
new UserHandle(mUserId));
if (!mBound && DEBUG) {
Slog.d(TAG, this + ": Bind failed");
}
} catch (SecurityException ex) {
if (DEBUG) {
Slog.d(TAG, this + ": Bind failed", ex);
}
}
}
}
private void unbind() {
if (mBound) {
if (DEBUG) {
Slog.d(TAG, this + ": Unbinding");
}
mBound = false;
disconnect();
mContext.unbindService(this);
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (DEBUG) {
Slog.d(TAG, this + ": Connected");
}
if (mBound) {
disconnect();
IRemoteDisplayProvider provider = IRemoteDisplayProvider.Stub.asInterface(service);
if (provider != null) {
Connection connection = new Connection(provider);
if (connection.register()) {
mActiveConnection = connection;
} else {
if (DEBUG) {
Slog.d(TAG, this + ": Registration failed");
}
}
} else {
Slog.e(TAG, this + ": Service returned invalid remote display provider binder");
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
if (DEBUG) {
Slog.d(TAG, this + ": Service disconnected");
}
disconnect();
}
private void onConnectionReady(Connection connection) {
if (mActiveConnection == connection) {
mConnectionReady = true;
if (mDiscoveryMode != RemoteDisplayState.DISCOVERY_MODE_NONE) {
mActiveConnection.setDiscoveryMode(mDiscoveryMode);
}
if (mSelectedDisplayId != null) {
mActiveConnection.connect(mSelectedDisplayId);
}
}
}
private void onConnectionDied(Connection connection) {
if (mActiveConnection == connection) {
if (DEBUG) {
Slog.d(TAG, this + ": Service connection died");
}
disconnect();
}
}
private void onDisplayStateChanged(Connection connection, RemoteDisplayState state) {
if (mActiveConnection == connection) {
if (DEBUG) {
Slog.d(TAG, this + ": State changed, state=" + state);
}
setDisplayState(state);
}
}
private void disconnect() {
if (mActiveConnection != null) {
if (mSelectedDisplayId != null) {
mActiveConnection.disconnect(mSelectedDisplayId);
}
mConnectionReady = false;
mActiveConnection.dispose();
mActiveConnection = null;
setDisplayState(null);
}
}
private void setDisplayState(RemoteDisplayState state) {
if (!Objects.equals(mDisplayState, state)) {
mDisplayState = state;
if (!mScheduledDisplayStateChangedCallback) {
mScheduledDisplayStateChangedCallback = true;
mHandler.post(mDisplayStateChanged);
}
}
}
@Override
public String toString() {
return "Service connection " + mComponentName.flattenToShortString();
}
private final Runnable mDisplayStateChanged = new Runnable() {
@Override
public void run() {
mScheduledDisplayStateChangedCallback = false;
if (mDisplayStateCallback != null) {
mDisplayStateCallback.onDisplayStateChanged(
RemoteDisplayProviderProxy.this, mDisplayState);
}
}
};
public interface Callback {
void onDisplayStateChanged(RemoteDisplayProviderProxy provider, RemoteDisplayState state);
}
private final class Connection implements DeathRecipient {
private final IRemoteDisplayProvider mProvider;
private final ProviderCallback mCallback;
public Connection(IRemoteDisplayProvider provider) {
mProvider = provider;
mCallback = new ProviderCallback(this);
}
public boolean register() {
try {
mProvider.asBinder().linkToDeath(this, 0);
mProvider.setCallback(mCallback);
mHandler.post(new Runnable() {
@Override
public void run() {
onConnectionReady(Connection.this);
}
});
return true;
} catch (RemoteException ex) {
binderDied();
}
return false;
}
public void dispose() {
mProvider.asBinder().unlinkToDeath(this, 0);
mCallback.dispose();
}
public void setDiscoveryMode(int mode) {
try {
mProvider.setDiscoveryMode(mode);
} catch (RemoteException ex) {
Slog.e(TAG, "Failed to deliver request to set discovery mode.", ex);
}
}
public void connect(String id) {
try {
mProvider.connect(id);
} catch (RemoteException ex) {
Slog.e(TAG, "Failed to deliver request to connect to display.", ex);
}
}
public void disconnect(String id) {
try {
mProvider.disconnect(id);
} catch (RemoteException ex) {
Slog.e(TAG, "Failed to deliver request to disconnect from display.", ex);
}
}
public void setVolume(String id, int volume) {
try {
mProvider.setVolume(id, volume);
} catch (RemoteException ex) {
Slog.e(TAG, "Failed to deliver request to set display volume.", ex);
}
}
public void adjustVolume(String id, int volume) {
try {
mProvider.adjustVolume(id, volume);
} catch (RemoteException ex) {
Slog.e(TAG, "Failed to deliver request to adjust display volume.", ex);
}
}
@Override
public void binderDied() {
mHandler.post(new Runnable() {
@Override
public void run() {
onConnectionDied(Connection.this);
}
});
}
void postStateChanged(final RemoteDisplayState state) {
mHandler.post(new Runnable() {
@Override
public void run() {
onDisplayStateChanged(Connection.this, state);
}
});
}
}
/**
* Receives callbacks from the service.
* <p>
* This inner class is static and only retains a weak reference to the connection
* to prevent the client from being leaked in case the service is holding an
* active reference to the client's callback.
* </p>
*/
private static final class ProviderCallback extends IRemoteDisplayCallback.Stub {
private final WeakReference<Connection> mConnectionRef;
public ProviderCallback(Connection connection) {
mConnectionRef = new WeakReference<Connection>(connection);
}
public void dispose() {
mConnectionRef.clear();
}
@Override
public void onStateChanged(RemoteDisplayState state) throws RemoteException {
Connection connection = mConnectionRef.get();
if (connection != null) {
connection.postStateChanged(state);
}
}
}
}
|