Skip to content

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

Launcher: the Live TV button opens the channel guide again once Live TV is showing

Cameron Cook199fb0292499
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 48bcf21be..2d82c43c6 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -4,6 +4,7 @@ import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@@ -75,6 +76,10 @@ public class HomeKeyService extends AccessibilityService {
private long powerOnUntil;
/** home_long_press_home: this service takes every Home press, held ones included. */
private boolean homeLongPressHome;
+ /** app_keys "front:" entries, and the package of the activity in front, from window changes while there are any. */
+ private Map<Integer, String> frontAppKeys = Collections.emptyMap();
+ private Map<Integer, String> frontAppScancodes = Collections.emptyMap();
+ private String frontPackage = "";
/** power_on_home_always: Home at every power-on, not only over Live TV. */
private boolean powerOnAlways;
/** power_on_home: re-arms after the screen turns on; null unless the key is on. */
@@ -190,6 +195,9 @@ public class HomeKeyService extends AccessibilityService {
appScancodes = RemoteKeys.parseAppScancodes(config.appKeys);
RemoteKeys.applyKeyFilter(this, config.keyFilter);
homeLongPressHome = config.homeLongPressHome;
+ frontAppKeys = RemoteKeys.parseFrontAppKeys(config.appKeys);
+ frontAppScancodes = RemoteKeys.parseFrontAppScancodes(config.appKeys);
+ boolean watchFront = !frontAppKeys.isEmpty() || !frontAppScancodes.isEmpty();
stockHome.clear();
stockHomePackage = config.stockHomeRedirect;
if (!config.stockHomeRedirect.isEmpty()) {
@@ -214,7 +222,7 @@ public class HomeKeyService extends AccessibilityService {
}
// home_key_service.xml subscribes to no events, so the redirects ask for window changes only when one is on.
AccessibilityServiceInfo info = getServiceInfo();
- if (info != null && (!stockHome.isEmpty() || !powerOnLiveTv.isEmpty())) {
+ if (info != null && (!stockHome.isEmpty() || !powerOnLiveTv.isEmpty() || watchFront)) {
info.eventTypes |= AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
setServiceInfo(info);
}
@@ -229,6 +237,13 @@ public class HomeKeyService extends AccessibilityService {
}
}
+ /** A tap's target: the "front:" entry while the button's own app is already in front, else the usual one. */
+ private String inFront(Map<Integer, String> keys, Map<Integer, String> front, int code) {
+ String target = keys.get(code);
+ String alt = front.get(code);
+ return alt != null && RemoteKeys.inFront(target, frontPackage) ? alt : target;
+ }
+
private boolean isSettingsKey(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_SETTINGS) return true;
for (int code : settingsScancodes) if (event.getScanCode() == code) return true;
@@ -245,7 +260,7 @@ public class HomeKeyService extends AccessibilityService {
}
if (consumeVolumeKeys && VolumeKeys.handle(this, event)) return true;
// A scancode entry is more specific than any keycode, so it goes first.
- if (RemoteKeys.handle(this, event, appScancodes.get(event.getScanCode()),
+ if (RemoteKeys.handle(this, event, inFront(appScancodes, frontAppScancodes, event.getScanCode()),
holdAppScancodes.get(event.getScanCode()))) return true;
// The stock launcher handled the remote's Settings button; with it disabled the button does nothing.
if (isSettingsKey(event)) {
@@ -260,7 +275,7 @@ public class HomeKeyService extends AccessibilityService {
}
return true;
}
- if (RemoteKeys.handle(this, event, appKeys.get(event.getKeyCode()),
+ if (RemoteKeys.handle(this, event, inFront(appKeys, frontAppKeys, event.getKeyCode()),
holdAppKeys.get(event.getKeyCode()))) return true;
// home_long_press_home: Android never sees the Home key, so a held press cannot turn into ALL_APPS.
if (homeLongPressHome && event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
@@ -286,6 +301,17 @@ public class HomeKeyService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) return;
+ // app_keys "front:" entries: only activities count, so a toast or the volume bar doesn't change the app in front.
+ if ((!frontAppKeys.isEmpty() || !frontAppScancodes.isEmpty())
+ && event.getPackageName() != null && event.getClassName() != null) {
+ try {
+ ComponentName window = new ComponentName(event.getPackageName().toString(), event.getClassName().toString());
+ getPackageManager().getActivityInfo(window, 0);
+ frontPackage = window.getPackageName();
+ } catch (PackageManager.NameNotFoundException notAnActivity) {
+ // a dialog, panel or overlay: the app under it is still in front
+ }
+ }
// power_on_home: the firmware opened Live TV by itself at power-on, before anyone pressed a key.
if (powerOnRedirect(powerOnLiveTv, event.getPackageName(), SystemClock.elapsedRealtime(), powerOnUntil)) {
openHome();
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 13bdb9778..14df9ae70 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
@@ -94,7 +94,20 @@ final class RemoteKeys {
}
static Map<Integer, String> parseHoldAppKeys(Iterable<String> entries, ToIntFunction<String> names) {
- return parseAppKeys(holdEntries(entries), names);
+ return parseAppKeys(prefixed(entries, "hold:"), names);
+ }
+
+ /** keycode -> target from the "front:" entries, used while the button's own app is already in front. */
+ static Map<Integer, String> parseFrontAppKeys(Iterable<String> entries) {
+ return parseFrontAppKeys(entries, KeyEvent::keyCodeFromString);
+ }
+
+ static Map<Integer, String> parseFrontAppKeys(Iterable<String> entries, ToIntFunction<String> names) {
+ return parseAppKeys(prefixed(entries, "front:"), names);
+ }
+
+ static Map<Integer, String> parseFrontAppScancodes(Iterable<String> entries) {
+ return parseAppScancodes(prefixed(entries, "front:"));
}
/**
@@ -134,18 +147,35 @@ final class RemoteKeys {
/** scancode -> package from the "hold:scan:..." entries; the rest reads as it does in parseAppScancodes. */
static Map<Integer, String> parseHoldAppScancodes(Iterable<String> entries) {
- return parseAppScancodes(holdEntries(entries));
+ return parseAppScancodes(prefixed(entries, "hold:"));
}
- /** The entries with the "hold:" prefix removed, so the normal parsers can read them. */
- private static Iterable<String> holdEntries(Iterable<String> entries) {
- List<String> holds = new ArrayList<>();
+ /** The entries with this prefix ("hold:", "front:"), the prefix removed, so the normal parsers can read them. */
+ private static Iterable<String> prefixed(Iterable<String> entries, String prefix) {
+ List<String> found = new ArrayList<>();
for (String entry : entries) {
if (entry == null) continue;
String text = entry.trim();
- if (text.startsWith("hold:")) holds.add(text.substring(5).trim());
+ if (text.startsWith(prefix)) found.add(text.substring(prefix.length()).trim());
}
- return holds;
+ return found;
+ }
+
+ /** The package of a "guide:<package>" target, or null for any other target. */
+ static String guidePackage(String target) {
+ if (target == null || !target.startsWith("guide:")) return null;
+ String pkg = target.substring(6).trim();
+ return pkg.isEmpty() ? null : pkg;
+ }
+
+ /**
+ * True when a tap's target is the app already in front ("package" or "package/.Activity"), so its "front:"
+ * entry applies instead. Inputs and action words never match.
+ */
+ static boolean inFront(String target, String front) {
+ if (target == null || front == null || front.isEmpty()) return false;
+ int slash = target.indexOf('/');
+ return (slash < 0 ? target : target.substring(0, slash)).equals(front);
}
/** Created on first use: building a Handler while the class loads breaks the JVM tests. */
@@ -334,6 +364,18 @@ final class RemoteKeys {
}
return;
}
+ String guide = guidePackage(pkg);
+ if (guide != null) {
+ // The program guide, asked for the way Android TV's Live TV apps take it: the programs directory.
+ Intent view = new Intent(Intent.ACTION_VIEW)
+ .setDataAndType(TvContract.Programs.CONTENT_URI, "vnd.android.cursor.dir/program")
+ .setPackage(guide).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ try {
+ ctx.startActivity(view);
+ } 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 --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xml
index e3b96ef37..a9987423b 100644
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@ -190,7 +190,12 @@
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 entry
still opens on a tap; "hold:scan:490:..." works for scancodes. A key with only a hold entry does nothing
- on a tap. A listed app that is not installed leaves the button doing nothing. Leave empty to omit. -->
+ on a tap. A listed app that is not installed leaves the button doing nothing. Prefix an entry with "front:"
+ for what a tap does while the button's own app is already in front (HOME_KEY_SERVICE=1), and use
+ "guide:<package>" as a target to open that Live TV app's program guide: "172:com.vt.tv.tvcenter" with
+ "front:172:guide:com.vt.tv.tvcenter" makes the Hisense hengshan Live TV button open Live TV, then its guide,
+ where the firmware hands KEYCODE_GUIDE (172) to the stock launcher and no app ever receives it. Leave empty
+ to omit. -->
<string-array name="app_keys" translatable="false">
</string-array>
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 e69fa0716..81fd1b202 100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
@@ -27,6 +27,33 @@ public class RemoteKeysTest {
return RemoteKeys.parseHoldAppKeys(Arrays.asList(entries), RemoteKeysTest::keyCode);
}
+ @Test public void inFrontMatchesTheTargetsPackageOnly() {
+ assertTrue(RemoteKeys.inFront("com.vt.tv.tvcenter", "com.vt.tv.tvcenter"));
+ assertTrue(RemoteKeys.inFront("io.github.maxhayim.brightness/.QuickActivity", "io.github.maxhayim.brightness"));
+ assertFalse(RemoteKeys.inFront("com.vt.tv.tvcenter", "com.gathin.tvhome"));
+ assertFalse(RemoteKeys.inFront("com.vt.tv.tvcenter", ""));
+ assertFalse(RemoteKeys.inFront(null, "com.vt.tv.tvcenter"));
+ assertFalse(RemoteKeys.inFront("input:com.vt.source.external/.HdmiInputService/HW2", "com.vt.source.external"));
+ }
+
+ @Test public void frontEntriesAreReadApartFromTheRest() {
+ List<String> entries = Arrays.asList("172:com.vt.tv.tvcenter", "front:172:guide:com.vt.tv.tvcenter",
+ "front:scan:490:com.wbd.hbomax", "hold:172:a.b");
+ assertEquals("com.vt.tv.tvcenter", RemoteKeys.parseAppKeys(entries, RemoteKeysTest::keyCode).get(172));
+ Map<Integer, String> front = RemoteKeys.parseFrontAppKeys(entries, RemoteKeysTest::keyCode);
+ assertEquals(1, front.size());
+ assertEquals("guide:com.vt.tv.tvcenter", front.get(172));
+ assertEquals("com.wbd.hbomax", RemoteKeys.parseFrontAppScancodes(entries).get(490));
+ assertTrue(RemoteKeys.parseAppScancodes(entries).isEmpty());
+ }
+
+ @Test public void guideTargetNamesItsPackage() {
+ assertEquals("com.vt.tv.tvcenter", RemoteKeys.guidePackage("guide:com.vt.tv.tvcenter"));
+ assertNull(RemoteKeys.guidePackage("guide:"));
+ assertNull(RemoteKeys.guidePackage("com.vt.tv.tvcenter"));
+ assertNull(RemoteKeys.guidePackage(null));
+ }
+
@Test public void keyNamesWorkLikeNumbers() {
Map<Integer, String> keys = parse("KEYCODE_BUTTON_3:com.google.android.youtube.tv", "KEYCODE_NOT_A_KEY:a.b");
assertEquals(1, keys.size());
diff --git a/themes/README.md b/themes/README.md
index 174166236..038b0d463 100644
--- a/themes/README.md
+++ b/themes/README.md
@@ -119,7 +119,7 @@ A new key needs one there in the same pull request.
| `settings_fallback_components` | list, `config.xml` only | Vendor screens tried in order when nothing usable answers the standard Settings action (Xiaomi China, TCL/FFALCON, and Fire OS, where the real menu lives in its launcher), each as `package/activity`; the first one whose package is installed wins, and when none is, the standard intent is sent anyway and may open nothing. Fire OS answers that intent with a `CTSDummy` stub, which counts as missing so the fallback runs. A package named here also has to be listed in the launcher's `<queries>` (Android 11 package visibility), so a set whose settings app is neither of these needs a build, not a theme push. Not read from a theme |
| `restore_key_services` | bool | After a force stop of the launcher, Home puts the remote's accessibility helpers (`HomeKeyService`, `InputKeyService`) back on the enabled list; needs `RESTORE_KEY_SERVICES=1` at install. Off by default |
| `input_key_keycodes` | list of keycodes | More remote buttons that open the input switcher, for `InputKeyService` (`KEYCODE_TV` and `KEYCODE_TV_INPUT` always do) |
-| `app_keys` | list of strings | Remote buttons that open an app, as `"190:com.google.android.youtube.tv"` or `"KEYCODE_BUTTON_3:com.google.android.youtube.tv"`, or by scancode as `"scan:490:com.wbd.hbomax"` for remotes that share one keycode between app buttons (Sony BRAVIA), for `InputKeyService` or `HomeKeyService`. `"input:<input id>"` in place of the package switches to a TV input instead, e.g. `"scan:227:input:com.tcl.tvinput/.passthroughinput.TvPassThroughService/HW1413744640"` for an HDMI port (ids from `dumpsys tv_input`). `"inputs"` in place of the package opens the Inputs menu, for a remote whose source button sends a key Android does not map, e.g. `"scan:365:inputs"` (TCL 65C745). An action word in place of the package runs that action: `back`, `home`, `recents`, `mute`, `sleep` (standby, Android 9 and later), `screensaver`, `settings` or `wifi`, e.g. `"KEYCODE_PROG_YELLOW:mute"`. Prefix an entry with `hold:` to make it the long-press action, e.g. `"hold:190:com.netflix.ninja"`, while the unprefixed entry still opens on a tap |
+| `app_keys` | list of strings | Remote buttons that open an app, as `"190:com.google.android.youtube.tv"` or `"KEYCODE_BUTTON_3:com.google.android.youtube.tv"`, or by scancode as `"scan:490:com.wbd.hbomax"` for remotes that share one keycode between app buttons (Sony BRAVIA), for `InputKeyService` or `HomeKeyService`. `"input:<input id>"` in place of the package switches to a TV input instead, e.g. `"scan:227:input:com.tcl.tvinput/.passthroughinput.TvPassThroughService/HW1413744640"` for an HDMI port (ids from `dumpsys tv_input`). `"inputs"` in place of the package opens the Inputs menu, for a remote whose source button sends a key Android does not map, e.g. `"scan:365:inputs"` (TCL 65C745). An action word in place of the package runs that action: `back`, `home`, `recents`, `mute`, `sleep` (standby, Android 9 and later), `screensaver`, `settings` or `wifi`, e.g. `"KEYCODE_PROG_YELLOW:mute"`. Prefix an entry with `hold:` to make it the long-press action, e.g. `"hold:190:com.netflix.ninja"`, while the unprefixed entry still opens on a tap. Prefix it with `front:` for what a tap does while the button's own app is already in front (`HomeKeyService` only), and use `"guide:<package>"` as a target to open that Live TV app's program guide: `"172:com.vt.tv.tvcenter"` with `"front:172:guide:com.vt.tv.tvcenter"` opens Live TV, then its guide, on a Hisense hengshan set whose firmware sends the Live TV button (`KEYCODE_GUIDE`) only to the stock launcher |
| `key_filter` | bool | `false` makes the key helpers stop filtering keys while staying enabled. Use it on firmware where any key-filtering accessibility service breaks long-press repeat (TCL Google TV on Android 12): the remote buttons above stop working, and volume repeat comes back |
| `gamepad_passthrough` | bool | `true` makes the key helpers stop filtering keys while a game controller is connected, so games see its buttons (Steam Link with an Xbox controller, Moonlight with two controllers); the remote buttons above do nothing until it disconnects. Needs `key_filter` on. A voice remote that reports a joystick does not count. Off by default |
| `consume_volume_keys` | bool | `true` makes the key helpers swallow Volume Up/Down and repeat a held one themselves (after 500 ms, every 60 ms). Use it on firmware that applies a volume key both before the helper and when the helper hands it back, so one tap moves two steps (TCL G03 / BeyondTV4, Android 11, #1100). Needs `key_filter` on. Off by default |