MediaSessionManager.java revision b214efbb9170a9f6a4991684a63ca59680074cc7
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 */ 179 public void removeActiveSessionsListener(@NonNull SessionListener listener) { 180 if (listener == null) { 181 throw new IllegalArgumentException("listener may not be null"); 182 } 183 try { 184 mService.removeSessionsListener(listener.mStub); 185 } catch (RemoteException e) { 186 Log.e(TAG, "Error in removeActiveSessionsListener.", e); 187 } 188 } 189 190 /** 191 * Set the remote volume controller to receive volume updates on. Only for 192 * use by system UI. 193 * 194 * @param rvc The volume controller to receive updates on. 195 * @hide 196 */ 197 public void setRemoteVolumeController(IRemoteVolumeController rvc) { 198 try { 199 mService.setRemoteVolumeController(rvc); 200 } catch (RemoteException e) { 201 Log.e(TAG, "Error in setRemoteVolumeController.", e); 202 } 203 } 204 205 /** 206 * Send a media key event. The receiver will be selected automatically. 207 * 208 * @param keyEvent The KeyEvent to send. 209 * @hide 210 */ 211 public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent) { 212 dispatchMediaKeyEvent(keyEvent, false); 213 } 214 215 /** 216 * Send a media key event. The receiver will be selected automatically. 217 * 218 * @param keyEvent The KeyEvent to send. 219 * @param needWakeLock True if a wake lock should be held while sending the key. 220 * @hide 221 */ 222 public void dispatchMediaKeyEvent(@NonNull KeyEvent keyEvent, boolean needWakeLock) { 223 try { 224 mService.dispatchMediaKeyEvent(keyEvent, needWakeLock); 225 } catch (RemoteException e) { 226 Log.e(TAG, "Failed to send key event.", e); 227 } 228 } 229 230 /** 231 * Dispatch an adjust volume request to the system. It will be sent to the 232 * most relevant audio stream or media session. The direction must be one of 233 * {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE}, 234 * {@link AudioManager#ADJUST_SAME}. 235 * 236 * @param suggestedStream The stream to fall back to if there isn't a 237 * relevant stream 238 * @param direction The direction to adjust volume in. 239 * @param flags Any flags to include with the volume change. 240 * @hide 241 */ 242 public void dispatchAdjustVolume(int suggestedStream, int direction, int flags) { 243 try { 244 mService.dispatchAdjustVolume(suggestedStream, direction, flags); 245 } catch (RemoteException e) { 246 Log.e(TAG, "Failed to send adjust volume.", e); 247 } 248 } 249 250 /** 251 * Listens for changes to the list of active sessions. This can be added 252 * using {@link #addActiveSessionsListener}. 253 */ 254 public static abstract class SessionListener { 255 /** 256 * Called when the list of active sessions has changed. This can be due 257 * to a session being added or removed or the order of sessions 258 * changing. The controllers will be provided in priority order with the 259 * most important controller at index 0. 260 * 261 * @param controllers The updated list of controllers for the user that 262 * changed. 263 */ 264 public abstract void onActiveSessionsChanged( 265 @Nullable List<MediaController> controllers); 266 267 private final IActiveSessionsListener.Stub mStub = new IActiveSessionsListener.Stub() { 268 @Override 269 public void onActiveSessionsChanged(List<MediaSession.Token> tokens) { 270 ArrayList<MediaController> controllers = new ArrayList<MediaController>(); 271 int size = tokens.size(); 272 for (int i = 0; i < size; i++) { 273 controllers.add(new MediaController(tokens.get(i))); 274 } 275 SessionListener.this.onActiveSessionsChanged(controllers); 276 } 277 }; 278 } 279} 280