diff options
author | Austin T. Conn <atc3030dev@gmail.com> | 2020-10-28 11:06:30 -0300 |
---|---|---|
committer | alk3pInjection <webmaster@raspii.tech> | 2022-05-07 00:20:58 +0800 |
commit | 2a071c3fd6f87e1a54e9e975804b8ab800e0a791 (patch) | |
tree | c0f7225fd0e63bb3212b07a843a6545221ff0dfd /services | |
parent | ffcc9052a214af8e2d516edfad34ad7b8392f426 (diff) |
base: Long press volume button to skip tracks [1/2]
Bring Forward Volume Rocker Track Change
Requires Settings Change
(packages/apps/Settings)
* rituj : AudioService: Allow volume-key long press even when screen is off
* mickaelmendes50 : Adapt to Android 11
* Drop previous changes of AudioService as, in theory, the event should
never reach it at all.
Change-Id: I70b00e08fe48707110725dd1daa56b1e48d15a03
Fix volume rocker skip track on Ambient Display and Lift to Wake
we need to check if dream service is dozing before checking
keyguard status
Change-Id: Ic3a6c830496188bb6edf27043cd24eb2d553bb82
Fix long press volume buttons skip tracks
Without that patch, the volume changes up or down prior
to skipping tracks because the sendvolumekeyevent doesn't
have the mayChangeVolume condition. Also, that part is
useless cause the mUseTvRouting condition is taken care
of correctly later.
Change-Id: I88757315f135f013e7692861d46b3a07fbfc07d5
Diffstat (limited to 'services')
-rw-r--r-- | services/core/java/com/android/server/policy/PhoneWindowManager.java | 121 |
1 files changed, 107 insertions, 14 deletions
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java index 12de08936d27..f26c7707d404 100644 --- a/services/core/java/com/android/server/policy/PhoneWindowManager.java +++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java @@ -508,6 +508,10 @@ public class PhoneWindowManager implements WindowManagerPolicy { private boolean mHandleVolumeKeysInWM; + // Behavior of volume button music controls + boolean mVolBtnMusicControls; + boolean mVolBtnLongPress; + private boolean mPendingKeyguardOccluded; private boolean mKeyguardOccludedChanged; @@ -624,6 +628,7 @@ public class PhoneWindowManager implements WindowManagerPolicy { private static final int MSG_HANDLE_ALL_APPS = 22; private static final int MSG_LAUNCH_ASSIST = 23; private static final int MSG_RINGER_TOGGLE_CHORD = 24; + private static final int MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK = 25; private class PolicyHandler extends Handler { @Override @@ -691,6 +696,13 @@ public class PhoneWindowManager implements WindowManagerPolicy { case MSG_RINGER_TOGGLE_CHORD: handleRingerChordGesture(); break; + case MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK: + KeyEvent event = (KeyEvent) msg.obj; + mVolBtnLongPress = true; + dispatchMediaKeyWithWakeLockToAudioService(event); + dispatchMediaKeyWithWakeLockToAudioService( + KeyEvent.changeAction(event, KeyEvent.ACTION_UP)); + break; } } } @@ -765,6 +777,9 @@ public class PhoneWindowManager implements WindowManagerPolicy { resolver.registerContentObserver(Settings.Global.getUriFor( Settings.Global.POWER_BUTTON_SUPPRESSION_DELAY_AFTER_GESTURE_WAKE), false, this, UserHandle.USER_ALL); + resolver.registerContentObserver(Settings.System.getUriFor( + Settings.System.VOLBTN_MUSIC_CONTROLS), false, this, + UserHandle.USER_ALL); updateSettings(); } @@ -2194,6 +2209,9 @@ public class PhoneWindowManager implements WindowManagerPolicy { Settings.Global.KEY_CHORD_POWER_VOLUME_UP, mContext.getResources().getInteger( com.android.internal.R.integer.config_keyChordPowerVolumeUp)); + mVolBtnMusicControls = Settings.System.getIntForUser(resolver, + Settings.System.VOLBTN_MUSIC_CONTROLS, 0, + UserHandle.USER_CURRENT) == 1; } if (updateRotation) { updateRotation(true); @@ -3461,6 +3479,20 @@ public class PhoneWindowManager implements WindowManagerPolicy { mDefaultDisplayPolicy.setHdmiPlugged(plugged, true /* force */); } + /** + * @return Whether music is being played right now "locally" (e.g. on the device's speakers + * or wired headphones) or "remotely" (e.g. on a device using the Cast protocol and + * controlled by this device, or through remote submix). + */ + private boolean isMusicActive() { + final AudioManager am = mContext.getSystemService(AudioManager.class); + if (am == null) { + Log.w(TAG, "isMusicActive: couldn't get AudioManager reference"); + return false; + } + return am.isMusicActive(); + } + // TODO(b/117479243): handle it in InputPolicy /** {@inheritDoc} */ @Override @@ -3661,16 +3693,54 @@ public class PhoneWindowManager implements WindowManagerPolicy { break; } } - if (mUseTvRouting || mHandleVolumeKeysInWM) { + if (mUseTvRouting || mHandleVolumeKeysInWM || !mVolBtnMusicControls) { // Defer special key handlings to // {@link interceptKeyBeforeDispatching()}. result |= ACTION_PASS_TO_USER; } else if ((result & ACTION_PASS_TO_USER) == 0) { - // If we aren't passing to the user and no one else - // handled it send it to the session manager to - // figure out. - MediaSessionLegacyHelper.getHelper(mContext).sendVolumeKeyEvent( - event, AudioManager.USE_DEFAULT_STREAM_TYPE, true); + boolean mayChangeVolume; + + if (isMusicActive()) { + if (mVolBtnMusicControls && (keyCode != KeyEvent.KEYCODE_VOLUME_MUTE)) { + // Detect long key presses. + if (down) { + mVolBtnLongPress = false; + int newKeyCode = event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP ? + KeyEvent.KEYCODE_MEDIA_NEXT + : KeyEvent.KEYCODE_MEDIA_PREVIOUS; + scheduleLongPressKeyEvent(event, newKeyCode); + // Consume key down events of all presses. + break; + } else { + mHandler.removeMessages(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK); + // Consume key up events of long presses only. + if (mVolBtnLongPress) { + 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; + } + } else { + result |= ACTION_PASS_TO_USER; + break; + } + if (mayChangeVolume) { + // If we aren't passing to the user and no one else + // 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( + newEvent, AudioManager.USE_DEFAULT_STREAM_TYPE, true); + } + break; } break; } @@ -4002,6 +4072,14 @@ public class PhoneWindowManager implements WindowManagerPolicy { } } + 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()); + } + /** * When the screen is off we ignore some keys that might otherwise typically * be considered wake keys. We filter them out here. @@ -4088,6 +4166,22 @@ public class PhoneWindowManager implements WindowManagerPolicy { return false; } + IDreamManager dreamManager = getDreamManager(); + boolean isDreaming = false; + try { + if (dreamManager != null && dreamManager.isDreaming()) { + isDreaming = true; + } + } catch (RemoteException e) { + Slog.e(TAG, "RemoteException when checking if dreaming", e); + } + + if (isDreaming && isVolumeKey(keyCode)) { + // We don't want the volume key events to be dispatched when the system is deaming in + // order to process them later + return false; + } + // Send events to keyguard while the screen is on and it's showing. if (isKeyguardShowingAndNotOccluded() && !displayOff) { return true; @@ -4106,14 +4200,8 @@ public class PhoneWindowManager implements WindowManagerPolicy { if (isDefaultDisplay) { // Send events to a dozing dream even if the screen is off since the dream // is in control of the state of the screen. - IDreamManager dreamManager = getDreamManager(); - - try { - if (dreamManager != null && dreamManager.isDreaming()) { - return true; - } - } catch (RemoteException e) { - Slog.e(TAG, "RemoteException when checking if dreaming", e); + if (isDreaming) { + return true; } } @@ -4122,6 +4210,11 @@ public class PhoneWindowManager implements WindowManagerPolicy { return false; } + private static boolean isVolumeKey(int code) { + return code == KeyEvent.KEYCODE_VOLUME_DOWN + || code == KeyEvent.KEYCODE_VOLUME_UP; + } + // pre-condition: event.getKeyCode() is one of KeyEvent.KEYCODE_VOLUME_UP, // KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE private void dispatchDirectAudioEvent(KeyEvent event) { |