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

Commit 4b033b4b authored by Vaibhav Devmurari's avatar Vaibhav Devmurari
Browse files

Flag guard modifier key remapping

Test: atest ModifierKeyRemappingTest
Test: atest KeyRemapperTests
Bug: 252812993
Change-Id: I3b5b071434675cb641d16ec52a9e45fe7d917b3e
parent 8178dc20
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.hardware.input.InputManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.ArrayMap;
import android.util.FeatureFlagUtils;
import android.view.InputDevice;

import com.android.internal.annotations.GuardedBy;
@@ -65,16 +67,25 @@ final class KeyRemapper implements InputManager.InputDeviceListener {
    }

    public void remapKey(int fromKey, int toKey) {
        if (!supportRemapping()) {
            return;
        }
        Message msg = Message.obtain(mHandler, MSG_REMAP_KEY, fromKey, toKey);
        mHandler.sendMessage(msg);
    }

    public void clearAllKeyRemappings() {
        if (!supportRemapping()) {
            return;
        }
        Message msg = Message.obtain(mHandler, MSG_CLEAR_ALL_REMAPPING);
        mHandler.sendMessage(msg);
    }

    public Map<Integer, Integer> getKeyRemapping() {
        if (!supportRemapping()) {
            return new ArrayMap<>();
        }
        synchronized (mDataStore) {
            return mDataStore.getKeyRemapping();
        }
@@ -124,6 +135,9 @@ final class KeyRemapper implements InputManager.InputDeviceListener {

    @Override
    public void onInputDeviceAdded(int deviceId) {
        if (!supportRemapping()) {
            return;
        }
        InputManager inputManager = Objects.requireNonNull(
                mContext.getSystemService(InputManager.class));
        InputDevice inputDevice = inputManager.getInputDevice(deviceId);
@@ -158,4 +172,9 @@ final class KeyRemapper implements InputManager.InputDeviceListener {
        }
        return false;
    }

    private boolean supportRemapping() {
        return FeatureFlagUtils.isEnabled(mContext,
                FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_MODIFIER_KEY);
    }
}
+62 −24
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.hardware.input.IInputManager
import android.hardware.input.InputManager
import android.os.test.TestLooper
import android.platform.test.annotations.Presubmit
import android.provider.Settings
import android.view.InputDevice
import android.view.KeyEvent
import androidx.test.core.app.ApplicationProvider
@@ -113,7 +114,8 @@ class KeyRemapperTests {
    }

    @Test
    fun testKeyRemapping() {
    fun testKeyRemapping_whenRemappingEnabled() {
        ModifierRemappingFlag(true).use {
            val keyboard = createKeyboard(DEVICE_ID)
            Mockito.`when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboard)

@@ -148,3 +150,39 @@ class KeyRemapperTests {
            )
        }
    }

    @Test
    fun testKeyRemapping_whenRemappingDisabled() {
        ModifierRemappingFlag(false).use {
            val keyboard = createKeyboard(DEVICE_ID)
            Mockito.`when`(iInputManager.getInputDevice(DEVICE_ID)).thenReturn(keyboard)

            mKeyRemapper.remapKey(REMAPPABLE_KEYS[0], REMAPPABLE_KEYS[1])
            testLooper.dispatchAll()

            val remapping = mKeyRemapper.keyRemapping
            assertEquals(
                "Remapping should not be done if modifier key remapping is disabled",
                0,
                remapping.size
            )
        }
    }

    private inner class ModifierRemappingFlag constructor(enabled: Boolean) : AutoCloseable {
        init {
            Settings.Global.putString(
                context.contentResolver,
                "settings_new_keyboard_modifier_key", enabled.toString()
            )
        }

        override fun close() {
            Settings.Global.putString(
                context.contentResolver,
                "settings_new_keyboard_modifier_key",
                ""
            )
        }
    }
}
 No newline at end of file