Skip to content

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

launcher: long-press remote buttons to open a second app

nikhiltalreja770ee98b4693
diff --git a/README.md b/README.md
index afe18950c..67a0e8157 100644
--- a/README.md
+++ b/README.md
@@ -459,6 +459,8 @@ tools/tv.py 'settings put secure screensaver_components com.gathin.tvhome/.Clock
(`INPUT_KEY_SERVICE=1` at install).
- 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:...`).
- `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 --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index 523e3a37b..38c13232f 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -306,7 +306,8 @@ tools/tv.py 'dumpsys accessibility'
- **Which keycode** a dead button sends: `dumpsys window policy` lists the "global keys" Android routes
to one app (on Google TV several go to launcherx, which is disabled), and Xiaomi firmware logs every
press as `handleAction<keycode>` in `logcat`. Put it in `input_key_keycodes` (opens Inputs) or
- `app_keys` (`keycode:package`), reinstall with `INPUT_KEY_SERVICE=1` and press the real button.
+ `app_keys` (`keycode:package`, or `hold:keycode:package` for a long press), reinstall with
+ `INPUT_KEY_SERVICE=1` and press the real button.
A press that `logcat` reports as `KEY_UNKNOWN` or `KEY_PROGRAM` cannot be matched by keycode at all,
because every unmapped button shares those codes: use the Linux scancode from
`tools/tv.py 'getevent -lt'` in `settings_key_scancodes` and install with `HOME_KEY_SERVICE=1`
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 538ae432a..d649a8a2c 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -24,9 +24,11 @@ public class HomeKeyService extends AccessibilityService {
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();
@Override
protected void onServiceConnected() {
@@ -38,6 +40,8 @@ public class HomeKeyService extends AccessibilityService {
settingsPanel = config.settingsKeyPanel;
appKeys = RemoteKeys.parseAppKeys(config.appKeys);
appScancodes = RemoteKeys.parseAppScancodes(config.appKeys);
+ holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
+ holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
}
private boolean isSettingsKey(KeyEvent event) {
@@ -50,11 +54,8 @@ public class HomeKeyService extends AccessibilityService {
@Override
protected boolean onKeyEvent(KeyEvent event) {
// A scancode entry is more specific than any keycode, so it goes first.
- String scanApp = appScancodes.get(event.getScanCode());
- if (scanApp != null) {
- if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) RemoteKeys.launch(this, scanApp);
- return true;
- }
+ if (RemoteKeys.handle(this, event, appScancodes.get(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)) {
if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
@@ -64,11 +65,8 @@ public class HomeKeyService extends AccessibilityService {
}
return true;
}
- String app = appKeys.get(event.getKeyCode());
- if (app != null) {
- if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) RemoteKeys.launch(this, app);
- return true;
- }
+ if (RemoteKeys.handle(this, event, appKeys.get(event.getKeyCode()),
+ holdAppKeys.get(event.getKeyCode()))) return true;
if (event.getKeyCode() != KeyEvent.KEYCODE_ALL_APPS) return false;
if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
Intent home = new Intent(Intent.ACTION_MAIN)
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 cdfc24d01..17dc56420 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@ -23,6 +23,8 @@ public class InputKeyService extends AccessibilityService {
private int[] inputKeycodes = new int[0];
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();
@Override
protected void onServiceConnected() {
@@ -32,6 +34,8 @@ public class InputKeyService extends AccessibilityService {
inputKeycodes = config.inputKeycodes;
appKeys = RemoteKeys.parseAppKeys(config.appKeys);
appScancodes = RemoteKeys.parseAppScancodes(config.appKeys);
+ holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
+ holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
}
private boolean isInputKey(int keyCode) {
@@ -43,21 +47,15 @@ public class InputKeyService extends AccessibilityService {
@Override
protected boolean onKeyEvent(KeyEvent event) {
// A scancode entry is more specific than any keycode, so it goes first.
- String scanApp = appScancodes.get(event.getScanCode());
- if (scanApp != null) {
- if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) RemoteKeys.launch(this, scanApp);
- return true;
- }
- String app = appKeys.get(event.getKeyCode());
- if (app == null && !isInputKey(event.getKeyCode())) return false;
+ if (RemoteKeys.handle(this, event, appScancodes.get(event.getScanCode()),
+ holdAppScancodes.get(event.getScanCode()))) return true;
+ if (RemoteKeys.handle(this, event, appKeys.get(event.getKeyCode()),
+ holdAppKeys.get(event.getKeyCode()))) return true;
+ if (!isInputKey(event.getKeyCode())) return false;
if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
- if (app != null) {
- RemoteKeys.launch(this, app);
- } else {
- try {
- startActivity(new Intent(ACTION_VIEW_INPUTS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
- } catch (ActivityNotFoundException ignored) {
- }
+ try {
+ startActivity(new Intent(ACTION_VIEW_INPUTS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
+ } catch (ActivityNotFoundException ignored) {
}
}
return true;
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 92cf977e8..063026e92 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
@@ -5,18 +5,26 @@ import android.content.Context;
import android.content.Intent;
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.
+ * "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.
*/
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. */
@@ -53,6 +61,19 @@ final class RemoteKeys {
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);
+ }
+
/**
* 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
@@ -73,8 +94,42 @@ final class RemoteKeys {
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. */
static void launch(Context ctx, String pkg) {
+ if (pkg == null) return;
Intent open = ctx.getPackageManager().getLeanbackLaunchIntentForPackage(pkg);
if (open == null) open = ctx.getPackageManager().getLaunchIntentForPackage(pkg);
if (open == null) return;
diff --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xml
index 4ca1b963c..18bfa581a 100644
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@ -92,7 +92,10 @@
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. A listed app that is not installed leaves 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. -->
<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 1d637512b..d816ef187 100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
@@ -1,15 +1,26 @@
package com.gathin.tvhome;
+import java.util.Arrays;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class RemoteKeysTest {
+ // Key names resolved by a stand-in: android.view.KeyEvent isn't available in JVM tests.
+ private static int keyCode(String name) {
+ if (name.equals("KEYCODE_BUTTON_3")) return 190;
+ if (name.equals("KEYCODE_BUTTON_8")) return 195;
+ return 0;
+ }
+
private static Map<Integer, String> parse(String... entries) {
- // Key names resolved by a stand-in: android.view.KeyEvent isn't available in JVM tests.
- return RemoteKeys.parseAppKeys(java.util.Arrays.asList(entries),
- name -> name.equals("KEYCODE_BUTTON_3") ? 190 : 0);
+ return RemoteKeys.parseAppKeys(Arrays.asList(entries), RemoteKeysTest::keyCode);
+ }
+
+ private static Map<Integer, String> parseHold(String... entries) {
+ return RemoteKeys.parseHoldAppKeys(Arrays.asList(entries), RemoteKeysTest::keyCode);
}
@Test public void keyNamesWorkLikeNumbers() {
@@ -38,7 +49,7 @@ public class RemoteKeysTest {
}
@Test public void scancodeEntries() {
- java.util.List<String> entries = java.util.Arrays.asList(
+ java.util.List<String> entries = Arrays.asList(
"scan:490:com.wbd.hbomax", " scan : 491 : a.b ", "scan:x:a.b", "scan:12:", "190:c.d", "scan:491:e.f");
Map<Integer, String> scans = RemoteKeys.parseAppScancodes(entries);
assertEquals(2, scans.size());
@@ -47,4 +58,30 @@ public class RemoteKeysTest {
// and the keycode parser leaves scancode entries alone
assertEquals(1, parse(entries.toArray(new String[0])).size());
}
+
+ @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 --git a/themes/README.md b/themes/README.md
index e9ae32cfe..a16a10435 100644
--- a/themes/README.md
+++ b/themes/README.md
@@ -70,7 +70,7 @@ is ignored, and a file that doesn't parse is ignored as a whole, so a theme can
| `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` |
+| `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 |