Rename the locked meta key constants for clarity.

Also added some tests for LED setting.

Change-Id: I3fd86322afd07ae8de52d1ccbc2fae2c6d586641
diff --git a/api/current.xml b/api/current.xml
index a10c05a..63ed5018 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -195045,7 +195045,7 @@
  visibility="public"
 >
 </method>
-<method name="isCapsLockLatched"
+<method name="isCapsLockOn"
  return="boolean"
  abstract="false"
  native="false"
@@ -195113,7 +195113,7 @@
 <parameter name="keyCode" type="int">
 </parameter>
 </method>
-<method name="isNumLockLatched"
+<method name="isNumLockOn"
  return="boolean"
  abstract="false"
  native="false"
@@ -195135,7 +195135,7 @@
  visibility="public"
 >
 </method>
-<method name="isScrollLockLatched"
+<method name="isScrollLockOn"
  return="boolean"
  abstract="false"
  native="false"
@@ -197228,7 +197228,7 @@
  visibility="public"
 >
 </field>
-<field name="META_CAPS_LOCK_LATCHED"
+<field name="META_CAPS_LOCK_ON"
  type="int"
  transient="false"
  volatile="false"
@@ -197316,7 +197316,7 @@
  visibility="public"
 >
 </field>
-<field name="META_NUM_LOCK_LATCHED"
+<field name="META_NUM_LOCK_ON"
  type="int"
  transient="false"
  volatile="false"
@@ -197327,7 +197327,7 @@
  visibility="public"
 >
 </field>
-<field name="META_SCROLL_LOCK_LATCHED"
+<field name="META_SCROLL_LOCK_ON"
  type="int"
  transient="false"
  volatile="false"
diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java
index 04c331f..e7a16a2 100755
--- a/core/java/android/view/KeyEvent.java
+++ b/core/java/android/view/KeyEvent.java
@@ -674,9 +674,9 @@
         "META_META_LEFT_ON",
         "META_META_RIGHT_ON",
         "0x00080000",
-        "META_CAPS_LOCK_LATCHED",
-        "META_NUM_LOCK_LATCHED",
-        "META_SCROLL_LOCK_LATCHED",
+        "META_CAPS_LOCK_ON",
+        "META_NUM_LOCK_ON",
+        "META_SCROLL_LOCK_ON",
         "0x00800000",
         "0x01000000",
         "0x02000000",
@@ -872,31 +872,31 @@
     public static final int META_META_RIGHT_ON = 0x40000;
 
     /**
-     * <p>This mask is used to check whether the CAPS LOCK meta key is latched.</p>
+     * <p>This mask is used to check whether the CAPS LOCK meta key is on.</p>
      *
-     * @see #isCapsLockLatched()
+     * @see #isCapsLockOn()
      * @see #getMetaState()
      * @see #KEYCODE_CAPS_LOCK
      */
-    public static final int META_CAPS_LOCK_LATCHED = 0x100000;
+    public static final int META_CAPS_LOCK_ON = 0x100000;
 
     /**
-     * <p>This mask is used to check whether the NUM LOCK meta key is latched.</p>
+     * <p>This mask is used to check whether the NUM LOCK meta key is on.</p>
      *
-     * @see #isNumLockLatched()
+     * @see #isNumLockOn()
      * @see #getMetaState()
      * @see #KEYCODE_NUM_LOCK
      */
-    public static final int META_NUM_LOCK_LATCHED = 0x200000;
+    public static final int META_NUM_LOCK_ON = 0x200000;
 
     /**
-     * <p>This mask is used to check whether the SCROLL LOCK meta key is latched.</p>
+     * <p>This mask is used to check whether the SCROLL LOCK meta key is on.</p>
      *
-     * @see #isScrollLockLatched()
+     * @see #isScrollLockOn()
      * @see #getMetaState()
      * @see #KEYCODE_SCROLL_LOCK
      */
-    public static final int META_SCROLL_LOCK_LATCHED = 0x400000;
+    public static final int META_SCROLL_LOCK_ON = 0x400000;
 
     /**
      * This mask is set if the device woke because of this key event.
@@ -1411,9 +1411,9 @@
      * @see #isCtrlPressed()
      * @see #isMetaPressed()
      * @see #isFunctionPressed()
-     * @see #isCapsLockLatched()
-     * @see #isNumLockLatched()
-     * @see #isScrollLockLatched()
+     * @see #isCapsLockOn()
+     * @see #isNumLockOn()
+     * @see #isScrollLockOn()
      * @see #META_ALT_ON
      * @see #META_ALT_LEFT_ON
      * @see #META_ALT_RIGHT_ON
@@ -1428,9 +1428,9 @@
      * @see #META_META_ON
      * @see #META_META_LEFT_ON
      * @see #META_META_RIGHT_ON
-     * @see #META_CAPS_LOCK_LATCHED
-     * @see #META_NUM_LOCK_LATCHED
-     * @see #META_SCROLL_LOCK_LATCHED
+     * @see #META_CAPS_LOCK_ON
+     * @see #META_NUM_LOCK_ON
+     * @see #META_SCROLL_LOCK_ON
      */
     public final int getMetaState() {
         return mMetaState;
@@ -1551,39 +1551,39 @@
     }
 
     /**
-     * <p>Returns the latched state of the CAPS LOCK meta key.</p>
+     * <p>Returns the locked state of the CAPS LOCK meta key.</p>
      *
-     * @return true if the CAPS LOCK key is latched, false otherwise
+     * @return true if the CAPS LOCK key is on, false otherwise
      *
      * @see #KEYCODE_CAPS_LOCK
-     * @see #META_CAPS_LOCK_LATCHED
+     * @see #META_CAPS_LOCK_ON
      */
-    public final boolean isCapsLockLatched() {
-        return (mMetaState & META_CAPS_LOCK_LATCHED) != 0;
+    public final boolean isCapsLockOn() {
+        return (mMetaState & META_CAPS_LOCK_ON) != 0;
     }
 
     /**
-     * <p>Returns the latched state of the NUM LOCK meta key.</p>
+     * <p>Returns the locked state of the NUM LOCK meta key.</p>
      *
-     * @return true if the NUM LOCK key is latched, false otherwise
+     * @return true if the NUM LOCK key is on, false otherwise
      *
      * @see #KEYCODE_NUM_LOCK
-     * @see #META_NUM_LOCK_LATCHED
+     * @see #META_NUM_LOCK_ON
      */
-    public final boolean isNumLockLatched() {
-        return (mMetaState & META_NUM_LOCK_LATCHED) != 0;
+    public final boolean isNumLockOn() {
+        return (mMetaState & META_NUM_LOCK_ON) != 0;
     }
 
     /**
-     * <p>Returns the latched state of the SCROLL LOCK meta key.</p>
+     * <p>Returns the locked state of the SCROLL LOCK meta key.</p>
      *
-     * @return true if the SCROLL LOCK key is latched, false otherwise
+     * @return true if the SCROLL LOCK key is on, false otherwise
      *
      * @see #KEYCODE_SCROLL_LOCK
-     * @see #META_SCROLL_LOCK_LATCHED
+     * @see #META_SCROLL_LOCK_ON
      */
-    public final boolean isScrollLockLatched() {
-        return (mMetaState & META_SCROLL_LOCK_LATCHED) != 0;
+    public final boolean isScrollLockOn() {
+        return (mMetaState & META_SCROLL_LOCK_ON) != 0;
     }
 
     /**
diff --git a/libs/ui/InputReader.cpp b/libs/ui/InputReader.cpp
index 120222c..b91e93a 100644
--- a/libs/ui/InputReader.cpp
+++ b/libs/ui/InputReader.cpp
@@ -129,11 +129,11 @@
     case AKEYCODE_META_RIGHT:
         return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
     case AKEYCODE_CAPS_LOCK:
-        return toggleLockedMetaState(AMETA_CAPS_LOCK_LATCHED, down, oldMetaState);
+        return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
     case AKEYCODE_NUM_LOCK:
-        return toggleLockedMetaState(AMETA_NUM_LOCK_LATCHED, down, oldMetaState);
+        return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
     case AKEYCODE_SCROLL_LOCK:
-        return toggleLockedMetaState(AMETA_SCROLL_LOCK_LATCHED, down, oldMetaState);
+        return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
     default:
         return oldMetaState;
     }
@@ -966,8 +966,8 @@
             // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
             if (mAssociatedDisplayId >= 0) {
                 int32_t orientation;
-                if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
-                    return;
+                if (!getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
+                    orientation = InputReaderPolicyInterface::ROTATION_0;
                 }
 
                 keyCode = rotateKeyCode(keyCode, orientation);
@@ -1058,11 +1058,11 @@
 
 void KeyboardInputMapper::updateLedStateLocked(bool reset) {
     updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
-            AMETA_CAPS_LOCK_LATCHED, reset);
+            AMETA_CAPS_LOCK_ON, reset);
     updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
-            AMETA_NUM_LOCK_LATCHED, reset);
+            AMETA_NUM_LOCK_ON, reset);
     updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
-            AMETA_SCROLL_LOCK_LATCHED, reset);
+            AMETA_SCROLL_LOCK_ON, reset);
 }
 
 void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
@@ -1228,7 +1228,7 @@
             // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
             int32_t orientation;
             if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
-                return;
+                orientation = InputReaderPolicyInterface::ROTATION_0;
             }
 
             float temp;
diff --git a/libs/ui/tests/InputReader_test.cpp b/libs/ui/tests/InputReader_test.cpp
index c19147f..ded0225 100644
--- a/libs/ui/tests/InputReader_test.cpp
+++ b/libs/ui/tests/InputReader_test.cpp
@@ -376,6 +376,7 @@
         KeyedVector<int32_t, int32_t> scanCodeStates;
         KeyedVector<int32_t, int32_t> switchStates;
         KeyedVector<int32_t, KeyInfo> keys;
+        KeyedVector<int32_t, bool> leds;
 
         Device(const String8& name, uint32_t classes) :
                 name(name), classes(classes) {
@@ -450,6 +451,16 @@
         device->keys.add(scanCode, info);
     }
 
+    void addLed(int32_t deviceId, int32_t led, bool initialState) {
+        Device* device = getDevice(deviceId);
+        device->leds.add(led, initialState);
+    }
+
+    bool getLedState(int32_t deviceId, int32_t led) {
+        Device* device = getDevice(deviceId);
+        return device->leds.valueFor(led);
+    }
+
     Vector<String8>& getExcludedDevices() {
         return mExcludedDevices;
     }
@@ -584,10 +595,22 @@
     }
 
     virtual bool hasLed(int32_t deviceId, int32_t led) const {
-        return false;
+        Device* device = getDevice(deviceId);
+        return device && device->leds.indexOfKey(led) >= 0;
     }
 
     virtual void setLedState(int32_t deviceId, int32_t led, bool on) {
+        Device* device = getDevice(deviceId);
+        if (device) {
+            ssize_t index = device->leds.indexOfKey(led);
+            if (index >= 0) {
+                device->leds.replaceValueAt(led, on);
+            } else {
+                ADD_FAILURE()
+                        << "Attempted to set the state of an LED that the EventHub declared "
+                        "was not present.  led=" << led;
+            }
+        }
     }
 
     virtual void dump(String8& dump) {
@@ -1703,6 +1726,81 @@
     ASSERT_FALSE(flags[1]);
 }
 
+TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
+    mFakeEventHub->addLed(DEVICE_ID, LED_CAPSL, true /*initially on*/);
+    mFakeEventHub->addLed(DEVICE_ID, LED_NUML, false /*initially off*/);
+    mFakeEventHub->addLed(DEVICE_ID, LED_SCROLLL, false /*initially off*/);
+
+    KeyboardInputMapper* mapper = new KeyboardInputMapper(mDevice, -1,
+            AINPUT_SOURCE_KEYBOARD, AINPUT_KEYBOARD_TYPE_ALPHABETIC);
+    addMapperAndConfigure(mapper);
+
+    // Initialization should have turned all of the lights off.
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
+
+    // Toggle caps lock on.
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0, 0);
+    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
+    ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper->getMetaState());
+
+    // Toggle num lock on.
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
+    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
+    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
+    ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper->getMetaState());
+
+    // Toggle caps lock off.
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 1, 0);
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
+    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
+    ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper->getMetaState());
+
+    // Toggle scroll lock on.
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
+    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
+    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
+    ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
+
+    // Toggle num lock off.
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 1, 0);
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0, 0);
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
+    ASSERT_TRUE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
+    ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper->getMetaState());
+
+    // Toggle scroll lock off.
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 1, 0);
+    process(mapper, ARBITRARY_TIME, DEVICE_ID,
+            EV_KEY, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0, 0);
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_CAPSL));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_NUML));
+    ASSERT_FALSE(mFakeEventHub->getLedState(DEVICE_ID, LED_SCROLLL));
+    ASSERT_EQ(AMETA_NONE, mapper->getMetaState());
+}
+
 
 // --- TrackballInputMapperTest ---
 
diff --git a/native/include/android/input.h b/native/include/android/input.h
index 729dd2e..861ec8b 100644
--- a/native/include/android/input.h
+++ b/native/include/android/input.h
@@ -116,14 +116,14 @@
     /* This mask is used to check whether the right META meta key is pressed. */
     AMETA_META_RIGHT_ON = 0x40000,
 
-    /* This mask is used to check whether the CAPS LOCK meta key is latched. */
-    AMETA_CAPS_LOCK_LATCHED = 0x100000,
+    /* This mask is used to check whether the CAPS LOCK meta key is on. */
+    AMETA_CAPS_LOCK_ON = 0x100000,
 
-    /* This mask is used to check whether the NUM LOCK meta key is latched. */
-    AMETA_NUM_LOCK_LATCHED = 0x200000,
+    /* This mask is used to check whether the NUM LOCK meta key is on. */
+    AMETA_NUM_LOCK_ON = 0x200000,
 
-    /* This mask is used to check whether the SCROLL LOCK meta key is latched. */
-    AMETA_SCROLL_LOCK_LATCHED = 0x400000,
+    /* This mask is used to check whether the SCROLL LOCK meta key is on. */
+    AMETA_SCROLL_LOCK_ON = 0x400000,
 };
 
 /*