Notify applications when input devices change.

This change allows the InputManager to keep track of what input
devices are registered with the system and when they change.
It needs to do this so that it can properly clear its cache of
input device properties (especially the key map!) when changes
occur.

Added new API so that applications can register listeners for
input device changes.

Fixed a minor bug in EventHub where it didn't handle EPOLLHUP
properly so it would spam the log about unsupposed epoll events
until inotify noticed that the device was gone and removed it.

Change-Id: I937d8c601f7185d4299038bce6a2934fe4fdd2b3
diff --git a/services/jni/com_android_server_input_InputManagerService.cpp b/services/jni/com_android_server_input_InputManagerService.cpp
index 85d6e11..f1536fd 100644
--- a/services/jni/com_android_server_input_InputManagerService.cpp
+++ b/services/jni/com_android_server_input_InputManagerService.cpp
@@ -59,6 +59,7 @@
 
 static struct {
     jmethodID notifyConfigurationChanged;
+    jmethodID notifyInputDevicesChanged;
     jmethodID notifyLidSwitchChanged;
     jmethodID notifyInputChannelBroken;
     jmethodID notifyANR;
@@ -82,6 +83,10 @@
 
 static struct {
     jclass clazz;
+} gInputDeviceClassInfo;
+
+static struct {
+    jclass clazz;
 } gKeyEventClassInfo;
 
 static struct {
@@ -179,6 +184,7 @@
 
     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
     virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
+    virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
 
     /* --- InputDispatcherPolicyInterface implementation --- */
 
@@ -486,6 +492,36 @@
     }
 }
 
+void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
+    JNIEnv* env = jniEnv();
+
+    size_t count = inputDevices.size();
+    jobjectArray inputDevicesObjArray = env->NewObjectArray(
+            count, gInputDeviceClassInfo.clazz, NULL);
+    if (inputDevicesObjArray) {
+        bool error = false;
+        for (size_t i = 0; i < count; i++) {
+            jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
+            if (!inputDeviceObj) {
+                error = true;
+                break;
+            }
+
+            env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
+            env->DeleteLocalRef(inputDeviceObj);
+        }
+
+        if (!error) {
+            env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
+                    inputDevicesObjArray);
+        }
+
+        env->DeleteLocalRef(inputDevicesObjArray);
+    }
+
+    checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
+}
+
 void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode,
         int32_t switchValue, uint32_t policyFlags) {
 #if DEBUG_INPUT_DISPATCHER_POLICY
@@ -1147,36 +1183,6 @@
     im->setSystemUiVisibility(visibility);
 }
 
-static jobject nativeGetInputDevice(JNIEnv* env,
-        jclass clazz, jint ptr, jint deviceId) {
-    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
-
-    InputDeviceInfo deviceInfo;
-    status_t status = im->getInputManager()->getReader()->getInputDeviceInfo(
-            deviceId, & deviceInfo);
-    if (status) {
-        return NULL;
-    }
-
-    return android_view_InputDevice_create(env, deviceInfo);
-}
-
-static jintArray nativeGetInputDeviceIds(JNIEnv* env,
-        jclass clazz, jint ptr) {
-    NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
-
-    Vector<int> deviceIds;
-    im->getInputManager()->getReader()->getInputDeviceIds(deviceIds);
-
-    jintArray deviceIdsObj = env->NewIntArray(deviceIds.size());
-    if (! deviceIdsObj) {
-        return NULL;
-    }
-
-    env->SetIntArrayRegion(deviceIdsObj, 0, deviceIds.size(), deviceIds.array());
-    return deviceIdsObj;
-}
-
 static void nativeGetInputConfiguration(JNIEnv* env,
         jclass clazz, jint ptr, jobject configObj) {
     NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
@@ -1273,10 +1279,6 @@
             (void*) nativeSetInputDispatchMode },
     { "nativeSetSystemUiVisibility", "(II)V",
             (void*) nativeSetSystemUiVisibility },
-    { "nativeGetInputDevice", "(II)Landroid/view/InputDevice;",
-            (void*) nativeGetInputDevice },
-    { "nativeGetInputDeviceIds", "(I)[I",
-            (void*) nativeGetInputDeviceIds },
     { "nativeGetInputConfiguration", "(ILandroid/content/res/Configuration;)V",
             (void*) nativeGetInputConfiguration },
     { "nativeTransferTouchFocus", "(ILandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
@@ -1316,6 +1318,9 @@
     GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
             "notifyConfigurationChanged", "(J)V");
 
+    GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
+            "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
+
     GET_METHOD_ID(gServiceClassInfo.notifyLidSwitchChanged, clazz,
             "notifyLidSwitchChanged", "(JZ)V");
 
@@ -1377,6 +1382,11 @@
     GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
             "getPointerIcon", "()Landroid/view/PointerIcon;");
 
+    // InputDevice
+
+    FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
+    gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
+
     // KeyEvent
 
     FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");