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

Commit e9c274b6 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira Committed by Gerrit Code Review
Browse files

Merge "Re-implement orientation aware volume buttons at lower level" into cm-10.2

parents 964de4cc 54819157
Loading
Loading
Loading
Loading
+11 −27
Original line number Diff line number Diff line
@@ -37,9 +37,7 @@ import android.os.ServiceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.VolumePanel;
import android.view.WindowManager;

import java.util.HashMap;

@@ -58,7 +56,6 @@ public class AudioManager {
    private final Binder mToken = new Binder();
    private static String TAG = "AudioManager";
    private final ProfileManager mProfileManager;
    private final WindowManager mWindowManager;

    /**
     * Broadcast intent, a hint for applications that audio is about to become
@@ -434,7 +431,6 @@ public class AudioManager {
        mUseVolumeKeySounds = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_useVolumeKeySounds);
        mProfileManager = (ProfileManager) context.getSystemService(Context.PROFILE_SERVICE);
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }

    private static IAudioService getService()
@@ -485,33 +481,21 @@ public class AudioManager {
                 * Adjust the volume in on key down since it is more
                 * responsive to the user.
                 */
                int direction;
                int swapKeys = Settings.System.getInt(mContext.getContentResolver(),
                        Settings.System.SWAP_VOLUME_KEYS_ON_ROTATION, 0);
                int rotation = mWindowManager.getDefaultDisplay().getRotation();
                if (swapKeys == 1 // phone or hybrid
                        && (rotation == Surface.ROTATION_90
                        || rotation == Surface.ROTATION_180)) {
                    direction = keyCode == KeyEvent.KEYCODE_VOLUME_UP
                            ? ADJUST_LOWER
                            : ADJUST_RAISE;
                } else if (swapKeys == 2 // tablet
                        && (rotation == Surface.ROTATION_180
                        || rotation == Surface.ROTATION_270)) {
                    direction = keyCode == KeyEvent.KEYCODE_VOLUME_UP
                            ? ADJUST_LOWER
                            : ADJUST_RAISE;
                } else {
                    direction = keyCode == KeyEvent.KEYCODE_VOLUME_UP
                            ? ADJUST_RAISE
                            : ADJUST_LOWER;
                }
                int flags = FLAG_SHOW_UI | FLAG_VIBRATE;

                if (mUseMasterVolume) {
                    adjustMasterVolume(direction, flags);
                    adjustMasterVolume(
                            keyCode == KeyEvent.KEYCODE_VOLUME_UP
                                    ? ADJUST_RAISE
                                    : ADJUST_LOWER,
                            flags);
                } else {
                    adjustSuggestedStreamVolume(direction, stream, flags);
                    adjustSuggestedStreamVolume(
                            keyCode == KeyEvent.KEYCODE_VOLUME_UP
                                    ? ADJUST_RAISE
                                    : ADJUST_LOWER,
                            stream,
                            flags);
                }
                break;
            case KeyEvent.KEYCODE_VOLUME_MUTE:
+26 −6
Original line number Diff line number Diff line
@@ -101,9 +101,10 @@ static inline const char* toString(bool value) {
}

static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
        const int32_t map[][4], size_t mapSize) {
        const int32_t map[][4], size_t mapSize,
        int32_t rotationMapOffset) {
    if (orientation != DISPLAY_ORIENTATION_0) {
        for (size_t i = 0; i < mapSize; i++) {
        for (size_t i = rotationMapOffset; i < mapSize; i++) {
            if (value == map[i][0]) {
                return map[i][orientation];
            }
@@ -115,6 +116,16 @@ static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
static const int32_t keyCodeRotationMap[][4] = {
        // key codes enumerated counter-clockwise with the original (unrotated) key first
        // no rotation,        90 degree rotation,  180 degree rotation, 270 degree rotation

        // volume keys - tablet
        { AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN },
        { AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_UP },

        // volume keys - phone or hybrid
        { AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP },
        { AKEYCODE_VOLUME_DOWN, AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_UP,   AKEYCODE_VOLUME_DOWN },

        // dpad keys - common
        { AKEYCODE_DPAD_DOWN,   AKEYCODE_DPAD_RIGHT,  AKEYCODE_DPAD_UP,     AKEYCODE_DPAD_LEFT },
        { AKEYCODE_DPAD_RIGHT,  AKEYCODE_DPAD_UP,     AKEYCODE_DPAD_LEFT,   AKEYCODE_DPAD_DOWN },
        { AKEYCODE_DPAD_UP,     AKEYCODE_DPAD_LEFT,   AKEYCODE_DPAD_DOWN,   AKEYCODE_DPAD_RIGHT },
@@ -123,9 +134,11 @@ static const int32_t keyCodeRotationMap[][4] = {
static const size_t keyCodeRotationMapSize =
        sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);

static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation,
        int32_t rotationMapOffset) {
    return rotateValueUsingRotationMap(keyCode, orientation,
            keyCodeRotationMap, keyCodeRotationMapSize);
            keyCodeRotationMap, keyCodeRotationMapSize,
            rotationMapOffset);
}

static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
@@ -2040,10 +2053,16 @@ void KeyboardInputMapper::configure(nsecs_t when,
            mOrientation = DISPLAY_ORIENTATION_0;
        }
    }
    if (!changes || (changes & InputReaderConfiguration::CHANGE_VOLUME_KEYS_ROTATION)) {
        // mode 0 (disabled) ~ offset 4
        // mode 1 (phone) ~ offset 2
        // mode 2 (tablet) ~ offset 0
        mRotationMapOffset = 4 - 2 * config->volumeKeysRotationMode;
    }
}

void KeyboardInputMapper::configureParameters() {
    mParameters.orientationAware = false;
    mParameters.orientationAware = !getDevice()->isExternal();
    getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
            mParameters.orientationAware);

@@ -2117,7 +2136,8 @@ void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
    if (down) {
        // Rotate key codes according to orientation if needed.
        if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
            keyCode = rotateKeyCode(keyCode, mOrientation);
            keyCode = rotateKeyCode(keyCode, mOrientation,
                    mRotationMapOffset);
        }

        // Add key down.
+12 −3
Original line number Diff line number Diff line
@@ -140,6 +140,9 @@ struct InputReaderConfiguration {
        // Stylus icon option changed.
        CHANGE_STYLUS_ICON_ENABLED = 1 << 6,

        // Volume keys rotation option changed.
        CHANGE_VOLUME_KEYS_ROTATION = 1 << 7,

        // All devices must be reopened.
        CHANGE_MUST_REOPEN = 1 << 31,
    };
@@ -230,6 +233,10 @@ struct InputReaderConfiguration {
    // True to show the pointer icon when a stylus is used.
    bool stylusIconEnabled;

    // Remap volume keys according to display rotation
    // 0 - disabled, 1 - phone or hybrid rotation mode, 2 - tablet rotation mode
    int volumeKeysRotationMode;

    // Ignore finger touches this long after the stylus has been used (including hover)
    nsecs_t stylusPalmRejectionTime;

@@ -251,7 +258,8 @@ struct InputReaderConfiguration {
            pointerGestureZoomSpeedRatio(0.3f),
            showTouches(false),
            stylusIconEnabled(false),
            stylusPalmRejectionTime(50 * 10000000LL) // 50 ms
            stylusPalmRejectionTime(50 * 10000000LL), // 50 ms
            volumeKeysRotationMode(0)
    { }

    bool getDisplayInfo(bool external, DisplayViewport* outViewport) const;
@@ -1043,7 +1051,8 @@ private:
    uint32_t mSource;
    int32_t mKeyboardType;

    int32_t mOrientation; // orientation for dpad keys
    int32_t mRotationMapOffset; // determines if and how volume keys rotate
    int32_t mOrientation; // orientation for dpad and volume keys

    Vector<KeyDown> mKeyDowns; // keys that are down
    int32_t mMetaState;
+20 −0
Original line number Diff line number Diff line
@@ -182,6 +182,7 @@ public class InputManagerService extends IInputManager.Stub
    private static native void nativeSetPointerSpeed(int ptr, int speed);
    private static native void nativeSetShowTouches(int ptr, boolean enabled);
    private static native void nativeSetStylusIconEnabled(int ptr, boolean enabled);
    private static native void nativeSetVolumeKeysRotation(int ptr, int mode);
    private static native void nativeVibrate(int ptr, int deviceId, long[] pattern,
            int repeat, int token);
    private static native void nativeCancelVibrate(int ptr, int deviceId, int token);
@@ -273,6 +274,7 @@ public class InputManagerService extends IInputManager.Stub
        registerPointerSpeedSettingObserver();
        registerShowTouchesSettingObserver();
        registerStylusIconEnabledSettingObserver();
        registerVolumeKeysRotationSettingObserver();

        mContext.registerReceiver(new BroadcastReceiver() {
            @Override
@@ -285,6 +287,7 @@ public class InputManagerService extends IInputManager.Stub
        updatePointerSpeedFromSettings();
        updateShowTouchesFromSettings();
        updateStylusIconEnabledFromSettings();
        updateVolumeKeysRotationFromSettings();
    }

    // TODO(BT) Pass in paramter for bluetooth system
@@ -1197,6 +1200,23 @@ public class InputManagerService extends IInputManager.Stub
        return result;
    }

    public void updateVolumeKeysRotationFromSettings() {
        int mode = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.SWAP_VOLUME_KEYS_ON_ROTATION, 0);
        nativeSetVolumeKeysRotation(mPtr, mode);
    }

    public void registerVolumeKeysRotationSettingObserver() {
        mContext.getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.SWAP_VOLUME_KEYS_ON_ROTATION), false,
                new ContentObserver(mHandler) {
                    @Override
                    public void onChange(boolean selfChange) {
                        updateVolumeKeysRotationFromSettings();
                    }
                });
    }

    public void updateShowTouchesFromSettings() {
        int setting = getShowTouchesSetting(0);
        nativeSetShowTouches(mPtr, setting != 0);
+31 −0
Original line number Diff line number Diff line
@@ -177,6 +177,7 @@ public:
    void setPointerSpeed(int32_t speed);
    void setShowTouches(bool enabled);
    void setStylusIconEnabled(bool enabled);
    void setVolumeKeysRotation(int mode);

    /* --- InputReaderPolicyInterface implementation --- */

@@ -240,6 +241,9 @@ private:
        // Show icon when stylus is used
        bool stylusIconEnabled;

        // Volume keys rotation mode (0 - off, 1 - phone, 2 - tablet)
        int32_t volumeKeysRotationMode;

        // Sprite controller singleton, created on first use.
        sp<SpriteController> spriteController;

@@ -279,6 +283,7 @@ NativeInputManager::NativeInputManager(jobject contextObj,
        mLocked.pointerGesturesEnabled = true;
        mLocked.showTouches = false;
        mLocked.stylusIconEnabled = false;
        mLocked.volumeKeysRotationMode = 0;
    }

    sp<EventHub> eventHub = new EventHub();
@@ -412,6 +417,7 @@ void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outCon
        outConfig->showTouches = mLocked.showTouches;

        outConfig->stylusIconEnabled = mLocked.stylusIconEnabled;
        outConfig->volumeKeysRotationMode = mLocked.volumeKeysRotationMode;

        outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
        outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
@@ -752,6 +758,22 @@ void NativeInputManager::setStylusIconEnabled(bool enabled) {
            InputReaderConfiguration::CHANGE_STYLUS_ICON_ENABLED);
}

void NativeInputManager::setVolumeKeysRotation(int mode) {
    { // acquire lock
        AutoMutex _l(mLock);

        if (mLocked.volumeKeysRotationMode == mode) {
            return;
        }

        ALOGI("Volume keys: rotation mode set to %d.", mode);
        mLocked.volumeKeysRotationMode = mode;
    } // release lock

    mInputManager->getReader()->requestRefreshConfiguration(
            InputReaderConfiguration::CHANGE_VOLUME_KEYS_ROTATION);
}

bool NativeInputManager::isScreenOn() {
    return android_server_PowerManagerService_isScreenOn();
}
@@ -1255,6 +1277,13 @@ static void nativeSetStylusIconEnabled(JNIEnv* env,
    im->setStylusIconEnabled(enabled);
}

static void nativeSetVolumeKeysRotation(JNIEnv* env,
        jclass clazz, jint ptr, int mode) {
    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);

    im->setVolumeKeysRotation(mode);
}

static void nativeVibrate(JNIEnv* env,
        jclass clazz, jint ptr, jint deviceId, jlongArray patternObj,
        jint repeat, jint token) {
@@ -1362,6 +1391,8 @@ static JNINativeMethod gInputManagerMethods[] = {
            (void*) nativeSetShowTouches },
    { "nativeSetStylusIconEnabled", "(IZ)V",
            (void*) nativeSetStylusIconEnabled },
    { "nativeSetVolumeKeysRotation", "(II)V",
            (void*) nativeSetVolumeKeysRotation },
    { "nativeVibrate", "(II[JII)V",
            (void*) nativeVibrate },
    { "nativeCancelVibrate", "(III)V",