Skip to content

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

Keep a usable Home when the default launcher query is inconclusive

am-tmac2fcc058a298f
diff --git a/tests/test_home_guard.py b/tests/test_home_guard.py
index 5a2218974..7f4ff13a4 100644
--- a/tests/test_home_guard.py
+++ b/tests/test_home_guard.py
@@ -46,6 +46,10 @@ def save():
json.dump(state, f)
+class Fail(Exception):
+ pass
+
+
def answer(command):
if command == "pm list packages":
return "".join(f"package:{name}\n" for name in state["installed"])
@@ -53,11 +57,11 @@ def answer(command):
return "".join(f"package:{name}\n" for name in state["disabled"])
if command.startswith("cmd package query-activities"):
if state["claims_fail"]:
- sys.exit(1)
+ raise Fail()
return "".join(f" {c if '/' in c else c + '/.MainActivity'}\n" for c in state["claims"])
if command.startswith("cmd package resolve-activity"):
if state["home_fails"]:
- sys.exit(1)
+ raise Fail()
return (state["home"] or "") + "\n"
if command.startswith("dumpsys activity"):
return state.get("home_process", "")
@@ -68,7 +72,11 @@ def answer(command):
class Device:
def shell(self, command, read_timeout_s=None):
- return answer(command)
+ # recovery.py's own queries: a failing one answers nothing, as adb_shell would on a bad reply.
+ try:
+ return answer(command)
+ except Fail:
+ return ""
def close(self):
pass
@@ -88,7 +96,12 @@ def main():
state["commands"].append(line)
save()
return 0
- print(answer(args[-1]), end="")
+ try:
+ print(answer(args[-1]), end="")
+ except Fail:
+ # Exercise a failed query that emitted partial stdout before the transport failed.
+ print(state.get("failed_output", ""), end="")
+ return 1 # tools/tv.py exits non-zero when the TV does not answer
return 0
@@ -292,6 +305,152 @@ class HomeGuardTest(unittest.TestCase):
self.assertIn(f"pm disable-user --user 0 {STOCK}", sent)
self.assertNotIn("Skipping", self.output(result))
+
+ # Review M1: Android's chooser ("android") or a failed/empty resolve used to skip the guard
+ # entirely, so a list holding the only launcher went through. Without a known owner the guard now
+ # falls back to the HOME claimants, and without those to the known stock launchers.
+ def test_chooser_answer_still_keeps_the_only_launcher(self):
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="android", claims=[STOCK, "com.android.tv.settings"])
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent,
+ "the chooser answer must not switch the Home guard off")
+ self.assertIn(f"Skipping {STOCK}", self.output(result))
+ self.assertIn("pm disable-user --user 0 com.example.before", sent)
+
+ def test_chooser_answer_is_reported_as_the_chooser(self):
+ # The TV did answer: Home has no default. Saying neither query named one is wrong here.
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="android", claims=[STOCK, CUSTOM])
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ out = " ".join(self.output(result).split())
+ self.assertIn("Home has no default on this TV", out)
+ self.assertNotIn("named one", out)
+
+ def test_failed_resolve_still_keeps_the_only_launcher(self):
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home_fails=True)
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+ self.assertIn(f"Skipping {STOCK}", self.output(result))
+
+ def test_failed_resolve_discards_partial_output(self):
+ self.tv(home_fails=True, failed_output="com.example.partial/.Home\n")
+ result, sent = self.disable("sh")
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+ self.assertIn("Skipping", result.stderr)
+
+ def test_unrecognised_resolve_answer_uses_claimants(self):
+ for entry in self.entries():
+ self.tv(home="No activity found")
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+
+ def test_empty_resolve_still_keeps_the_only_launcher(self):
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="")
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+
+ def test_chooser_with_an_unlisted_launcher_lets_the_list_through(self):
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(installed=["com.example.before", "com.example.after", STOCK, CUSTOM],
+ home="android", claims=[STOCK, CUSTOM])
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertIn(f"pm disable-user --user 0 {STOCK}", sent)
+ self.assertNotIn("Skipping", self.output(result))
+
+ def test_chooser_with_only_a_vendor_fallback_keeps_the_launcher(self):
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="android", claims=[STOCK, "com.tcl.keycustomfunctionservice/.FallbackHome"])
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+
+ def test_no_owner_and_no_claimants_keeps_known_launchers(self):
+ # Neither query answers: skip every listed known stock launcher, fail-closed, and say why.
+ self.write_list(["com.example.before", STOCK, "com.mitv.tvhome", "com.example.after"])
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home_fails=True, claims_fail=True)
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+ self.assertNotIn("pm disable-user --user 0 com.mitv.tvhome", sent)
+ self.assertIn("pm disable-user --user 0 com.example.before", sent)
+ self.assertIn("could not", self.output(result).lower())
+
+ def test_chooser_does_not_count_a_factory_test_app_as_another_home(self):
+ # The factory-test exclusion lives in the shared claimant filter, so it holds without an owner
+ # too: Android's chooser plus a Mi TV 4A FactoryLauncher must not let the only launcher go.
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="android", claims=["com.duokan.factorytest/.FactoryLauncher", STOCK])
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent,
+ "a factory test app is not a launcher to fall back on")
+ self.assertIn(f"Skipping {STOCK}", self.output(result))
+
+ def test_no_cmd_and_no_home_process_falls_back_to_claimants(self):
+ # #795 meets the fail-closed path: resolve-activity is missing and dumpsys has no Home process
+ # (mHomeProcess: null), so no owner is known. The guard still keeps the only launcher, and the
+ # run still says it could not name Home.
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="/system/bin/sh: cmd: not found", home_process=" mHomeProcess: null\n")
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+ self.assertIn(f"Skipping {STOCK}", self.output(result))
+ self.assertIn("Could not tell which app owns Home", self.output(result))
+ self.assertIn("pm disable-user --user 0 com.example.before", sent)
+
+ def test_no_cmd_and_no_home_process_lets_an_unlisted_launcher_through(self):
+ # Same blind spot, but another launcher stays enabled: the stock one in the list may go.
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(installed=["com.example.before", "com.example.after", STOCK, CUSTOM],
+ home="/system/bin/sh: cmd: not found", home_process="",
+ claims=[STOCK, CUSTOM])
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertIn(f"pm disable-user --user 0 {STOCK}", sent)
+ self.assertNotIn("Skipping", self.output(result))
+
+ def test_dumpsys_owner_with_unlistable_claimants_keeps_the_owner(self):
+ # dumpsys names the owner but the claimant query fails: the owner is the only Home known.
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="/system/bin/sh: cmd: not found", claims_fail=True,
+ home_process=" mHomeProcess: ProcessRecord{52f4a5c8 2f4b:" + STOCK + "/u0a56}")
+ result, sent = self.disable(entry)
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertNotIn(f"pm disable-user --user 0 {STOCK}", sent)
+ self.assertIn(f"Skipping {STOCK}", self.output(result))
+ self.assertNotIn("Could not tell which app owns Home", self.output(result))
+
+ def test_force_overrides_the_chooser_fallback(self):
+ for entry in self.entries():
+ with self.subTest(entry=entry):
+ self.tv(home="android", claims_fail=True)
+ result, sent = self.disable(entry, {"FORCE_HOME": "1"})
+ self.assertEqual(result.returncode, 0, self.output(result))
+ self.assertIn(f"pm disable-user --user 0 {STOCK}", sent)
+
def test_the_skipped_launcher_stays_in_the_recovery_record(self):
self.tv()
result, sent = self.disable("sh")
diff --git a/tools/disable.ps1 b/tools/disable.ps1
index 796890736..5d5594767 100644
--- a/tools/disable.ps1
+++ b/tools/disable.ps1
@@ -48,46 +48,88 @@ $ErrorActionPreference = 'Stop'
# Home guard (#239), the same check as disable.sh: a list that disables the app owning Home leaves the
# TV with nothing to start Home with. Skip the owner while nothing else on the TV can answer HOME;
-# `$env:FORCE_HOME=1` disables it anyway, for a TV reachable another way.
-if (-not $env:FORCE_HOME) {
+# `$env:FORCE_HOME=1` disables it anyway, for a TV reachable another way. With no single owner (Android's
+# chooser "android", or a failed/empty answer) the guard fails closed instead of being skipped.
+$tvPy = Join-Path $repo 'tools\tv.py'
+$claimQuery = 'cmd package query-activities --brief -a android.intent.action.MAIN -c android.intent.category.HOME'
+# The setup wizard and the settings apps answer HOME too, but they are not launchers (disable.sh).
+$notLaunchers = @('com.google.android.tungsten.setupwraith', 'com.android.tv.settings', 'com.android.settings', 'com.android.provision')
+$knownLaunchers = @('com.google.android.tvlauncher', 'com.google.android.apps.tv.launcherx', 'com.google.android.leanbacklauncher', 'com.mitv.tvhome')
+# Usable HOME claimants the TV can start now; Ok is false when the TV could not list any.
+function Read-HomeLaunchers {
+ # Windows PowerShell turns redirected stderr into an error that 'Stop' would throw (see getprop).
+ $ErrorActionPreference = 'Continue'
+ $lines = @(& $py -I $tvPy $claimQuery 2>$null)
+ $answered = $LASTEXITCODE -eq 0
+ # Keep the class: a vendor fallback is named FallbackHome and is not a usable launcher
+ # (TCL Android TV sets register com.tcl.keycustomfunctionservice/.FallbackHome).
+ # Trim first: a trailing CR from the TV would stop the `$` anchor from matching any line.
+ $parsed = @($lines | ForEach-Object {
+ if ("$_".Trim() -match '^([A-Za-z0-9_.]+)/([A-Za-z0-9_.$]+)$') { [pscustomobject]@{ Pkg = $Matches[1]; Cls = $Matches[2] } }
+ })
+ # Nor is a factory-test app (com.duokan.factorytest/.FactoryLauncher answers HOME on a Mi TV 4A).
+ $usable = @($parsed | Where-Object { $_.Cls -notmatch 'FallbackHome$' -and $notLaunchers -notcontains $_.Pkg -and $_.Pkg -notmatch 'factorytest|factory_test|factorymode' } |
+ 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) {
$homeQuery = 'cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME'
- $homeLine = @(& $py -I (Join-Path $repo 'tools\tv.py') $homeQuery) |
- Where-Object { $_ -match '\S' } | Select-Object -Last 1
+ $ErrorActionPreference = 'Continue'
+ $homeLine = @(& $py -I $tvPy $homeQuery 2>$null) | Where-Object { $_ -match '\S' } | Select-Object -Last 1
+ if ($LASTEXITCODE -ne 0) { $homeLine = '' }
+ $ErrorActionPreference = 'Stop'
$homePkg = "$homeLine".Trim() -split '/' | Select-Object -First 1
+ # "android" is the chooser: the TV did answer, Home just has no default. Kept for the warning below,
+ # which runs after $homePkg is cleared.
+ $homeChooser = $homePkg -eq 'android'
# `cmd` arrived with Android 7 (Nougat): on an older set this query answers nothing, or the shell's
# own "cmd: not found", and the guard was skipped without a word (disable.sh, #795). `dumpsys
# activity` prints the package holding Home on those sets: mHomeProcess: ProcessRecord{... pkg/u0a56}
- $named = $homePkg -and ($homePkg -match '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$')
- if (-not $named) {
- $fallback = @(& $py -I (Join-Path $repo 'tools\tv.py') 'dumpsys activity' | ForEach-Object {
+ $pkgShape = '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$'
+ if (-not ($homePkg -and ($homePkg -match $pkgShape))) {
+ $ErrorActionPreference = 'Continue'
+ $fallback = @(& $py -I $tvPy 'dumpsys activity' 2>$null | ForEach-Object {
$line = "$_".Trim()
if ($line -match 'mHomeProcess:.*[: ]([A-Za-z0-9_.]+)/[A-Za-z0-9_.]+') { $Matches[1] }
} | Select-Object -First 1)
- $homePkg = if ($fallback.Count -gt 0) { "$($fallback[0])" } else { '' }
+ $ErrorActionPreference = 'Stop'
+ $homePkg = if ($fallback.Count -gt 0 -and "$($fallback[0])" -match $pkgShape) { "$($fallback[0])" } else { '' }
}
- if ($homePkg -and ($pkgs -contains $homePkg)) {
- # Every package with a HOME activity the TV can start now; a disabled one does not answer.
- $claimQuery = 'cmd package query-activities --brief -a android.intent.action.MAIN -c android.intent.category.HOME'
- $claims = @(& $py -I (Join-Path $repo 'tools\tv.py') $claimQuery | ForEach-Object {
- # Keep the class: a vendor fallback is named FallbackHome and is not a usable launcher
- # (TCL Android TV sets register com.tcl.keycustomfunctionservice/.FallbackHome).
- # Trim first: a trailing CR from the TV would stop the `$` anchor from matching any line.
- if ("$_".Trim() -match '^([A-Za-z0-9_.]+)/([A-Za-z0-9_.$]+)$') {
- if ($Matches[2] -notmatch 'FallbackHome$') { $Matches[1] }
+ if ($homePkg) {
+ if ($pkgs -contains $homePkg) {
+ $found = Read-HomeLaunchers
+ $others = @(@($found.Launchers) | Where-Object { $_ -ne $homePkg -and $pkgs -notcontains $_ })
+ if (-not $found.Ok -or $others.Count -eq 0) {
+ Write-Warning "Skipping ${homePkg}: it owns Home, and no other app that can answer Home is installed and staying enabled. Make the new launcher Home first (launcher\build-and-install.ps1), then run this list again to disable the old one; or set `$env:FORCE_HOME=1 if you can still reach the TV."
+ $pkgs = @($pkgs | Where-Object { $_ -ne $homePkg })
+ }
+ }
+ } else {
+ # No single owner: Android's chooser ("android", launchers tie), or neither query named one.
+ # Fail closed: judge by the HOME claimants, and without those keep the known stock launchers.
+ $found = Read-HomeLaunchers
+ if ($found.Ok) {
+ $listed = @(@($found.Launchers) | Where-Object { $pkgs -contains $_ })
+ $others = @(@($found.Launchers) | Where-Object { $pkgs -notcontains $_ })
+ if ($listed.Count -gt 0 -and $others.Count -eq 0) {
+ foreach ($p in $listed) {
+ Write-Warning "Skipping ${p}: Home has no default, and once the list is applied no other app could answer Home. Make the new launcher Home first, or set `$env:FORCE_HOME=1."
+ $pkgs = @($pkgs | Where-Object { $_ -ne $p })
+ }
+ }
+ } else {
+ foreach ($p in $knownLaunchers) {
+ if ($pkgs -contains $p) {
+ Write-Warning "Skipping ${p}: could not ask the TV which apps answer Home, and it is a stock launcher. Run again once the TV answers, or set `$env:FORCE_HOME=1 if you can still reach the TV."
+ $pkgs = @($pkgs | Where-Object { $_ -ne $p })
+ }
}
- } | Sort-Object -Unique)
- # The setup wizard and the settings apps answer HOME too, but they are not launchers (disable.sh).
- $notLaunchers = @('com.google.android.tungsten.setupwraith', 'com.android.tv.settings', 'com.android.settings', 'com.android.provision')
- # Nor is a factory-test app (com.duokan.factorytest/.FactoryLauncher answers HOME on a Mi TV 4A).
- $others = @($claims | Where-Object { $_ -ne $homePkg -and $pkgs -notcontains $_ -and $notLaunchers -notcontains $_ -and $_ -notmatch 'factorytest|factory_test|factorymode' })
- if ($others.Count -eq 0) {
- Write-Warning "Skipping ${homePkg}: it owns Home, and no other app that can answer Home is installed and staying enabled. Make the new launcher Home first (launcher\build-and-install.ps1), then run this list again to disable the old one; or set `$env:FORCE_HOME=1 if you can still reach the TV."
- $pkgs = @($pkgs | Where-Object { $_ -ne $homePkg })
}
}
- # Neither query named Home, so the guard could not run. Say so (disable.sh, #795).
+ # Neither query named Home, so the guard could only go by the apps that answer Home. Say so (disable.sh, #795).
if (-not $homePkg -and $pkgs.Count -gt 0) {
- Write-Warning 'Could not tell which app owns Home on this TV, so the Home guard did not run: neither "cmd package resolve-activity" (added in Android 7) nor "dumpsys activity" named one. Check by hand that the list does not hold your launcher before running it: the replacement launcher needs Android 8.0 (minSdk 26), so on an older set the stock launcher is the only Home there is, and a set with no recovery has no way back (#795).'
+ $why = if ($homeChooser) { 'Home has no default on this TV (resolve-activity answered "android", the chooser), so the Home guard went by the apps that answer Home instead.' } else { 'Could not tell which app owns Home on this TV, so the Home guard went by the apps that answer Home instead: neither "cmd package resolve-activity" (added in Android 7) nor "dumpsys activity" named one.' }
+ Write-Warning "$why Check by hand that the list does not hold your launcher before running it: the replacement launcher needs Android 8.0 (minSdk 26), so on an older set the stock launcher is the only Home there is, and a set with no recovery has no way back (#795)."
}
}
# Same as disable.sh (#321): a package that asks for android.permission.TV_INPUT_HARDWARE is the software
diff --git a/tools/disable.sh b/tools/disable.sh
index 7a978817a..a12c664fa 100755
--- a/tools/disable.sh
+++ b/tools/disable.sh
@@ -57,54 +57,104 @@ ADB_SECURE="$("$PY" -I tools/tv.py "getprop ro.adb.secure" 2>/dev/null | tr -d '
# next time Home is pressed, and on Google TV the setup wizard can take the category instead and
# crash-loop system_server (docs/TROUBLESHOOTING.md). Skip the owner while nothing else on the TV can
# answer HOME; FORCE_HOME=1 disables it anyway, for a TV reachable another way.
-# The extra query is worth it only when the list holds Home's own package, which is how a list that
-# leaves the TV without a launcher looks before anything is disabled.
HOME_QUERY='cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME'
-HOME_PKG=$("$PY" -I tools/tv.py "$HOME_QUERY" 2>/dev/null | tail -n 1 | tr -d '\r' | cut -d/ -f1 || true)
-# `cmd` arrived with Android 7 (Nougat): on an older set the query answers nothing, or the shell's own
-# "cmd: not found", and the guard below was skipped without a word - which is how a list disabled the
-# only launcher on a KitKat TV that had no replacement to hand Home over to (#795). Ask the framework
-# instead, which has printed the package holding Home since 4.x:
-# mHomeProcess: ProcessRecord{52f4a5c8 2f4b:com.example.launcher/u0a56}
-# Only a package-shaped answer counts; on a set where Home has not started yet the line says null.
-# grep -c, not -q: under pipefail an early exit could SIGPIPE printf and fail open (see above).
-HOME_SHAPE=$(printf '%s\n' "$HOME_PKG" | grep -cE '^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$' || true)
-if [ -n "$PKGS" ] && [ "${HOME_SHAPE:-0}" -eq 0 ]; then
- HOME_PKG=$("$PY" -I tools/tv.py 'dumpsys activity' 2>/dev/null | tr -d '\r' \
- | sed -nE 's/.*mHomeProcess:.*[: ]([A-Za-z0-9_.]+)\/[A-Za-z0-9_.]+.*/\1/p' | head -n 1 || true)
-fi
-# grep without -q: an early exit could SIGPIPE printf and fail open under pipefail (see above).
-if [ -n "$HOME_PKG" ] && printf '%s\n' "$PKGS" | grep -xF "$HOME_PKG" >/dev/null && [ -z "${FORCE_HOME:-}" ]; then
- # Every package with a HOME activity the TV can start now: disabled packages do not answer, so this
- # is "what could take Home if the list went through".
- CLAIMS=$("$PY" -I tools/tv.py 'cmd package query-activities --brief -a android.intent.action.MAIN -c android.intent.category.HOME' 2>/dev/null \
- | tr -d '\r' | grep -oE '^ *[A-Za-z0-9_.]+/[A-Za-z0-9_.$]+' | sed -E 's/^ +//' | sort -u || true)
- # Nothing left to answer Home once the list is applied, or the TV could not say who does: either
- # way the owner is the only Home this run knows about, so it stays enabled.
- # The setup wizard and the settings apps answer HOME too, but they are not launchers: on Google TV,
- # setupwraith taking Home is the crash loop this guard exists to prevent. They don't count.
- # A vendor's own fallback does not count either: TCL Android TV sets register
- # com.tcl.keycustomfunctionservice/.FallbackHome (and com.android.tv.settings/.system.FallbackHome),
- # a placeholder the firmware shows when no launcher is enabled, not a home the viewer can use.
- # Nor does a factory-test app: on a Xiaomi Mi TV 4A com.duokan.factorytest/.FactoryLauncher answers
- # HOME, and handing Home to it leaves the viewer on the factory test screen.
- OTHERS=$(printf '%s\n' "$CLAIMS" | grep -vE '/[^ ]*FallbackHome$' | cut -d/ -f1 | sort -u \
+CLAIM_QUERY='cmd package query-activities --brief -a android.intent.action.MAIN -c android.intent.category.HOME'
+# Launchers the install script knows as stock Home; the last resort when the TV cannot say who answers HOME.
+KNOWN_LAUNCHERS="com.google.android.tvlauncher com.google.android.apps.tv.launcherx com.google.android.leanbacklauncher com.mitv.tvhome"
+PKG_RE='^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+$'
+drop_pkg() { PKGS="$(printf '%s\n' "$PKGS" | grep -vxF "$1" || true)"; }
+# Every package with a usable HOME activity the TV can start now (disabled packages do not answer, so
+# this is "what could take Home if the list went through"); fails when the TV answers nothing.
+# The setup wizard and the settings apps answer HOME too, but they are not launchers: on Google TV,
+# setupwraith taking Home is the crash loop this guard exists to prevent. They don't count.
+# A vendor's own fallback does not count either: TCL Android TV sets register
+# com.tcl.keycustomfunctionservice/.FallbackHome (and com.android.tv.settings/.system.FallbackHome),
+# a placeholder the firmware shows when no launcher is enabled, not a home the viewer can use.
+# Nor does a factory-test app: on a Xiaomi Mi TV 4A com.duokan.factorytest/.FactoryLauncher answers
+# HOME, and handing Home to it leaves the viewer on the factory test screen.
+home_launchers() {
+ local raw claims
+ raw=$("$PY" -I tools/tv.py "$CLAIM_QUERY" 2>/dev/null) || return 1
+ claims=$(printf '%s\n' "$raw" | tr -d '\r' | grep -oE '^ *[A-Za-z0-9_.]+/[A-Za-z0-9_.$]+' | sed -E 's/^ +//' | sort -u || true)
+ [ -n "$claims" ] || return 1
+ printf '%s\n' "$claims" | grep -vE '/[^ ]*FallbackHome$' | cut -d/ -f1 | sort -u \
| grep -viE 'factorytest|factory_test|factorymode' \
- | grep -vxF "$HOME_PKG" | grep -vxF "$PKGS" \
- | grep -vxF -e com.google.android.tungsten.setupwraith -e com.android.tv.settings -e com.android.settings -e com.android.provision || true)
- if [ -z "$OTHERS" ]; then
- echo "Skipping $HOME_PKG: it owns Home, and no other app that can answer Home is installed and" >&2
- echo "staying enabled. Make the new launcher Home first (launcher/build-and-install.sh), then run" >&2
- echo "this list again to disable the old one; or set FORCE_HOME=1 if you can still reach the TV." >&2
- PKGS="$(printf '%s\n' "$PKGS" | grep -vxF "$HOME_PKG" || true)"
+ | grep -vxF -e com.google.android.tungsten.setupwraith -e com.android.tv.settings -e com.android.settings -e com.android.provision || true
+}
+HOME_PKG=""
+HOME_CHOOSER=""
+if [ -z "${FORCE_HOME:-}" ] && [ -n "$PKGS" ]; then
+ HOME_PKG=$("$PY" -I tools/tv.py "$HOME_QUERY" 2>/dev/null | tail -n 1 | tr -d '\r' | cut -d/ -f1) || HOME_PKG=""
+ # "android" is the chooser: the TV did answer, Home just has no default. Remember it for the message
+ # below, which runs after HOME_PKG is cleared.
+ [ "$HOME_PKG" = "android" ] && HOME_CHOOSER=1
+ # `cmd` arrived with Android 7 (Nougat): on an older set the query answers nothing, or the shell's own
+ # "cmd: not found", and the guard below was skipped without a word - which is how a list disabled the
+ # only launcher on a KitKat TV that had no replacement to hand Home over to (#795). Ask the framework
+ # instead, which has printed the package holding Home since 4.x:
+ # mHomeProcess: ProcessRecord{52f4a5c8 2f4b:com.example.launcher/u0a56}
+ # Only a package-shaped answer counts; on a set where Home has not started yet the line says null.
+ # grep -c, not -q: under pipefail an early exit could SIGPIPE printf and fail open (see above).
+ HOME_SHAPE=$(printf '%s\n' "$HOME_PKG" | grep -cE "$PKG_RE" || true)
+ if [ "${HOME_SHAPE:-0}" -eq 0 ]; then
+ HOME_PKG=$("$PY" -I tools/tv.py 'dumpsys activity' 2>/dev/null | tr -d '\r' \
+ | sed -nE 's/.*mHomeProcess:.*[: ]([A-Za-z0-9_.]+)\/[A-Za-z0-9_.]+.*/\1/p' | head -n 1 || true)
+ HOME_SHAPE=$(printf '%s\n' "$HOME_PKG" | grep -cE "$PKG_RE" || true)
+ # Diagnostic text is not an owner; use the same fail-closed path as an empty answer.
+ [ "${HOME_SHAPE:-0}" -gt 0 ] || HOME_PKG=""
+ fi
+ if [ -n "$HOME_PKG" ]; then
+ # The extra query is worth it only when the list holds Home's own package, which is how a list
+ # that leaves the TV without a launcher looks before anything is disabled.
+ # grep without -q: an early exit could SIGPIPE printf and fail open under pipefail (see above).
+ if printf '%s\n' "$PKGS" | grep -xF "$HOME_PKG" >/dev/null; then
+ # Nothing left to answer Home once the list is applied, or the TV could not say who does: either
+ # way the owner is the only Home this run knows about, so it stays enabled.
+ LAUNCHERS=$(home_launchers || true)
+ OTHERS=$(printf '%s\n' "$LAUNCHERS" | sed '/^$/d' | grep -vxF "$HOME_PKG" | grep -vxF "$PKGS" || true)
+ if [ -z "$OTHERS" ]; then
+ echo "Skipping $HOME_PKG: it owns Home, and no other app that can answer Home is installed and" >&2
+ echo "staying enabled. Make the new launcher Home first (launcher/build-and-install.sh), then run" >&2
+ echo "this list again to disable the old one; or set FORCE_HOME=1 if you can still reach the TV." >&2
+ drop_pkg "$HOME_PKG"
+ fi
+ fi
+ else
+ # No single owner: Android's chooser ("android", launchers tie), or neither query named one.
+ # Skipping the guard here would let a list take every launcher, so fail closed: judge by the HOME
+ # claimants, and if the TV cannot list those either, keep every known stock launcher.
+ if LAUNCHERS=$(home_launchers); then
+ LISTED=$(printf '%s\n' "$LAUNCHERS" | sed '/^$/d' | grep -xF "$PKGS" || true)
+ OTHERS=$(printf '%s\n' "$LAUNCHERS" | sed '/^$/d' | grep -vxF "$PKGS" || true)
+ if [ -n "$LISTED" ] && [ -z "$OTHERS" ]; then
+ for p in $LISTED; do
+ echo "Skipping $p: Home has no default, and once the list is applied no other app could" >&2
+ echo "answer Home. Make the new launcher Home first, or set FORCE_HOME=1." >&2
+ drop_pkg "$p"
+ done
+ fi
+ else
+ for p in $KNOWN_LAUNCHERS; do
+ printf '%s\n' "$PKGS" | grep -xF "$p" >/dev/null || continue
+ echo "Skipping $p: could not ask the TV which apps answer Home, and it is a stock launcher." >&2
+ echo "Run again once the TV answers, or set FORCE_HOME=1 if you can still reach the TV." >&2
+ drop_pkg "$p"
+ done
+ fi
fi
fi
-# Neither query named Home, so the guard above could not run at all. Say so: a silent skip looks
-# exactly like a check that passed, and on a set too old for the launcher there is nothing to lose by
-# keeping every listed package (#795).
+# Neither query named Home, so the guard above could only go by the apps that answer Home. Say so: a
+# silent fallback looks exactly like a check that passed, and on a set too old for the launcher there
+# is nothing to lose by keeping every listed package (#795).
if [ -n "$PKGS" ] && [ -z "$HOME_PKG" ] && [ -z "${FORCE_HOME:-}" ]; then
- echo "Could not tell which app owns Home on this TV, so the Home guard did not run: neither" >&2
- echo '"cmd package resolve-activity" (added in Android 7) nor "dumpsys activity" named one.' >&2
+ if [ -n "$HOME_CHOOSER" ]; then
+ echo "Home has no default on this TV (resolve-activity answered \"android\", the chooser), so the Home" >&2
+ echo 'guard went by the apps that answer Home instead.' >&2
+ else
+ echo "Could not tell which app owns Home on this TV, so the Home guard went by the apps that answer" >&2
+ echo 'Home instead: neither "cmd package resolve-activity" (added in Android 7) nor "dumpsys activity"' >&2
+ echo 'named one.' >&2
+ fi
echo 'Check by hand that the list does not hold your launcher before running it. The replacement' >&2
echo 'launcher needs Android 8.0 (minSdk 26), so on an older set the stock launcher is the only Home' >&2
echo 'there is: disabled, it leaves nothing to start, and a set with no recovery has no way back (#795).' >&2