Skip to content

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

RemoteKeys: a pending hold ends with the helper that saw its DOWN

NewPlayer021393a369653b0c
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/GamepadPassthrough.java b/launcher/app/src/main/java/com/gathin/tvhome/GamepadPassthrough.java
index 4bccd3e0c..91cedf291 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/GamepadPassthrough.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/GamepadPassthrough.java
@@ -57,6 +57,8 @@ final class GamepadPassthrough implements InputManager.InputDeviceListener {
boolean filter = !gamepadConnected();
boolean filtering = (info.flags & AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS) != 0;
if (filter == filtering) return;
+ // Without the flag no more keys come, the UP of a button held right now included.
+ if (!filter) RemoteKeys.release(service);
info.flags ^= AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
service.setServiceInfo(info);
}
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 233f9820d..5f0c136e3 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -138,6 +138,7 @@ public class HomeKeyService extends AccessibilityService {
@Override
public void onDestroy() {
if (running == this) running = null;
+ RemoteKeys.release(this);
stopWaitingForUnlock();
if (screenOnReceiver != null) unregisterReceiver(screenOnReceiver);
if (standbyExit != null) {
@@ -294,6 +295,7 @@ public class HomeKeyService extends AccessibilityService {
/** Unbound by Android: the user turned the helper off, so Home doesn't put it back (ServiceRepair). */
@Override
public boolean onUnbind(Intent intent) {
+ RemoteKeys.release(this);
if (repairRecord && !steppingOut) ServiceRepair.disconnected(this, getClass());
return super.onUnbind(intent);
}
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 4b8f5746f..7f8eac110 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@ -81,12 +81,14 @@ public class InputKeyService extends AccessibilityService {
/** Unbound by Android: the user turned the helper off, so Home doesn't put it back (ServiceRepair). */
@Override
public boolean onUnbind(Intent intent) {
+ RemoteKeys.release(this);
if (repairRecord) ServiceRepair.disconnected(this, getClass());
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
+ RemoteKeys.release(this);
if (gamepads != null) gamepads.stop();
if (consumeVolumeKeys) VolumeKeys.stop();
super.onDestroy();
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
index 06326d8a6..690209975 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
@@ -17,6 +17,7 @@ import android.view.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@@ -196,7 +197,8 @@ final class RemoteKeys {
}
String button = event.getDeviceId() + ":" + event.getKeyCode() + ":" + event.getScanCode();
if (event.getAction() == KeyEvent.ACTION_DOWN) {
- presses.down(button, event.getEventTime(), event.getRepeatCount(), holdApp, target -> launch(ctx, target));
+ presses.down(button, event.getEventTime(), event.getRepeatCount(), holdApp, ctx,
+ target -> launch(ctx, target));
} else if (event.getAction() == KeyEvent.ACTION_UP) {
presses.up(button, event.getEventTime(), event.getDownTime(), event.isCanceled(), app, holdApp,
target -> launch(ctx, target));
@@ -204,6 +206,15 @@ final class RemoteKeys {
return true;
}
+ /**
+ * For a helper that stops receiving keys (unbound, destroyed, or no longer filtering): no UP will reach it to
+ * cancel a hold it saw go down, so the hold goes to the other helper if that one saw the DOWN too, and is
+ * cancelled otherwise. Safe to call more than once.
+ */
+ static void release(Context helper) {
+ if (presses != null) presses.release(helper);
+ }
+
/**
* Each button's presses, kept per device, keycode and scancode: KeyEvent.getDownTime() can't tell presses apart,
* since a key pressed together with another can report the other's. Both services get every event, so a DOWN
@@ -220,6 +231,8 @@ final class RemoteKeys {
long downAt = -1, upAt = -1;
boolean fired;
Runnable hold;
+ /** The helpers that saw this DOWN while its hold is pending, each with the way it opens a target. */
+ final Map<Object, Consumer<String>> owners = new LinkedHashMap<>();
}
private final Map<String, Press> buttons = new HashMap<>();
@@ -229,23 +242,42 @@ final class RemoteKeys {
this.timer = timer;
}
- void down(String button, long eventTime, int repeatCount, String holdApp, Consumer<String> open) {
+ void down(String button, long eventTime, int repeatCount, String holdApp, Object owner,
+ Consumer<String> open) {
if (repeatCount != 0) return;
Press press = buttons.computeIfAbsent(button, b -> new Press());
- if (press.downAt == eventTime) return;
+ if (press.downAt == eventTime) {
+ // The other helper's copy: it can answer the hold too, should the first one leave.
+ if (press.hold != null) press.owners.putIfAbsent(owner, open);
+ return;
+ }
if (press.hold != null) timer.cancel(press.hold); // its UP never came
press.downAt = eventTime;
press.fired = false;
press.hold = null;
+ press.owners.clear();
if (holdApp == null) return;
+ press.owners.put(owner, open);
press.hold = () -> {
+ Consumer<String> first = press.owners.values().iterator().next();
press.hold = null;
+ press.owners.clear();
press.fired = true;
- open.accept(holdApp);
+ first.accept(holdApp);
};
timer.post(press.hold, HOLD_MS);
}
+ /** Drops this helper from every pending hold, cancelling a hold it was the last to own. */
+ void release(Object owner) {
+ for (Press press : buttons.values()) {
+ if (press.hold == null || press.owners.remove(owner) == null || !press.owners.isEmpty()) continue;
+ timer.cancel(press.hold);
+ press.hold = null;
+ press.fired = true; // the press is over: an UP that still comes later opens nothing
+ }
+ }
+
void up(String button, long eventTime, long downTime, boolean canceled, String app, String holdApp,
Consumer<String> open) {
Press press = buttons.computeIfAbsent(button, b -> new Press());
@@ -255,6 +287,7 @@ final class RemoteKeys {
if (press.hold != null) {
timer.cancel(press.hold);
press.hold = null;
+ press.owners.clear();
}
if (canceled || (sawDown && press.fired)) return;
// Without its DOWN (the service started while the button was held), the event's own down time will do.
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
index d60fc0a6c..db0be6f25 100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
@@ -174,7 +174,12 @@ public class RemoteKeysTest {
});
void down(String button, long at, String holdApp) {
- presses.down(button, at, 0, holdApp, opened::add);
+ presses.down(button, at, 0, holdApp, this, opened::add);
+ }
+
+ /** A DOWN as one of the two helpers sees it; what it opens is noted with its name. */
+ void down(String helper, String button, long at, String holdApp) {
+ presses.down(button, at, 0, holdApp, helper, target -> opened.add(helper + ": " + target));
}
void up(String button, long at, long downTime, String app, String holdApp) {
@@ -235,6 +240,68 @@ public class RemoteKeysTest {
assertTrue(remote.opened.isEmpty());
}
+ @Test public void aHelperUnboundBeforeTheHoldCancelsIt() {
+ Remote remote = new Remote();
+ remote.down("home", "1:190:0", 1000, "hold");
+ remote.presses.release("home");
+ remote.presses.release("home"); // onUnbind, then onDestroy
+ remote.holdTimeout();
+ assertTrue(remote.opened.isEmpty());
+ assertTrue(remote.holds.isEmpty());
+ }
+
+ @Test public void losingOneHelperLeavesTheHoldWithTheOther() {
+ Remote remote = new Remote();
+ remote.down("home", "1:190:0", 1000, "hold");
+ remote.down("input", "1:190:0", 1000, "hold");
+ remote.presses.release("home");
+ remote.holdTimeout();
+ remote.up("1:190:0", 1800, 1000, "tap", "hold");
+ assertEquals(Arrays.asList("input: hold"), remote.opened);
+ }
+
+ @Test public void losingOneHelperKeepsTheOthersTap() {
+ Remote remote = new Remote();
+ remote.down("home", "1:190:0", 1000, "hold");
+ remote.down("input", "1:190:0", 1000, "hold");
+ remote.presses.release("input");
+ remote.up("1:190:0", 1200, 1000, "tap", "hold");
+ remote.holdTimeout();
+ assertEquals(Arrays.asList("tap"), remote.opened);
+ }
+
+ @Test public void aHelperThatStopsFilteringReleasesItsHold() {
+ // gamepad_passthrough drops the key filter when a controller connects: the UP never reaches that helper.
+ Remote remote = new Remote();
+ remote.down("home", "1:190:0", 1000, "hold");
+ remote.down("input", "1:190:0", 1000, "hold");
+ remote.presses.release("home");
+ remote.presses.release("input");
+ remote.holdTimeout();
+ assertTrue(remote.opened.isEmpty());
+ }
+
+ @Test public void anUpAfterTheHoldWasReleasedOpensNothing() {
+ Remote remote = new Remote();
+ remote.down("home", "1:190:0", 1000, "hold");
+ remote.presses.release("home");
+ remote.up("1:190:0", 1200, 1000, "tap", "hold");
+ remote.holdTimeout();
+ // The next press is answered as usual.
+ remote.down("home", "1:190:0", 2000, "hold");
+ remote.up("1:190:0", 2100, 2000, "tap", "hold");
+ assertEquals(Arrays.asList("tap"), remote.opened);
+ }
+
+ @Test public void releasingAHelperLeavesAnotherButtonsHoldAlone() {
+ Remote remote = new Remote();
+ remote.down("home", "1:190:0", 1000, "a.hold");
+ remote.down("input", "1:191:0", 1100, "b.hold");
+ remote.presses.release("home");
+ remote.holdTimeout();
+ assertEquals(Arrays.asList("input: b.hold"), remote.opened);
+ }
+
@Test public void anUpWithoutItsDownUsesTheEventsDownTime() {
Remote remote = new Remote();
remote.up("1:190:0", 1100, 1000, "tap", "hold");