Skip to content

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.

adb: Action.UNINSTALL for user-installed apps only

jesse-agent-2320f4d6f06749c
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
index 41cf01c..8d3d5c8 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
@@ -2,10 +2,10 @@
package app.tellyscope.adb
/**
- * The only changes Tellyscope makes. There is no uninstall, by design. Undo for [STOP] is
- * [OPEN] ("open again"); a real undo isn't possible.
+ * The only changes Tellyscope makes. Undo for [STOP] is [OPEN] ("open again"); a real undo isn't
+ * possible. [UNINSTALL] has no undo at all, so it only ever runs on apps the user installed.
*/
-enum class Action { STOP, DISABLE, ENABLE, OPEN }
+enum class Action { STOP, DISABLE, ENABLE, OPEN, UNINSTALL }
sealed interface ActionResult {
data class Done(val undo: Action?) : ActionResult
@@ -27,9 +27,21 @@ class Actions(private val shell: AdbShell) {
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 }
Action.OPEN -> open(pkg)
+ Action.UNINSTALL -> uninstall(pkg)
}
}
+ /**
+ * Only for packages `pm list packages -3` lists, i.e. installed by the user. Plain
+ * `pm uninstall`, never `--user 0`: that form removes system apps for a user, and Tellyscope
+ * never does that.
+ */
+ private suspend fun uninstall(pkg: String): ActionResult {
+ val userApps = Parsers.packages(shell.shell(Commands.USER_PACKAGES).output).map { it.name }
+ if (pkg !in userApps) return ActionResult.Refused(ProtectedReason.CAME_WITH_TV)
+ return exec("pm uninstall $pkg", undo = null) { "Success" in it }
+ }
+
/**
* Launchers to protect: the default one, or every installed launcher when there's no default
* (then resolve-activity answers with the system chooser, package `android`).
@@ -55,7 +67,7 @@ class Actions(private val shell: AdbShell) {
return ActionResult.Failed(last.trim())
}
- private suspend fun exec(command: String, undo: Action, ok: (String) -> Boolean = { true }): ActionResult {
+ private suspend fun exec(command: String, undo: Action?, ok: (String) -> Boolean = { true }): ActionResult {
val r = shell.shell("$command 2>&1")
return if (r.exitCode == 0 && ok(r.output)) ActionResult.Done(undo) else ActionResult.Failed(r.output.trim())
}
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Commands.kt b/adb/src/main/kotlin/app/tellyscope/adb/Commands.kt
index c43a812..4f6b223 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Commands.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Commands.kt
@@ -35,6 +35,8 @@ internal object Commands {
val TRAFFIC = listOf("dumpsys netstats --uid", "cat /proc/net/xt_qtaguid/stats").batch()
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"
const val HOME_ACTIVITY = "cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME"
const val HOME_ACTIVITIES = "cmd package query-activities --brief -a android.intent.action.MAIN -c android.intent.category.HOME"
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt b/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt
index be977f1..570285b 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt
@@ -8,6 +8,9 @@ enum class ProtectedReason {
/** The TV didn't say which app is its home screen, so nothing is stopped or disabled. */
UNKNOWN_HOME,
+
+ /** Not installed by the user, so it can't be uninstalled; it can still be disabled. */
+ CAME_WITH_TV,
}
/**
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
index fb62571..4e9c595 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
@@ -66,6 +66,41 @@ class ActionsTest {
assertTrue(Actions(sh).run(Action.DISABLE, "com.foo") is ActionResult.Failed)
}
+ @Test fun uninstallsOnlyWhatTheUserInstalled() = runTest {
+ val sh = FakeShell(mapOf(
+ home,
+ "pm list packages -3" to ok("package:com.netflix.ninja\npackage:com.amazon.amazonvideo.livingroom\n"),
+ "pm uninstall" to ok("Success\n"),
+ ))
+ assertEquals(ActionResult.Done(null), Actions(sh).run(Action.UNINSTALL, "com.netflix.ninja"))
+ assertTrue(sh.ran.contains("pm uninstall com.netflix.ninja 2>&1"))
+ }
+
+ @Test fun refusesToUninstallWhatCameWithTheTv() = runTest {
+ val sh = FakeShell(mapOf(home, "pm list packages -3" to ok("package:com.netflix.ninja\n")))
+ assertEquals(ActionResult.Refused(ProtectedReason.CAME_WITH_TV), Actions(sh).run(Action.UNINSTALL, "com.google.android.youtube.tv"))
+ assertTrue(sh.ran.none { it.startsWith("pm uninstall") })
+ }
+
+ @Test fun neverUninstallsForOneUser() = runTest {
+ val sh = FakeShell(mapOf(home, "pm list packages -3" to ok("package:com.netflix.ninja\n"), "pm uninstall" to ok("Success\n")))
+ Actions(sh).run(Action.UNINSTALL, "com.netflix.ninja")
+ assertTrue(sh.ran.none { "--user" in it && "uninstall" in it })
+ }
+
+ @Test fun uninstallRefusesProtectedAndUnknownHome() = runTest {
+ val user = "pm list packages -3" to ok("package:com.google.android.gms\npackage:com.foo.ads\n")
+ assertEquals(ActionResult.Refused(ProtectedReason.PLAY_SERVICES), Actions(FakeShell(mapOf(home, user))).run(Action.UNINSTALL, "com.google.android.gms"))
+ val noHome = FakeShell(mapOf(user, "cmd package" to ShellResult("/system/bin/sh: cmd: not found\n", 127)))
+ assertEquals(ActionResult.Refused(ProtectedReason.UNKNOWN_HOME), Actions(noHome).run(Action.UNINSTALL, "com.foo.ads"))
+ assertTrue(noHome.ran.none { it.startsWith("pm uninstall") })
+ }
+
+ @Test fun uninstallFailureIsReported() = runTest {
+ val sh = FakeShell(mapOf(home, "pm list packages -3" to ok("package:com.foo\n"), "pm uninstall" to ok("Failure [DELETE_FAILED_INTERNAL_ERROR]\n")))
+ assertTrue(Actions(sh).run(Action.UNINSTALL, "com.foo") is ActionResult.Failed)
+ }
+
@Test fun openFallsBackToPhoneLauncher() = runTest {
val sh = FakeShell(mapOf(
"monkey -p com.foo -c android.intent.category.LEANBACK_LAUNCHER" to ok("** No activities found to run, monkey aborted.\n"),