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

Commit 245b5e00 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira Committed by Rick C
Browse files

Port "Bind VolUp+VolDown to toggle the ringer"

Squash of http://review.cyanogenmod.org/#/c/20256/ and
http://review.cyanogenmod.org/#/c/22716/

Change-Id: Ied3767969482a7ea44f7f0e9222a2f78fe8f507c
parent 62cf1ebc
Loading
Loading
Loading
Loading
+76 −11
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import android.os.Vibrator;
import android.provider.Settings;

import com.android.internal.R;
import com.android.internal.app.ThemeUtils;
import com.android.internal.policy.PolicyManager;
import com.android.internal.policy.impl.keyguard.KeyguardViewManager;
import com.android.internal.policy.impl.keyguard.KeyguardViewMediator;
@@ -153,7 +154,11 @@ import android.view.KeyCharacterMap.FallbackAction;
import android.view.accessibility.AccessibilityEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.VolumePanel;
import android.widget.Toast;
import android.media.IAudioService;
import android.media.AudioService;
import android.media.AudioManager;

import java.io.File;
import java.io.FileReader;
@@ -475,13 +480,15 @@ public class PhoneWindowManager implements WindowManagerPolicy {

    // Screenshot trigger states
    // Time to volume and power must be pressed within this interval of each other.
    private static final long SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS = 150;
    private static final long ACTION_CHORD_DEBOUNCE_DELAY_MILLIS = 150;
    // Increase the chord delay when taking a screenshot from the keyguard
    private static final float KEYGUARD_SCREENSHOT_CHORD_DELAY_MULTIPLIER = 2.5f;
    private boolean mScreenshotChordEnabled;
    private boolean mVolumeDownKeyTriggered;
    private long mVolumeDownKeyTime;
    private boolean mVolumeDownKeyConsumedByScreenshotChord;
    private long mVolumeUpKeyTime;
    private boolean mVolumeDownKeyConsumedByChord;
    private boolean mVolumeUpKeyConsumedByChord;
    private boolean mVolumeUpKeyTriggered;
    private boolean mPowerKeyTriggered;
    private long mPowerKeyTime;
@@ -755,9 +762,9 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        if (mScreenshotChordEnabled
                && mVolumeDownKeyTriggered && mPowerKeyTriggered && !mVolumeUpKeyTriggered) {
            final long now = SystemClock.uptimeMillis();
            if (now <= mVolumeDownKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS
                    && now <= mPowerKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS) {
                mVolumeDownKeyConsumedByScreenshotChord = true;
            if (now <= mVolumeDownKeyTime + ACTION_CHORD_DEBOUNCE_DELAY_MILLIS
                    && now <= mPowerKeyTime + ACTION_CHORD_DEBOUNCE_DELAY_MILLIS) {
                mVolumeDownKeyConsumedByChord = true;
                cancelPendingPowerKeyAction();

                mHandler.postDelayed(mScreenshotChordLongPress, getScreenshotChordLongPressDelay());
@@ -779,6 +786,24 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        mHandler.removeCallbacks(mScreenshotChordLongPress);
    }

    private void interceptRingerChord() {
        if (mVolumeDownKeyTriggered && !mPowerKeyTriggered && mVolumeUpKeyTriggered) {
            final long now = SystemClock.uptimeMillis();
            if (now <= mVolumeDownKeyTime + ACTION_CHORD_DEBOUNCE_DELAY_MILLIS
                    && now <= mVolumeUpKeyTime + ACTION_CHORD_DEBOUNCE_DELAY_MILLIS) {
                mVolumeDownKeyConsumedByChord = true;
                mVolumeUpKeyConsumedByChord = true;

                mHandler.postDelayed(mRingerChordLongPress,
                        ViewConfiguration.getGlobalActionKeyTimeout());
            }
        }
    }

    private void cancelPendingRingerChordAction() {
        mHandler.removeCallbacks(mRingerChordLongPress);
    }

    private final Runnable mPowerLongPress = new Runnable() {
        @Override
        public void run() {
@@ -820,6 +845,26 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        }
    };

    private final Runnable mRingerChordLongPress = new Runnable() {
        public void run() {
            // Do the switch
            final AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
            final int ringerMode = am.getRingerMode();
            final VolumePanel volumePanel = new VolumePanel(ThemeUtils.createUiContext(mContext),
                                                              (AudioService) getAudioService());
            if (ringerMode == AudioManager.RINGER_MODE_NORMAL) {
                boolean vibrateSetting = Settings.System.getInt(mContext.getContentResolver(),
                                           Settings.System.VIBRATE_WHEN_RINGING, 0) != 0;
                am.setRingerMode(vibrateSetting ? AudioManager.RINGER_MODE_VIBRATE :
                                   AudioManager.RINGER_MODE_SILENT);
            } else {
                am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            }
            volumePanel.postVolumeChanged(AudioManager.STREAM_RING,AudioManager.FLAG_SHOW_UI
                                          | AudioManager.FLAG_VIBRATE);
        }
    };

    Runnable mBackLongPress = new Runnable() {
        public void run() {
            try {
@@ -1937,21 +1982,34 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                    + " canceled=" + canceled);
        }

        // If we think we might have a volume down & power key chord on the way
        // If we think we might have a volume down & power/volume-up key chord on the way
        // but we're not sure, then tell the dispatcher to wait a little while and
        // try again later before dispatching.
        if (mScreenshotChordEnabled && (flags & KeyEvent.FLAG_FALLBACK) == 0) {
            if (mVolumeDownKeyTriggered && !mPowerKeyTriggered) {
            if (mVolumeDownKeyTriggered && !mPowerKeyTriggered && !mVolumeUpKeyTriggered) {
                final long now = SystemClock.uptimeMillis();
                final long timeoutTime = mVolumeDownKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILLIS;
                final long timeoutTime = mVolumeDownKeyTime + ACTION_CHORD_DEBOUNCE_DELAY_MILLIS;
                if (now < timeoutTime) {
                    return timeoutTime - now;
                }
            } else if (mVolumeUpKeyTriggered && !mVolumeDownKeyTriggered) {
                final long now = SystemClock.uptimeMillis();
                final long timeoutTime = mVolumeUpKeyTime + ACTION_CHORD_DEBOUNCE_DELAY_MILLIS;
                if (now < timeoutTime) {
                    return timeoutTime - now;
                }
            }

            if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
                    && mVolumeDownKeyConsumedByScreenshotChord) {
                    && mVolumeDownKeyConsumedByChord) {
                if (!down) {
                    mVolumeDownKeyConsumedByChord = false;
                }
                return -1;
            } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
                    && mVolumeUpKeyConsumedByChord) {
                if (!down) {
                    mVolumeDownKeyConsumedByScreenshotChord = false;
                    mVolumeUpKeyConsumedByChord = false;
                }
                return -1;
            }
@@ -3591,25 +3649,31 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                                && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
                            mVolumeDownKeyTriggered = true;
                            mVolumeDownKeyTime = event.getDownTime();
                            mVolumeDownKeyConsumedByScreenshotChord = false;
                            mVolumeDownKeyConsumedByChord = false;
                            cancelPendingPowerKeyAction();
                            interceptScreenshotChord();
                            interceptRingerChord();
                        }
                    } else {
                        mVolumeDownKeyTriggered = false;
                        cancelPendingScreenshotChordAction();
                        cancelPendingRingerChordAction();
                    }
                } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                    if (down) {
                        if (isScreenOn && !mVolumeUpKeyTriggered
                                && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
                            mVolumeUpKeyTriggered = true;
                            mVolumeUpKeyTime = event.getDownTime();
                            mVolumeUpKeyConsumedByChord = false;
                            cancelPendingPowerKeyAction();
                            cancelPendingScreenshotChordAction();
                            interceptRingerChord();
                        }
                    } else {
                        mVolumeUpKeyTriggered = false;
                        cancelPendingScreenshotChordAction();
                        cancelPendingRingerChordAction();
                    }
                }
                if (down) {
@@ -3686,6 +3750,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                            && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
                        mPowerKeyTriggered = true;
                        mPowerKeyTime = event.getDownTime();
                        cancelPendingRingerChordAction();
                        interceptScreenshotChord();
                    }