Skip to content

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

disable.sh: stop three safety checks passing silently (#1753)

suantea (asuan_suan)66e83196056e
diff --git a/tools/check.py b/tools/check.py
index 2519a44d6..42a0ab773 100644
--- a/tools/check.py
+++ b/tools/check.py
@@ -64,6 +64,10 @@ INPUT_PERMISSION = "android.permission.TV_INPUT_HARDWARE"
# otherwise match through its shared user's permissions, the same reason disable.sh scopes its grep.
REQUESTED_RE = re.compile(r"requested permissions:(.*?)"
r"(?:\n\s*(?:install|runtime) permissions:|\n\s*User \d+:|$)", re.S)
+# Lollipop's dumpsys package prints no "requested permissions:" block, only the package's own
+# "grantedPermissions:" (#1753): the fallback disable.sh reads when the requested block is absent.
+GRANTED_RE = re.compile(r"grantedPermissions:(.*?)"
+ r"(?:\n\s*(?:install|runtime) permissions:|\n\s*User \d+:|$)", re.S)
HOME_QUERY = ("cmd package resolve-activity --brief -a android.intent.action.MAIN "
"-c android.intent.category.HOME")
CLAIMS_QUERY = ("cmd package query-activities --brief -a android.intent.action.MAIN "
@@ -166,9 +170,22 @@ def setupwraith_owns_wifi(dev):
def asks_for_input_hardware(dev, name):
- """Whether the package itself asks for TV_INPUT_HARDWARE, i.e. it drives the HDMI/AV inputs."""
- block = REQUESTED_RE.search(recovery.shell(dev, f"dumpsys package {name}"))
- return bool(block and INPUT_PERMISSION in block.group(1))
+ """Whether the package itself asks for TV_INPUT_HARDWARE, i.e. it drives the HDMI/AV inputs.
+
+ True or False from a dump that can be read; None when the dump shows neither a requested nor a
+ granted permission block, so the check could not run — Android 5.x prints neither (#1753)."""
+ dump = recovery.shell(dev, f"dumpsys package {name}")
+ block = REQUESTED_RE.search(dump)
+ if block:
+ return INPUT_PERMISSION in block.group(1)
+ granted = GRANTED_RE.search(dump)
+ if granted:
+ return INPUT_PERMISSION in granted.group(1)
+ if "Service Resolver Table:" in dump:
+ # Lollipop's per-package dump has no permission block but lists what serves the input
+ # service: the decisive check the requested-permission grep stands in for.
+ return "android.media.tv.TvInputService" in dump
+ return None
def handler_apps(dev):
@@ -205,7 +222,15 @@ def safety_checks(dev, names):
for name in names:
if any(name == skipped_name for skipped_name, _ in skipped):
continue
- if asks_for_input_hardware(dev, name):
+ hw = asks_for_input_hardware(dev, name)
+ if hw is None:
+ # Fail closed, the same as disable.sh: a dump this firmware cannot be read from
+ # keeps the package on (#1753), and the owner is told what it costs.
+ skipped.append((name, "could not check whether it drives the HDMI/AV inputs (the "
+ "TV's dumpsys shows neither permission block, as Android 5.x "
+ "prints neither): it stays enabled; set TVINPUT_ALLOW=1 to "
+ "disable it once every input still works after a reboot"))
+ elif hw:
skipped.append((name, "it asks for android.permission.TV_INPUT_HARDWARE, so it is "
"the software layer under the HDMI/AV/tuner inputs: with it "
"disabled those ports stay black and only a full restore and "
@@ -213,6 +238,12 @@ def safety_checks(dev, names):
"it)"))
if not os.environ.get("HANDLERS_ALLOW") and any(n not in {k for k, _ in skipped} for n in names):
inputs, settings = handler_apps(dev)
+ if not settings and not inputs:
+ # A working query always has some app answering android.settings.SETTINGS; an empty
+ # answer means the firmware has no `cmd`, and the guard below would pass in silence.
+ print(" could not check which listed packages open the TV's inputs and Settings: the "
+ "query answered with nothing (this firmware may have no 'cmd'); open an HDMI "
+ "input and Settings from the launcher after --fix")
for name in names:
if any(name == skipped_name for skipped_name, _ in skipped):
continue
diff --git a/tools/disable.ps1 b/tools/disable.ps1
index 8a3512f45..ef579aecc 100644
--- a/tools/disable.ps1
+++ b/tools/disable.ps1
@@ -17,6 +17,23 @@ if (-not (Test-Path $py)) { throw 'run: python -m venv tools\venv; tools\venv\Sc
if (-not $TvHost) { throw 'pass -TvHost <tv ip> or set $env:TV_HOST' }
$env:TV_HOST = $TvHost
+# The four guard overrides are read once here and then removed from the process environment, so an
+# override set the way the README and the scripts' own messages suggest covers exactly this run
+# (#1845): a value meant for one TV and one list must not silently skip the setupwraith, Home,
+# input-driver and input/Settings guards of every later run in the same window, including
+# `check.py --fix`. TV_HOST stays: it is per window by design.
+$forceSetupwraith = [bool]$env:FORCE_SETUPWRAITH
+$forceHome = [bool]$env:FORCE_HOME
+$tvinputAllow = [bool]$env:TVINPUT_ALLOW
+$handlersAllow = [bool]$env:HANDLERS_ALLOW
+foreach ($name in 'FORCE_SETUPWRAITH', 'FORCE_HOME', 'TVINPUT_ALLOW', 'HANDLERS_ALLOW') {
+ Remove-Item -Path "Env:$name" -ErrorAction SilentlyContinue
+}
+if ($forceSetupwraith) { Write-Warning 'FORCE_SETUPWRAITH is set for this run only: setupwraith is disabled even if it owns the saved Wi-Fi network.' }
+if ($forceHome) { Write-Warning 'FORCE_HOME is set for this run only: the Home guard is skipped for this run.' }
+if ($tvinputAllow) { Write-Warning 'TVINPUT_ALLOW is set for this run only: input-driver packages are disabled even if they ask for TV_INPUT_HARDWARE.' }
+if ($handlersAllow) { Write-Warning 'HANDLERS_ALLOW is set for this run only: input and Settings handlers are disabled even if the launcher needs them.' }
+
$pkgs = Get-Content $List | ForEach-Object { ($_ -replace '#.*$', '').Trim() } | Where-Object { $_ } | ForEach-Object { ($_ -split '\s+')[0] }
# Lists come from contributors and each name is pasted into a shell command on the TV (#165).
$bad = @($pkgs | Where-Object { $_ -notmatch '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$' })
@@ -41,7 +58,7 @@ if ($core.Count -gt 0) {
# A Wi-Fi network saved during the first-run wizard belongs to setupwraith, and Android deletes it when
# setupwraith is disabled. Skip it while it owns the saved network; `$env:FORCE_SETUPWRAITH=1` overrides.
$setup = 'com.google.android.tungsten.setupwraith'
-if ($pkgs -contains $setup -and -not $env:FORCE_SETUPWRAITH) {
+if ($pkgs -contains $setup -and -not $forceSetupwraith) {
$wifi = (& $py -I (Join-Path $repo 'tools\tv.py') 'dumpsys wifi') -join "`n"
# Be safe: if we cannot read the owners (tv.py failed, timed out, or showed no network at all),
# do not disable setupwraith, so the TV cannot lose the network it is on.
@@ -87,7 +104,7 @@ function Read-HomeLaunchers {
ForEach-Object { $_.Pkg } | Sort-Object -Unique)
[pscustomobject]@{ Ok = ($answered -and $parsed.Count -gt 0); Launchers = $usable }
}
-if (-not $env:FORCE_HOME -and $pkgs.Count -gt 0) {
+if (-not $forceHome -and $pkgs.Count -gt 0) {
$homeQuery = 'cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME'
$ErrorActionPreference = 'Continue'
$homeLine = @(& $py -I $tvPy $homeQuery 2>$null) | Where-Object { $_ -match '\S' } | Select-Object -Last 1
@@ -151,10 +168,15 @@ if (-not $env:FORCE_HOME -and $pkgs.Count -gt 0) {
# 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) {
+if (-not $tvinputAllow -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 | sed -n -E ''/requested permissions:/,/(install|runtime) permissions:|User [0-9]+:/p'' | grep -q TV_INPUT_HARDWARE && echo $p; done; true'
+ # Per package: the "requested permissions" block (Android 6.0+); on a dump with no such block —
+ # Lollipop prints only "grantedPermissions:" — the package's own granted block; then its Service
+ # Resolver Table, which Lollipop's per-package dump prints and which names what serves
+ # android.media.tv.TvInputService. "Unable to find" is a package not on this TV. A dump with
+ # neither block nor error (NOCHK) is one the guard cannot read: the package is kept on (#1753).
+ $loop = 'for p in ' + ($pkgs -join ' ') + '; do d=$(dumpsys package $p 2>/dev/null); case $d in *"requested permissions:"*) printf ''%s\n'' "$d" | sed -n -E ''/requested permissions:/,/(install|runtime) permissions:|User [0-9]+:/p'' | grep -q TV_INPUT_HARDWARE && echo $p;; *"grantedPermissions:"*) printf ''%s\n'' "$d" | sed -n -E ''/grantedPermissions:/,/(install|runtime) permissions:|User [0-9]+:/p'' | grep -q TV_INPUT_HARDWARE && echo $p;; *"Service Resolver Table:"*) printf ''%s\n'' "$d" | grep -q ''android.media.tv.TvInputService'' && echo $p;; *"Unable to find"*) :;; *) echo "NOCHK $p";; esac; 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)
@@ -163,7 +185,14 @@ if (-not $env:TVINPUT_ALLOW -and $pkgs.Count -gt 0) {
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 { $_ })) {
+ foreach ($p in ($tvhw | Where-Object { $_ -match '^NOCHK ' })) {
+ $p = ($p -replace '^NOCHK ', '').Trim()
+ if ($pkgs -contains $p) {
+ Write-Warning "Skipping ${p}: the TV's dumpsys shows neither a requested nor a granted permission block for it, so this firmware's answer cannot be read for the input-driver check (Android 5.x prints neither). It stays enabled: once every input still works after a reboot, set `$env:TVINPUT_ALLOW=1 to disable it (#321)."
+ $pkgs = @($pkgs | Where-Object { $_ -ne $p })
+ }
+ }
+ foreach ($p in ($tvhw | ForEach-Object { $_.Trim() } | Where-Object { $_ -and $_ -notmatch '^NOCHK ' })) {
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 })
@@ -173,7 +202,7 @@ if (-not $env:TVINPUT_ALLOW -and $pkgs.Count -gt 0) {
}
# Same as disable.sh: keep the apps the TV opens its HDMI inputs (VIEW on a passthrough channel) and
# Settings with, asked from the TV rather than guessed by name. `$env:HANDLERS_ALLOW=1` disables them anyway.
-if (-not $env:HANDLERS_ALLOW -and $pkgs.Count -gt 0) {
+if (-not $handlersAllow -and $pkgs.Count -gt 0) {
$q = 'cmd package query-activities --brief'
$ErrorActionPreference = 'Continue'
$handlers = @(& $py -I (Join-Path $repo 'tools\tv.py') "$q -a android.intent.action.VIEW -t vnd.android.cursor.item/channel 2>/dev/null; echo ==; $q -a android.settings.SETTINGS 2>/dev/null; true" 2>$null)
@@ -185,6 +214,17 @@ if (-not $env:HANDLERS_ALLOW -and $pkgs.Count -gt 0) {
# `@()` so an empty or one-line answer is still an array: IndexOf throws on $null.
$lines = @($handlers | ForEach-Object { $_.Trim() })
$split = [array]::IndexOf($lines, '==')
+ # A working query always has some app answering android.settings.SETTINGS. No name after the
+ # "==" line means the firmware has no `cmd` (or answered nothing), and the loop below would
+ # pass in silence (#973, #1089).
+ $settingsSeen = $false
+ for ($i = 0; $i -lt $lines.Count; $i++) {
+ if ($lines[$i] -notmatch '^([A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+)/') { continue }
+ if ($split -ge 0 -and $i -gt $split) { $settingsSeen = $true; break }
+ }
+ if (-not $settingsSeen) {
+ Write-Warning 'Could not check which listed packages open the TV''s inputs and Settings: the query answered with nothing at all (this firmware may have no ''cmd''). After this run, open an HDMI input and Settings from the launcher before you put the remote down.'
+ }
for ($i = 0; $i -lt $lines.Count; $i++) {
if ($lines[$i] -notmatch '^([A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+)/') { continue }
$p = $Matches[1]
diff --git a/tools/disable.sh b/tools/disable.sh
index d097ef056..d3c36c585 100755
--- a/tools/disable.sh
+++ b/tools/disable.sh
@@ -28,7 +28,7 @@ export MSYS_NO_PATHCONV=1
# so a list whose entries are all commented out (what CONTRIBUTING asks for while one is untested)
# read exactly like a run that died halfway.
PKGS=$(LC_ALL=C awk 'NR==1{sub(/^\357\273\277/,"")} {sub(/\r$/,""); print}' "$LIST" \
- | grep -vE '^\s*(#|$)' | sed -E 's/\s+#.*$//' | awk '{print $1}' || true)
+ | grep -vE '^\s*(#|$)' | sed -E 's/[[:space:]]+#.*$//' | awk '{print $1}' || true)
BAD=$(printf '%s\n' "$PKGS" | grep -vE '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$' || true)
if [ -n "$BAD" ]; then
{ echo "refusing $LIST: not a package name:"; printf '%s\n' "$BAD" | sed 's/^/ /'; } >&2
@@ -213,9 +213,27 @@ if [ -n "$PKGS" ] && [ -z "${TVINPUT_ALLOW:-}" ]; then
# 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.
+ # Per package, in the order that costs the least: the "requested permissions" block (Android 6.0+),
+ # then on a dump with no such block at all — Lollipop prints only "grantedPermissions:" — the
+ # package's own granted block, then its Service Resolver Table, which Lollipop's per-package dump
+ # prints and which names what serves android.media.tv.TvInputService. "Unable to find" is a package
+ # that is not on this TV, not a check that failed. A dump with neither block nor error (NOCHK) is
+ # one the guard cannot read: the package is kept off the batch below instead of passing silently.
if TV_HW="$("$PY" -I tools/tv.py \
- "for p in $NAMES; do dumpsys package \$p | sed -n -E '/requested permissions:/,/(install|runtime) permissions:|User [0-9]+:/p' | grep -q TV_INPUT_HARDWARE && echo \$p; done; true" \
+ "for p in $NAMES; do d=\$(dumpsys package \$p 2>/dev/null); case \$d in *'requested permissions:'*) printf '%s\n' \"\$d\" | sed -n -E '/requested permissions:/,/(install|runtime) permissions:|User [0-9]+:/p' | grep -q TV_INPUT_HARDWARE && echo \$p;; *'grantedPermissions:'*) printf '%s\n' \"\$d\" | sed -n -E '/grantedPermissions:/,/(install|runtime) permissions:|User [0-9]+:/p' | grep -q TV_INPUT_HARDWARE && echo \$p;; *'Service Resolver Table:'*) printf '%s\n' \"\$d\" | grep -q 'android.media.tv.TvInputService' && echo \$p;; *'Unable to find'*) :;; *) echo \"NOCHK \$p\";; esac; done; true" \
</dev/null 2>/dev/null | tr -d '\r')"; then
+ # Packages whose dump shows neither block: keep them enabled (a black input is easy to blame on
+ # the cable, and the ports are the one thing the owner cannot test from here) and say why.
+ UNCHECKED="$(printf '%s\n' "$TV_HW" | sed -n -E 's/^NOCHK ([^[:space:]]+)$/\1/p')"
+ for p in $UNCHECKED; do
+ printf '%s\n' "$PKGS" | grep -xF "$p" >/dev/null || continue
+ echo "Skipping $p: the TV's dumpsys shows neither a requested nor a granted permission block" >&2
+ echo "for it, so this firmware's answer cannot be read for the input-driver check (Android 5.x" >&2
+ echo "prints neither). It stays enabled: once every input still works after a reboot, set" >&2
+ echo "TVINPUT_ALLOW=1 to disable it (#321)." >&2
+ PKGS="$(printf '%s\n' "$PKGS" | grep -vxF "$p" || true)"
+ done
+ TV_HW="$(printf '%s\n' "$TV_HW" | grep -v '^NOCHK ' || true)"
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
@@ -255,6 +273,14 @@ if [ -n "$PKGS" ] && [ -z "${HANDLERS_ALLOW:-}" ]; then
NAME_RE='s#^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+)/.*#\1#p'
INPUT_APPS="$(printf '%s\n' "$HANDLERS" | sed '/^==$/,$d' | sed -n -E "$NAME_RE" | sort -u)"
SETTINGS_APPS="$(printf '%s\n' "$HANDLERS" | sed '1,/^==$/d' | sed -n -E "$NAME_RE" | sort -u)"
+ # A working query always has some app answering android.settings.SETTINGS. An empty answer means
+ # the firmware has no `cmd` (or answered nothing at all): both lists are empty, nothing below is
+ # skipped, and without this line the guard passes in silence (#973, #1089).
+ if [ -z "$SETTINGS_APPS" ]; then
+ echo "Could not check which listed packages open the TV's inputs and Settings: the query" >&2
+ echo "answered with nothing at all (this firmware may have no 'cmd'). After this run, open an" >&2
+ echo "HDMI input and Settings from the launcher before you put the remote down." >&2
+ fi
for p in $INPUT_APPS; do
printf '%s\n' "$PKGS" | grep -xF "$p" >/dev/null || continue
echo "Skipping $p: the TV opens its HDMI/AV inputs and channels with it, so with it disabled the" >&2