Skip to content

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

tools: keep the packages that drive the HDMI/AV inputs (#321)

xbsheng6d15883f12e2
diff --git a/tests/test_tvinput_guard.py b/tests/test_tvinput_guard.py
new file mode 100644
index 000000000..ef5ddaae1
--- /dev/null
+++ b/tests/test_tvinput_guard.py
@@ -0,0 +1,119 @@
+"""tools/disable.sh must keep the packages that drive the HDMI/AV/tuner inputs (#321).
+
+On a OnePlus TV (Android 11, MediaTek/MStar) disabling them left every HDMI port detecting the cable
+and showing "No Signal", and only a full restore plus a cold boot brought the picture back.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import os
+from pathlib import Path
+import shlex
+import shutil
+import subprocess
+import tempfile
+import unittest
+
+
+ROOT = Path(__file__).resolve().parents[1]
+# On MediaTek sets this is the package behind every HDMI, AV and tuner input.
+MEDIATEK = "com.mediatek.tvinput"
+LIST = f"# My TV\ncom.example.before\n{MEDIATEK} # the inputs\ncom.example.after\n"
+BATCH = ["pm disable-user --user 0 com.example.before",
+ f"pm disable-user --user 0 {MEDIATEK}",
+ "pm disable-user --user 0 com.example.after",
+ "pm list packages -d"]
+
+
+class TvInputGuardTest(unittest.TestCase):
+ def run_disable(self, providers, extra_env=None, exit_code=0, eol="\n"):
+ """Run the real disable.sh against a fake tv.py client.
+
+ The fake answers the TV_INPUT_HARDWARE lookup with `providers` and tells every other command
+ apart: the ones this test cares about (the batch) land in CAPTURE, the rest are answered the
+ way a working TV would, so the other guards in the script stay out of the way.
+ """
+ with tempfile.TemporaryDirectory() as tmp:
+ repo = Path(tmp) / "repo"
+ tools = repo / "tools"
+ tools.mkdir(parents=True)
+ shutil.copy2(ROOT / "tools" / "disable.sh", tools / "disable.sh")
+ client = tools / "venv" / "bin" / "python"
+ client.parent.mkdir(parents=True)
+ # Quote every name so the fake echoes it verbatim: the test is about what the script does
+ # with a hostile name from the TV, not what sh does with one inside the fake.
+ eol_escaped = "\\r\\n" if eol == "\r\n" else "\\n"
+ answer = "".join(f"printf '%s{eol_escaped}' {shlex.quote(p)}\n" for p in providers)
+ if exit_code:
+ answer += f"exit {exit_code}\n"
+ client.write_text(
+ "#!/bin/sh\n"
+ 'case "$*" in\n'
+ f" *TV_INPUT_HARDWARE*) {answer or 'true'} ;;\n"
+ # The other guards ask who owns Home; none of them is about this test.
+ ' *resolve-activity*) echo "com.google.android.tvlauncher/.MainActivity" ;;\n'
+ ' *query-activities*) echo " com.google.android.tvlauncher/.MainActivity" ;;\n'
+ ' *getprop*) echo 1 ;;\n'
+ # recovery.py --save and the batch: the batch is the last write, so CAPTURE holds it.
+ ' *) cat > "$CAPTURE" ;;\n'
+ "esac\n")
+ client.chmod(0o755)
+ (repo / "debloat.txt").write_text(LIST)
+ capture = Path(tmp) / "commands.txt"
+ env = dict(os.environ, CAPTURE=str(capture), **(extra_env or {}))
+ result = subprocess.run(["bash", str(tools / "disable.sh"), "debloat.txt"], cwd=repo,
+ env=env, capture_output=True, text=True, stdin=subprocess.DEVNULL)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ commands = capture.read_text().splitlines() if capture.exists() else []
+ return commands, result.stderr
+
+ def test_input_package_is_skipped(self):
+ commands, stderr = self.run_disable([MEDIATEK])
+ self.assertEqual(commands, ["pm disable-user --user 0 com.example.before",
+ "pm disable-user --user 0 com.example.after",
+ "pm list packages -d"])
+ self.assertIn(f"Skipping {MEDIATEK}", stderr)
+ self.assertIn("TV_INPUT_HARDWARE", stderr)
+ self.assertIn("TVINPUT_ALLOW=1", stderr)
+
+ def test_crlf_from_adb_still_matches(self):
+ # adb can end a line with CRLF; a stray CR would leave the name unmatched and the port broken.
+ commands, stderr = self.run_disable([MEDIATEK], eol="\r\n")
+ self.assertNotIn(f"pm disable-user --user 0 {MEDIATEK}", commands)
+ self.assertIn(f"Skipping {MEDIATEK}", stderr)
+
+ def test_disabled_when_the_tv_reports_no_hardware_inputs(self):
+ commands, stderr = self.run_disable([])
+ self.assertEqual(commands, BATCH)
+ self.assertNotIn("Skipping", stderr)
+
+ def test_force(self):
+ commands, stderr = self.run_disable([MEDIATEK], {"TVINPUT_ALLOW": "1"})
+ self.assertEqual(commands, BATCH)
+ self.assertNotIn("Skipping", stderr)
+
+ def test_failed_check_warns_and_keeps_the_list(self):
+ # tv.py failed (no connection, a timeout): do not skip on a guess, but say the check did not
+ # happen, because a black input is easy to blame on the cable.
+ commands, stderr = self.run_disable([], exit_code=1)
+ self.assertEqual(commands, BATCH)
+ self.assertIn("Could not check", stderr)
+
+ def test_only_names_from_the_list_are_matched(self):
+ # A package the TV reports that is not in this list must not lose a line or add one.
+ commands, stderr = self.run_disable([MEDIATEK, "com.android.tv"])
+ self.assertEqual(commands, ["pm disable-user --user 0 com.example.before",
+ "pm disable-user --user 0 com.example.after",
+ "pm list packages -d"])
+ self.assertNotIn("com.android.tv", stderr)
+
+ def test_a_name_from_the_tv_cannot_add_a_command(self):
+ # The lookup output comes from the TV, so it is only ever compared against the list, never
+ # pasted back into a command.
+ commands, stderr = self.run_disable(["com.example.before; reboot"])
+ self.assertEqual(commands, BATCH)
+ self.assertNotIn("reboot", "\n".join(commands))
+ self.assertNotIn("Skipping", stderr)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/disable.ps1 b/tools/disable.ps1
index 831fb41fe..946038c11 100644
--- a/tools/disable.ps1
+++ b/tools/disable.ps1
@@ -69,6 +69,30 @@ if (-not $env:FORCE_HOME) {
}
}
}
+# Same as disable.sh (#321): a package that asks for android.permission.TV_INPUT_HARDWARE is the software
+# layer under the HDMI, AV and tuner inputs. Disable one and the port still notices the cable but shows
+# "No Signal" for good; re-enabling the pieces in batches does not bring the picture back, a full restore
+# and a cold boot do. Keep them; `$env:TVINPUT_ALLOW=1` disables them anyway.
+if (-not $env:TVINPUT_ALLOW -and $pkgs.Count -gt 0) {
+ # Trailing `; true`: the loop's status is the last package's `grep -q`, which is nonzero whenever the
+ # last listed package is not an input driver, and would look like a failed check.
+ $loop = 'for p in ' + ($pkgs -join ' ') + '; do dumpsys package $p | grep -q TV_INPUT_HARDWARE && echo $p; done; true'
+ # Windows PowerShell turns redirected stderr into an error that 'Stop' would throw (see getprop).
+ $ErrorActionPreference = 'Continue'
+ $tvhw = @(& $py -I (Join-Path $repo 'tools\tv.py') $loop 2>$null)
+ $checked = $LASTEXITCODE -eq 0
+ $ErrorActionPreference = 'Stop'
+ if (-not $checked) {
+ Write-Warning 'Could not check which listed packages drive the HDMI/AV inputs (tv.py failed). Check every input after this run: a port that notices the cable and stays black means one of them was disabled, and only a full restore and a cold boot bring it back (#321).'
+ } else {
+ foreach ($p in ($tvhw | ForEach-Object { $_.Trim() } | Where-Object { $_ })) {
+ if ($pkgs -contains $p) {
+ Write-Warning "Skipping ${p}: it asks for android.permission.TV_INPUT_HARDWARE, so it is the software layer under the HDMI/AV/tuner inputs. With it disabled those inputs still notice the cable but stay black (No Signal), and re-enabling the pieces in batches does not restore the picture: a full restore plus a cold boot does (#321). Set `$env:TVINPUT_ALLOW=1 to disable it."
+ $pkgs = @($pkgs | Where-Object { $_ -ne $p })
+ }
+ }
+ }
+}
# Same as disable.sh: the state undo needs is recorded before the first command reaches the TV, and
# a failed save leaves the TV untouched (#239).
& $py -I (Join-Path $repo 'tools\recovery.py') --save $List
diff --git a/tools/disable.sh b/tools/disable.sh
index 8d8161f65..d189fb64d 100755
--- a/tools/disable.sh
+++ b/tools/disable.sh
@@ -81,6 +81,41 @@ if [ -n "$HOME_PKG" ] && printf '%s\n' "$PKGS" | grep -xF "$HOME_PKG" >/dev/null
fi
fi
+# A package that asks for android.permission.TV_INPUT_HARDWARE is the software layer under the HDMI, AV
+# and tuner inputs: routing, the HDCP handshake and scaling live in it. Disable one and the port still
+# notices the cable but shows "No Signal" for good; re-enabling the pieces in batches does not bring the
+# picture back, a full restore and a cold boot do (#321, OnePlus TV on Android 11). Keep those packages;
+# TVINPUT_ALLOW=1 disables them anyway.
+if [ -n "$PKGS" ] && [ -z "${TVINPUT_ALLOW:-}" ]; then
+ # One round trip: the loop runs on the TV, where `dumpsys package` prints the permissions a package
+ # asks for. Every name was checked as a bare package name above, so none of them can add shell syntax
+ # here. </dev/null so the client cannot read this script's stdin (that is the piped batch below);
+ # tr -d '\r' because adb can end lines with CRLF, which would leave a CR on every name.
+ NAMES="$(printf '%s ' $PKGS)"
+ # Trailing `; true`: the loop's status is the last package's `grep -q`, which is nonzero whenever
+ # the last listed package is not an input driver — the common case — and would look like a failed
+ # check. Only a real failure (tv.py cannot reach the TV) should reach the warning below.
+ if TV_HW="$("$PY" -I tools/tv.py \
+ "for p in $NAMES; do dumpsys package \$p | grep -q TV_INPUT_HARDWARE && echo \$p; done; true" \
+ </dev/null 2>/dev/null | tr -d '\r')"; then
+ for p in $TV_HW; do
+ # grep without -q: with pipefail an early exit could SIGPIPE printf and fail the run (see above).
+ printf '%s\n' "$PKGS" | grep -xF "$p" >/dev/null || continue
+ echo "Skipping $p: it asks for android.permission.TV_INPUT_HARDWARE, so it is the software layer" >&2
+ echo "under the HDMI/AV/tuner inputs. With it disabled those inputs still notice the cable but stay" >&2
+ echo "black (\"No Signal\"), and re-enabling the pieces in batches does not restore the picture: a" >&2
+ echo "full restore plus a cold boot does (#321). Set TVINPUT_ALLOW=1 to disable it." >&2
+ PKGS="$(printf '%s\n' "$PKGS" | grep -vxF "$p" || true)"
+ done
+ else
+ # Do not skip anything on a guess, but say the check did not run: a black input is easy to blame on
+ # the cable, and the ports are the one thing the owner cannot test from here.
+ echo "Could not check which listed packages drive the HDMI/AV inputs (tv.py failed). Check every" >&2
+ echo "input after this run: a port that notices the cable and stays black means one of them was" >&2
+ echo "disabled, and only a full restore and a cold boot bring it back (#321)." >&2
+ fi
+fi
+
# The state undo needs goes to disk before the first command reaches the TV, and a failed save stops
# here with the TV untouched: a record written afterwards would describe a TV that is already broken,
# and a retry must not overwrite the one taken before the first change (#239).