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/include/androidfw/KeyCharacterMap.h b/include/androidfw/KeyCharacterMap.h
index 679dd2c..3cc1cb2 100644
--- a/include/androidfw/KeyCharacterMap.h
+++ b/include/androidfw/KeyCharacterMap.h
@@ -19,12 +19,17 @@
 
 #include <stdint.h>
 
+#if HAVE_ANDROID_OS
+#include <binder/IBinder.h>
+#endif
+
 #include <androidfw/Input.h>
 #include <utils/Errors.h>
 #include <utils/KeyedVector.h>
 #include <utils/Tokenizer.h>
 #include <utils/String8.h>
 #include <utils/Unicode.h>
+#include <utils/RefBase.h>
 
 namespace android {
 
@@ -32,8 +37,10 @@
  * Describes a mapping from Android key codes to characters.
  * Also specifies other functions of the keyboard such as the keyboard type
  * and key modifier semantics.
+ *
+ * This object is immutable after it has been loaded.
  */
-class KeyCharacterMap {
+class KeyCharacterMap : public RefBase {
 public:
     enum KeyboardType {
         KEYBOARD_TYPE_UNKNOWN = 0,
@@ -50,9 +57,11 @@
         int32_t metaState;
     };
 
-    ~KeyCharacterMap();
+    /* Loads a key character map from a file. */
+    static status_t load(const String8& filename, sp<KeyCharacterMap>* outMap);
 
-    static status_t load(const String8& filename, KeyCharacterMap** outMap);
+    /* Returns an empty key character map. */
+    static sp<KeyCharacterMap> empty();
 
     /* Gets the keyboard type. */
     int32_t getKeyboardType() const;
@@ -92,6 +101,17 @@
     bool getEvents(int32_t deviceId, const char16_t* chars, size_t numChars,
             Vector<KeyEvent>& outEvents) const;
 
+#if HAVE_ANDROID_OS
+    /* Reads a key map from a parcel. */
+    static sp<KeyCharacterMap> readFromParcel(Parcel* parcel);
+
+    /* Writes a key map to a parcel. */
+    void writeToParcel(Parcel* parcel) const;
+#endif
+
+protected:
+    virtual ~KeyCharacterMap();
+
 private:
     struct Behavior {
         Behavior();
@@ -162,6 +182,8 @@
         status_t parseCharacterLiteral(char16_t* outCharacter);
     };
 
+    static sp<KeyCharacterMap> sEmpty;
+
     KeyedVector<int32_t, Key*> mKeys;
     int mType;