Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9de55042 authored by Michael Bestas's avatar Michael Bestas Committed by Steve Kondik
Browse files

Fix volume key music controls and wake up

- Forward port code from cm-11.0 and adjust for 5.0
- Fix not being able to adjust volume when music control is on
- Disable screen off volume/music control when wake key is enabled

Change-Id: Ie0ad83965c41fd33120490c25a4ff3d9b2cbeb37
parent 0f19a45d
Loading
Loading
Loading
Loading
+56 −28
Original line number Diff line number Diff line
@@ -833,13 +833,17 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                case MSG_DISPOSE_INPUT_CONSUMER:
                    disposeInputConsumer((InputConsumer) msg.obj);
                    break;
                case MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK:
                    dispatchMediaKeyWithWakeLockToAudioService((KeyEvent)msg.obj);
                    dispatchMediaKeyWithWakeLockToAudioService(KeyEvent.changeAction((KeyEvent)msg.obj, KeyEvent.ACTION_UP));
                case MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK: {
                    KeyEvent event = (KeyEvent) msg.obj;
                    mIsLongPress = true;
                    dispatchMediaKeyWithWakeLockToAudioService(event);
                    dispatchMediaKeyWithWakeLockToAudioService(
                            KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
                    break;
                }
            }
        }
    }

    private UEventObserver mHDMIObserver = new UEventObserver() {
        @Override
@@ -5902,40 +5906,56 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                            .sendVolumeKeyEvent(event, true);
                }

                if (isMusicActive() && (result & ACTION_PASS_TO_USER) == 0) {
                    if (mVolBtnMusicControls && down && (keyCode != KeyEvent.KEYCODE_VOLUME_MUTE)) {
                // Disable music and volume control when used as wake key
                if ((result & ACTION_PASS_TO_USER) == 0 && !mVolumeWakeScreen) {
                    boolean mayChangeVolume = false;

                    if (isMusicActive()) {
                        if (mVolBtnMusicControls && (keyCode != KeyEvent.KEYCODE_VOLUME_MUTE)) {
                            // Detect long key presses.
                            if (down) {
                                mIsLongPress = false;
                                // TODO: Long press of MUTE could be mapped to KEYCODE_MEDIA_PLAY_PAUSE
                                int newKeyCode = event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP ?
                                        KeyEvent.KEYCODE_MEDIA_NEXT : KeyEvent.KEYCODE_MEDIA_PREVIOUS;
                        Message msg = mHandler.obtainMessage(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK,
                                new KeyEvent(event.getDownTime(), event.getEventTime(), event.getAction(), newKeyCode, 0));
                        msg.setAsynchronous(true);
                        mHandler.sendMessageDelayed(msg, ViewConfiguration.getLongPressTimeout());
                                scheduleLongPressKeyEvent(event, newKeyCode);
                                // Consume key down events of all presses.
                                break;
                            } else {
                        if (mVolBtnMusicControls && !down) {
                                mHandler.removeMessages(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK);
                                // Consume key up events of long presses only.
                                if (mIsLongPress) {
                                    break;
                                }
                                // Change volume only on key up events of short presses.
                                mayChangeVolume = true;
                            }
                        } else {
                            // Long key press detection not applicable, change volume only
                            // on key down events
                            mayChangeVolume = down;
                        }
                    }

                if ((result & ACTION_PASS_TO_USER) == 0) {
                    if (mayChangeVolume) {
                        if (mUseTvRouting) {
                            dispatchDirectAudioEvent(event);
                        } else {
                            // If we aren't passing to the user and no one else
                        // handled it send it to the session manager to
                        // figure out.
                            // handled it send it to the session manager to figure
                            // out.

                            // Rewrite the event to use key-down as sendVolumeKeyEvent will
                            // only change the volume on key down.
                            KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
                            MediaSessionLegacyHelper.getHelper(mContext)
                                .sendVolumeKeyEvent(event, true);
                                    .sendVolumeKeyEvent(newEvent, true);
                        }
                    break;
                    }
                    break;
                }
                break;
            }

            case KeyEvent.KEYCODE_ENDCALL: {
                result &= ~ACTION_PASS_TO_USER;
@@ -6105,6 +6125,14 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        return result;
    }

    private void scheduleLongPressKeyEvent(KeyEvent origEvent, int keyCode) {
        KeyEvent event = new KeyEvent(origEvent.getDownTime(), origEvent.getEventTime(),
                origEvent.getAction(), keyCode, 0);
        Message msg = mHandler.obtainMessage(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK, event);
        msg.setAsynchronous(true);
        mHandler.sendMessageDelayed(msg, ViewConfiguration.getLongPressTimeout());
    }

    /**
     * Returns true if the key can have global actions attached to it.
     * We reserve all power management keys for the system since they require