Skip to content

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

Launcher: consume_volume_keys, for firmware that doubles a filtered volume key (#1100)

SiirGustav45be1e148080
diff --git a/docs/switches.md b/docs/switches.md
index 17f253536..1ae883dd7 100644
--- a/docs/switches.md
+++ b/docs/switches.md
@@ -29,3 +29,4 @@ the rules. Keys from before this rule are on its wanted list; a screenshot for o
| `settings_key_panel` | <img src="quick-settings-panel.jpg" width="320" alt="settings_key_panel on a TV"> |
| `home_idle_clock_minutes` | <img src="clock-screensaver.png" width="320" alt="home_idle_clock_minutes on a TV"> |
| `gamepad_passthrough` | <img src="switches/gamepad_passthrough.jpg" width="320" alt="gamepad_passthrough on a TV"> |
+| `consume_volume_keys` | <img src="switches/consume_volume_keys.jpg" width="320" alt="consume_volume_keys on a TV"> |
diff --git a/docs/switches/consume_volume_keys.jpg b/docs/switches/consume_volume_keys.jpg
new file mode 100644
index 000000000..f63c742b4
Binary files /dev/null and b/docs/switches/consume_volume_keys.jpg differ
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/Config.java b/launcher/app/src/main/java/com/gathin/tvhome/Config.java
index 6252d6d4b..b009a347b 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/Config.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Config.java
@@ -143,6 +143,8 @@ final class Config {
final boolean keyFilter;
/** With keyFilter on, the key services stop filtering while a game controller is connected (gamepad_passthrough). */
final boolean gamepadPassthrough;
+ /** With keyFilter on, the key services swallow Volume Up/Down and repeat a held one themselves (consume_volume_keys). */
+ final boolean consumeVolumeKeys;
/** Stock launcher whose home screen is sent back here if the firmware revives it (config.xml stock_home_redirect). */
final String stockHomeRedirect;
/** The Settings button opens the quick settings panel instead of the TV's Settings (settings_key_panel). */
@@ -220,7 +222,7 @@ final class Config {
List<RaceWeekend.Race> raceCalendar, boolean cinematicHome, boolean ambientGlow, boolean appGlow, boolean fadeRows, int homeIdleClockMinutes,
boolean keyFilter, boolean dockGlass, String glowStyle, boolean wallpaperCaptions,
boolean handleViewInputs, boolean tileMenu, boolean dockEvenPadding, boolean cinematicDots, boolean cinematicWhiteFocus, boolean wifiStatusColor, int cinematicScrim, int cinematicArt,
- Map<String, Set<String>> wallpaperMoods, boolean gamepadPassthrough) {
+ Map<String, Set<String>> wallpaperMoods, boolean gamepadPassthrough, boolean consumeVolumeKeys) {
this.handleViewInputs = handleViewInputs;
this.tileMenu = tileMenu;
this.inputKeycodes = inputKeycodes;
@@ -300,6 +302,7 @@ final class Config {
this.cinematicScrim = cinematicScrim;
this.wallpaperMoods = wallpaperMoods;
this.gamepadPassthrough = gamepadPassthrough;
+ this.consumeVolumeKeys = consumeVolumeKeys;
this.glowStyle = glowStyle(glowStyle);
this.wallpaperCaptions = wallpaperCaptions;
}
@@ -378,6 +381,7 @@ final class Config {
int idleClockMinutes = res.getInteger(R.integer.home_idle_clock_minutes);
boolean keyFilter = res.getBoolean(R.bool.key_filter);
boolean gamepadPassthrough = res.getBoolean(R.bool.gamepad_passthrough);
+ boolean consumeVolumeKeys = res.getBoolean(R.bool.consume_volume_keys);
boolean dockGlass = res.getBoolean(R.bool.dock_glass);
boolean dockEvenPadding = res.getBoolean(R.bool.dock_even_padding);
boolean cinematicDots = res.getBoolean(R.bool.cinematic_dots);
@@ -457,6 +461,7 @@ final class Config {
idleClockMinutes = integer(json, "home_idle_clock_minutes", idleClockMinutes, 0, 1440);
keyFilter = bool(json, "key_filter", keyFilter);
gamepadPassthrough = bool(json, "gamepad_passthrough", gamepadPassthrough);
+ consumeVolumeKeys = bool(json, "consume_volume_keys", consumeVolumeKeys);
dockGlass = bool(json, "dock_glass", dockGlass);
dockEvenPadding = bool(json, "dock_even_padding", dockEvenPadding);
cinematicDots = bool(json, "cinematic_dots", cinematicDots);
@@ -520,7 +525,7 @@ final class Config {
Math.max(0, Math.min(100, skyIntensity)), settingsKeycodes, settingsKeyPanel, restoreKeyServices, wallpaperEnabled, wallpaper,
wallpapers, rotationMinutes, wallpaperDim, dockOpacity, wallpaperOrder, applied,
festival, inputKeycodes, appKeys, stockHomeRedirect, json == null ? Collections.emptyList() : raceCalendar(json), cinematic, ambientGlow, appGlow, fadeRows,
- Math.max(0, idleClockMinutes), keyFilter, dockGlass, glowStyle, wallpaperCaptions, handleViewInputs, tileMenu, dockEvenPadding, cinematicDots, cinematicWhiteFocus, wifiStatusColor, cinematicScrim, cinematicArt, wallpaperMoods, gamepadPassthrough);
+ Math.max(0, idleClockMinutes), keyFilter, dockGlass, glowStyle, wallpaperCaptions, handleViewInputs, tileMenu, dockEvenPadding, cinematicDots, cinematicWhiteFocus, wifiStatusColor, cinematicScrim, cinematicArt, wallpaperMoods, gamepadPassthrough, consumeVolumeKeys);
}
/**
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 edc3b498b..f4f1e21cf 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -46,6 +46,8 @@ public class HomeKeyService extends AccessibilityService {
private int[] settingsScancodes = new int[0];
private int[] settingsKeycodes = new int[0];
private boolean settingsPanel;
+ /** consume_volume_keys: Volume Up/Down go to VolumeKeys instead of on to the firmware. */
+ private boolean consumeVolumeKeys;
/** 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();
@@ -96,6 +98,7 @@ public class HomeKeyService extends AccessibilityService {
public void onDestroy() {
stopWaitingForUnlock();
if (gamepads != null) gamepads.stop();
+ if (consumeVolumeKeys) VolumeKeys.stop();
super.onDestroy();
}
@@ -108,6 +111,7 @@ public class HomeKeyService extends AccessibilityService {
if (repairRecord) ServiceRepair.connected(this, getClass());
settingsKeycodes = config.settingsKeycodes;
settingsPanel = config.settingsKeyPanel;
+ consumeVolumeKeys = config.consumeVolumeKeys;
appKeys = RemoteKeys.parseAppKeys(config.appKeys);
appScancodes = RemoteKeys.parseAppScancodes(config.appKeys);
RemoteKeys.applyKeyFilter(this, config.keyFilter);
@@ -143,6 +147,7 @@ public class HomeKeyService extends AccessibilityService {
@Override
protected boolean onKeyEvent(KeyEvent event) {
+ 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()),
holdAppScancodes.get(event.getScanCode()))) return true;
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 af7a7c79b..4b8f5746f 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@ -27,6 +27,8 @@ public class InputKeyService extends AccessibilityService {
private Map<Integer, String> holdAppScancodes = Collections.emptyMap();
/** restore_key_services was on when the config was read: this helper is noted for ServiceRepair. */
private boolean repairRecord;
+ /** consume_volume_keys: Volume Up/Down go to VolumeKeys instead of on to the firmware. */
+ private boolean consumeVolumeKeys;
/** Stops key filtering while a game controller is connected; null unless gamepad_passthrough and key_filter. */
private GamepadPassthrough gamepads;
@@ -39,6 +41,7 @@ public class InputKeyService extends AccessibilityService {
repairRecord = config.restoreKeyServices;
if (repairRecord) ServiceRepair.connected(this, getClass());
inputKeycodes = config.inputKeycodes;
+ consumeVolumeKeys = config.consumeVolumeKeys;
appKeys = RemoteKeys.parseAppKeys(config.appKeys);
appScancodes = RemoteKeys.parseAppScancodes(config.appKeys);
RemoteKeys.applyKeyFilter(this, config.keyFilter);
@@ -59,6 +62,7 @@ public class InputKeyService extends AccessibilityService {
@Override
protected boolean onKeyEvent(KeyEvent event) {
+ 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()),
holdAppScancodes.get(event.getScanCode()))) return true;
@@ -84,6 +88,7 @@ public class InputKeyService extends AccessibilityService {
@Override
public void onDestroy() {
if (gamepads != null) gamepads.stop();
+ if (consumeVolumeKeys) VolumeKeys.stop();
super.onDestroy();
}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/VolumeKeys.java b/launcher/app/src/main/java/com/gathin/tvhome/VolumeKeys.java
new file mode 100644
index 000000000..0c5817d88
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/VolumeKeys.java
@@ -0,0 +1,67 @@
+package com.gathin.tvhome;
+
+import android.content.Context;
+import android.media.AudioManager;
+import android.os.Handler;
+import android.os.Looper;
+import android.view.KeyEvent;
+
+/**
+ * consume_volume_keys: some firmware (TCL G03, Android 11, #1100) applies a volume key before the key
+ * services' filter and again when the service hands it back, so one tap moved two steps. The services swallow
+ * Volume Up/Down instead, which leaves the firmware's first step; a swallowed key gets no auto-repeat, so a held
+ * one is repeated here, from the same delay and at about the rate Android uses.
+ *
+ * The state is per process, not per service: with HomeKeyService and InputKeyService both bound, each sees the
+ * same press, and the second call restarts the same repeat instead of adding a second one.
+ */
+final class VolumeKeys {
+ static final long REPEAT_DELAY_MS = 500;
+ static final long REPEAT_INTERVAL_MS = 60;
+
+ /** Created on first use, so the class loads in JVM tests that only call the pure methods. */
+ private static Handler handler;
+ private static Runnable step;
+
+ private VolumeKeys() {
+ }
+
+ /** AudioManager direction for a volume key, or 0 for any other key. */
+ static int direction(int keyCode) {
+ if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) return AudioManager.ADJUST_RAISE;
+ if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) return AudioManager.ADJUST_LOWER;
+ return 0;
+ }
+
+ /** Whether this event starts or ends a press; a repeated down event changes nothing. */
+ static boolean changesPress(int action, int repeatCount) {
+ return action != KeyEvent.ACTION_DOWN || repeatCount == 0;
+ }
+
+ /** Swallows a volume key (true) and repeats it while held; any other key is left alone (false). */
+ static boolean handle(Context context, KeyEvent event) {
+ int direction = direction(event.getKeyCode());
+ if (direction == 0) return false;
+ if (!changesPress(event.getAction(), event.getRepeatCount())) return true;
+ stop();
+ if (event.getAction() != KeyEvent.ACTION_DOWN) return true;
+ AudioManager audio = context.getSystemService(AudioManager.class);
+ if (audio == null) return true;
+ if (handler == null) handler = new Handler(Looper.getMainLooper());
+ step = new Runnable() {
+ @Override
+ public void run() {
+ audio.adjustVolume(direction, AudioManager.FLAG_SHOW_UI);
+ handler.postDelayed(this, REPEAT_INTERVAL_MS);
+ }
+ };
+ handler.postDelayed(step, REPEAT_DELAY_MS);
+ return true;
+ }
+
+ /** Ends a repeat in progress, e.g. when the service goes away while a key is held. */
+ static void stop() {
+ if (handler != null && step != null) handler.removeCallbacks(step);
+ step = null;
+ }
+}
diff --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xml
index f600b7d60..e8910c5ee 100644
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@ -108,6 +108,12 @@
remote that reports a joystick does not count. A theme can set it ("gamepad_passthrough"). -->
<bool name="gamepad_passthrough">false</bool>
+ <!-- With key_filter on, the key helpers swallow Volume Up/Down and repeat a held one themselves. For firmware
+ that applies a volume key before the filter and again when the helper hands it back, so one tap moves two
+ steps (TCL G03 / BeyondTV4 on Android 11, #1100); a swallowed key gets no auto-repeat from Android, hence
+ the helper's own. Leave it off where a tap is one step. A theme can set it ("consume_volume_keys"). -->
+ <bool name="consume_volume_keys">false</bool>
+
<!-- Vendor source numbers for the Inputs menu, "HW<n>=<number>" by the framework input id's last part
(`tools/tv.py 'dumpsys tv_input'` lists them). A mapped input opens by sending input_action with the
number as the int extra "input"; "HW<n>=-" leaves a port the board reports but the set does not have
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/VolumeKeysTest.java b/launcher/app/src/test/java/com/gathin/tvhome/VolumeKeysTest.java
new file mode 100644
index 000000000..37f46e9c1
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/VolumeKeysTest.java
@@ -0,0 +1,40 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class VolumeKeysTest {
+ // Android's values, written out: android.view.KeyEvent and AudioManager aren't available in JVM tests.
+ private static final int KEYCODE_VOLUME_UP = 24;
+ private static final int KEYCODE_VOLUME_DOWN = 25;
+ private static final int KEYCODE_VOLUME_MUTE = 164;
+ private static final int KEYCODE_DPAD_UP = 19;
+ private static final int ADJUST_RAISE = 1;
+ private static final int ADJUST_LOWER = -1;
+ private static final int ACTION_DOWN = 0;
+ private static final int ACTION_UP = 1;
+
+ @Test
+ public void volumeUpAndDownAreHandled() {
+ assertEquals(ADJUST_RAISE, VolumeKeys.direction(KEYCODE_VOLUME_UP));
+ assertEquals(ADJUST_LOWER, VolumeKeys.direction(KEYCODE_VOLUME_DOWN));
+ }
+
+ @Test
+ public void otherKeysPassThrough() {
+ // Mute is one press with nothing to repeat, and the firmware did not double it.
+ assertEquals(0, VolumeKeys.direction(KEYCODE_VOLUME_MUTE));
+ assertEquals(0, VolumeKeys.direction(KEYCODE_DPAD_UP));
+ }
+
+ @Test
+ public void onlyTheFirstDownAndTheUpChangeThePress() {
+ assertTrue(VolumeKeys.changesPress(ACTION_DOWN, 0));
+ assertTrue(VolumeKeys.changesPress(ACTION_UP, 0));
+ // A remote that sends its own repeats must not restart the 500 ms delay on every one.
+ assertFalse(VolumeKeys.changesPress(ACTION_DOWN, 1));
+ assertFalse(VolumeKeys.changesPress(ACTION_DOWN, 12));
+ }
+}
diff --git a/themes/README.md b/themes/README.md
index fd66ad533..e2bda3b97 100644
--- a/themes/README.md
+++ b/themes/README.md
@@ -103,6 +103,7 @@ A new key needs one there in the same pull request.
| `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`). 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 |
| `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); 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 |
| `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`) |
| `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) |