Skip to content

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

Merge PR #719: launcher: long-press remote buttons to open a second app

Gathin232745ce66780b
diff --cc launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
index 8a9a32c4d,d649a8a2c..80baab2a5
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@@ -37,15 -24,12 +37,17 @@@ public class HomeKeyService extends Acc
private int[] settingsScancodes = new int[0];
private int[] settingsKeycodes = new int[0];
private boolean settingsPanel;
- /** app_keys, the same map InputKeyService reads, for setups that run only this service. */
+ /** app_keys, the same maps InputKeyService reads, for setups that run only this service. */
private Map<Integer, String> appKeys = Collections.emptyMap();
private Map<Integer, String> appScancodes = Collections.emptyMap();
+ private Map<Integer, String> holdAppKeys = Collections.emptyMap();
+ private Map<Integer, String> holdAppScancodes = Collections.emptyMap();
+ /** Waits for the user to unlock when Android binds this service at boot; null once the config is read. */
+ private BroadcastReceiver unlockReceiver;
+ /** The stock launcher's HOME activities (stock_home_redirect), disabled ones included. */
+ private final Set<String> stockHome = new HashSet<>();
+
@Override
protected void onServiceConnected() {
super.onServiceConnected();
@@@ -92,14 -40,8 +94,16 @@@
settingsPanel = config.settingsKeyPanel;
appKeys = RemoteKeys.parseAppKeys(config.appKeys);
appScancodes = RemoteKeys.parseAppScancodes(config.appKeys);
+ RemoteKeys.applyKeyFilter(this, config.keyFilter);
+ stockHome.clear();
+ if (!config.stockHomeRedirect.isEmpty()) {
+ Intent home = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setPackage(config.stockHomeRedirect);
+ for (ResolveInfo ri : getPackageManager().queryIntentActivities(home, PackageManager.MATCH_DISABLED_COMPONENTS)) {
+ stockHome.add(ri.activityInfo.name);
+ }
+ }
+ holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
+ holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
}
private boolean isSettingsKey(KeyEvent event) {
diff --cc launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
index ec7939c98,17dc56420..36d9d0fd9
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@@ -32,7 -34,8 +34,9 @@@ public class InputKeyService extends Ac
inputKeycodes = config.inputKeycodes;
appKeys = RemoteKeys.parseAppKeys(config.appKeys);
appScancodes = RemoteKeys.parseAppScancodes(config.appKeys);
+ RemoteKeys.applyKeyFilter(this, config.keyFilter);
+ holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
+ holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
}
private boolean isInputKey(int keyCode) {
diff --cc launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
index b30fb8526,063026e92..dd1a8a146
--- a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
@@@ -5,21 -3,28 +5,30 @@@ import android.accessibilityservice.Acc
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
+import android.media.tv.TvContract;
import android.view.KeyEvent;
+ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
+ import java.util.List;
import java.util.Map;
import java.util.function.ToIntFunction;
/**
* Remote buttons that only the stock launcher answered, for InputKeyService and HomeKeyService: app_keys
* (config.xml or a theme) maps a keycode to the app it opens, as "190:com.google.android.youtube.tv" or
- * "KEYCODE_BUTTON_3:com.google.android.youtube.tv", or a scancode as "scan:490:com.wbd.hbomax". One map for both services; whichever filters the key first
- * consumes it. A target of "input:<input id>" switches to that TV input (an HDMI port) instead of opening an app.
+ * "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.
++ * A target of "input:<input id>" switches to that TV input (an HDMI port) instead of opening an app.
*/
final class RemoteKeys {
+
+ /** How long a button is held before its "hold:" entry wins (Android's long-press timeout). */
+ static final long HOLD_MS = 500;
+
private RemoteKeys() { }
/** keycode -> package, with key names resolved by Android. */
@@@ -56,21 -61,19 +65,34 @@@
return keys;
}
+ /** keycode -> package from the "hold:" entries; the rest of each entry reads as it does in parseAppKeys. */
+ static Map<Integer, String> parseHoldAppKeys(String[] entries) {
+ return parseHoldAppKeys(Arrays.asList(entries), KeyEvent::keyCodeFromString);
+ }
+
+ static Map<Integer, String> parseHoldAppKeys(Iterable<String> entries) {
+ return parseHoldAppKeys(entries, KeyEvent::keyCodeFromString);
+ }
+
+ static Map<Integer, String> parseHoldAppKeys(Iterable<String> entries, ToIntFunction<String> names) {
+ return parseAppKeys(holdEntries(entries), names);
+ }
+
+ /**
+ * Applies the key_filter choice (config.xml or a theme): when false the service drops
+ * FLAG_REQUEST_FILTER_KEY_EVENTS at runtime, so it stays enabled for nothing but stops filtering.
+ * Some firmware (TCL Google TV on Android 12) drops key auto-repeat once ANY service filters keys:
+ * long-pressing volume then steps once per press. Filtering can only be skipped while the service
+ * runs, since the flag is also declared in the manifest metadata.
+ */
+ static void applyKeyFilter(AccessibilityService service, boolean keyFilter) {
+ if (keyFilter) return;
+ AccessibilityServiceInfo info = service.getServiceInfo();
+ if (info == null) return;
+ info.flags &= ~AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
+ service.setServiceInfo(info);
+ }
+
/**
* scancode -> package, from the "scan:490:com.wbd.hbomax" entries of the same list. For remotes that send
* several app buttons as one keycode and tell them apart by scancode (Sony BRAVIA: YouTube, Prime Video and
@@@ -91,29 -94,42 +113,63 @@@
return keys;
}
+ /** 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));
+ }
+
+ /** 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<>();
+ for (String entry : entries) {
+ if (entry == null) continue;
+ String text = entry.trim();
+ if (text.startsWith("hold:")) holds.add(text.substring(5).trim());
+ }
+ return holds;
+ }
+
+ /**
+ * Consumes a key mapped by either list. On release it opens the hold app after HOLD_MS, otherwise the normal
+ * one; false means neither list has this key, so the caller can pass it on.
+ */
+ static boolean handle(Context ctx, KeyEvent event, String app, String holdApp) {
+ if (app == null && holdApp == null) return false;
+ if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
+ launch(ctx, target(app, holdApp, event.getEventTime() - event.getDownTime()));
+ }
+ return true;
+ }
+
+ /** The app a mapped key opens: its hold entry after HOLD_MS, otherwise its normal one. */
+ static String target(String app, String holdApp, long heldMs) {
+ return heldMs >= HOLD_MS && holdApp != null ? holdApp : app;
+ }
+
- /** Opens an app from a remote button: its TV entry if it has one, else its phone entry. */
+ /** The TV input id of an "input:<id>" target, e.g. "input:com.tcl.tvinput/.passthroughinput.TvPassThroughService/HW1413743104"; null for a package. */
+ static String inputId(String target) {
+ if (target == null || !target.startsWith("input:")) return null;
+ String id = target.substring("input:".length()).trim();
+ return id.isEmpty() ? null : id;
+ }
+
+ /** 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. */
static void launch(Context ctx, String pkg) {
+ if (pkg == null) return;
+ String inputId = inputId(pkg);
+ if (inputId != null) {
+ // The same passthrough URI the Inputs menu opens, which the Live TV app handles.
+ Intent view = new Intent(Intent.ACTION_VIEW)
+ .setDataAndType(TvContract.buildChannelUriForPassthroughInput(inputId),
+ "vnd.android.cursor.item/channel")
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ try {
+ ctx.startActivity(view);
+ } catch (ActivityNotFoundException ignored) {
+ }
+ return;
+ }
Intent open = ctx.getPackageManager().getLeanbackLaunchIntentForPackage(pkg);
if (open == null) open = ctx.getPackageManager().getLaunchIntentForPackage(pkg);
if (open == null) return;
diff --cc launcher/app/src/main/res/values/config.xml
index 90abc76f8,18bfa581a..dab50feed
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@@ -141,10 -92,11 +141,13 @@@
and TL;DR 200. Some remotes send several app buttons as one keycode and tell them apart only by scancode
(Sony BRAVIA: YouTube, Prime Video and Disney+ are all keycode 196, scancodes 485, 490 and 491); list those
as "scan:scancode:package", e.g. scan:490:com.wbd.hbomax, found with `tools/tv.py 'getevent -lt'` (the
- EV_KEY code, in hex). A scancode entry wins over a keycode. 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 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. -->
+ 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/HW1413744640
+ for HDMI 3 on a TCL Smart TV Pro; the ids are in `tools/tv.py 'dumpsys tv_input'` (hdmi_port=N under
- TvInputHardwareInfo). A listed app that is not installed leaves the button doing nothing. Leave empty to omit. -->
++ TvInputHardwareInfo). 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 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. -->
<string-array name="app_keys" translatable="false">
</string-array>
diff --cc launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
index db0fb5395,d816ef187..2186a2903
--- a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
@@@ -47,13 -58,30 +58,39 @@@ public class RemoteKeysTest
// and the keycode parser leaves scancode entries alone
assertEquals(1, parse(entries.toArray(new String[0])).size());
}
+ @Test public void inputTargets() {
+ String hdmi = "com.tcl.tvinput/.passthroughinput.TvPassThroughService/HW1413743104";
+ Map<Integer, String> keys = parse("4062:input:" + hdmi);
+ assertEquals(hdmi, RemoteKeys.inputId(keys.get(4062)));
+ assertEquals(hdmi, RemoteKeys.inputId(RemoteKeys.parseAppScancodes(
+ java.util.Arrays.asList("scan:586:input:" + hdmi)).get(586)));
+ assertEquals(null, RemoteKeys.inputId("com.netflix.ninja"));
+ assertEquals(null, RemoteKeys.inputId("input: "));
+ }
+
+ @Test public void holdEntriesGetTheirOwnMap() {
+ Map<Integer, String> holds = parseHold(
+ "hold:KEYCODE_BUTTON_8:org.xbmc.kodi", " hold:190:a.b ", "hold:KEYCODE_NOT_A_KEY:x.y",
+ "hold:195:", "190:c.d", "hold:scan:490:d.e", "hold:");
+ assertEquals(2, holds.size());
+ assertEquals("org.xbmc.kodi", holds.get(195));
+ assertEquals("a.b", holds.get(190));
+ // the normal parser leaves hold entries alone
+ assertTrue(parse("hold:190:a.b").isEmpty());
+ }
+
+ @Test public void holdScancodeEntries() {
+ Map<Integer, String> scans = RemoteKeys.parseHoldAppScancodes(Arrays.asList(
+ "hold:scan:490:com.wbd.hbomax", "hold:scan:491:", "hold:190:a.b", "scan:492:c.d"));
+ assertEquals(1, scans.size());
+ assertEquals("com.wbd.hbomax", scans.get(490));
+ }
+
+ @Test public void holdTimePicksTheHoldApp() {
+ assertEquals("hold.app", RemoteKeys.target("app", "hold.app", RemoteKeys.HOLD_MS));
+ assertEquals("hold.app", RemoteKeys.target("app", "hold.app", 5000));
+ assertEquals("app", RemoteKeys.target("app", "hold.app", RemoteKeys.HOLD_MS - 1));
+ assertEquals("app", RemoteKeys.target("app", null, 5000));
+ assertNull(RemoteKeys.target(null, "hold.app", 0));
+ }
}
diff --cc themes/README.md
index ca5980116,a16a10435..16cbab12f
--- a/themes/README.md
+++ b/themes/README.md
@@@ -81,11 -70,7 +81,12 @@@ is ignored, and a file that doesn't par
| `settings_key_keycodes` | list of keycodes | Remote buttons that open Settings, for `HomeKeyService` (TCL Google TV gear: `[83]`) |
| `settings_key_panel` | bool | Those buttons open a small quick settings panel (TV settings, Display, Network, Apps, Launcher settings) instead of the TV's Settings |
| `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` |
+| `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 |
+| `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`) |
+| `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"`, for `InputKeyService` or `HomeKeyService` |
+| `stock_home_redirect` | string | Stock launcher package; if the firmware turns it back on (XGIMI), its home screen is sent straight back here (`HOME_KEY_SERVICE=1`) |
+ | `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`. Prefix an entry with `hold:` to open a second app on a long press, e.g. `"hold:190:com.netflix.ninja"` |
| `race_calendar` | list of races | F1 race-weekend mode: from 12 hours before a weekend's first session until 4 hours after the race starts, a panel under the clock shows the next session with a countdown (or the live one) and the weekend's session times in the TV's time zone. Write it with `tools/race_calendar.py <theme folder>`, which fetches the season from the free Jolpica API; the launcher itself never goes online |
| `home_idle_clock_minutes` | 0 to 1440 | Minutes Home may sit untouched before it shows the clock screensaver (a thin clock and date drifting over a slow glow, as the system "Clock" screensaver does). Any key returns to Home. It starts only from Home, never over Live TV or an app; 0 is off (default) |
| `cinematic_home` | bool | Cinematic top shelf: the focused dock app's artwork, softened, fills the top of the screen in its colour, with a larger name, "Last opened" (apps opened from Home), dock position dots, a focus glow in the app's colour and a translucent dock band; off by default |