diff options
author | RoboErik <epastern@google.com> | 2014-05-19 19:28:54 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2014-05-19 19:28:54 +0000 |
commit | b4f4f69e4c56ad1217be9a3c792d4723f6db2256 (patch) | |
tree | 623f66215f0a0ae3c984f0da6b5aa41d1797094c /media | |
parent | d069cd5164ec4e6edcafa3fb3927675c55e5fe32 (diff) | |
parent | 8a2cfc309ab9126e90022916967c65a793c034f0 (diff) | |
download | frameworks_base-b4f4f69e4c56ad1217be9a3c792d4723f6db2256.zip frameworks_base-b4f4f69e4c56ad1217be9a3c792d4723f6db2256.tar.gz frameworks_base-b4f4f69e4c56ad1217be9a3c792d4723f6db2256.tar.bz2 |
Merge "Move media key processing to sessions"
Diffstat (limited to 'media')
-rw-r--r-- | media/java/android/media/AudioManager.java | 29 | ||||
-rw-r--r-- | media/java/android/media/AudioService.java | 29 | ||||
-rw-r--r-- | media/java/android/media/session/ISessionCallback.aidl | 2 | ||||
-rw-r--r-- | media/java/android/media/session/ISessionManager.aidl | 2 | ||||
-rw-r--r-- | media/java/android/media/session/MediaSessionLegacyHelper.java | 101 | ||||
-rw-r--r-- | media/java/android/media/session/Session.java | 13 | ||||
-rw-r--r-- | media/java/android/media/session/SessionManager.java | 27 |
7 files changed, 166 insertions, 37 deletions
diff --git a/media/java/android/media/AudioManager.java b/media/java/android/media/AudioManager.java index 1dcfcb8..3a3f76d 100644 --- a/media/java/android/media/AudioManager.java +++ b/media/java/android/media/AudioManager.java @@ -428,7 +428,6 @@ public class AudioManager { public static final int USE_DEFAULT_STREAM_TYPE = Integer.MIN_VALUE; private static IAudioService sService; - private MediaSessionLegacyHelper mSessionHelper; /** * @hide @@ -439,9 +438,6 @@ public class AudioManager { com.android.internal.R.bool.config_useMasterVolume); mUseVolumeKeySounds = mContext.getResources().getBoolean( com.android.internal.R.bool.config_useVolumeKeySounds); - if (USE_SESSIONS) { - mSessionHelper = MediaSessionLegacyHelper.getHelper(context); - } } private static IAudioService getService() @@ -478,11 +474,16 @@ public class AudioManager { * or {@link KeyEvent#KEYCODE_MEDIA_AUDIO_TRACK}. */ public void dispatchMediaKeyEvent(KeyEvent keyEvent) { - IAudioService service = getService(); - try { - service.dispatchMediaKeyEvent(keyEvent); - } catch (RemoteException e) { - Log.e(TAG, "dispatchMediaKeyEvent threw exception ", e); + if (USE_SESSIONS) { + MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(mContext); + helper.sendMediaButtonEvent(keyEvent, false); + } else { + IAudioService service = getService(); + try { + service.dispatchMediaKeyEvent(keyEvent); + } catch (RemoteException e) { + Log.e(TAG, "dispatchMediaKeyEvent threw exception ", e); + } } } @@ -2178,7 +2179,8 @@ public class AudioManager { Log.e(TAG, "Dead object in registerMediaButtonIntent"+e); } if (USE_SESSIONS) { - mSessionHelper.addMediaButtonListener(pi, mContext); + MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(mContext); + helper.addMediaButtonListener(pi, mContext); } } @@ -2254,7 +2256,8 @@ public class AudioManager { Log.e(TAG, "Dead object in unregisterMediaButtonIntent"+e); } if (USE_SESSIONS) { - mSessionHelper.removeMediaButtonListener(pi); + MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(mContext); + helper.removeMediaButtonListener(pi); } } @@ -2281,7 +2284,7 @@ public class AudioManager { Log.e(TAG, "Dead object in registerRemoteControlClient"+e); } if (USE_SESSIONS) { - rcClient.registerWithSession(mSessionHelper); + rcClient.registerWithSession(MediaSessionLegacyHelper.getHelper(mContext)); } } @@ -2303,7 +2306,7 @@ public class AudioManager { Log.e(TAG, "Dead object in unregisterRemoteControlClient"+e); } if (USE_SESSIONS) { - rcClient.unregisterWithSession(mSessionHelper); + rcClient.unregisterWithSession(MediaSessionLegacyHelper.getHelper(mContext)); } } diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java index 724022b..bb8cfa6 100644 --- a/media/java/android/media/AudioService.java +++ b/media/java/android/media/AudioService.java @@ -46,6 +46,7 @@ import android.database.ContentObserver; import android.hardware.usb.UsbManager; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; +import android.media.session.MediaSessionLegacyHelper; import android.os.Binder; import android.os.Build; import android.os.Environment; @@ -108,6 +109,10 @@ public class AudioService extends IAudioService.Stub { /** Debug volumes */ protected static final boolean DEBUG_VOL = false; + /** Reroute calls to media session apis */ + private static final boolean USE_SESSIONS = true; + private static final boolean DEBUG_SESSIONS = true; + /** How long to delay before persisting a change in volume/ringer mode. */ private static final int PERSIST_DELAY = 500; @@ -3472,7 +3477,7 @@ public class AudioService extends IAudioService.Stub { if (volume < 0) { volFloat = (float)Math.pow(10, (float)sSoundEffectVolumeDb/20); } else { - volFloat = (float) volume / 1000.0f; + volFloat = volume / 1000.0f; } if (SOUND_EFFECT_FILES_MAP[effectType][1] > 0) { @@ -3554,7 +3559,7 @@ public class AudioService extends IAudioService.Stub { } Settings.System.putFloatForUser(mContentResolver, Settings.System.VOLUME_MASTER, - (float)msg.arg1 / (float)1000.0, + msg.arg1 / (float)1000.0, UserHandle.USER_CURRENT); break; @@ -4325,11 +4330,27 @@ public class AudioService extends IAudioService.Stub { } public void dispatchMediaKeyEvent(KeyEvent keyEvent) { - mMediaFocusControl.dispatchMediaKeyEvent(keyEvent); + if (USE_SESSIONS) { + if (DEBUG_SESSIONS) { + int pid = getCallingPid(); + Log.w(TAG, "Call to dispatchMediaKeyEvent from " + pid); + } + MediaSessionLegacyHelper.getHelper(mContext).sendMediaButtonEvent(keyEvent, false); + } else { + mMediaFocusControl.dispatchMediaKeyEvent(keyEvent); + } } public void dispatchMediaKeyEventUnderWakelock(KeyEvent keyEvent) { - mMediaFocusControl.dispatchMediaKeyEventUnderWakelock(keyEvent); + if (USE_SESSIONS) { + if (DEBUG_SESSIONS) { + int pid = getCallingPid(); + Log.w(TAG, "Call to dispatchMediaKeyEventUnderWakelock from " + pid); + } + MediaSessionLegacyHelper.getHelper(mContext).sendMediaButtonEvent(keyEvent, true); + } else { + mMediaFocusControl.dispatchMediaKeyEventUnderWakelock(keyEvent); + } } //========================================================================================== diff --git a/media/java/android/media/session/ISessionCallback.aidl b/media/java/android/media/session/ISessionCallback.aidl index 1552513..7b0412e 100644 --- a/media/java/android/media/session/ISessionCallback.aidl +++ b/media/java/android/media/session/ISessionCallback.aidl @@ -28,7 +28,7 @@ import android.os.ResultReceiver; */ oneway interface ISessionCallback { void onCommand(String command, in Bundle extras, in ResultReceiver cb); - void onMediaButton(in Intent mediaButtonIntent); + void onMediaButton(in Intent mediaButtonIntent, in ResultReceiver cb); void onRequestRouteChange(in RouteInfo route); void onRouteConnected(in RouteInfo route, in RouteOptions options); void onRouteDisconnected(in RouteInfo route, int reason); diff --git a/media/java/android/media/session/ISessionManager.aidl b/media/java/android/media/session/ISessionManager.aidl index e341647..38b9293 100644 --- a/media/java/android/media/session/ISessionManager.aidl +++ b/media/java/android/media/session/ISessionManager.aidl @@ -19,6 +19,7 @@ import android.content.ComponentName; import android.media.session.ISession; import android.media.session.ISessionCallback; import android.os.Bundle; +import android.view.KeyEvent; /** * Interface to the MediaSessionManagerService @@ -27,4 +28,5 @@ import android.os.Bundle; interface ISessionManager { ISession createSession(String packageName, in ISessionCallback cb, String tag, int userId); List<IBinder> getSessions(in ComponentName compName, int userId); + void dispatchMediaKeyEvent(in KeyEvent keyEvent, boolean needWakeLock); }
\ No newline at end of file diff --git a/media/java/android/media/session/MediaSessionLegacyHelper.java b/media/java/android/media/session/MediaSessionLegacyHelper.java index c07229d..e1eae09 100644 --- a/media/java/android/media/session/MediaSessionLegacyHelper.java +++ b/media/java/android/media/session/MediaSessionLegacyHelper.java @@ -35,6 +35,7 @@ import android.view.KeyEvent; */ public class MediaSessionLegacyHelper { private static final String TAG = "MediaSessionHelper"; + private static final boolean DEBUG = true; private static final Object sLock = new Object(); private static MediaSessionLegacyHelper sInstance; @@ -52,6 +53,9 @@ public class MediaSessionLegacyHelper { } public static MediaSessionLegacyHelper getHelper(Context context) { + if (DEBUG) { + Log.d(TAG, "Attempting to get helper with context " + context); + } synchronized (sLock) { if (sInstance == null) { sInstance = new MediaSessionLegacyHelper(context); @@ -65,12 +69,25 @@ public class MediaSessionLegacyHelper { return holder == null ? null : holder.mSession; } - public void addRccListener(PendingIntent pi, TransportPerformer.Listener listener) { + public void sendMediaButtonEvent(KeyEvent keyEvent, boolean needWakeLock) { + mSessionManager.dispatchMediaKeyEvent(keyEvent, needWakeLock); + if (DEBUG) { + Log.d(TAG, "dispatched media key " + keyEvent); + } + } + public void addRccListener(PendingIntent pi, TransportPerformer.Listener listener) { + if (pi == null) { + Log.w(TAG, "Pending intent was null, can't add rcc listener."); + return; + } SessionHolder holder = getHolder(pi, true); TransportPerformer performer = holder.mSession.getTransportPerformer(); if (holder.mRccListener != null) { if (holder.mRccListener == listener) { + if (DEBUG) { + Log.d(TAG, "addRccListener listener already added."); + } // This is already the registered listener, ignore return; } @@ -82,9 +99,15 @@ public class MediaSessionLegacyHelper { holder.mFlags |= Session.FLAG_HANDLES_TRANSPORT_CONTROLS; holder.mSession.setFlags(holder.mFlags); holder.update(); + if (DEBUG) { + Log.d(TAG, "Added rcc listener for " + pi + "."); + } } public void removeRccListener(PendingIntent pi) { + if (pi == null) { + return; + } SessionHolder holder = getHolder(pi, false); if (holder != null && holder.mRccListener != null) { holder.mSession.getTransportPerformer().removeListener(holder.mRccListener); @@ -92,30 +115,56 @@ public class MediaSessionLegacyHelper { holder.mFlags &= ~Session.FLAG_HANDLES_TRANSPORT_CONTROLS; holder.mSession.setFlags(holder.mFlags); holder.update(); + if (DEBUG) { + Log.d(TAG, "Removed rcc listener for " + pi + "."); + } } } public void addMediaButtonListener(PendingIntent pi, Context context) { + if (pi == null) { + Log.w(TAG, "Pending intent was null, can't addMediaButtonListener."); + return; + } SessionHolder holder = getHolder(pi, true); if (holder.mMediaButtonListener != null) { - // Already have this listener registered + // Already have this listener registered, but update it anyway as + // the extras may have changed. + if (DEBUG) { + Log.d(TAG, "addMediaButtonListener already added " + pi); + } return; } holder.mMediaButtonListener = new MediaButtonListener(pi, context); holder.mFlags |= Session.FLAG_HANDLES_MEDIA_BUTTONS; holder.mSession.setFlags(holder.mFlags); holder.mSession.getTransportPerformer().addListener(holder.mMediaButtonListener, mHandler); + + holder.mMediaButtonReceiver = new MediaButtonReceiver(pi, context); + holder.mSession.addCallback(holder.mMediaButtonReceiver, mHandler); + if (DEBUG) { + Log.d(TAG, "addMediaButtonListener added " + pi); + } } public void removeMediaButtonListener(PendingIntent pi) { + if (pi == null) { + return; + } SessionHolder holder = getHolder(pi, false); if (holder != null && holder.mMediaButtonListener != null) { holder.mSession.getTransportPerformer().removeListener(holder.mMediaButtonListener); holder.mFlags &= ~Session.FLAG_HANDLES_MEDIA_BUTTONS; holder.mSession.setFlags(holder.mFlags); holder.mMediaButtonListener = null; + + holder.mSession.removeCallback(holder.mMediaButtonReceiver); + holder.mMediaButtonReceiver = null; holder.update(); + if (DEBUG) { + Log.d(TAG, "removeMediaButtonListener removed " + pi); + } } } @@ -130,7 +179,32 @@ public class MediaSessionLegacyHelper { return holder; } - public static class MediaButtonListener extends TransportPerformer.Listener { + private static void sendKeyEvent(PendingIntent pi, Context context, Intent intent) { + try { + pi.send(context, 0, intent); + } catch (CanceledException e) { + Log.e(TAG, "Error sending media key down event:", e); + // Don't bother sending up if down failed + return; + } + } + + private static final class MediaButtonReceiver extends Session.Callback { + private final PendingIntent mPendingIntent; + private final Context mContext; + + public MediaButtonReceiver(PendingIntent pi, Context context) { + mPendingIntent = pi; + mContext = context; + } + + @Override + public void onMediaButton(Intent mediaButtonIntent) { + MediaSessionLegacyHelper.sendKeyEvent(mPendingIntent, mContext, mediaButtonIntent); + } + } + + private static final class MediaButtonListener extends TransportPerformer.Listener { private final PendingIntent mPendingIntent; private final Context mContext; @@ -179,20 +253,14 @@ public class MediaSessionLegacyHelper { Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON); intent.putExtra(Intent.EXTRA_KEY_EVENT, ke); - try { - mPendingIntent.send(mContext, 0, intent); - } catch (CanceledException e) { - Log.e(TAG, "Error sending media key down event:", e); - // Don't bother sending up if down failed - return; - } + MediaSessionLegacyHelper.sendKeyEvent(mPendingIntent, mContext, intent); ke = new KeyEvent(KeyEvent.ACTION_UP, keyCode); intent.putExtra(Intent.EXTRA_KEY_EVENT, ke); - try { - mPendingIntent.send(mContext, 0, intent); - } catch (CanceledException e) { - Log.e(TAG, "Error sending media key up event:", e); + MediaSessionLegacyHelper.sendKeyEvent(mPendingIntent, mContext, intent); + + if (DEBUG) { + Log.d(TAG, "Sent " + keyCode + " to pending intent " + mPendingIntent); } } } @@ -201,6 +269,7 @@ public class MediaSessionLegacyHelper { public final Session mSession; public final PendingIntent mPi; public MediaButtonListener mMediaButtonListener; + public MediaButtonReceiver mMediaButtonReceiver; public TransportPerformer.Listener mRccListener; public int mFlags; @@ -213,10 +282,6 @@ public class MediaSessionLegacyHelper { if (mMediaButtonListener == null && mRccListener == null) { mSession.release(); mSessions.remove(mPi); - } else if (mMediaButtonListener != null && mRccListener != null) { - // TODO set session to active - } else { - // TODO set session to inactive } } } diff --git a/media/java/android/media/session/Session.java b/media/java/android/media/session/Session.java index 2ffced6..e439772 100644 --- a/media/java/android/media/session/Session.java +++ b/media/java/android/media/session/Session.java @@ -114,6 +114,13 @@ public final class Session { */ public static final int DISCONNECT_REASON_SESSION_DESTROYED = 5; + /** + * Status code indicating the call was handled. + * + * @hide + */ + public static final int RESULT_SUCCESS = 0; + private static final int MSG_MEDIA_BUTTON = 1; private static final int MSG_COMMAND = 2; private static final int MSG_ROUTE_CHANGE = 3; @@ -544,11 +551,15 @@ public final class Session { } @Override - public void onMediaButton(Intent mediaButtonIntent) throws RemoteException { + public void onMediaButton(Intent mediaButtonIntent, ResultReceiver cb) + throws RemoteException { Session session = mMediaSession.get(); if (session != null) { session.postMediaButton(mediaButtonIntent); } + if (cb != null) { + cb.send(RESULT_SUCCESS, null); + } } @Override diff --git a/media/java/android/media/session/SessionManager.java b/media/java/android/media/session/SessionManager.java index 1eb3b7a..1838132 100644 --- a/media/java/android/media/session/SessionManager.java +++ b/media/java/android/media/session/SessionManager.java @@ -25,6 +25,7 @@ import android.os.ServiceManager; import android.os.UserHandle; import android.service.notification.NotificationListenerService; import android.util.Log; +import android.view.KeyEvent; import java.util.ArrayList; import java.util.List; @@ -138,4 +139,30 @@ public final class SessionManager { } return controllers; } + + /** + * Send a media key event. The receiver will be selected automatically. + * + * @param keyEvent The KeyEvent to send. + * @hide + */ + public void dispatchMediaKeyEvent(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(KeyEvent keyEvent, boolean needWakeLock) { + try { + mService.dispatchMediaKeyEvent(keyEvent, needWakeLock); + } catch (RemoteException e) { + Log.e(TAG, "Failed to send key event.", e); + } + } } |