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.
Protect only the launcher on Home; uninstall it after switching Home back
jesse-agent-232314d89b1ec26
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt b/adb/src/main/kotlin/app/tellyscope/adb/Actions.ktindex 87402f7..bf8723c 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt@@ -30,7 +30,7 @@ class Actions(private val shell: AdbShell) {if (action != Action.ENABLE && action != Action.OPEN) {// No launcher answer (no `cmd` before Android 7, or an error): we can't tell what's safe.val home = homePackages().ifEmpty { return ActionResult.Refused(ProtectedReason.UNKNOWN_HOME) }- Protected.reason(pkg, home)?.let { return ActionResult.Refused(it) }+ Protected.reason(pkg, home, userApp = pkg in userPackages())?.let { return ActionResult.Refused(it) }}return when (action) {Action.STOP -> exec("am force-stop $pkg", Action.OPEN)@@ -47,11 +47,12 @@ class Actions(private val shell: AdbShell) {* 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)+ if (pkg !in userPackages()) return ActionResult.Refused(ProtectedReason.CAME_WITH_TV)return exec("pm uninstall $pkg", undo = null) { "Success" in it }}+ private suspend fun userPackages() = Parsers.packages(shell.shell(Commands.USER_PACKAGES).output).map { it.name }.toSet()+/*** 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`).diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt b/adb/src/main/kotlin/app/tellyscope/adb/Protected.ktindex 4dcd6be..6f0ae94 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Protected.kt@@ -50,7 +50,7 @@ object Protected {/** Name fragments, checked in order; manufacturer names vary, so these match loosely. */private val PATTERNS: List<Pair<String, ProtectedReason>> = listOf(- "launcher" to ProtectedReason.HOME_SCREEN, // second guard behind the live home lookup+ "launcher" to ProtectedReason.HOME_SCREEN, // second guard behind the live home lookup; built-in apps only"networkstack" to ProtectedReason.SYSTEM, // Wi-Fi and Ethernet setup; we'd cut our own link// Wi-Fi config, connectivity resources and their OEM overlays: same reason, and once Wi-Fi is// gone so is the ADB link an undo would need.@@ -77,13 +77,17 @@ object Protected {"provision" to ProtectedReason.SETUP_WIZARD,)- /** [homePackages] are the device's launchers, read live; they are protected whatever their name. */- fun reason(pkg: String, homePackages: Set<String> = emptySet()): ProtectedReason? {+ /**+ * [homePackages] are the device's launchers, read live; they are protected whatever their name.+ * A [userApp] (one the user installed) named like a launcher isn't: only the live lookup+ * protects it, so FLauncher can go while another launcher is Home.+ */+ fun reason(pkg: String, homePackages: Set<String> = emptySet(), userApp: Boolean = false): ProtectedReason? {if (pkg in homePackages) return ProtectedReason.HOME_SCREENEXACT[pkg]?.let { return it }if (CORE.matches(pkg) && !CORE_OK.matches(pkg)) return ProtectedReason.SYSTEMval lower = pkg.lowercase()- PATTERNS.firstOrNull { (fragment, _) -> fragment in lower }?.let { return it.second }+ PATTERNS.firstOrNull { (fragment, why) -> fragment in lower && !(userApp && why == ProtectedReason.HOME_SCREEN) }?.let { return it.second }return if (UPDATER.containsMatchIn(lower)) ProtectedReason.UPDATES else null}}diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.ktindex 6fe99d7..4d053f0 100644--- a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt+++ b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt@@ -86,6 +86,13 @@ class ActionsTest {assertTrue(sh.ran.contains("pm uninstall com.netflix.ninja 2>&1"))}+ @Test fun uninstallsAnAddedLauncherThatIsNotHome() = runTest {+ val projectivy = "cmd package resolve-activity" to ok("priority=0\ncom.spocky.projengmenu/.ui.home.MainActivity\n")+ val sh = FakeShell(mapOf(projectivy, "pm list packages -3" to ok("package:com.spocky.projengmenu\npackage:me.efesser.flauncher\n"), "pm uninstall" to ok("Success\n")))+ assertEquals(ActionResult.Done(null), Actions(sh).run(Action.UNINSTALL, "me.efesser.flauncher"))+ assertEquals(ActionResult.Refused(ProtectedReason.HOME_SCREEN), Actions(sh).run(Action.UNINSTALL, "com.spocky.projengmenu"))+ }+@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"))diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.ktindex 36395c4..f4a6bf1 100644--- a/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.kt+++ b/adb/src/test/kotlin/app/tellyscope/adb/ProtectedTest.kt@@ -6,6 +6,14 @@ import kotlin.test.assertEqualsimport kotlin.test.assertNullclass ProtectedTest {+ @Test fun aLauncherTheUserInstalledIsProtectedOnlyWhileItIsHome() {+ val projectivyHome = setOf("com.spocky.projengmenu")+ assertNull(Protected.reason("me.efesser.flauncher", projectivyHome, userApp = true))+ assertEquals(ProtectedReason.HOME_SCREEN, Protected.reason("me.efesser.flauncher", setOf("me.efesser.flauncher"), userApp = true))+ // A built-in one keeps the name guard, Home or not.+ assertEquals(ProtectedReason.HOME_SCREEN, Protected.reason("com.google.android.tvlauncher", projectivyHome))+ }+@Test fun coreRuleWithSpareProviders() {assertEquals(ProtectedReason.SYSTEM, Protected.reason("com.google.android.permissioncontroller"))assertEquals(ProtectedReason.SYSTEM, Protected.reason("com.android.providers.tv"))diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.ktindex d7cf622..15256ef 100644--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt@@ -397,7 +397,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {// for a built-in launcher that is off.protectedReason = when {p.name in disabled -> null- home.isNotEmpty() -> Protected.reason(p.name, home)+ home.isNotEmpty() -> Protected.reason(p.name, home, userApp = p.name in userInstalled)else -> ProtectedReason.UNKNOWN_HOME}?.let(::reasonText),version = "",@@ -490,6 +490,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {return switchHome(deviceId, packageName, undoing, again ?: service)}if (action == Action.HomeBack) return homeBack(deviceId, packageName, undoing)+ if (action == Action.Uninstall && Actions(shell).homeOptions()?.current == packageName) return uninstallHome(deviceId, packageName)val r = Actions(shell).run(AdbAction.valueOf(action.name.uppercase()), packageName)return when (r) {is ActionResult.Done -> {@@ -542,6 +543,13 @@ class AdbDeviceSource(context: Context) : DeviceSource {}}+ /**+ * Uninstall of the launcher on Home: Home goes back to the TV's own first, its accessibility+ * service off with it, as Switch home back does. Any step that fails stops it before the uninstall.+ */+ private suspend fun uninstallHome(deviceId: String, pkg: String): HistoryEntry =+ uninstallAfterHomeBack({ homeBack(deviceId, pkg, null) }, { actNow(deviceId, Action.Uninstall, pkg, null) })+/*** Makes Home again the launcher that [added], now Home, replaced: the one the last switch saved,* else the TV's own. [added] stays on; the entry's undo makes it Home again.@@ -937,3 +945,20 @@ internal suspend fun applyTweak(}return record(TvSettings.label(tweak, t.read(tweak)), before)}++/**+ * Runs [switchBack] (Home to the TV's own, the launcher's service off), and [uninstall] only once+ * it worked. A failure says which step stopped it.+ */+internal suspend fun uninstallAfterHomeBack(switchBack: suspend () -> Unit, uninstall: suspend () -> HistoryEntry): HistoryEntry {+ try {+ switchBack()+ } catch (e: CancellationException) {+ throw e+ } catch (e: Exception) {+ val why = e.message ?: "the TV refused"+ // "switched back, but …": Home moved, its service didn't turn off.+ throw IllegalStateException(if (why.startsWith("switched back")) "$why. Nothing was uninstalled" else "Home didn't switch back: $why. Nothing was uninstalled")+ }+ return uninstall()+}diff --git a/app/src/test/java/app/tellyscope/UninstallHomeTest.kt b/app/src/test/java/app/tellyscope/UninstallHomeTest.ktnew file mode 100644index 0000000..808395b--- /dev/null+++ b/app/src/test/java/app/tellyscope/UninstallHomeTest.kt@@ -0,0 +1,40 @@+// SPDX-License-Identifier: GPL-3.0-only++package app.tellyscope++import app.tellyscope.data.Action+import app.tellyscope.data.HistoryEntry+import app.tellyscope.data.uninstallAfterHomeBack+import kotlinx.coroutines.test.runTest+import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test++/** Uninstalling the launcher on Home: Home goes back first, and nothing is removed if that fails. */+class UninstallHomeTest {+ private val steps = mutableListOf<String>()+ private val removed = HistoryEntry(1, "tv", 0L, Action.Uninstall, "com.spocky.projengmenu", "Projectivy Launcher", null, null)++ private suspend fun uninstall(): HistoryEntry {+ steps += "uninstall"+ return removed+ }++ @Test fun homeGoesBackBeforeTheUninstall() = runTest {+ assertEquals(removed, uninstallAfterHomeBack({ steps += "home back" }, ::uninstall))+ assertEquals(listOf("home back", "uninstall"), steps)+ }++ @Test fun aSwitchThatFailsUninstallsNothingAndSaysSo() = runTest {+ val e = runCatching { uninstallAfterHomeBack({ throw IllegalStateException("the TV kept the home screen it has") }, ::uninstall) }.exceptionOrNull()+ assertEquals("Home didn't switch back: the TV kept the home screen it has. Nothing was uninstalled", e?.message)+ assertTrue(steps.isEmpty())+ }++ @Test fun aServiceStillOnUninstallsNothing() = runTest {+ val why = "switched back, but Projectivy Launcher's accessibility service is still on. Turn it off in the TV's Settings → Accessibility"+ val e = runCatching { uninstallAfterHomeBack({ throw IllegalStateException(why) }, ::uninstall) }.exceptionOrNull()+ assertEquals("$why. Nothing was uninstalled", e?.message)+ assertTrue(steps.isEmpty())+ }+}