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/bench.py: put the accessibility list back without wiping it, and after the measurement
eddybb7fe8b7d85a
diff --git a/tests/test_bench_accessibility.py b/tests/test_bench_accessibility.pynew file mode 100644index 000000000..652cbf291--- /dev/null+++ b/tests/test_bench_accessibility.py@@ -0,0 +1,113 @@+"""tools/bench.py must not wipe the accessibility list, and must not warm the cold start.++Two problems, both found on a TCL BeyondTV2 (Android TV 9) while measuring the launcher:++1. `am force-stop` removes that package's services from `enabled_accessibility_services`, and they+ do not come back when the launcher starts again (#503 reported it on a TCL 55C715). The old+ `restore_accessibility` wrote each service on its own with+ `settings put secure enabled_accessibility_services <one service>`, which **replaces the whole+ list**: the launcher's other services and any other app's were wiped.++2. It restored the list *before* the cold-start measurement. Writing that setting makes the system+ bind the service, which starts the launcher's process — so the "cold start" measured a warm one.++The TV is a dict here: the point is the commands the tool would send.++Run from the repository root:+ python -m unittest tests.test_bench_accessibility -v+"""+import unittest++from tools import bench++OURS = "com.gathin.tvhome/.KeyRemapService"+OTHER = "com.exemplo.outroapp/.Servico"+++class FakeTV:+ """Answers the two settings commands the tool uses, and keeps the value in memory."""++ def __init__(self, setting=""):+ self.setting = setting+ self.commands = []++ def shell(self, command, read_timeout_s=None):+ self.commands.append(command)+ if command.startswith("settings get secure enabled_accessibility_services"):+ return self.setting+ if command.startswith("settings put secure enabled_accessibility_services "):+ self.setting = command.split(" ", 4)[4]+ return ""+ if command.startswith("am force-stop "):+ # This is what the TV does: the stopped package's services leave the list.+ package = command.split(" ", 2)[2]+ self.setting = ":".join(+ part for part in self.setting.split(":") if part and not part.startswith(package + "/"))+ return ""+ return ""++ def package_of(self, component):+ return component.split("/")[0]+++class RestoreAccessibilityTest(unittest.TestCase):+ def test_another_apps_service_is_kept(self):+ tv = FakeTV("com.outro.novo/.Svc")+ bench.restore_accessibility(tv, "com.gathin.tvhome", [OURS])+ self.assertEqual(tv.setting, f"com.outro.novo/.Svc:{OURS}")++ def test_does_not_add_the_same_service_twice(self):+ tv = FakeTV(OURS)+ bench.restore_accessibility(tv, "com.gathin.tvhome", [OURS])+ self.assertEqual(tv.setting, OURS)+ self.assertEqual(len(tv.commands), 1, "nothing to write means no command at all")++ def test_short_and_long_component_forms_are_the_same_component(self):+ """The TV rewrites `pkg/pkg.Classe` as `pkg/.Classe`; restoring must not duplicate it."""+ tv = FakeTV("com.gathin.tvhome/com.gathin.tvhome.KeyRemapService")+ bench.restore_accessibility(tv, "com.gathin.tvhome", [OURS])+ self.assertEqual(tv.setting.count("KeyRemapService"), 1, tv.setting)++ def test_null_setting_is_treated_as_empty(self):+ tv = FakeTV("null")+ bench.restore_accessibility(tv, "com.gathin.tvhome", [OURS])+ self.assertEqual(tv.setting, OURS)+++class ColdStartOrderTest(unittest.TestCase):+ def test_services_are_restored_after_the_measurement(self):+ """A `settings put` starts the process; doing it first would measure a warm start."""+ tv = FakeTV(OURS)+ order = []++ original_shell = tv.shell+ def spy(command, read_timeout_s=None):+ if command.startswith("settings put secure enabled_accessibility_services"):+ order.append("restore")+ elif command.startswith("am start -W -n com.gathin.tvhome"):+ order.append("measure")+ return original_shell(command, read_timeout_s)+ tv.shell = spy++ # `am start -W` has to answer something with a WaitTime.+ def shell_with_start(command, read_timeout_s=None):+ out = spy(command, read_timeout_s)+ if command.startswith("am start -W"):+ return "WaitTime: 420"+ return out+ tv.shell = shell_with_start++ bench.other_app = lambda _tv: "com.android.tv.settings/.MainSettings"+ try:+ bench.cold_start(tv, "com.gathin.tvhome/.MainActivity", 1)+ finally:+ pass++ self.assertIn("measure", order, order)+ self.assertIn("restore", order, order)+ self.assertLess(order.index("measure"), order.index("restore"),+ f"restore happened before the measurement: {order}")+++if __name__ == "__main__":+ unittest.main()diff --git a/tools/bench.py b/tools/bench.pyindex f0f85a4ca..44f135a5d 100644--- a/tools/bench.py+++ b/tools/bench.py@@ -273,44 +273,76 @@ def accessibility_services(tv, package):return [part.strip() for part in out.split(":") if part.strip().startswith(package + "/")]+def _norm_component(component):+ """`pkg/pkg.Classe` and `pkg/.Classe` are the same component; normalise so it is not added twice."""+ if "/" not in component:+ return component+ package, cls = component.split("/", 1)+ if cls.startswith(package + "."):+ return package + "/" + cls[len(package):]+ return component++def restore_accessibility(tv, package, services):- """Re-enable the accessibility services a force-stop took down.+ """Put back the accessibility services a force-stop took down.++ `am force-stop` kills the process the service lives in, and on some TVs it also **removes that+ package's services** from `enabled_accessibility_services` — they do not come back when the+ launcher starts again. Reported on a TCL 55C715 (#503) and reproduced on a TCL BeyondTV2+ (Android TV 9): the setting went from one service to empty after a single stop.- `am force-stop` kills the process the service lives in, and Android unbinds it. The- service has to be re-enabled by hand, or the launcher comes back without it. `am kill`- would leave the service alone, but it also leaves the process alive, and then there is no- cold start to measure.+ The list is a colon-separated set shared by every app, so this **appends** what is missing+ instead of writing each service on its own: `settings put secure enabled_accessibility_services+ <one service>` replaces the whole list, which would wipe the launcher's other services and any+ other app's."""if not services:return+ current = tv.shell("settings get secure enabled_accessibility_services").strip()+ if current == "null":+ current = ""+ merged = [part for part in current.split(":") if part]+ known = {_norm_component(part) for part in merged}+ added = Falsefor service in services:- tv.shell(f"settings put secure enabled_accessibility_services {service}")- tv.shell("settings put secure accessibility_enabled 1")+ if _norm_component(service) not in known:+ known.add(_norm_component(service))+ merged.append(service)+ added = True+ if added:+ tv.shell("settings put secure enabled_accessibility_services " + ":".join(merged))+ tv.shell("settings put secure accessibility_enabled 1")def cold_start(tv, component, repeats):- """The `am start -W` WaitTime of the launcher, in ms."""+ """The `am start -W` WaitTime of the launcher, in ms.++ The accessibility services are put back **after** the measurement, not before. Writing that+ setting makes the system bind the service, which starts the launcher's process — the one thing a+ cold start must not have. Restoring first turned every "cold" start into a warm one.+ """values = []other = other_app(tv)if not other:print("Could not find another app to switch to; the cold start is not measured.")return valuespackage = tv.package_of(component)- for _ in range(repeats):- tv.shell(f"am start -W -n {other}")- time.sleep(2)- # The services are read before the stop, because a force-stop takes them down with- # the process, and put back before the measurement, so the launcher is measured as a- # viewer would actually have it.- services = accessibility_services(tv, package)- tv.shell(f"am force-stop {package}")- time.sleep(3)+ # Read once, before the first stop: the force-stop clears the setting on these TVs, so after the+ # first run there would be nothing left to read.+ services = accessibility_services(tv, package)+ try:+ for _ in range(repeats):+ tv.shell(f"am start -W -n {other}")+ time.sleep(2)+ tv.shell(f"am force-stop {package}")+ time.sleep(3)+ out = tv.shell(f"am start -W -n {component}")+ match = re.search(r"WaitTime:\s*(\d+)", out)+ if match:+ values.append(int(match.group(1)))+ time.sleep(3)+ finally:restore_accessibility(tv, package, services)- out = tv.shell(f"am start -W -n {component}")- match = re.search(r"WaitTime:\s*(\d+)", out)- if match:- values.append(int(match.group(1)))- time.sleep(3)return values