Request key maps from input manager service.

Instead of each application loading the KeyCharacterMap from
the file system, get them from the input manager service as
part of the InputDevice object.

Refactored InputManager to be a proper singleton instead of
having a bunch of static methods.

InputManager now maintains a cache of all InputDevice objects
that it has loaded.  Currently we never invalidate the cache
which can cause InputDevice to return stale motion ranges if
the device is reconfigured.  This will be fixed in a future change.

Added a fake InputDevice with ID -1 to represent the virtual keyboard.

Change-Id: If7a695839ad0972317a5aab89e9d1e42ace28eb7
diff --git a/services/input/EventHub.h b/services/input/EventHub.h
index bd21a3d..c35df109 100644
--- a/services/input/EventHub.h
+++ b/services/input/EventHub.h
@@ -19,6 +19,7 @@
 #define _RUNTIME_EVENT_HUB_H
 
 #include <androidfw/Input.h>
+#include <androidfw/InputDevice.h>
 #include <androidfw/Keyboard.h>
 #include <androidfw/KeyLayoutMap.h>
 #include <androidfw/KeyCharacterMap.h>
@@ -43,6 +44,13 @@
 
 namespace android {
 
+enum {
+    // Device id of a special "virtual" keyboard that is always present.
+    VIRTUAL_KEYBOARD_ID = -1,
+    // Device id of the "built-in" keyboard if there is one.
+    BUILT_IN_KEYBOARD_ID = 0,
+};
+
 /*
  * A raw event as retrieved from the EventHub.
  */
@@ -107,6 +115,9 @@
     /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
     INPUT_DEVICE_CLASS_JOYSTICK      = 0x00000100,
 
+    /* The input device is virtual (not a real device, not part of UI configuration). */
+    INPUT_DEVICE_CLASS_VIRTUAL       = 0x40000000,
+
     /* The input device is external (not built-in). */
     INPUT_DEVICE_CLASS_EXTERNAL      = 0x80000000,
 };
@@ -208,7 +219,7 @@
     virtual void getVirtualKeyDefinitions(int32_t deviceId,
             Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
 
-    virtual String8 getKeyCharacterMapFile(int32_t deviceId) const = 0;
+    virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const = 0;
 
     /* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
     virtual void requestReopenDevices() = 0;
@@ -266,7 +277,7 @@
     virtual void getVirtualKeyDefinitions(int32_t deviceId,
             Vector<VirtualKeyDefinition>& outVirtualKeys) const;
 
-    virtual String8 getKeyCharacterMapFile(int32_t deviceId) const;
+    virtual sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const;
 
     virtual void requestReopenDevices();
 
@@ -282,7 +293,7 @@
     struct Device {
         Device* next;
 
-        int fd;
+        int fd; // may be -1 if device is virtual
         const int32_t id;
         const String8 path;
         const InputDeviceIdentifier identifier;
@@ -305,11 +316,15 @@
         ~Device();
 
         void close();
+
+        inline bool isVirtual() const { return fd < 0; }
     };
 
     status_t openDeviceLocked(const char *devicePath);
-    status_t closeDeviceByPathLocked(const char *devicePath);
+    void createVirtualKeyboardLocked();
+    void addDeviceLocked(Device* device);
 
+    status_t closeDeviceByPathLocked(const char *devicePath);
     void closeDeviceLocked(Device* device);
     void closeAllDevicesLocked();
 
@@ -331,8 +346,13 @@
     // Protect all internal state.
     mutable Mutex mLock;
 
-    // The actual id of the built-in keyboard, or -1 if none.
+    // The actual id of the built-in keyboard, or NO_BUILT_IN_KEYBOARD if none.
     // EventHub remaps the built-in keyboard to id 0 externally as required by the API.
+    enum {
+        // Must not conflict with any other assigned device ids, including
+        // the virtual keyboard id (-1).
+        NO_BUILT_IN_KEYBOARD = -2,
+    };
     int32_t mBuiltInKeyboardId;
 
     int32_t mNextDeviceId;