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

Commit e92f5f52 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira
Browse files

Bind VolUp+VolDown to toggle the ringer

Add a keyboard chord to toggle the state of the ringer, respecting
the user's vibration settings.
This is using the pre-existing debouncing variables and logic from the
screenshot chord, and renamed some variables to make them a bit
more generic

Change-Id: Ia30364eb28a41642c245fa2222be46fb1a781a90
parent 0166186c
Loading
Loading
Loading
Loading
+78 −11
Original line number Diff line number Diff line
/*
 * File modifications copyright (C) 2012 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -63,6 +64,7 @@ import android.os.Vibrator;
import android.provider.Settings;

import com.android.internal.R;
import com.android.internal.app.ThemeUtils;
import com.android.internal.os.DeviceKeyHandler;
import com.android.internal.policy.PolicyManager;
import com.android.internal.statusbar.IStatusBarService;
@@ -147,7 +149,12 @@ 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;
@@ -520,11 +527,13 @@ 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;
    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;
@@ -787,9 +796,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,
@@ -802,6 +811,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() {
        public void run() {
            // The context isn't read
@@ -834,6 +861,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 = am.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER)
                                           == AudioManager.VIBRATE_SETTING_ON;
                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 {
@@ -1885,21 +1932,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;
            }
@@ -3476,25 +3536,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) {
@@ -3568,6 +3634,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                            && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {
                        mPowerKeyTriggered = true;
                        mPowerKeyTime = event.getDownTime();
                        cancelPendingRingerChordAction();
                        interceptScreenshotChord();
                    }