From d9786c6f7cfa547839df8ade4be01f5614cd5905 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 14 Mar 2021 18:03:42 +0100 Subject: [PATCH] Use device id 0 for touch/mouse events Virtual device is only for keyboard sources, not mouse or touchscreen sources. Here is the value of InputDevice.getDevice(-1).toString(): Input Device -1: Virtual Descriptor: ... Generation: 2 Location: built-in Keyboard Type: alphabetic Has Vibrator: false Has mic: false Sources: 0x301 ( keyboard dpad ) InputDevice.getDeviceId() documentation says: > An id of zero indicates that the event didn't come from a physical > device and maps to the default keymap. However, injecting events with a device id of 0 causes event.getDevice() to be null on the client-side. Commit 26529d377fe8aae2fd1ffa91405aa801a0ce8a57 used -1 as a workaround to avoid a NPE on a specific Android TV device. But this is a bug in the device system, which wrongly assumes that input device may not be null. A similar issue was present in Flutter, but it is now fixed: - - On the other hand, using an id of -1 for touchscreen events (which is invalid) causes issues for some apps: Therefore, use a device id of 0. An alternative could be to find an existing device matching the source, like "adb shell input" does. See getInputDeviceId(): But it seems better to indicate that the event didn't come from a physical device, and it would not solve #962 anyway, because an Android TV has no touchscreen. Refs #962 Fixes #2125