Skip to content

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

Merge PR #323: launcher: bring back remote buttons only the stock launcher answered (YouTube 190, Xiaomi PatchWall)

Gathin234eb6ff0c7034
diff --cc docs/TROUBLESHOOTING.md
index dcee4334d,10035c042..9c50fbe8d
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@@ -130,7 -122,7 +130,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`, `KEYCODE_TV_INPUT` |
-| Input button | `com.gathin.tvhome/.InputKeyService` | `KEYCODE_TV`, plus the keycodes in `config.xml` `input_key_keycodes` and `app_keys` |
++| Input button | `com.gathin.tvhome/.InputKeyService` | `KEYCODE_TV`, `KEYCODE_TV_INPUT`, plus the keycodes in `input_key_keycodes` and `app_keys` (`config.xml` or a theme) |
- **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.
diff --cc launcher/app/src/main/AndroidManifest.xml
index 17d96ef3c,a9a86e4e9..f56e008ea
--- a/launcher/app/src/main/AndroidManifest.xml
+++ b/launcher/app/src/main/AndroidManifest.xml
@@@ -112,7 -112,8 +112,8 @@@
</intent-filter>
</service>
- <!-- The remote's Input button (KEYCODE_TV or KEYCODE_TV_INPUT) 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. -->
++ <!-- The remote's Input button (KEYCODE_TV or KEYCODE_TV_INPUT) opens the input switcher, and input_key_keycodes and
++ app_keys (config.xml or a theme) bring back other remote buttons; see InputKeyService. -->
<service
android:name=".InputKeyService"
android:exported="false"
diff --cc launcher/app/src/main/java/com/gathin/tvhome/Config.java
index 8a5547988,a567f620d..95540b6e6
--- a/launcher/app/src/main/java/com/gathin/tvhome/Config.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Config.java
@@@ -72,6 -70,6 +72,10 @@@ final class Config
final int skyIntensity;
/** Remote keycodes that open Settings through HomeKeyService (config.xml, settings_key_keycodes). */
final int[] settingsKeycodes;
++ /** Extra keycodes that open the input switcher in InputKeyService (config.xml input_key_keycodes). */
++ final int[] inputKeycodes;
++ /** Remote buttons that open an app, "keycode:package" or "KEYCODE_NAME:package" (config.xml app_keys). */
++ final List<String> appKeys;
/** The Settings button opens the quick settings panel instead of the TV's Settings (settings_key_panel). */
final boolean settingsKeyPanel;
/** Full-screen picture behind the launcher, or null for the plain colour. */
@@@ -84,10 -82,10 +88,12 @@@
private Config(List<String> dockApps, boolean fillDock, List<String> hiddenApps, String liveTvPackage,
String liveTvAction, String sourcePanelService, String sourcePanelAction, String inputAction,
int columns, int rowsBelowDock, boolean animations, boolean advancedEffects,
- boolean shamsiDate, boolean moduleCards, boolean reducedMotion, boolean dockTitle, boolean dockRow, boolean reorderButton, String vpnApp,
+ boolean shamsiDate, String greetingName, boolean moduleCards, boolean reducedMotion, boolean dockTitle, boolean dockRow, boolean reorderButton, String vpnApp,
boolean showHomeSettingsShortcut, boolean showStats, boolean appLibrary, int backgroundColor, int skyIntensity,
int[] settingsKeycodes, boolean settingsKeyPanel, File wallpaper, boolean overlayApplied,
-- Festival festival) {
++ Festival festival, int[] inputKeycodes, List<String> appKeys) {
++ this.inputKeycodes = inputKeycodes;
++ this.appKeys = Collections.unmodifiableList(appKeys);
this.dockApps = Collections.unmodifiableList(dockApps);
this.fillDock = fillDock;
this.hiddenApps = Collections.unmodifiableList(hiddenApps);
@@@ -154,6 -150,6 +160,8 @@@
int skyIntensity = res.getInteger(R.integer.sky_intensity);
int[] settingsKeycodes = res.getIntArray(R.array.settings_key_keycodes);
boolean settingsKeyPanel = res.getBoolean(R.bool.settings_key_panel);
++ int[] inputKeycodes = res.getIntArray(R.array.input_key_keycodes);
++ List<String> appKeys = new ArrayList<>(Arrays.asList(res.getStringArray(R.array.app_keys)));
boolean applied = false;
File dir = dir(ctx);
@@@ -188,6 -183,6 +196,8 @@@
skyIntensity = integer(json, "sky_intensity", skyIntensity, 0, 100);
settingsKeycodes = intArray(json, "settings_key_keycodes", settingsKeycodes);
settingsKeyPanel = bool(json, "settings_key_panel", settingsKeyPanel);
++ inputKeycodes = intArray(json, "input_key_keycodes", inputKeycodes);
++ appKeys = stringList(json, "app_keys", appKeys);
}
// A theme push or reset invalidates choices made on the TV. On first use, remember the
// current theme without clearing choices from an older launcher version.
@@@ -207,9 -202,9 +217,9 @@@
return new Config(dock, fillDock, hidden, liveTv, liveTvAction, panelService, panelAction, inputAction,
Math.max(1, columns), Math.max(0, rowsBelow), animations, advancedEffects,
- shamsi, moduleCards, reducedMotion, dockTitle, dockRow, reorderButton, vpn, homeSettings, showStats, library, background,
+ shamsi, greetingName, moduleCards, reducedMotion, dockTitle, dockRow, reorderButton, vpn, homeSettings, showStats, library, background,
Math.max(0, Math.min(100, skyIntensity)), settingsKeycodes, settingsKeyPanel, wallpaper, applied,
-- festival);
++ festival, inputKeycodes, appKeys);
}
private static void syncThemeChoices(SharedPreferences choices, File dir, File config) {
diff --cc launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
index 9a09a5b0e,9a09a5b0e..2cc4aa27d
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@@ -6,6 -6,6 +6,9 @@@ import android.provider.Settings
import android.view.KeyEvent;
import android.view.accessibility.AccessibilityEvent;
++import java.util.Collections;
++import java.util.Map;
++
/**
* Sends the remote's Home button to this launcher on remotes that emit KEYCODE_ALL_APPS for Home
* (Xiaomi sticks and boxes). Android TV 9 routes that key only to the system-image launcher, so once
@@@ -21,6 -21,6 +24,8 @@@ 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. */
++ private Map<Integer, String> appKeys = Collections.emptyMap();
@Override
protected void onServiceConnected() {
@@@ -30,6 -30,6 +35,7 @@@
Config config = Config.load(this);
settingsKeycodes = config.settingsKeycodes;
settingsPanel = config.settingsKeyPanel;
++ appKeys = RemoteKeys.parseAppKeys(config.appKeys);
}
private boolean isSettingsKey(KeyEvent event) {
@@@ -50,6 -50,6 +56,11 @@@
}
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 (event.getKeyCode() != KeyEvent.KEYCODE_ALL_APPS) return false;
if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
Intent home = new Intent(Intent.ACTION_MAIN)
diff --cc launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
index 2c2585b4f,d41ce69bd..26e2d0cbc
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@@ -8,23 -10,44 +10,46 @@@ 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. 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.
+ * remote send KEYCODE_TV for it, others (KTC and Kalley Realtek Google TV remotes) KEYCODE_TV_INPUT; on Google TV
- * the stock launcher handles both, so with the stock launcher disabled the button does nothing. Only those two
- * keys are consumed; every other key passes through untouched.
++ * the stock launcher handles both, so with the stock launcher disabled the button does nothing. Buttons listed in
++ * input_key_keycodes (config.xml or a theme) open the input switcher the same way, and 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));
++ // From config.xml or a theme's config.json; theme.sh rebinds this service after a push.
++ Config config = Config.load(this);
++ inputKeycodes = config.inputKeycodes;
++ appKeys = RemoteKeys.parseAppKeys(config.appKeys);
+ }
+
+ private boolean isInputKey(int keyCode) {
- if (keyCode == KeyEvent.KEYCODE_TV) return true;
++ if (keyCode == KeyEvent.KEYCODE_TV || keyCode == KeyEvent.KEYCODE_TV_INPUT) return true;
+ for (int code : inputKeycodes) if (keyCode == code) return true;
+ return false;
+ }
+
@Override
protected boolean onKeyEvent(KeyEvent event) {
- int code = event.getKeyCode();
- if (code != KeyEvent.KEYCODE_TV && code != KeyEvent.KEYCODE_TV_INPUT) return false;
- if (event.getAction() == KeyEvent.ACTION_UP) {
- try {
- startActivity(new Intent(ACTION_VIEW_INPUTS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
- } catch (ActivityNotFoundException ignored) {
+ 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(open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
- } catch (ActivityNotFoundException ignored) {
++ if (app != null) {
++ RemoteKeys.launch(this, app);
++ } else {
++ try {
++ startActivity(new Intent(ACTION_VIEW_INPUTS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
++ } catch (ActivityNotFoundException ignored) {
++ }
}
}
return true;
diff --cc launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
index 000000000,9666616d9..21e4345fa
mode 000000,100644..100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/RemoteKeys.java
@@@ -1,0 -1,26 +1,66 @@@
+ package com.gathin.tvhome;
+
++import android.content.ActivityNotFoundException;
++import android.content.Context;
++import android.content.Intent;
++import android.view.KeyEvent;
++
++import java.util.Arrays;
+ import java.util.HashMap;
+ import java.util.Map;
++import java.util.function.ToIntFunction;
+
+ /**
- * 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").
++ * 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". One map for both services; whichever filters the key first
++ * consumes it.
+ */
+ 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. */
++ /** keycode -> package, with key names resolved by Android. */
+ static Map<Integer, String> parseAppKeys(String[] entries) {
++ return parseAppKeys(Arrays.asList(entries), KeyEvent::keyCodeFromString);
++ }
++
++ static Map<Integer, String> parseAppKeys(Iterable<String> entries) {
++ return parseAppKeys(entries, KeyEvent::keyCodeFromString);
++ }
++
++ /**
++ * keycode -> package. The key is a number or a KEYCODE_ name (resolved by {@code names}; 0, KEYCODE_UNKNOWN,
++ * means it isn't one). Entries without a colon, with an unknown key or with no package are skipped; for a
++ * keycode listed twice the last entry wins.
++ */
++ static Map<Integer, String> parseAppKeys(Iterable<String> entries, ToIntFunction<String> names) {
+ Map<Integer, String> keys = new HashMap<>();
+ for (String entry : entries) {
++ if (entry == null) continue;
+ 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) {
++ String key = parts[0].trim();
++ int code;
++ if (key.matches("[0-9]{1,6}")) {
++ code = Integer.parseInt(key);
++ } else if (key.matches("KEYCODE_[A-Z0-9_]+")) {
++ code = names.applyAsInt(key); // keyCodeFromString takes digits only from API 29, names always
++ } else {
++ continue;
+ }
++ if (code > 0) keys.put(code, parts[1].trim());
+ }
+ return keys;
+ }
++
++ /** 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) {
++ Intent open = ctx.getPackageManager().getLeanbackLaunchIntentForPackage(pkg);
++ if (open == null) open = ctx.getPackageManager().getLaunchIntentForPackage(pkg);
++ if (open == null) return;
++ try {
++ ctx.startActivity(open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
++ } catch (ActivityNotFoundException ignored) {
++ }
++ }
+ }
diff --cc launcher/app/src/main/res/values/config.xml
index e9854a5af,7a20b1825..d415e695b
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@@ -61,6 -61,20 +61,22 @@@
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
++ <!-- Remote buttons that open an app, as "keycode:package" or "KEYCODE_NAME:package" (INPUT_KEY_SERVICE=1 or
++ HOME_KEY_SERVICE=1; both read this one list, and a theme's config.json can set it 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. -->
++ it back. The KTC LUMIO VISION remote (Realtek Google TV) sends Netflix 191, YouTube 190, Prime Video 195
++ and TL;DR 200. 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>
+
<!-- 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 --cc launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
index 000000000,b46df1aa2..80908ea2a
mode 000000,100644..100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/RemoteKeysTest.java
@@@ -1,0 -1,27 +1,39 @@@
+ 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 {
++ 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);
++ }
++
++ @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());
++ assertEquals("com.google.android.youtube.tv", keys.get(190));
++ }
++
+ @Test public void keycodeAndPackage() {
- Map<Integer, String> keys = RemoteKeys.parseAppKeys(new String[] {
- "190:com.google.android.youtube.tv", " 193 : com.netflix.ninja "});
++ Map<Integer, String> keys = parse(
++ "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:", ""});
++ Map<Integer, String> keys = parse(
++ "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"});
++ Map<Integer, String> keys = parse("190:a.b", "190:c.d");
+ assertEquals("c.d", keys.get(190));
+ }
+ }
diff --cc launcher/build-and-install.sh
index ba2cbf87d,4a8cf7a9e..169977997
--- a/launcher/build-and-install.sh
+++ b/launcher/build-and-install.sh
@@@ -147,8 -147,9 +147,9 @@@ esa
# 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
+# INPUT_KEY_SERVICE=1 the remote's Input button (KEYCODE_TV or TV_INPUT) 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)
++# It also handles input_key_keycodes and app_keys (config.xml or a theme; 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
diff --cc themes/README.md
index 82e4dd60e,5e1ba8979..e7c2cd8a5
--- a/themes/README.md
+++ b/themes/README.md
@@@ -56,6 -55,6 +56,8 @@@ is ignored, and a file that doesn't par
| `accent_color` | `#RRGGBB` | Colour of the focus glow while the festive skin is on; diya gold if not set |
| `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"`, for `InputKeyService` or `HomeKeyService` |
## Wallpapers