Skip to content

Commit

Permalink
Fix volume rocker music controls and wake up
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mikeNG authored and rascarlo committed Dec 29, 2014
1 parent 09ed328 commit b3c8f58
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,6 @@ public void handleMessage(Message msg) {
case MSG_DISPATCH_MEDIA_KEY_REPEAT_WITH_WAKE_LOCK:
dispatchMediaKeyRepeatWithWakeLock((KeyEvent)msg.obj);
break;
case MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK:
dispatchMediaKeyWithWakeLockToAudioService((KeyEvent)msg.obj);
dispatchMediaKeyWithWakeLockToAudioService(KeyEvent.changeAction((KeyEvent)msg.obj, KeyEvent.ACTION_UP));
break;
case MSG_DISPATCH_SHOW_RECENTS:
showRecentApps(false);
break;
Expand All @@ -601,6 +597,14 @@ public void handleMessage(Message msg) {
case MSG_LAUNCH_VOICE_ASSIST_WITH_WAKE_LOCK:
launchVoiceAssistWithWakeLock(msg.arg1 != 0);
break;
case MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK: {
KeyEvent event = (KeyEvent) msg.obj;
mIsLongPress = true;
dispatchMediaKeyWithWakeLockToAudioService(event);
dispatchMediaKeyWithWakeLockToAudioService(
KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
break;
}
}
}
}
Expand Down Expand Up @@ -4434,38 +4438,52 @@ public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
}
}

if (isMusicActive() && (result & ACTION_PASS_TO_USER) == 0) {
if (mVolBtnMusicControls && down && (keyCode != KeyEvent.KEYCODE_VOLUME_MUTE)) {
mIsLongPress = false;
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());
break;
} else {
if (mVolBtnMusicControls && !down) {
mHandler.removeMessages(MSG_DISPATCH_VOLKEY_WITH_WAKE_LOCK);
if (mIsLongPress) {
// Disable music and volume control when used as wake key
if ((result & ACTION_PASS_TO_USER) == 0 && !mVolumeRockerWake) {
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;
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 (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 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, true);
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, true);
}
break;
}
}
break;
}

case KeyEvent.KEYCODE_ENDCALL: {
result &= ~ACTION_PASS_TO_USER;
Expand Down Expand Up @@ -4635,6 +4653,14 @@ public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
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());
}

/**
* When the screen is off we ignore some keys that might otherwise typically
* be considered wake keys. We filter them out here.
Expand Down

0 comments on commit b3c8f58

Please sign in to comment.