2

I'm looking for a way to identify the input device that is external.

I notice the Android API for [InputDevice] class have a function called [isExternal]. But when I tried to use it, it tells me that it cannot resolve method. I check the online API reference and notice that the function does not exist. So I wonder why is the function in the API but not in the online reference.

Reference: https://developer.android.com/reference/android/view/InputDevice.html https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/InputDevice.java

Eugene Lim
  • 269
  • 3
  • 15

1 Answers1

5

isExternal is a hidden method that is not accessible through the SDK. However, you can still invoke it using java reflection.

public boolean isExternal(InputDevice inputDevice) {
    try {
        Method m = InputDevice.class.getMethod("isExternal");
        return (Boolean) m.invoke(inputDevice);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
        return false;
    }
}

source: What does @hide mean in the Android source code?

Trent Bennett
  • 66
  • 1
  • 3
  • Since Android api 29 this method is fully accessible without reflection :) https://developer.android.com/reference/android/view/InputDevice#isExternal() – Ariel Yust Nov 29 '20 at 17:20