Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
tools: manage.py set refuses a keycode list the launcher would throw away
xbsheng6e264c87929b
diff --git a/tests/test_manage_set_keycodes.py b/tests/test_manage_set_keycodes.pynew file mode 100644index 000000000..b0c791ebc--- /dev/null+++ b/tests/test_manage_set_keycodes.py@@ -0,0 +1,144 @@+"""A keycode list `tools/manage.py set` writes is one the launcher keeps, entry by entry.++themes/README.md hands a theme author `set settings_key_keycodes '[83]'` for a remote's gear button, and the+set script's own docstring promises it "refuses one the launcher would ignore". The list was read as a list+and nothing else, and Config.intArray() is stricter than that in a way that costs the whole key:++ if (!(item instanceof Number)) return fallback;+ out[i] = ((Number) item).intValue();+ if (out[i] < 1) return fallback;++so one entry that is a word, or a number under 1, and the launcher keeps config.xml's `<integer-array>`, which+ships empty for both keys - the gear the owner was adding opens nothing, while set printed+`settings_key_keycodes = [83, 0] in ~/my-theme/config.json` and pushed the file. Measured on d451b175 with+`set settings_key_keycodes '[83, 0]'` and `set input_key_keycodes '["KEYCODE_TV"]'`: both were written and+both are dropped by intArray() as a whole. A list of any other kind is not read this way - Config.stringList()+drops the one entry it cannot read and keeps the rest - so the rule below is the keycode cell's alone.++Run from the repository root: python3 -m unittest discover -s tests -v+"""+import io+import json+import sys+import tempfile+import unittest+from pathlib import Path+from unittest import mock++ROOT = Path(__file__).resolve().parent.parent+sys.path.insert(0, str(ROOT / "tools"))+import manage # noqa: E402++# The two keys the Keys table gives the `list of keycodes` cell, and the sentence both rows now carry.+KEYCODE_KEYS = {"settings_key_keycodes", "input_key_keycodes"}+++class KeycodeListTest(unittest.TestCase):+ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.addCleanup(self.tmp.cleanup)+ self.theme = Path(self.tmp.name) / "my-theme"+ self.theme.mkdir()+ self.config = self.theme / "config.json"+ # cmd_set ends by pushing the theme with tools/theme.sh; the tests stop before the TV.+ patcher = mock.patch.object(manage, "run")+ self.push = patcher.start()+ self.push.return_value = 0+ self.addCleanup(patcher.stop)++ def set_key(self, key, value):+ """Runs set the way main() does; returns the refusal text, or None when the value was written."""+ try:+ with mock.patch("sys.stdout"):+ manage.cmd_set(key, value, str(self.theme))+ except SystemExit as stop:+ return str(stop.code)+ return None++ def written(self):+ return json.loads(self.config.read_text(encoding="utf-8")) if self.config.is_file() else None++ def test_a_zero_entry_is_refused_with_what_the_launcher_would_do(self):+ # `[83, 0]` is the shape a reader types when they are copying `[83]` and adding a code by hand.+ refusal = self.set_key("settings_key_keycodes", "[83, 0]")+ self.assertIsNotNone(refusal, "a list holding 0 was written, and intArray() drops the whole list for it")+ self.assertIn("settings_key_keycodes", refusal)+ self.assertIn("0 is below 1", refusal)+ self.assertIn("ignores the whole list", refusal)+ self.assertIsNone(self.written(), "a refused list must not touch the theme")+ self.push.assert_not_called()++ def test_a_list_of_words_is_refused(self):+ # The key is a list of keycodes, not of the constants' names: intArray() has no answer for a String.+ refusal = self.set_key("input_key_keycodes", '["KEYCODE_TV"]')+ self.assertIsNotNone(refusal, "a list of words was written, and intArray() keeps config.xml's array for it")+ self.assertIn("'KEYCODE_TV' is not a number", refusal)+ self.assertIsNone(self.written())+ self.push.assert_not_called()++ def test_a_fraction_is_refused_with_the_number_the_launcher_would_read(self):+ # Number.intValue() truncates rather than dropping, so 83.5 is key 83 - not the button that was meant.+ refusal = self.set_key("settings_key_keycodes", "[83.5]")+ self.assertIsNotNone(refusal, "a fraction was written, and the launcher reads it as key 83")+ self.assertIn("83.5 is not a whole number", refusal)+ self.assertIn("reads it as 83", refusal)+ self.assertIsNone(self.written())++ def test_both_keys_take_whole_numbers(self):+ for key in sorted(KEYCODE_KEYS):+ with self.subTest(key=key):+ self.assertIsNone(self.set_key(key, "[83, 176]"))+ self.assertEqual([83, 176], self.written()[key])++ def test_a_whole_number_written_with_a_decimal_point_is_taken(self):+ # 83.0 is a whole number, and the launcher reads it as 83: refusing it would be stricter than intArray().+ self.assertIsNone(self.set_key("settings_key_keycodes", "[83.0]"))+ self.assertEqual(83, int(self.written()["settings_key_keycodes"][0]))++ def test_an_empty_list_is_taken(self):+ # `[]` is how an owner clears buttons they added; intArray() keeps an empty array as it is.+ self.assertIsNone(self.set_key("settings_key_keycodes", "[]"))+ self.assertEqual([], self.written()["settings_key_keycodes"])++ def test_a_list_of_another_kind_is_not_held_to_keycodes(self):+ # dock_apps is a list of package names, read by stringList(), which drops only the entry it cannot read.+ self.assertIsNone(self.set_key("dock_apps", '["com.example.player", "com.example.tv"]'))+ self.assertEqual(["com.example.player", "com.example.tv"], self.written()["dock_apps"])+++class KeycodeProblemTest(unittest.TestCase):+ def test_the_entries_intArray_would_keep(self):+ self.assertIsNone(manage.keycode_problem([]))+ self.assertIsNone(manage.keycode_problem([83, 176]))+ self.assertIsNone(manage.keycode_problem([1]))+ self.assertIsNone(manage.keycode_problem([83.0]))++ def test_the_entries_it_would_throw_the_list_away_for(self):+ self.assertIn("is not a number", manage.keycode_problem(["83"]))+ self.assertIn("is not a number", manage.keycode_problem([True]))+ self.assertIn("0 is below 1", manage.keycode_problem([83, 0]))+ self.assertIn("-4 is below 1", manage.keycode_problem([-4]))+ self.assertIn("not a whole number", manage.keycode_problem([83.5]))+++class KeysTableTest(unittest.TestCase):+ """The table is where set learns which keys are keycode lists, and where an author reads the rule."""++ def keycode_rows(self):+ return {item["id"]: item for item in manage.switches() if (item.get("type") or "").strip() == manage.KEYCODES}++ def test_the_shipped_keycode_rows_are_the_two_keys(self):+ # A key added to the table with this cell joins the refusal above; one that moves off it stops being read+ # this way, so the set of keys is pinned here rather than assumed.+ self.assertEqual(KEYCODE_KEYS, set(self.keycode_rows()))++ def test_every_keycode_row_states_the_rule_set_applies(self):+ for key, item in sorted(self.keycode_rows().items()):+ with self.subTest(key=key):+ self.assertEqual("list", manage.declared_kind(item), f"{key} is not read as a list")+ self.assertIn("whole number", item["what"], f"{key}'s row does not state the whole-number rule")+ self.assertIn("at least 1", item["what"], f"{key}'s row does not say the floor is 1")+++if __name__ == "__main__":+ unittest.main()diff --git a/themes/README.md b/themes/README.mdindex e3736d7f7..83927c226 100644--- a/themes/README.md+++ b/themes/README.md@@ -165,11 +165,11 @@ A new key needs one there in the same pull request.| `greeting` | string | Replaces the time-of-day greeting while the festive skin is on || `accent_color` | `#RRGGBB` | Colour of the focus glow while the festive skin is on; diya gold if not set, pumpkin orange for `halloween` || `settings_key_scancodes` | list, `config.xml` only | The same buttons by Linux scancode, for `HomeKeyService` (`HOME_KEY_SERVICE=1`); the Xiaomi/Fengmi projector remote's gear is scan 384. `Config.java` reads `settings_key_keycodes` out of a theme but never this one, so a `config.json` that sets it is dropped - it belongs in `config.xml` before the build. Find a code with `tools/tv.py 'getevent -lt'` |-| `settings_key_keycodes` | list of keycodes | Remote buttons that open Settings, for `HomeKeyService` (TCL Google TV gear: `[83]`) |+| `settings_key_keycodes` | list of keycodes | Remote buttons that open Settings, for `HomeKeyService` (TCL Google TV gear: `[83]`). Every entry is a whole number of at least 1: one entry that is a word or a number under 1 and the launcher keeps `config.xml`'s list instead of yours, so no button is added, and tools/manage.py's `set` refuses such a list before writing it || `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 || `settings_fallback_components` | list, `config.xml` only | Vendor screens tried in order when nothing usable answers the standard Settings action (Xiaomi China, TCL/FFALCON, and Fire OS, where the real menu lives in its launcher), each as `package/activity`; the first one whose package is installed wins, and when none is, the standard intent is sent anyway and may open nothing. Fire OS answers that intent with a `CTSDummy` stub, which counts as missing so the fallback runs. A package named here also has to be listed in the launcher's `<queries>` (Android 11 package visibility), so a set whose settings app is neither of these needs a build, not a theme push. Not read from a theme || `restore_key_services` | bool | After a force stop of the launcher, Home puts the remote's accessibility helpers (`HomeKeyService`, `InputKeyService`) back on the enabled list; needs `RESTORE_KEY_SERVICES=1` (`-RestoreKeyServices` on Windows) on every install, since an install without it takes the grant back. Off by default |-| `input_key_keycodes` | list of keycodes | More remote buttons that open the input switcher, for `InputKeyService` (`KEYCODE_TV` and `KEYCODE_TV_INPUT` always do) |+| `input_key_keycodes` | list of keycodes | More remote buttons that open the input switcher, for `InputKeyService` (`KEYCODE_TV` and `KEYCODE_TV_INPUT` always do). Every entry is a whole number of at least 1, and one the launcher cannot read as a keycode drops the whole list - tools/manage.py's `set` refuses one before writing it || `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`); the switch asks `live_tv_package` first, so set it, or an app that also claims the channel URI (a file manager on a TCL G03) can take the press and turn it into a chooser, while `input_uri_scheme` and `input_action` shape the Inputs rows and a tile's `launch_actions` rather than this target. `"inputs"` in place of the package opens the Inputs menu, for a remote whose source button sends a key Android does not map, e.g. `"scan:365:inputs"` (TCL 65C745). An action word in place of the package runs that action: `back`, `home`, `recents`, `mute`, `sleep` (standby, Android 9 and later), `screensaver`, `settings`, `wifi`, `library` (Home with the App library open, for a remote's Apps button, `"284:library"`; needs `app_library`) or `ignore` (swallows the button so it does nothing, for a shortcut pressed by accident), e.g. `"KEYCODE_PROG_YELLOW:mute"` or `"scan:586:ignore"`. 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. Prefix it with `front:` for what a tap does while the button's own app is already in front (`HomeKeyService` only), and use `"guide:<package>"` as a target to open that Live TV app's program guide: `"172:com.vt.tv.tvcenter"` with `"front:172:guide:com.vt.tv.tvcenter"` opens Live TV, then its guide, on a Hisense hengshan set whose firmware sends the Live TV button (`KEYCODE_GUIDE`) only to the stock launcher || `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, Moonlight with two controllers); 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 |diff --git a/tools/manage.py b/tools/manage.pyindex 90dd0b11c..a44d154c9 100644--- a/tools/manage.py+++ b/tools/manage.py@@ -16,7 +16,9 @@ pushes it with the platform's theme script, as themes/README.md describes; it ne--theme at the project's themes/ or examples/ is refused before anything is written. set reads the value the waythe key's own type in the Keys table needs - true or false for a bool, a JSON list, object or number where thetable says so, the text itself for a string - and refuses one the launcher would ignore or a number outside the-range the table states. A theme's config.json is hand-edited, so one that cannot be read, is not JSON, or is not a+range the table states. A list of keycodes is read the same way one entry at a time, because the launcher's+intArray() answers a list holding a word or a number under 1 with the built-in default instead of the list.+A theme's config.json is hand-edited, so one that cannot be read, is not JSON, or is not aJSON object is named before the key is written, and a write it cannot make says so instead of ending in a traceback.set's own arguments are read first: a switch without a value, a --theme without the folder behind it, and a wordleft over are answered with the usage line and exit 2 before a theme folder is read or written. list, status,@@ -218,6 +220,29 @@ def number_in_range(key, item, parsed, text):return value+# The Keys-table cell of a list the launcher reads with intArray() - `[83, 176]` for the remote buttons those+# keys add. A list with anything else in it is answered with the built-in default, not the list.+KEYCODES = "list of keycodes"+++def keycode_problem(values):+ """The first entry intArray() would throw the whole list away for, or None.++ intArray() is `if (!(item instanceof Number)) return fallback; ... if (out[i] < 1) return fallback;`, so one+ entry that is a word, or a number under 1, gives the launcher config.xml's array instead of the list: the+ remote buttons the key was meant to add do nothing while set has already reported the list as written.+ Number.intValue() truncates, so a fraction is read as the whole number below it rather than dropped.+ """+ for entry in values:+ if isinstance(entry, bool) or not isinstance(entry, (int, float)):+ return f"{entry!r} is not a number"+ if entry != int(entry):+ return f"{entry} is not a whole number (the launcher reads it as {int(entry)})"+ if int(entry) < 1:+ return f"{int(entry)} is below 1"+ return None++def value_as(key, item, text):"""The argument read the way the key's type needs, or a refusal naming what the key takes."""kind = declared_kind(item)@@ -245,6 +270,12 @@ def value_as(key, item, text):sys.exit(f"{key} takes {item.get('type')}, so pass {example}, not {text}")if kind == "number":return number_in_range(key, item, parsed, text)+ if (item.get("type") or "").strip() == KEYCODES:+ problem = keycode_problem(parsed)+ if problem:+ sys.exit(f"{key} takes {KEYCODES}, so every entry has to be a whole number of at least 1: {problem}, "+ f"and the launcher ignores the whole list when one entry is not, so those remote buttons "+ f"would do nothing. Nothing was written or pushed.")return parsed