MediaSessionManager.java revision 1ff5b1648a051e9650614f0c0f1b3f449777db81
1/* 2 * Copyright (C) 2014 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.media.session; 18 19import android.annotation.NonNull; 20import android.annotation.Nullable; 21import android.content.ComponentName; 22import android.content.Context; 23import android.media.AudioManager; 24import android.media.IRemoteVolumeController; 25import android.media.session.ISessionManager; 26import android.os.IBinder; 27import android.os.RemoteException; 28import android.os.ServiceManager; 29import android.os.UserHandle; 30import android.service.notification.NotificationListenerService; 31import android.text.TextUtils; 32import android.util.Log; 33import android.view.KeyEvent; 34 35import java.util.ArrayList; 36import java.util.List; 37 38/** 39 * Provides support for interacting with {@link MediaSession media sessions} 40 * that applications have published to express their ongoing media playback state. 41 * <p> 42 * Use <code>Context.getSystemService(Context.MEDIA_SESSION_SERVICE)</code> to 43 * get an instance of this class. 44 * <p> 45 * 46 * @see MediaSession 47 * @see MediaController 48 */ 49public final class MediaSessionManager { 50 private static final String TAG = "SessionManager"; 51 52 private final ISessionManager mService; 53 54 private Context mContext; 55 56 /** 57 * @hide 58 */ 59 public MediaSessionManager(Context context) { 60 // Consider rewriting like DisplayManagerGlobal 61 // Decide if we need context 62 mContext = context; 63 IBinder b = ServiceManager.getService(Context.MEDIA_SESSION_SERVICE); 64 mService = ISessionManager.Stub.asInterface(b); 65 } 66 67 /** 68 * Create a new session in the system and get the binder for it. 69 * 70 * @param tag A short name for debugging purposes. 71 * @return The binder object from the system 72 * @hide 73 */ 74 public @NonNull ISession createSession(@NonNull MediaSession.CallbackStub cbStub, 75 @NonNull String tag, int userId) throws RemoteException { 76 return mService.createSession(mContext.getPackageName(), cbStub, tag, userId); 77 } 78 79 /** 80 * Get a list of controllers for all ongoing sessions. The controllers will 81 * be provided in priority order with the most important controller at index 82 * 0. 83 * <p> 84 * This requires the android.Manifest.permission.MEDIA_CONTENT_CONTROL 85 * permission be held by the calling app. You may also retrieve this list if 86 * your app is an enabled notification listener using the 87 * {@link NotificationListenerService} APIs, in which case you must pass the 88 * {@link ComponentName} of your enabled listener. 89 * 90 * @param notificationListener The enabled notification listener component. 91 * May be null. 92 * @return A list of controllers for ongoing sessions. 93 */ 94 public @NonNull List<MediaController> getActiveSessions( 95 @Nullable ComponentName notificationListener) { 96 return getActiveSessionsForUser(notificationListener, UserHandle.myUserId()); 97 } 98 99 /** 100 * Get active sessions for a specific user. To retrieve actions for a user 101 * other than your own you must hold the 102 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission 103 * in addition to any other requirements. If you are an enabled notification 104 * listener you may only get sessions for the users you are enabled for. 105 * 106 * @param notificationListener The enabled notification listener component. 107 * May be null. 108 * @param userId The user id to fetch sessions for. 109 * @return A list of controllers for ongoing sessions. 110 * @hide 111 */ 112 public @NonNull List<MediaController> getActiveSessionsForUser( 113 @Nullable ComponentName notificationListener, int userId) { 114 ArrayList<MediaController> controllers = new ArrayList<MediaController>(); 115 try { 116 List<IBinder> binders = mService.getSessions(notificationListener, userId); 117 int size = binders.size(); 118 for (int i = 0; i < size; i++) { 119 MediaController controller = new MediaController(ISessionController.Stub 120 .asInterface(binders.get(i))); 121 controllers.add(controller); 122 } 123 } catch (RemoteException e) { 124 Log.e(TAG, "Failed to get active sessions: ", e); 125 } 126 return controllers; 127 } 128 129 /** 130 * Add a listener to be notified when the list of active sessions 131 * changes.This requires the 132 * android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by 133 * the calling app. You may also retrieve this list if your app is an 134 * enabled notification listener using the 135 * {@link NotificationListenerService} APIs, in which case you must pass the 136 * {@link ComponentName} of your enabled listener. 137 * 138 * @param sessionListener The listener to add. 139 * @param notificationListener The enabled notification listener component. 140 * May be null. 141 */ 142 public void addActiveSessionsListener(SessionListener sessionListener, 143 ComponentName notificationListener) { 144 addActiveSessionsListener(sessionListener, notificationListener, UserHandle.myUserId()); 145 } 146 147 /** 148 * Add a listener to be notified when the list of active sessions 149 * changes.This requires the 150 * android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by 151 * the calling app. You may also retrieve this list if your app is an 152 * enabled notification listener using the 153 * {@link NotificationListenerService} APIs, in which case you must pass the 154 * {@link ComponentName} of your enabled listener. 155 * 156 * @param sessionListener The listener to add. 157 * @param notificationListener The enabled notification listener component. 158 * May be null. 159 * @param userId The userId to listen for changes on. 160 * @hide 161 */ 162 public void addActiveSessionsListener(@NonNull SessionListener sessionListener, 163 @Nullable ComponentName notificationListener, int userId) { 164 if (sessionListener == null) { 165 throw new IllegalArgumentException("listener may not be null"); 166 } 167 try { 168 mService.addSessionsListener(sessionListener.mStub, notificationListener, userId); 169 } catch (RemoteException e) { 170 Log.e(TAG, "Error in addActiveSessionsListener.", e); 171 } 172 } 173 174 /** 175 * Stop receiving active sessions updates on the specified listener. 176 * 177 * @param listener The listener to remove. 178 * @hide 179 */ 180 public void removeActiveSessionsListener(@NonNull SessionListener listener) { 181 if (listener == null) { 182 throw new IllegalArgumentException("listener may not be null"); 183 } 184 try { 185 mService.removeSessionsListener(listener.mStub); 186 } catch (RemoteException e) { 187 Log.e(TAG, "Error in removeActiveSessionsListener.", e); 188 } 189 } 190 191 /** 192 * Set the remote volume controller to receive volume updates on. Only for 193 * use by system UI. 194 * 195 * @param rvc The volume controller to receive updates on. 196 * @hide 197 */ 198 public void setRemoteVolumeController(IRemoteVolumeController rvc) { 199 try { 200 mService.setRemoteVolumeController(rvc); 201 } catch (RemoteException e) { 202 Log.e(TAG, "Error in setRemoteVolumeController.", e); 203 } 204 } 205 206 /** 207 * Send a media key event. The receiver will be selected automatically. 208 * 209 * @param keyEvent The KeyEvent to send. 210 * @hide 211 */ 212 public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent) { 213 dispatchMediaKeyEvent(keyEvent, false); 214 } 215 216 /** 217 * Send a media key event. The receiver will be selected automatically. 218 * 219 * @param keyEvent The KeyEvent to send. 220 * @param needWakeLock True if a wake lock should be held while sending the key. 221 * @hide 222 */ 223 public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent, boolean needWakeLock) { 224 try { 225 mService.dispatchMediaKeyEvent(keyEvent, needWakeLock); 226 } catch (RemoteException e) { 227 Log.e(TAG, "Failed to send key event.", e); 228 } 229 } 230 231 /** 232 * Dispatch an adjust volume request to the system. It will be sent to the 233 * most relevant audio stream or media session. The direction must be one of 234 * {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE}, 235 * {@link AudioManager#ADJUST_SAME}. 236 * 237 * @param suggestedStream The stream to fall back to if there isn't a 238 * relevant stream 239 * @param direction The direction to adjust volume in. 240 * @param flags Any flags to include with the volume change. 241 * @hide 242 */ 243 public void dispatchAdjustVolume(int suggestedStream, int direction, int flags) { 244 try { 245 mService.dispatchAdjustVolume(suggestedStream, direction, flags); 246 } catch (RemoteException e) { 247 Log.e(TAG, "Failed to send adjust volume.", e); 248 } 249 } 250 251 /** 252 * Listens for changes to the list of active sessions. This can be added 253 * using {@link #addActiveSessionsListener}. 254 */ 255 public static abstract class SessionListener { 256 /** 257 * Called when the list of active sessions has changed. This can be due 258 * to a session being added or removed or the order of sessions 259 * changing. The controllers will be provided in priority order with the 260 * most important controller at index 0. 261 * 262 * @param controllers The updated list of controllers for the user that 263 * changed. 264 */ 265 public abstract void onActiveSessionsChanged( 266 @Nullable List<MediaController> controllers); 267 268 private final IActiveSessionsListener.Stub mStub = new IActiveSessionsListener.Stub() { 269 @Override 270 public void onActiveSessionsChanged(List<MediaSession.Token> tokens) { 271 ArrayList<MediaController> controllers = new ArrayList<MediaController>(); 272 int size = tokens.size(); 273 for (int i = 0; i < size; i++) { 274 controllers.add(new MediaController(tokens.get(i))); 275 } 276 SessionListener.this.onActiveSessionsChanged(controllers); 277 } 278 }; 279 } 280} 281