Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

launcher: the key services stop filtering keys while a game controller is connected

Guilhermee2b74a3647da
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/GamepadPassthrough.java b/launcher/app/src/main/java/com/gathin/tvhome/GamepadPassthrough.java
new file mode 100644
index 000000000..db69c40a7
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/GamepadPassthrough.java
@@ -0,0 +1,74 @@
+package com.gathin.tvhome;
+
+import android.accessibilityservice.AccessibilityService;
+import android.accessibilityservice.AccessibilityServiceInfo;
+import android.content.Context;
+import android.hardware.input.InputManager;
+import android.view.InputDevice;
+
+/**
+ * Stops HomeKeyService and InputKeyService filtering keys while a game controller is connected. With key filtering on,
+ * Android routes every key through the accessibility layer and re-injects the ones the service passes on, and games
+ * (Steam Link with an Xbox controller, on a TCL G07) stop seeing the controller's buttons as the controller's. The
+ * remote buttons the services handle do nothing until the controller disconnects; filtering then comes back.
+ * Started only with key_filter on: with it off the services never filter, and this must not turn filtering back on.
+ */
+final class GamepadPassthrough implements InputManager.InputDeviceListener {
+ private final AccessibilityService service;
+ private final InputManager input;
+
+ GamepadPassthrough(AccessibilityService service) {
+ this.service = service;
+ this.input = (InputManager) service.getSystemService(Context.INPUT_SERVICE);
+ }
+
+ void start() {
+ input.registerInputDeviceListener(this, null);
+ update();
+ }
+
+ void stop() {
+ input.unregisterInputDeviceListener(this);
+ }
+
+ /**
+ * A gamepad has all the SOURCE_GAMEPAD bits. SOURCE_JOYSTICK alone is not enough: Bluetooth voice remotes report
+ * it too (TCL's BT_RC833_U1: 0x01000311, an Xbox controller: 0x01000511).
+ */
+ static boolean isGamepad(int sources) {
+ return (sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD;
+ }
+
+ private boolean gamepadConnected() {
+ for (int id : input.getInputDeviceIds()) {
+ InputDevice device = input.getInputDevice(id);
+ if (device != null && !device.isVirtual() && isGamepad(device.getSources())) return true;
+ }
+ return false;
+ }
+
+ private void update() {
+ AccessibilityServiceInfo info = service.getServiceInfo();
+ if (info == null) return;
+ boolean filter = !gamepadConnected();
+ boolean filtering = (info.flags & AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS) != 0;
+ if (filter == filtering) return;
+ info.flags ^= AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
+ service.setServiceInfo(info);
+ }
+
+ @Override
+ public void onInputDeviceAdded(int deviceId) {
+ update();
+ }
+
+ @Override
+ public void onInputDeviceRemoved(int deviceId) {
+ update();
+ }
+
+ @Override
+ public void onInputDeviceChanged(int deviceId) {
+ update();
+ }
+}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
index 3fd973d5b..cb2de4738 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -50,6 +50,8 @@ public class HomeKeyService extends AccessibilityService {
private BroadcastReceiver unlockReceiver;
/** The stock launcher's HOME activities (stock_home_redirect), disabled ones included. */
private final Set<String> stockHome = new HashSet<>();
+ /** Stops key filtering while a game controller is connected; null with key_filter off or before the unlock. */
+ private GamepadPassthrough gamepads;
@Override
protected void onServiceConnected() {
@@ -87,6 +89,7 @@ public class HomeKeyService extends AccessibilityService {
@Override
public void onDestroy() {
stopWaitingForUnlock();
+ if (gamepads != null) gamepads.stop();
super.onDestroy();
}
@@ -117,6 +120,11 @@ public class HomeKeyService extends AccessibilityService {
}
holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
+ // key_filter off: the service never filters, so there is nothing to hand over to a game controller.
+ if (config.keyFilter && gamepads == null) {
+ gamepads = new GamepadPassthrough(this);
+ gamepads.start();
+ }
}
private boolean isSettingsKey(KeyEvent event) {
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
index 45e54135c..23483cd8b 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@ -27,6 +27,8 @@ public class InputKeyService extends AccessibilityService {
private Map<Integer, String> holdAppScancodes = Collections.emptyMap();
/** restore_key_services was on when the config was read: this helper is noted for ServiceRepair. */
private boolean repairRecord;
+ /** Stops key filtering while a game controller is connected; null with key_filter off. */
+ private GamepadPassthrough gamepads;
@Override
protected void onServiceConnected() {
@@ -42,6 +44,11 @@ public class InputKeyService extends AccessibilityService {
RemoteKeys.applyKeyFilter(this, config.keyFilter);
holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
+ // key_filter off: the service never filters, so there is nothing to hand over to a game controller.
+ if (config.keyFilter && gamepads == null) {
+ gamepads = new GamepadPassthrough(this);
+ gamepads.start();
+ }
}
private boolean isInputKey(int keyCode) {
@@ -74,6 +81,12 @@ public class InputKeyService extends AccessibilityService {
return super.onUnbind(intent);
}
+ @Override
+ public void onDestroy() {
+ if (gamepads != null) gamepads.stop();
+ super.onDestroy();
+ }
+
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
diff --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xml
index c38384e0f..ff50ea62b 100644
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@ -97,7 +97,9 @@
<!-- Whether HomeKeyService and InputKeyService actually request key filtering (config.xml or a theme's
"key_filter"). Some firmware (TCL Google TV on Android 12) drops key auto-repeat once ANY service
filters keys, so long-pressing volume only steps once: set false and the service keeps running but
- stops filtering, which brings repeat back at the price of the remote buttons below doing nothing. -->
+ stops filtering, which brings repeat back at the price of the remote buttons below doing nothing.
+ With it true, filtering still pauses while a game controller is connected, so games see its buttons
+ (Steam Link with an Xbox controller); the remote buttons below come back when it disconnects. -->
<bool name="key_filter">true</bool>
<!-- Vendor source numbers for the Inputs menu, "HW<n>=<number>" by the framework input id's last part
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/GamepadPassthroughTest.java b/launcher/app/src/test/java/com/gathin/tvhome/GamepadPassthroughTest.java
new file mode 100644
index 000000000..f3fc5902e
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/GamepadPassthroughTest.java
@@ -0,0 +1,20 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class GamepadPassthroughTest {
+ // Sources as dumpsys input reports them on a TCL G07.
+ @Test public void xboxControllerIsAGamepad() {
+ assertTrue(GamepadPassthrough.isGamepad(0x01000511));
+ }
+
+ @Test public void voiceRemoteWithJoystickIsNot() {
+ assertFalse(GamepadPassthrough.isGamepad(0x01000311));
+ }
+
+ @Test public void keypadIsNot() {
+ assertFalse(GamepadPassthrough.isGamepad(0x00000101));
+ }
+}