@@ -3625,12 +3625,14 @@ private void filterMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) {
36253625 Log .e (TAG , "not dispatching invalid media key event " + keyEvent );
36263626 return ;
36273627 }
3628- // event filtering based on audio mode
3628+ // event filtering for telephony
36293629 synchronized (mRingingLock ) {
3630- if (mIsRinging || (getMode () == AudioSystem .MODE_IN_CALL ) ||
3631- (getMode () == AudioSystem .MODE_IN_COMMUNICATION ) ||
3632- (getMode () == AudioSystem .MODE_RINGTONE ) ) {
3633- return ;
3630+ synchronized (mRCStack ) {
3631+ if ((mMediaReceiverForCalls != null ) &&
3632+ (mIsRinging || (getMode () == AudioSystem .MODE_IN_CALL ))) {
3633+ dispatchMediaKeyEventForCalls (keyEvent , needWakeLock );
3634+ return ;
3635+ }
36343636 }
36353637 }
36363638 // event filtering based on voice-based interactions
@@ -3641,6 +3643,25 @@ private void filterMediaKeyEvent(KeyEvent keyEvent, boolean needWakeLock) {
36413643 }
36423644 }
36433645
3646+ /**
3647+ * Handles the dispatching of the media button events to the telephony package.
3648+ * Precondition: mMediaReceiverForCalls != null
3649+ * @param keyEvent a non-null KeyEvent whose key code is one of the supported media buttons
3650+ * @param needWakeLock true if a PARTIAL_WAKE_LOCK needs to be held while this key event
3651+ * is dispatched.
3652+ */
3653+ private void dispatchMediaKeyEventForCalls (KeyEvent keyEvent , boolean needWakeLock ) {
3654+ Intent keyIntent = new Intent (Intent .ACTION_MEDIA_BUTTON , null );
3655+ keyIntent .putExtra (Intent .EXTRA_KEY_EVENT , keyEvent );
3656+ keyIntent .setPackage (mMediaReceiverForCalls .getPackageName ());
3657+ if (needWakeLock ) {
3658+ mMediaEventWakeLock .acquire ();
3659+ keyIntent .putExtra (EXTRA_WAKELOCK_ACQUIRED , WAKELOCK_RELEASE_ON_FINISHED );
3660+ }
3661+ mContext .sendOrderedBroadcast (keyIntent , null , mKeyEventDone ,
3662+ mAudioHandler , Activity .RESULT_OK , null , null );
3663+ }
3664+
36443665 /**
36453666 * Handles the dispatching of the media button events to one of the registered listeners,
36463667 * or if there was none, broadcast an ACTION_MEDIA_BUTTON intent to the rest of the system.
@@ -4027,6 +4048,12 @@ protected void finalize() throws Throwable {
40274048 */
40284049 private final Stack <RemoteControlStackEntry > mRCStack = new Stack <RemoteControlStackEntry >();
40294050
4051+ /**
4052+ * The component the telephony package can register so telephony calls have priority to
4053+ * handle media button events
4054+ */
4055+ private ComponentName mMediaReceiverForCalls = null ;
4056+
40304057 /**
40314058 * Helper function:
40324059 * Display in the log the current entries in the remote control focus stack
@@ -4380,6 +4407,35 @@ public void unregisterMediaButtonIntent(PendingIntent mediaIntent, ComponentName
43804407 }
43814408 }
43824409
4410+ /**
4411+ * see AudioManager.registerMediaButtonEventReceiverForCalls(ComponentName c)
4412+ * precondition: c != null
4413+ */
4414+ public void registerMediaButtonEventReceiverForCalls (ComponentName c ) {
4415+ if (mContext .checkCallingPermission ("android.permission.MODIFY_PHONE_STATE" )
4416+ != PackageManager .PERMISSION_GRANTED ) {
4417+ Log .e (TAG , "Invalid permissions to register media button receiver for calls" );
4418+ return ;
4419+ }
4420+ synchronized (mRCStack ) {
4421+ mMediaReceiverForCalls = c ;
4422+ }
4423+ }
4424+
4425+ /**
4426+ * see AudioManager.unregisterMediaButtonEventReceiverForCalls()
4427+ */
4428+ public void unregisterMediaButtonEventReceiverForCalls () {
4429+ if (mContext .checkCallingPermission ("android.permission.MODIFY_PHONE_STATE" )
4430+ != PackageManager .PERMISSION_GRANTED ) {
4431+ Log .e (TAG , "Invalid permissions to unregister media button receiver for calls" );
4432+ return ;
4433+ }
4434+ synchronized (mRCStack ) {
4435+ mMediaReceiverForCalls = null ;
4436+ }
4437+ }
4438+
43834439 /**
43844440 * see AudioManager.registerRemoteControlClient(ComponentName eventReceiver, ...)
43854441 * Note: using this method with rcClient == null is a way to "disable" the IRemoteControlClient
0 commit comments