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

Commit fa0c6a58 authored by Danesh M's avatar Danesh M
Browse files

Lockscreen : Vol long press skip tracks (1/2)

Straight port from cm7

Credit : Kmobs,Sven,Cvps..and all who've refined it

Patchset 2 : Minor fixes
Patchset 3 : Fix misplaced closing bracket
Patchset 4 : Rework the conditional logic
Patchset 5 : Rebase
Patchset 6 : Fix vol change for wake screen
Patchset 7 : Cleanup

Change-Id: I74584932772ddabbd203acb4d6a65270fd6b20c2
parent bcab1a93
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -2262,6 +2262,12 @@ public final class Settings {
         */
        public static final String VOLUME_WAKE_SCREEN = "volume_wake_screen";

        /**
         * Whether or not volume button music controls should be enabled to seek media tracks
         * @hide
         */
        public static final String VOLBTN_MUSIC_CONTROLS = "volbtn_music_controls";

        /**
         * Whether national data roaming should be used.
         * @hide
+105 −40
Original line number Diff line number Diff line
@@ -40,7 +40,6 @@ import android.database.ContentObserver;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@@ -356,6 +355,10 @@ public class PhoneWindowManager implements WindowManagerPolicy {
    // Behavior of volume wake
    boolean mVolumeWakeScreen;

    // Behavior of volbtn music controls
    boolean mVolBtnMusicControls;
    boolean mIsLongPress;

    private final InputHandler mPointerLocationInputHandler = new BaseInputHandler() {
        @Override
        public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {
@@ -484,6 +487,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.VOLUME_WAKE_SCREEN), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.VOLBTN_MUSIC_CONTROLS), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.ACCELEROMETER_ROTATION), false, this);
            resolver.registerContentObserver(Settings.System.getUriFor(
@@ -622,6 +627,53 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    }

    /**
     * When a volumeup-key longpress expires, skip songs based on key press
     */
    Runnable mVolumeUpLongPress = new Runnable() {
        public void run() {
            // set the long press flag to true
            mIsLongPress = true;

            // Shamelessly copied from Kmobs LockScreen controls, works for Pandora, etc...
            sendMediaButtonEvent(KeyEvent.KEYCODE_MEDIA_NEXT);
        };
    };

    /**
     * When a volumedown-key longpress expires, skip songs based on key press
     */
    Runnable mVolumeDownLongPress = new Runnable() {
        public void run() {
            // set the long press flag to true
            mIsLongPress = true;

            // Shamelessly copied from Kmobs LockScreen controls, works for Pandora, etc...
            sendMediaButtonEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
        };
    };

    private void sendMediaButtonEvent(int code) {
        long eventtime = SystemClock.uptimeMillis();
        Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
        KeyEvent keyEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, code, 0);
        keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        mContext.sendOrderedBroadcast(keyIntent, null);
        keyEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_UP);
        keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        mContext.sendOrderedBroadcast(keyIntent, null);
    }

    void handleVolumeLongPress(int keycode) {
        mHandler.postDelayed(keycode == KeyEvent.KEYCODE_VOLUME_UP ? mVolumeUpLongPress :
            mVolumeDownLongPress, ViewConfiguration.getLongPressTimeout());
    }

    void handleVolumeLongPressAbort() {
        mHandler.removeCallbacks(mVolumeUpLongPress);
        mHandler.removeCallbacks(mVolumeDownLongPress);
    }

    private void interceptScreenshotChord() {
        if (mVolumeDownKeyTriggered && mPowerKeyTriggered && !mVolumeUpKeyTriggered) {
            final long now = SystemClock.uptimeMillis();
@@ -959,6 +1011,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_DEFAULT);
            mVolumeWakeScreen = (Settings.System.getInt(resolver,
                    Settings.System.VOLUME_WAKE_SCREEN, 0) == 1);
            mVolBtnMusicControls = (Settings.System.getInt(resolver,
                    Settings.System.VOLBTN_MUSIC_CONTROLS, 1) == 1);
            int accelerometerDefault = Settings.System.getInt(resolver,
                    Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);
            
@@ -2803,13 +2857,8 @@ public class PhoneWindowManager implements WindowManagerPolicy {

            final boolean isWakeKey = (policyFlags
                    & (WindowManagerPolicy.FLAG_WAKE | WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0
                    || ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) && mVolumeWakeScreen)
                    || ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) && mVolumeWakeScreen);

            // make sure keyevent get's handled as power key on volume-wake
            if(!isScreenOn && mVolumeWakeScreen && isWakeKey && ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)
                    || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)))
                keyCode=KeyEvent.KEYCODE_POWER;
                    || (((keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN))
                            && mVolumeWakeScreen && !isScreenOn);

            if (down && isWakeKey) {
                if (keyguardActive) {
@@ -2825,6 +2874,36 @@ public class PhoneWindowManager implements WindowManagerPolicy {

        // Handle special keys.
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENDCALL: {
                result &= ~ACTION_PASS_TO_USER;
                if (down) {
                    ITelephony telephonyService = getTelephonyService();
                    boolean hungUp = false;
                    if (telephonyService != null) {
                        try {
                            hungUp = telephonyService.endCall();
                        } catch (RemoteException ex) {
                            Log.w(TAG, "ITelephony threw RemoteException", ex);
                        }
                    }
                    interceptPowerKeyDown(!isScreenOn || hungUp);
                } else {
                    if (interceptPowerKeyUp(canceled)) {
                        if ((mEndcallBehavior
                                & Settings.System.END_BUTTON_BEHAVIOR_HOME) != 0) {
                            if (goHome()) {
                                break;
                            }
                        }
                        if ((mEndcallBehavior
                                & Settings.System.END_BUTTON_BEHAVIOR_SLEEP) != 0) {
                            result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;
                        }
                    }
                }
                break;
            }

            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_MUTE: {
@@ -2889,45 +2968,31 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                            Log.w(TAG, "ITelephony threw RemoteException", ex);
                        }
                    }

                    if (isMusicActive() && (result & ACTION_PASS_TO_USER) == 0) {
                        // If music is playing but we decided not to pass the key to the
                        // application, handle the volume change here.
                        handleVolumeKey(AudioManager.STREAM_MUSIC, keyCode);
                        break;
                    }
                }
                if (isMusicActive() && (result & ACTION_PASS_TO_USER) == 0) {
                    if (mVolBtnMusicControls && down && (keyCode != KeyEvent.KEYCODE_VOLUME_MUTE)) {
                        mIsLongPress = false;
                        handleVolumeLongPress(keyCode);
                        break;
            }

            case KeyEvent.KEYCODE_ENDCALL: {
                result &= ~ACTION_PASS_TO_USER;
                if (down) {
                    ITelephony telephonyService = getTelephonyService();
                    boolean hungUp = false;
                    if (telephonyService != null) {
                        try {
                            hungUp = telephonyService.endCall();
                        } catch (RemoteException ex) {
                            Log.w(TAG, "ITelephony threw RemoteException", ex);
                        }
                    }
                    interceptPowerKeyDown(!isScreenOn || hungUp);
                    } else {
                    if (interceptPowerKeyUp(canceled)) {
                        if ((mEndcallBehavior
                                & Settings.System.END_BUTTON_BEHAVIOR_HOME) != 0) {
                            if (goHome()) {
                        if (mVolBtnMusicControls && !down) {
                            handleVolumeLongPressAbort();
                            if (mIsLongPress) {
                                break;
                            }
                        }
                        if ((mEndcallBehavior
                                & Settings.System.END_BUTTON_BEHAVIOR_SLEEP) != 0) {
                            result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;
                        if (!isScreenOn && !mVolumeWakeScreen) {
                            handleVolumeKey(AudioManager.STREAM_MUSIC, keyCode);
                        }
                    }
                }
                if (isScreenOn || !mVolumeWakeScreen) {
                    break;
                } else if (keyguardActive) {
                    keyCode = KeyEvent.KEYCODE_POWER;
                    mKeyguardMediator.onWakeKeyWhenKeyguardShowingTq(keyCode,
                            mDockMode != Intent.EXTRA_DOCK_STATE_UNDOCKED);
                }
            }

            case KeyEvent.KEYCODE_POWER: {