Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
Merge PR #1287: RemoteKeys: hold fires after HOLD_MS, one launch per press, app_keys activity targets
Gathin23ba1dea102b8b
diff --cc README.mdindex 27a634364,15cdfc91f..e06cee5a1--- a/README.md+++ b/README.md@@@ -675,9 -596,8 +675,10 @@@ tools/tv.py 'settings put secure screen- It can bring back other buttons only the stock launcher answered, such as the **YouTube** button(`app_keys` and `input_key_keycodes`, in config.xml or a theme; `HOME_KEY_SERVICE=1` reads `app_keys` too).- A `hold:` prefix on an `app_keys` entry gives that button a second app: a normal press opens the- unprefixed app, holding for half a second opens the hold one (by keycode or `hold:scan:...`).+ unprefixed app, holding for half a second opens the hold one (by keycode or `hold:scan:...`). An entry can+ name an activity (`package/.Activity`), such as an add-on's quick bar, instead of the app's launcher entry.+- In place of an app, an `app_keys` entry can name an action: `back`, `home`, `recents`, `mute`, `sleep`,+ `screensaver`, `settings` or `wifi`, so a spare colour button can mute the TV or, held, put it in standby.- `HOME_KEY_SERVICE=1` also fixes sticks whose Home key sends ALL_APPS, and opens Settings from the remote's**gear** button, even one Android does not map (scancodes or keycodes in config.xml).- With `settings_key_panel` the gear opens a [small quick settings panel](docs/quick-settings-panel.jpg)diff --cc launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.javaindex 6b49bddd6,6c60c18b7..13bdb9778--- a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java+++ b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java@@@ -3,12 -3,12 +3,15 @@@ package com.gathin.tvhomeimport android.accessibilityservice.AccessibilityService;import android.accessibilityservice.AccessibilityServiceInfo;import android.content.ActivityNotFoundException;+ import android.content.ComponentName;import android.content.Context;import android.content.Intent;+import android.media.AudioManager;import android.media.tv.TvContract;+import android.os.Build;+import android.provider.Settings;+ import android.os.Handler;+ import android.os.Looper;import android.view.KeyEvent;import java.util.ArrayList;@@@ -16,8 -16,8 +19,9 @@@ import java.util.Arraysimport java.util.HashMap;import java.util.List;import java.util.Map;+ import java.util.function.Consumer;import java.util.function.ToIntFunction;+import java.util.regex.Pattern;/*** Remote buttons that only the stock launcher answered, for InputKeyService and HomeKeyService: app_keys@@@ -25,10 -25,10 +29,12 @@@* "KEYCODE_BUTTON_3:com.google.android.youtube.tv", or a scancode as "scan:490:com.wbd.hbomax". A "hold:"* prefix makes an entry the long-press action instead: "hold:KEYCODE_BUTTON_8:org.xbmc.kodi" opens Kodi when* that button is held for HOLD_MS, while the unprefixed entry still opens on a tap; "hold:scan:490:..." does- * the same for a scancode. One set of maps for both services; whichever filters the key first consumes it.+ * the same for a scancode. One set of maps for both services; Android hands a key to every service that filters,+ * so both may call handle() for one event, and it answers each press once.- * A target of "input:<input id>" switches to that TV input (an HDMI port) instead of opening an app, and one of+ * A target of "input:<input id>" switches to that TV input (an HDMI port) instead of opening an app, a target- * of "inputs" opens the input switcher, for a remote whose source button sends a key Android does not map, and- * one of the action words (back, home, recents, mute, sleep, screensaver, settings, wifi) does that action.++ * of "inputs" opens the input switcher, for a remote whose source button sends a key Android does not map,++ * one of the action words (back, home, recents, mute, sleep, screensaver, settings, wifi) does that action, and+ * "package/.Activity" opens that activity rather than the app's launcher entry (an add-on's quick bar, say).*/final class RemoteKeys {@@@ -166,87 -231,18 +244,96 @@@return id.isEmpty() ? null : id;}+ /**+ * The accessibility global action for an action word, or 0 when the target is something else. "sleep" is+ * GLOBAL_ACTION_LOCK_SCREEN, which puts a TV in standby (as the Sleep Timer add-on does); Android has it from 28.+ */+ static int globalAction(String target, int sdk) {+ switch (target) {+ case "back": return AccessibilityService.GLOBAL_ACTION_BACK;+ case "home": return AccessibilityService.GLOBAL_ACTION_HOME;+ case "recents": return AccessibilityService.GLOBAL_ACTION_RECENTS;+ case "sleep": return sdk >= 28 ? AccessibilityService.GLOBAL_ACTION_LOCK_SCREEN : 0;+ default: return 0;+ }+ }++ /** Whether a target is an action word rather than an app or an input. A package name always has a dot. */+ static boolean isAction(String target) {+ switch (target) {+ case "back": case "home": case "recents": case "mute": case "sleep":+ case "screensaver": case "settings": case "wifi":+ return true;+ default:+ return false;+ }+ }++ /** Runs an action word; false for an app or an input. Needs no permission the launcher does not already have. */+ private static boolean runAction(Context ctx, String target) {+ if (!isAction(target)) return false;+ int global = globalAction(target, Build.VERSION.SDK_INT);+ if (global != 0) {+ if (ctx instanceof AccessibilityService) ((AccessibilityService) ctx).performGlobalAction(global);+ return true;+ }+ switch (target) {+ case "mute":+ // The media stream a TV plays everything on, with its own volume bar, as a remote's mute button would.+ AudioManager audio = ctx.getSystemService(AudioManager.class);+ if (audio != null) audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,+ AudioManager.ADJUST_TOGGLE_MUTE, AudioManager.FLAG_SHOW_UI);+ break;+ case "screensaver":+ // The screensaver chosen in the TV's settings, through the system's "start now" shortcut; the+ // launcher's own clock where the firmware has no such shortcut.+ Intent dream = new Intent(Intent.ACTION_MAIN)+ .setClassName("com.android.systemui", "com.android.systemui.Somnambulator");+ if (ctx.getPackageManager().resolveActivity(dream, 0) == null) dream = new Intent(ctx, ClockActivity.class);+ start(ctx, dream);+ break;+ case "settings":+ start(ctx, SystemSettings.intent(ctx, Settings.ACTION_SETTINGS));+ break;+ case "wifi":+ start(ctx, SystemSettings.intent(ctx, Settings.ACTION_WIFI_SETTINGS));+ break;+ default:+ break; // "sleep" below Android 9: consumed, nothing to do+ }+ return true;+ }++ private static void start(Context ctx, Intent intent) {+ try {+ ctx.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));+ } catch (ActivityNotFoundException | SecurityException ignored) {+ }+ }++ /**+ * True for a "package/.Activity" target. An input id has slashes too, and ComponentName.unflattenFromString()+ * would read "input:com.tcl.tvinput" as its package, so an "input:" target never counts.+ */+ static boolean isActivity(String target) {+ return target != null && inputId(target) == null && target.indexOf('/') > 0;+ }+/** Opens an app from a remote button: its TV entry if it has one, else its phone entry. An "input:<id>" target- * switches to that TV input (an HDMI port) instead, and an action word runs that action. */- * switches to that TV input (an HDMI port) instead; a "package/.Activity" target opens that activity. */++ * switches to that TV input (an HDMI port) instead, an action word runs that action, and a++ * "package/.Activity" target opens that activity. */static void launch(Context ctx, String pkg) {- if (pkg == null) return;+ if (pkg == null || runAction(ctx, pkg)) return;+ if (INPUTS.equals(pkg)) {+ // This launcher's own Inputs menu, as InputsRequestActivity opens it: addressed to Home directly, so a+ // vendor input picker that also answers VIEW_INPUTS never turns it into a chooser.+ try {+ ctx.startActivity(new Intent(ctx, MainActivity.class).setAction(MainActivity.ACTION_VIEW_INPUTS)+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));+ } catch (ActivityNotFoundException ignored) {+ }+ return;+ }String inputId = inputId(pkg);if (inputId != null) {// The same passthrough URI the Inputs menu opens, which the Live TV app handles.diff --cc launcher/app/src/main/res/values/config.xmlindex 580e748f8,60319b282..df98aa677--- a/launcher/app/src/main/res/values/config.xml+++ b/launcher/app/src/main/res/values/config.xml@@@ -182,10 -169,8 +182,12 @@@EV_KEY code, in hex). A scancode entry wins over a keycode. In place of a package, "input:<input id>"switches to that TV input, e.g. scan:227:input:com.tcl.tvinput/.passthroughinput.TvPassThroughService/HW1413744640for HDMI 3 on a TCL Smart TV Pro; the ids are in `tools/tv.py 'dumpsys tv_input'` (hdmi_port=N under- TvInputHardwareInfo). "package/.Activity" opens that activity instead of the app's launcher entry, e.g.- hold:191:io.github.maxhayim.brightness/.QuickActivity for an add-on's quick bar. Prefix an entry with "hold:" for the same button's long press:+ TvInputHardwareInfo). An action word in place of a package runs that action instead: back, home, recents,+ mute (toggles the sound, with the TV's volume bar), sleep (standby, Android 9 and later), screensaver (the+ one chosen in the TV's settings, or the launcher's clock), settings or wifi; e.g. KEYCODE_PROG_YELLOW:mute- and hold:KEYCODE_PROG_YELLOW:sleep. Prefix an entry with "hold:" for the same button's long press:++ and hold:KEYCODE_PROG_YELLOW:sleep. "package/.Activity" opens that activity instead of the app's launcher++ entry, e.g. hold:191:io.github.maxhayim.brightness/.QuickActivity for an add-on's quick bar. Prefix an++ entry with "hold:" for the same button's long press:"hold:KEYCODE_BUTTON_8:org.xbmc.kodi" opens Kodi when held for half a second, while the unprefixed entrystill opens on a tap; "hold:scan:490:..." works for scancodes. A key with only a hold entry does nothingon a tap. A listed app that is not installed leaves the button doing nothing. Leave empty to omit. -->diff --cc launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.javaindex 3682e40ef,46fc5160c..e69fa0716--- a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java+++ b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java@@@ -1,7 -1,8 +1,9 @@@package com.gathin.tvhome;+import android.accessibilityservice.AccessibilityService;+ import java.util.ArrayList;import java.util.Arrays;+ import java.util.List;import java.util.Map;import org.junit.Test;import static org.junit.Assert.assertEquals;@@@ -96,33 -97,90 +98,120 @@@ public class RemoteKeysTestassertNull(RemoteKeys.target(null, "hold.app", 0));}+ @Test public void inputsTargetKeptForAScancode() {+ Map<Integer, String> scans = RemoteKeys.parseAppScancodes(Arrays.asList("scan:365:inputs"));+ assertEquals(RemoteKeys.INPUTS, scans.get(365));+ assertNull(RemoteKeys.inputId(RemoteKeys.INPUTS));+ }++ @Test public void actionWordsAreTargets() {+ Map<Integer, String> keys = parse("190:mute", "KEYCODE_BUTTON_8:screensaver");+ assertEquals("mute", keys.get(190));+ assertEquals("screensaver", keys.get(195));+ assertEquals("sleep", parseHold("hold:190:sleep").get(190));+ for (String word : new String[] {"back", "home", "recents", "mute", "sleep", "screensaver", "settings", "wifi"}) {+ assertTrue(word, RemoteKeys.isAction(word));+ }+ assertFalse(RemoteKeys.isAction("com.netflix.ninja"));+ assertFalse(RemoteKeys.isAction("input:com.tcl.tvinput/.passthroughinput.TvPassThroughService/HW1"));+ assertFalse(RemoteKeys.isAction("Mute"));+ }++ @Test public void globalActions() {+ assertEquals(AccessibilityService.GLOBAL_ACTION_BACK, RemoteKeys.globalAction("back", 26));+ assertEquals(AccessibilityService.GLOBAL_ACTION_HOME, RemoteKeys.globalAction("home", 26));+ assertEquals(AccessibilityService.GLOBAL_ACTION_RECENTS, RemoteKeys.globalAction("recents", 26));+ assertEquals(AccessibilityService.GLOBAL_ACTION_LOCK_SCREEN, RemoteKeys.globalAction("sleep", 28));+ // No lock-screen action before Android 9: the key is consumed and nothing happens.+ assertEquals(0, RemoteKeys.globalAction("sleep", 27));+ assertEquals(0, RemoteKeys.globalAction("mute", 34));+ assertEquals(0, RemoteKeys.globalAction("com.netflix.ninja", 34));+ }+++ @Test public void anInputTargetIsNotAnActivity() {+ // Its id has slashes, but it must reach the passthrough branch, not ComponentName.unflattenFromString().+ assertFalse(RemoteKeys.isActivity("input:com.tcl.tvinput/.passthroughinput.TvPassThroughService/HW1413744640"));+ assertTrue(RemoteKeys.isActivity("io.github.maxhayim.brightness/.QuickActivity"));+ assertFalse(RemoteKeys.isActivity("com.netflix.ninja"));+ assertFalse(RemoteKeys.isActivity("/.QuickActivity"));+ assertFalse(RemoteKeys.isActivity(null));+ }++ /** Presses with a timer the test runs by hand, and the targets they open. */+ private static final class Remote {+ final List<Runnable> holds = new ArrayList<>();+ final List<String> opened = new ArrayList<>();+ final RemoteKeys.Presses presses = new RemoteKeys.Presses(new RemoteKeys.Presses.Timer() {+ @Override public void post(Runnable hold, long delayMs) { holds.add(hold); }+ @Override public void cancel(Runnable hold) { holds.remove(hold); }+ });++ void down(String button, long at, String holdApp) {+ presses.down(button, at, 0, holdApp, opened::add);+ }++ void up(String button, long at, long downTime, String app, String holdApp) {+ presses.up(button, at, downTime, false, app, holdApp, opened::add);+ }++ /** HOLD_MS has passed: every hold still pending fires. */+ void holdTimeout() {+ for (Runnable hold : new ArrayList<>(holds)) hold.run();+ holds.clear();+ }+ }++ @Test public void chordedKeysKeepTheirOwnHold() {+ Remote remote = new Remote();+ remote.down("1:190:0", 1000, "a.hold");+ remote.down("1:191:0", 1100, "b.hold");+ // A is released reporting B's down time; it is still A's tap, and only A's hold is cancelled.+ remote.up("1:190:0", 1200, 1100, "a.tap", "a.hold");+ remote.holdTimeout();+ remote.up("1:191:0", 1700, 1100, "b.tap", "b.hold");+ assertEquals(Arrays.asList("a.tap", "b.hold"), remote.opened);+ }++ @Test public void holdsOnTwoDevicesStayApart() {+ Remote remote = new Remote();+ remote.down("1:190:0", 1000, "hold");+ remote.holdTimeout();+ remote.down("2:190:0", 1000, "hold");+ remote.holdTimeout();+ remote.up("1:190:0", 1800, 1000, "tap", "hold");+ remote.up("2:190:0", 1900, 1000, "tap", "hold");+ assertEquals(Arrays.asList("hold", "hold"), remote.opened);+ }++ @Test public void bothServicesAnswerEachPressOnce() {+ Remote remote = new Remote();+ // A tap, each event delivered to both services, in either order.+ remote.down("1:190:0", 1000, "hold");+ remote.down("1:190:0", 1000, "hold");+ remote.up("1:190:0", 1100, 1000, "tap", "hold");+ remote.up("1:190:0", 1100, 1000, "tap", "hold");+ // A hold, with the second service's DOWN arriving after the hold fired.+ remote.down("1:190:0", 2000, "hold");+ remote.holdTimeout();+ remote.down("1:190:0", 2000, "hold");+ remote.up("1:190:0", 2800, 2000, "tap", "hold");+ remote.up("1:190:0", 2800, 2000, "tap", "hold");+ assertEquals(Arrays.asList("tap", "hold"), remote.opened);+ assertTrue(remote.holds.isEmpty());+ }++ @Test public void aCanceledUpOpensNothingAndDropsTheHold() {+ Remote remote = new Remote();+ remote.down("1:190:0", 1000, "hold");+ remote.presses.up("1:190:0", 1200, 1000, true, "tap", "hold", remote.opened::add);+ remote.holdTimeout();+ assertTrue(remote.opened.isEmpty());+ }++ @Test public void anUpWithoutItsDownUsesTheEventsDownTime() {+ Remote remote = new Remote();+ remote.up("1:190:0", 1100, 1000, "tap", "hold");+ remote.up("1:191:0", 1700, 1000, "tap", "hold");+ assertEquals(Arrays.asList("tap", "hold"), remote.opened);+ }}