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
|
/*
* Copyright (C) 2014 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 android.media.session;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.media.AudioManager;
import android.media.IRemoteVolumeController;
import android.media.session.ISessionManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService;
import android.util.ArrayMap;
import android.util.Log;
import android.view.KeyEvent;
import java.util.ArrayList;
import java.util.List;
/**
* Provides support for interacting with {@link MediaSession media sessions}
* that applications have published to express their ongoing media playback
* state.
* <p>
* Use <code>Context.getSystemService(Context.MEDIA_SESSION_SERVICE)</code> to
* get an instance of this class.
*
* @see MediaSession
* @see MediaController
*/
public final class MediaSessionManager {
private static final String TAG = "SessionManager";
private final ArrayMap<OnActiveSessionsChangedListener, SessionsChangedWrapper> mListeners
= new ArrayMap<OnActiveSessionsChangedListener, SessionsChangedWrapper>();
private final Object mLock = new Object();
private final ISessionManager mService;
private Context mContext;
/**
* @hide
*/
public MediaSessionManager(Context context) {
// Consider rewriting like DisplayManagerGlobal
// Decide if we need context
mContext = context;
IBinder b = ServiceManager.getService(Context.MEDIA_SESSION_SERVICE);
mService = ISessionManager.Stub.asInterface(b);
}
/**
* Create a new session in the system and get the binder for it.
*
* @param tag A short name for debugging purposes.
* @return The binder object from the system
* @hide
*/
public @NonNull ISession createSession(@NonNull MediaSession.CallbackStub cbStub,
@NonNull String tag, int userId) throws RemoteException {
return mService.createSession(mContext.getPackageName(), cbStub, tag, userId);
}
/**
* Get a list of controllers for all ongoing sessions. The controllers will
* be provided in priority order with the most important controller at index
* 0.
* <p>
* This requires the android.Manifest.permission.MEDIA_CONTENT_CONTROL
* permission be held by the calling app. You may also retrieve this list if
* your app is an enabled notification listener using the
* {@link NotificationListenerService} APIs, in which case you must pass the
* {@link ComponentName} of your enabled listener.
*
* @param notificationListener The enabled notification listener component.
* May be null.
* @return A list of controllers for ongoing sessions.
*/
public @NonNull List<MediaController> getActiveSessions(
@Nullable ComponentName notificationListener) {
return getActiveSessionsForUser(notificationListener, UserHandle.myUserId());
}
/**
* Get active sessions for a specific user. To retrieve actions for a user
* other than your own you must hold the
* {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission
* in addition to any other requirements. If you are an enabled notification
* listener you may only get sessions for the users you are enabled for.
*
* @param notificationListener The enabled notification listener component.
* May be null.
* @param userId The user id to fetch sessions for.
* @return A list of controllers for ongoing sessions.
* @hide
*/
public @NonNull List<MediaController> getActiveSessionsForUser(
@Nullable ComponentName notificationListener, int userId) {
ArrayList<MediaController> controllers = new ArrayList<MediaController>();
try {
List<IBinder> binders = mService.getSessions(notificationListener, userId);
int size = binders.size();
for (int i = 0; i < size; i++) {
MediaController controller = new MediaController(mContext, ISessionController.Stub
.asInterface(binders.get(i)));
controllers.add(controller);
}
} catch (RemoteException e) {
Log.e(TAG, "Failed to get active sessions: ", e);
}
return controllers;
}
/**
* Add a listener to be notified when the list of active sessions
* changes.This requires the
* android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
* the calling app. You may also retrieve this list if your app is an
* enabled notification listener using the
* {@link NotificationListenerService} APIs, in which case you must pass the
* {@link ComponentName} of your enabled listener. Updates will be posted to
* the thread that registered the listener.
*
* @param sessionListener The listener to add.
* @param notificationListener The enabled notification listener component.
* May be null.
*/
public void addOnActiveSessionsChangedListener(
@NonNull OnActiveSessionsChangedListener sessionListener,
@Nullable ComponentName notificationListener) {
addOnActiveSessionsChangedListener(sessionListener, notificationListener, null);
}
/**
* Add a listener to be notified when the list of active sessions
* changes.This requires the
* android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
* the calling app. You may also retrieve this list if your app is an
* enabled notification listener using the
* {@link NotificationListenerService} APIs, in which case you must pass the
* {@link ComponentName} of your enabled listener. Updates will be posted to
* the handler specified or to the caller's thread if the handler is null.
*
* @param sessionListener The listener to add.
* @param notificationListener The enabled notification listener component.
* May be null.
* @param handler The handler to post events to.
*/
public void addOnActiveSessionsChangedListener(
@NonNull OnActiveSessionsChangedListener sessionListener,
@Nullable ComponentName notificationListener, @Nullable Handler handler) {
addOnActiveSessionsChangedListener(sessionListener, notificationListener,
UserHandle.myUserId(), handler);
}
/**
* Add a listener to be notified when the list of active sessions
* changes.This requires the
* android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
* the calling app. You may also retrieve this list if your app is an
* enabled notification listener using the
* {@link NotificationListenerService} APIs, in which case you must pass the
* {@link ComponentName} of your enabled listener.
*
* @param sessionListener The listener to add.
* @param notificationListener The enabled notification listener component.
* May be null.
* @param userId The userId to listen for changes on.
* @param handler The handler to post updates on.
* @hide
*/
public void addOnActiveSessionsChangedListener(
@NonNull OnActiveSessionsChangedListener sessionListener,
@Nullable ComponentName notificationListener, int userId, @Nullable Handler handler) {
if (sessionListener == null) {
throw new IllegalArgumentException("listener may not be null");
}
if (handler == null) {
handler = new Handler();
}
synchronized (mLock) {
if (mListeners.get(sessionListener) != null) {
Log.w(TAG, "Attempted to add session listener twice, ignoring.");
return;
}
SessionsChangedWrapper wrapper = new SessionsChangedWrapper(sessionListener, handler);
try {
mService.addSessionsListener(wrapper.mStub, notificationListener, userId);
mListeners.put(sessionListener, wrapper);
} catch (RemoteException e) {
Log.e(TAG, "Error in addOnActiveSessionsChangedListener.", e);
}
}
}
/**
* Stop receiving active sessions updates on the specified listener.
*
* @param listener The listener to remove.
*/
public void removeOnActiveSessionsChangedListener(
@NonNull OnActiveSessionsChangedListener listener) {
if (listener == null) {
throw new IllegalArgumentException("listener may not be null");
}
synchronized (mLock) {
SessionsChangedWrapper wrapper = mListeners.remove(listener);
if (wrapper != null) {
try {
mService.removeSessionsListener(wrapper.mStub);
} catch (RemoteException e) {
Log.e(TAG, "Error in removeOnActiveSessionsChangedListener.", e);
}
}
}
}
/**
* Set the remote volume controller to receive volume updates on. Only for
* use by system UI.
*
* @param rvc The volume controller to receive updates on.
* @hide
*/
public void setRemoteVolumeController(IRemoteVolumeController rvc) {
try {
mService.setRemoteVolumeController(rvc);
} catch (RemoteException e) {
Log.e(TAG, "Error in setRemoteVolumeController.", e);
}
}
/**
* Send a media key event. The receiver will be selected automatically.
*
* @param keyEvent The KeyEvent to send.
* @hide
*/
public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent) {
dispatchMediaKeyEvent(keyEvent, false);
}
/**
* Send a media key event. The receiver will be selected automatically.
*
* @param keyEvent The KeyEvent to send.
* @param needWakeLock True if a wake lock should be held while sending the key.
* @hide
*/
public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent, boolean needWakeLock) {
try {
mService.dispatchMediaKeyEvent(keyEvent, needWakeLock);
} catch (RemoteException e) {
Log.e(TAG, "Failed to send key event.", e);
}
}
/**
* Dispatch an adjust volume request to the system. It will be sent to the
* most relevant audio stream or media session. The direction must be one of
* {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
* {@link AudioManager#ADJUST_SAME}.
*
* @param suggestedStream The stream to fall back to if there isn't a
* relevant stream
* @param direction The direction to adjust volume in.
* @param flags Any flags to include with the volume change.
* @hide
*/
public void dispatchAdjustVolume(int suggestedStream, int direction, int flags) {
try {
mService.dispatchAdjustVolume(suggestedStream, direction, flags);
} catch (RemoteException e) {
Log.e(TAG, "Failed to send adjust volume.", e);
}
}
/**
* Check if the global priority session is currently active. This can be
* used to decide if media keys should be sent to the session or to the app.
*
* @hide
*/
public boolean isGlobalPriorityActive() {
try {
return mService.isGlobalPriorityActive();
} catch (RemoteException e) {
Log.e(TAG, "Failed to check if the global priority is active.", e);
}
return false;
}
/**
* Listens for changes to the list of active sessions. This can be added
* using {@link #addOnActiveSessionsChangedListener}.
*/
public interface OnActiveSessionsChangedListener {
public void onActiveSessionsChanged(@Nullable List<MediaController> controllers);
}
private final class SessionsChangedWrapper {
private final OnActiveSessionsChangedListener mListener;
private final Handler mHandler;
public SessionsChangedWrapper(OnActiveSessionsChangedListener listener, Handler handler) {
mListener = listener;
mHandler = handler;
}
private final IActiveSessionsListener.Stub mStub = new IActiveSessionsListener.Stub() {
@Override
public void onActiveSessionsChanged(final List<MediaSession.Token> tokens) {
if (mHandler != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
ArrayList<MediaController> controllers
= new ArrayList<MediaController>();
int size = tokens.size();
for (int i = 0; i < size; i++) {
controllers.add(new MediaController(mContext, tokens.get(i)));
}
mListener.onActiveSessionsChanged(controllers);
}
});
}
}
};
}
}
|