Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.
Refuse turning on apps the TV disabled, and keep raw errors out of the snackbar
jesse-agent-821e3aa8c95e3a6
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt b/adb/src/main/kotlin/app/tellyscope/adb/Actions.ktindex e55905b..23d3164 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt@@ -16,12 +16,13 @@ sealed interface ActionResult {/*** What [Actions.makeHome] did. [from] and [to] are the old and new home activities, for Undo;* [from] is null when the TV had no default home and no launcher of its own to replace. [kept]:- * the old launcher was one the user added, so it stayed on.+ * the old launcher was one the user added, so it stayed on. A [Failed.output] is one plain line;+ * [Failed.details] keeps what the TV printed, for a problem report.*/sealed interface HomeResult {data class Switched(val from: String?, val to: String, val kept: Boolean = false) : HomeResultdata class Refused(val reason: ProtectedReason) : HomeResult- data class Failed(val output: String) : HomeResult+ data class Failed(val output: String, val details: String = output) : HomeResult}/**@@ -47,6 +48,7 @@ class Actions(private val shell: AdbShell) {Action.STOP -> exec("am force-stop $pkg", Action.OPEN)Action.DISABLE -> exec("pm disable-user --user 0 $pkg", Action.ENABLE) { "disabled-user" in it }Action.ENABLE -> exec("pm enable --user 0 $pkg", Action.DISABLE) { "enabled" in it }+ .let { if (it is ActionResult.Failed && SHELL_REFUSED in it.output) ActionResult.Refused(ProtectedReason.DISABLED_BY_TV) else it }Action.OPEN -> open(pkg)Action.UNINSTALL -> uninstall(pkg)}@@ -127,15 +129,15 @@ class Actions(private val shell: AdbShell) {val off = exec("pm disable-user --user 0 $old", undo = null) { "disabled-user" in it }if (off is ActionResult.Failed) {setHome(from)- return HomeResult.Failed(off.output)+ return HomeResult.Failed("the TV wouldn't turn the old home screen off, so nothing changed", off.output)}if (checked || setHome(to)) return HomeResult.Switched(from, to)val on = exec("pm enable --user 0 $old", undo = null) { "enabled" in it }setHome(from)- return HomeResult.Failed(- if (on is ActionResult.Failed) "Home didn't open it, and the old home screen didn't turn back on: ${on.output}. Turn it back on from Apps → Built-in"- else "Home still didn't open it, so the old home screen was turned back on",- )+ if (on is ActionResult.Failed) {+ return HomeResult.Failed("Home didn't open it, and the old home screen didn't turn back on. Turn it back on from Apps → Built-in", on.output)+ }+ return HomeResult.Failed("Home still didn't open it, so the old home screen was turned back on")}/**@@ -149,7 +151,7 @@ class Actions(private val shell: AdbShell) {val from = currentHome() ?: return HomeResult.Refused(ProtectedReason.UNKNOWN_HOME)if (from.substringBefore('/') == pkg) return HomeResult.Failed("it's already the home screen")val on = exec("pm enable --user 0 $pkg", undo = null) { "enabled" in it }- if (on is ActionResult.Failed) return HomeResult.Failed(on.output)+ if (on is ActionResult.Failed) return HomeResult.Failed("the TV wouldn't turn it back on", on.output)val to = activity ?: launchers()[pkg] ?: return HomeResult.Failed("it has no home screen")if (!setHome(to)) {setHome(from)@@ -165,6 +167,9 @@ class Actions(private val shell: AdbShell) {return currentHome()?.substringBefore('/') == activity.substringBefore('/')}+ /** Apps the TV turned off itself, which [run] can't turn back on ([ProtectedReason.DISABLED_BY_TV]). */+ suspend fun disabledByTv(): Set<String> = Parsers.disabledByTv(shell.shell(Commands.ENABLED_STATES).output)+/** Packages the TV can open from a launcher icon; null when it didn't say (no `cmd` before Android 7). */suspend fun launchable(): Set<String>? =packagesIn(shell.shell(Commands.LAUNCHABLE).output).ifEmpty { null }@@ -282,5 +287,11 @@ class Actions(private val shell: AdbShell) {internal companion object {val PACKAGE = Regex("[A-Za-z][A-Za-z0-9_]*(\\.[A-Za-z0-9_]+)+|android")val ACTIVITY = Regex("(${PACKAGE.pattern})/[A-Za-z0-9_.$]+")++ /**+ * Android's SecurityException when ADB tries to turn on an app the TV disabled itself:+ * shell may only move a package between default, enabled and disabled-user.+ */+ const val SHELL_REFUSED = "Shell cannot change component state"}}diff --git a/adb/src/main/kotlin/app/tellyscope/adb/BatchedShell.kt b/adb/src/main/kotlin/app/tellyscope/adb/BatchedShell.ktindex 4e69fdd..9b3a551 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/BatchedShell.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/BatchedShell.kt@@ -21,6 +21,7 @@ class BatchedShell private constructor(Commands.HOME_ACTIVITIES,Commands.SYSTEM_PACKAGES,"pm list packages -d",+ Commands.ENABLED_STATES,Commands.USER_PACKAGES,Commands.LAUNCHABLE,"${Commands.TV_LAUNCHABLE} 2>/dev/null",diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Commands.kt b/adb/src/main/kotlin/app/tellyscope/adb/Commands.ktindex dae5b00..5bf2fc8 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Commands.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Commands.kt@@ -40,6 +40,8 @@ internal object Commands {const val PACKAGES = "pm list packages -U"/** Apps the user installed: the only ones Tellyscope may uninstall. */const val USER_PACKAGES = "pm list packages -3"+ /** Each package's `Package [name]` line and its `User 0: … enabled=N` line, for [Parsers.disabledByTv]. */+ const val ENABLED_STATES = "dumpsys package packages | grep -e '^ Package \\[' -e '^ User 0:'"/** The apps the TV came with, disabled ones included. */const val SYSTEM_PACKAGES = "pm list packages -s"const val HOME_ACTIVITY = "cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME"diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Parsers.kt b/adb/src/main/kotlin/app/tellyscope/adb/Parsers.ktindex 74a4539..9df4972 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Parsers.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Parsers.kt@@ -85,6 +85,25 @@ object Parsers {Package(name, uid, version).takeIf { name.isNotEmpty() }}.distinctBy { it.name }.toList()+ /**+ * Packages the TV disabled itself, from [Commands.ENABLED_STATES]: enabled state 2 (disabled)+ * or 4 (disabled until used). 3 is the user's own disable, which ADB can undo. Hidden system+ * packages are listed again after the installed ones, so each package's first state counts.+ */+ fun disabledByTv(dump: String): Set<String> {+ val states = mutableMapOf<String, Int>()+ var pkg: String? = null+ for (line in dump.lineSequence().map(String::trim)) {+ if (line.startsWith("Package [")) pkg = line.substringAfter('[').substringBefore(']')+ else if (line.startsWith("User 0:")) {+ val state = line.substringAfter(" enabled=", "").substringBefore(' ').toIntOrNull()+ if (pkg != null && state != null) states.putIfAbsent(pkg, state)+ pkg = null+ }+ }+ return states.filterValues { it == 2 || it == 4 }.keys+ }+/** [cpuPercent] is null from [psProcesses], which reads memory only. */data class Process(val pid: Int, val user: String, val rssKb: Long, val cpuPercent: Double?, val name: String)diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt b/adb/src/main/kotlin/app/tellyscope/adb/Protected.ktindex 6f0ae94..096031e 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt@@ -11,6 +11,12 @@ enum class ProtectedReason {/** Not installed by the user, so it can't be uninstalled; it can still be disabled. */CAME_WITH_TV,++ /**+ * The TV turned it off itself (enabled state 2 or 4, not the user's 3). Android lets ADB turn on+ * only what the user turned off, so it stays off.+ */+ DISABLED_BY_TV,}/**diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.ktindex 548f0a5..dd18edc 100644--- a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt+++ b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt@@ -365,6 +365,7 @@ class ActionsTest {sh.answer.let { prev -> sh.answer = { if (it.startsWith("pm enable")) ok("Error: refused\n") else prev(it) } }val r = Actions(sh).makeHome("com.spocky.projengmenu") as HomeResult.FailedassertTrue(r.output.endsWith("Turn it back on from Apps → Built-in"), r.output)+ assertEquals("Error: refused", r.details)}@Test fun failedDisablePutsHomeBack() = runTest {@@ -442,4 +443,45 @@ class ActionsTest {val sh = FakeShell(mapOf("cmd package" to ShellResult("", 127)))assertEquals(null, Actions(sh).launchable())}++ // What `pm enable` prints on Android 14 for a package the TV disabled (enabled=2).+ private val shellRefused = ShellResult(+ "Exception occurred while executing 'enable':\n" ++ "java.lang.SecurityException: Shell cannot change component state for com.android.nfc/null to 1\n" ++ "\tat com.android.server.pm.PackageManagerService.setEnabledSettings(PackageManagerService.java:3929)\n",+ 255,+ )++ @Test fun enablingWhatTheTvDisabledIsRefused() = runTest {+ val sh = FakeShell(mapOf("pm enable" to shellRefused))+ assertEquals(ActionResult.Refused(ProtectedReason.DISABLED_BY_TV), Actions(sh).run(Action.ENABLE, "com.android.nfc"))+ }++ @Test fun enablingWhatTheUserDisabledStillWorks() = runTest {+ val sh = FakeShell(mapOf("pm enable" to ok("Package com.foo.ads new state: enabled\n")))+ assertEquals(ActionResult.Done(Action.DISABLE), Actions(sh).run(Action.ENABLE, "com.foo.ads"))+ }++ @Test fun otherEnableFailuresStayFailed() = runTest {+ val sh = FakeShell(mapOf("pm enable" to ShellResult("Error: Unknown package: com.gone\n", 255)))+ assertEquals(ActionResult.Failed("Error: Unknown package: com.gone"), Actions(sh).run(Action.ENABLE, "com.gone"))+ }++ @Test fun disabledByTvIsStateTwoOrFourOnly() = runTest {+ val dump = """+ | Package [com.android.nfc] (3a1b2c):+ | User 0: ceDataInode=0 installed=true hidden=false suspended=false distractionFlags=0 stopped=true notLaunched=false enabled=2 instant=false virtual=false+ | Package [com.foo.ads] (4d5e6f):+ | User 0: ceDataInode=0 installed=true hidden=false suspended=false distractionFlags=0 stopped=true notLaunched=false enabled=3 instant=false virtual=false+ | Package [com.oem.lazy] (7a8b9c):+ | User 0: installed=true hidden=false stopped=false notLaunched=false enabled=4+ | Package [com.google.android.youtube.tv] (1f2e3d):+ | User 0: ceDataInode=0 installed=true hidden=false suspended=false distractionFlags=0 stopped=false notLaunched=false enabled=0 instant=false virtual=false+ | Package [com.foo.ads] (0a0b0c):+ | User 0: installed=true hidden=false stopped=false notLaunched=false enabled=2+ |""".trimMargin()+ // The second com.foo.ads is its hidden system package, listed after the installed ones.+ val sh = FakeShell(mapOf("dumpsys package packages" to ok(dump)))+ assertEquals(setOf("com.android.nfc", "com.oem.lazy"), Actions(sh).disabledByTv())+ }}diff --git a/adb/src/test/kotlin/app/tellyscope/adb/BatchedShellTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/BatchedShellTest.ktindex 1396cd7..23fa461 100644--- a/adb/src/test/kotlin/app/tellyscope/adb/BatchedShellTest.kt+++ b/adb/src/test/kotlin/app/tellyscope/adb/BatchedShellTest.kt@@ -35,7 +35,7 @@ class BatchedShellTest {@Test fun theAppListsReadsAreOneLine() = runTest {val tv = FakeShell()- tv.answer = { if (it.startsWith("{ ")) ShellResult("${sep}0\n".repeat(10), 0) else null }+ tv.answer = { if (it.startsWith("{ ")) ShellResult("${sep}0\n".repeat(11), 0) else null }val shell = BatchedShell.appList(tv)Actions(shell).homePackages()Actions(shell).homeOptions()@@ -46,6 +46,7 @@ class BatchedShellTest {shell.shell(Commands.USER_PACKAGES)shell.shell("pm list packages --show-versioncode")Actions(shell).serviceOn("a/b")+ Actions(shell).disabledByTv()assertEquals(1, tv.ran.size)}}diff --git a/app/src/main/java/app/tellyscope/data/ActionFailed.kt b/app/src/main/java/app/tellyscope/data/ActionFailed.ktnew file mode 100644index 0000000..59bec19--- /dev/null+++ b/app/src/main/java/app/tellyscope/data/ActionFailed.kt@@ -0,0 +1,5 @@+// SPDX-License-Identifier: GPL-3.0-only+package app.tellyscope.data++/** A change the TV refused. [message] is one plain line for the snackbar; [details] is what the TV printed, for a problem report. */+class ActionFailed(message: String, val details: String) : IllegalStateException(message)diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.ktindex 5f40fb5..d77d9ce 100644--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt@@ -377,6 +377,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {val homeOptions = Actions(shell).homeOptions()val system = Parsers.packages(shell.shell("pm list packages -s").output).map { it.name }.toSet()val disabled = Parsers.packages(shell.shell("pm list packages -d").output).map { it.name }.toSet()+ val byTv = Actions(shell).disabledByTv()val userInstalled = Parsers.packages(shell.shell("pm list packages -3").output).map { it.name }.toSet()// Before Android 7 there's no asking which apps have an icon; the ones the user installed do.val launchable = Actions(shell).launchable() ?: userInstalled@@ -407,6 +408,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {iconAccent = name.tileAccent,system = p.name in system,enabled = p.name !in disabled,+ disabledByTv = p.name in disabled && p.name in byTv,// Same rule as Actions.run: no known launcher, nothing can be stopped or disabled.// A disabled app stays unprotected so Enable (always allowed) still shows, even// for a built-in launcher that is off.@@ -532,7 +534,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {).also { if (back == false) throw IllegalStateException(homeNotBack(it.label)) }}is ActionResult.Refused -> throw IllegalStateException(reasonText(r.reason))- is ActionResult.Failed -> throw IllegalStateException(r.output.ifEmpty { "the TV refused" })+ is ActionResult.Failed -> throw ActionFailed(failedText(action, labelOf(packageName)), r.output.ifEmpty { "(no output)" })}}@@ -606,7 +608,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {).also { if (off == false) throw IllegalStateException(serviceNotOff(labels[added] ?: AppNames.fallback(added))) }}is HomeResult.Refused -> throw IllegalStateException(reasonText(r.reason))- is HomeResult.Failed -> throw IllegalStateException(r.output.ifEmpty { "the TV refused" })+ is HomeResult.Failed -> throw ActionFailed(r.output.ifEmpty { "the TV refused" }, r.details)}}@@ -1074,6 +1076,17 @@ class AdbDeviceSource(context: Context) : DeviceSource {ProtectedReason.UPDATES -> "the TV gets its updates through this"ProtectedReason.UNKNOWN_HOME -> "this TV didn't say which app is its home screen, so Tellyscope won't change anything"ProtectedReason.CAME_WITH_TV -> "it came with the TV, so it can only be disabled"+ ProtectedReason.DISABLED_BY_TV -> "the TV turned this off itself, so it can't be turned back on from here"+ }++ /** One line for a change the TV refused; what it printed goes in [ActionFailed.details]. */+ private fun failedText(action: Action, label: String) = when (action) {+ Action.Stop -> "the TV wouldn't stop $label"+ Action.Disable -> "the TV wouldn't turn $label off"+ Action.Enable -> "the TV wouldn't turn $label on"+ Action.Open -> "the TV wouldn't open $label"+ Action.Uninstall -> "the TV wouldn't uninstall $label"+ else -> "the TV refused"}}@@ -1116,7 +1129,7 @@ internal suspend fun switchHome(}}is HomeResult.Refused -> throw IllegalStateException(reason(r.reason))- is HomeResult.Failed -> throw IllegalStateException(r.output.ifEmpty { "the TV refused" })+ is HomeResult.Failed -> throw ActionFailed(r.output.ifEmpty { "the TV refused" }, r.details)}/**diff --git a/app/src/main/java/app/tellyscope/data/Model.kt b/app/src/main/java/app/tellyscope/data/Model.ktindex 4630a55..e28feb6 100644--- a/app/src/main/java/app/tellyscope/data/Model.kt+++ b/app/src/main/java/app/tellyscope/data/Model.kt@@ -150,6 +150,8 @@ data class AppInfo(val launchable: Boolean = false,/** Has an icon on the TV's own home screen, not only a phone-style one. True when the TV can't say. */val tvHome: Boolean = true,+ /** Off, and the TV turned it off itself rather than the user: Android won't let ADB turn it back on. */+ val disabledByTv: Boolean = false,)/** Where an install from the Play Store stands, read every few seconds while the TV shows the Store. */