Skip to content

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

launcher: InputKeyService brings back remote buttons only the stock launcher answered

MagiciansMac5ad4d80381dc
diff --git a/README.md b/README.md
index ab7bed540..593c3b270 100644
--- a/README.md
+++ b/README.md
@@ -289,7 +289,7 @@ the install script finds `~/Android/Sdk` and the `java` on your PATH.
- **Module cards** let any TV app declare its own tile (label, caption, glow). The launcher asks you once per app, Keep hidden by default, and remembers the answer for that app's signing key and card name. Hold OK on Settings and choose Module cards to change your mind. How to make one: [`docs/modules.md`](docs/modules.md); the contract: [`docs/specs/card.md`](docs/specs/card.md).
- **Clock screensaver**: a drifting clock over the glow, so nothing burns in. Pick "Clock" in the TV's screensaver settings, or over ADB:
`tools/tv.py 'settings put secure screensaver_components com.gathin.tvhome/.ClockDream; settings put secure screensaver_enabled 1'`
-- **Remote keys**: an optional accessibility service keeps the remote's Input button working on Google TV (`INPUT_KEY_SERVICE=1` at install), another fixes sticks whose Home key sends ALL_APPS and opens Settings from the remote's gear button, even one Android does not map (`HOME_KEY_SERVICE=1`, scancodes or keycodes in `config.xml`). With `settings_key_panel` the gear opens a [small quick settings panel](docs/quick-settings-panel.jpg) instead, over Home or any app: TV settings, Display, Network, Apps and Launcher settings.
+- **Remote keys**: an optional accessibility service keeps the remote's Input button working on Google TV (`INPUT_KEY_SERVICE=1` at install) and can bring back other buttons only the stock launcher answered, such as the YouTube button (`app_keys` and `input_key_keycodes` in `config.xml`), another fixes sticks whose Home key sends ALL_APPS and opens Settings from the remote's gear button, even one Android does not map (`HOME_KEY_SERVICE=1`, scancodes or keycodes in `config.xml`). With `settings_key_panel` the gear opens a [small quick settings panel](docs/quick-settings-panel.jpg) instead, over Home or any app: TV settings, Display, Network, Apps and Launcher settings.
- **Navigation** is deliberate: Up and Down move one row, the top buttons never fall through to tiles and Down from them returns to the dock tile you came from, the last focused app is remembered, and installing or removing an app refreshes the tiles while Home is open.
- **Artwork**: app banners load at xhdpi or better, so low-density TV UIs do not downsample them before the focus zoom.
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index 6fd0137db..10035c042 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -122,7 +122,7 @@ tools/tv.py 'dumpsys accessibility'
| Helper | Component in the enabled-services list | Key it filters |
| --- | --- | --- |
| Home button | `com.gathin.tvhome/.HomeKeyService` | `KEYCODE_ALL_APPS` (not an ordinary `KEYCODE_HOME`) |
-| Input button | `com.gathin.tvhome/.InputKeyService` | `KEYCODE_TV` |
+| Input button | `com.gathin.tvhome/.InputKeyService` | `KEYCODE_TV`, plus the keycodes in `config.xml` `input_key_keycodes` and `app_keys` |
- **Enabled** means the component is in that user's colon-separated list (the class may be written in
full, `com.gathin.tvhome/com.gathin.tvhome.HomeKeyService`). Match the whole component.
@@ -130,7 +130,13 @@ tools/tv.py 'dumpsys accessibility'
builds). Give it a few seconds after enabling and read again. Labels in the dump can be translated or
shared with other services; a line containing "Home" is not proof.
- **Working** means the physical button does the right thing from inside another app. An injected key
- over ADB proves navigation, not that the remote sends the code this helper filters.
+ over ADB proves navigation, not that the remote sends the code this helper filters. It does not even
+ reach the helper: `input keyevent` skips accessibility key filtering, so an injected `KEYCODE_TV` does
+ nothing even when the real Input button works.
+- **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.
Enabled but never bound points at a firmware restriction on auto-starting sideloaded services (some TCL
builds log `OP_AUTO_START` denials). Look at `logcat` for the binding failure before changing settings,
diff --git a/launcher/app/src/main/AndroidManifest.xml b/launcher/app/src/main/AndroidManifest.xml
index fbf0f9eee..a9a86e4e9 100644
--- a/launcher/app/src/main/AndroidManifest.xml
+++ b/launcher/app/src/main/AndroidManifest.xml
@@ -112,7 +112,8 @@
</intent-filter>
</service>
- <!-- The remote's Input button (KEYCODE_TV) opens the input switcher; see InputKeyService. -->
+ <!-- The remote's Input button (KEYCODE_TV) opens the input switcher, and config.xml input_key_keycodes and
+ app_keys bring back other remote buttons; see InputKeyService. -->
<service
android:name=".InputKeyService"
android:exported="false"
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 f3a684c68..d41ce69bd 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@ -5,23 +5,48 @@ import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
+import java.util.Collections;
+import java.util.Map;
/**
* Maps the remote's Input button to the system input switcher, from any app. Remotes such as the Chromecast voice
* remote send KEYCODE_TV for it; on Google TV the stock launcher handles that key, so with the stock launcher
- * disabled the button does nothing. Only KEYCODE_TV is consumed; every other key passes through untouched.
+ * disabled the button does nothing. Buttons listed in config.xml are handled the same way: input_key_keycodes
+ * open the input switcher, app_keys start an app (a YouTube button that only the stock launcher answered).
+ * Every other key passes through untouched.
* Opt-in: INPUT_KEY_SERVICE=1 build-and-install.sh (or -InputKeyService on Windows) enables it.
*/
public class InputKeyService extends AccessibilityService {
private static final String ACTION_VIEW_INPUTS = "com.android.tv.action.VIEW_INPUTS";
+ private int[] inputKeycodes = new int[0];
+ private Map<Integer, String> appKeys = Collections.emptyMap();
+
+ @Override
+ protected void onServiceConnected() {
+ super.onServiceConnected();
+ inputKeycodes = getResources().getIntArray(R.array.input_key_keycodes);
+ appKeys = RemoteKeys.parseAppKeys(getResources().getStringArray(R.array.app_keys));
+ }
+
+ private boolean isInputKey(int keyCode) {
+ if (keyCode == KeyEvent.KEYCODE_TV) return true;
+ for (int code : inputKeycodes) if (keyCode == code) return true;
+ return false;
+ }
+
@Override
protected boolean onKeyEvent(KeyEvent event) {
- if (event.getKeyCode() != KeyEvent.KEYCODE_TV) return false;
- if (event.getAction() == KeyEvent.ACTION_UP) {
+ String app = appKeys.get(event.getKeyCode());
+ if (app == null && !isInputKey(event.getKeyCode())) return false;
+ if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
+ Intent open = app == null ? new Intent(ACTION_VIEW_INPUTS)
+ : getPackageManager().getLeanbackLaunchIntentForPackage(app);
+ if (open == null) open = getPackageManager().getLaunchIntentForPackage(app);
+ if (open == null) return true;
try {
- startActivity(new Intent(ACTION_VIEW_INPUTS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
+ startActivity(open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} catch (ActivityNotFoundException ignored) {
}
}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
new file mode 100644
index 000000000..9666616d9
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
@@ -0,0 +1,26 @@
+package com.gathin.tvhome;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Remote buttons that only the stock launcher answered, for InputKeyService: config.xml app_keys maps a
+ * keycode to the app it opens ("190:com.google.android.youtube.tv").
+ */
+final class RemoteKeys {
+ private RemoteKeys() { }
+
+ /** keycode -> package. Entries without a colon, with a keycode that is not a number or with no package are skipped. */
+ static Map<Integer, String> parseAppKeys(String[] entries) {
+ Map<Integer, String> keys = new HashMap<>();
+ for (String entry : entries) {
+ String[] parts = entry.split(":", 2);
+ if (parts.length != 2 || parts[1].trim().isEmpty()) continue;
+ try {
+ keys.put(Integer.parseInt(parts[0].trim()), parts[1].trim());
+ } catch (NumberFormatException ignored) {
+ }
+ }
+ return keys;
+ }
+}
diff --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xml
index 671009292..7a20b1825 100644
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@ -61,6 +61,20 @@
Both keys can also be set from a theme's config.json ("settings_key_keycodes": [83]). -->
<bool name="settings_key_panel">false</bool>
+ <!-- More remote buttons that open the input switcher, by Android keycode (INPUT_KEY_SERVICE=1
+ build-and-install.sh; KEYCODE_TV always does). For buttons only a disabled app answered: the Xiaomi
+ MiTV-AXFR2 remote's PatchWall button is 192 (KEYCODE_BUTTON_5). `dumpsys window policy` lists which
+ app each such "global key" was routed to. Check with the real remote: keys injected over ADB never
+ reach InputKeyService. Leave empty to omit. -->
+ <integer-array name="input_key_keycodes">
+ </integer-array>
+ <!-- Remote buttons that open an app, as "keycode:package" (INPUT_KEY_SERVICE=1 too). On Google TV the
+ YouTube button sends 190 (KEYCODE_BUTTON_3) to the stock launcher, so it does nothing once launcherx
+ is disabled (Chromecast with Google TV, Xiaomi MiTV-AXFR2): 190:com.google.android.youtube.tv brings
+ it back. Leave empty to omit. -->
+ <string-array name="app_keys" translatable="false">
+ </string-array>
+
<!-- Show the Solar Hijri (Shamsi) date used in Iran and Afghanistan, e.g. "27 Mehr 1405", above the
Gregorian date. -->
<bool name="shamsi_date">false</bool>
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
new file mode 100644
index 000000000..b46df1aa2
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
@@ -0,0 +1,27 @@
+package com.gathin.tvhome;
+
+import java.util.Map;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class RemoteKeysTest {
+ @Test public void keycodeAndPackage() {
+ Map<Integer, String> keys = RemoteKeys.parseAppKeys(new String[] {
+ "190:com.google.android.youtube.tv", " 193 : com.netflix.ninja "});
+ assertEquals(2, keys.size());
+ assertEquals("com.google.android.youtube.tv", keys.get(190));
+ assertEquals("com.netflix.ninja", keys.get(193));
+ }
+
+ @Test public void badEntriesAreSkipped() {
+ Map<Integer, String> keys = RemoteKeys.parseAppKeys(new String[] {
+ "com.google.android.youtube.tv", "youtube:com.google.android.youtube.tv", "190:", ""});
+ assertTrue(keys.isEmpty());
+ }
+
+ @Test public void theLastEntryForAKeycodeWins() {
+ Map<Integer, String> keys = RemoteKeys.parseAppKeys(new String[] {"190:a.b", "190:c.d"});
+ assertEquals("c.d", keys.get(190));
+ }
+}
diff --git a/launcher/build-and-install.ps1 b/launcher/build-and-install.ps1
index 989b76acd..46d578b37 100644
--- a/launcher/build-and-install.ps1
+++ b/launcher/build-and-install.ps1
@@ -4,7 +4,8 @@
#
# Usage: launcher\build-and-install.ps1 -TvHost 192.168.1.50 [-StockLauncher <package>|none]
# The stock launcher is detected from the TV when -StockLauncher is not given.
-# [-InputKeyService] remote's Input button opens the input switcher (Google TV); re-pass on every install
+# [-InputKeyService] remote's Input button opens the input switcher (Google TV), plus config.xml
+# input_key_keycodes and app_keys; re-pass on every install
# [-HomeKeyService] remotes whose Home button sends KEYCODE_ALL_APPS (Xiaomi sticks)
param(
[string]$TvHost = $env:TV_HOST,
diff --git a/launcher/build-and-install.sh b/launcher/build-and-install.sh
index e00685245..4a8cf7a9e 100755
--- a/launcher/build-and-install.sh
+++ b/launcher/build-and-install.sh
@@ -148,7 +148,8 @@ esac
# Optional accessibility services. Off unless asked for, and Android switches them off again on
# every reinstall, so pass the flag each time you run this script.
# INPUT_KEY_SERVICE=1 the remote's Input button (KEYCODE_TV) opens the input switcher from any
-# app; Google TV needs this once the stock launcher is gone (InputKeyService)
+# app; Google TV needs this once the stock launcher is gone (InputKeyService).
+# It also handles config.xml input_key_keycodes and app_keys (a YouTube button)
# HOME_KEY_SERVICE=1 remotes whose Home button sends KEYCODE_ALL_APPS instead of HOME, e.g. Xiaomi
# sticks: the key is routed to the launcher (HomeKeyService)
enable_service() { # appends to enabled_accessibility_services without dropping other services