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.

Hand off to the new Home only after the service changes

jesse-agent-8219a00cb28a5a2
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
index 1aedad9..cb78a47 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
@@ -16,12 +16,12 @@ 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. [left]: Home moved, but the old
- * launcher didn't close or the new one didn't open; null when both did. A [Failed.output] is one
- * plain line; [Failed.details] keeps what the TV printed, for a problem report.
+ * the old launcher was one the user added, so it stayed on. Once any service change is done, the
+ * caller hands the TV over with [Actions.handOff]. 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, val left: String? = null) : HomeResult
+ data class Switched(val from: String?, val to: String, val kept: Boolean = false) : HomeResult
data class Refused(val reason: ProtectedReason) : HomeResult
data class Failed(val output: String, val details: String = output) : HomeResult
}
@@ -102,7 +102,7 @@ class Actions(private val shell: AdbShell) {
* disabled; if it doesn't, the old home is set back and nothing is turned off. The one
* exception: Android skips the default while a launcher with a higher Home priority is on, as
* Android TV Home is. Such an old launcher is turned off first, and turned back on and made
- * Home again if Home still doesn't open [pkg]. Once switched, the TV goes to [pkg] ([handOff]).
+ * Home again if Home still doesn't open [pkg].
*/
suspend fun makeHome(pkg: String): HomeResult {
require(PACKAGE.matches(pkg)) { "not a package name: $pkg" }
@@ -117,7 +117,7 @@ class Actions(private val shell: AdbShell) {
// guard has done its job. Any other reason still stands.
Protected.reason(old)?.takeIf { it != ProtectedReason.HOME_SCREEN }?.let { return HomeResult.Refused(it) }
if (old in userPackages()) {
- if (setHome(to)) return HomeResult.Switched(from, to, kept = true, left = handOff(old, to))
+ if (setHome(to)) return HomeResult.Switched(from, to, kept = true)
setHome(from)
return HomeResult.Failed("the TV kept the home screen it has")
}
@@ -132,8 +132,7 @@ class Actions(private val shell: AdbShell) {
setHome(from)
return HomeResult.Failed("the TV wouldn't turn the old home screen off, so nothing changed", off.output)
}
- // Turning it off closed it too: only the new one is left to open.
- if (checked || setHome(to)) return HomeResult.Switched(from, to, left = handOff(null, to))
+ if (checked || setHome(to)) return HomeResult.Switched(from, to)
val on = exec("pm enable --user 0 $old", undo = null) { "enabled" in it }
setHome(from)
if (on is ActionResult.Failed) {
@@ -145,8 +144,7 @@ class Actions(private val shell: AdbShell) {
/**
* Makes [pkg] Home again after another launcher took over: turns it on if it's off, then sets
* [activity], the one the switch away from it saved, or else its own home activity. The
- * launcher that was Home stays on, so it can be made Home again or uninstalled; it's closed,
- * and the TV goes to [pkg] ([handOff]).
+ * launcher that was Home stays on, so it can be made Home again or uninstalled.
*/
suspend fun homeBack(pkg: String, activity: String? = null): HomeResult {
require(PACKAGE.matches(pkg)) { "not a package name: $pkg" }
@@ -160,7 +158,7 @@ class Actions(private val shell: AdbShell) {
setHome(from)
return HomeResult.Failed("the TV kept the home screen it has")
}
- return HomeResult.Switched(from, to, left = handOff(from.substringBefore('/'), to))
+ return HomeResult.Switched(from, to)
}
/** Makes [activity] (`package/class`) the default home, then checks that Home opens it. */
@@ -228,15 +226,18 @@ class Actions(private val shell: AdbShell) {
private suspend fun firstHome(pkg: String): HomeResult {
val to = launchers()[pkg] ?: return HomeResult.Refused(ProtectedReason.UNKNOWN_HOME)
if (!setHome(to)) return HomeResult.Failed("the TV still asks which home screen to use")
- return HomeResult.Switched(null, to, left = handOff(null, to))
+ return HomeResult.Switched(null, to)
}
/**
* After a switch, so the TV shows the new home screen without a Home press: closes [old], the
- * launcher that was Home if it's still on, then opens [to]. Home has moved either way, so a
- * step that fails is only reported. Null when both took.
+ * launcher that was Home if it's still on, then opens [to]. Last, after any accessibility
+ * service change: a service still on can pull Home straight back. Home has moved either way,
+ * so a step that fails is only reported: what went wrong, or null when both took.
*/
- private suspend fun handOff(old: String?, to: String): String? {
+ suspend fun handOff(old: String?, to: String): String? {
+ require(old == null || PACKAGE.matches(old)) { "not a package name: $old" }
+ require(ACTIVITY.matches(to)) { "not an activity: $to" }
val stop = old?.takeIf { it != to.substringBefore('/') }?.let { exec("am force-stop $it", undo = null) }
val open = shell.shell("${Commands.OPEN_HOME} '$to' 2>&1")
return when {
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
index 461e44c..7a9a278 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
@@ -485,53 +485,42 @@ class ActionsTest {
assertEquals(setOf("com.android.nfc", "com.oem.lazy"), Actions(sh).disabledByTv())
}
- @Test fun aSwitchFromTheUsersLauncherStopsItThenOpensTheNewOne() = runTest {
+ private val openFlauncher = "am start -a android.intent.action.MAIN -c android.intent.category.HOME -n 'me.efesser.flauncher/.MainActivity' 2>&1"
+
+ @Test fun aSwitchOnlySwitches() = runTest {
+ // The hand-off waits for the caller: a service change may still have to come first.
val sh = stockOff("com.spocky.projengmenu", "me.efesser.flauncher")
Actions(sh).makeHome("com.spocky.projengmenu")
- sh.ran.clear()
- Actions(sh).makeHome("me.efesser.flauncher")
- val set = sh.ran.indexOf("cmd package set-home-activity --user 0 'me.efesser.flauncher/.MainActivity' 2>&1")
- val stop = sh.ran.indexOf("am force-stop com.spocky.projengmenu 2>&1")
- val open = sh.ran.indexOf("am start -a android.intent.action.MAIN -c android.intent.category.HOME -n 'me.efesser.flauncher/.MainActivity' 2>&1")
- assertTrue(set >= 0 && stop > set && open > stop, sh.ran.toString())
- assertTrue(sh.ran.none { it == "am force-stop me.efesser.flauncher 2>&1" })
+ assertTrue(Actions(sh).makeHome("me.efesser.flauncher") is HomeResult.Switched)
+ val tv = twoLaunchers()
+ assertTrue(Actions(tv).makeHome("com.spocky.projengmenu") is HomeResult.Switched)
+ assertTrue(Actions(tv).homeBack("com.google.android.tvlauncher", "com.google.android.tvlauncher/.MainActivity") is HomeResult.Switched)
+ assertTrue((sh.ran + tv.ran).none { it.startsWith("am ") }, (sh.ran + tv.ran).toString())
}
- @Test fun aSwitchFromTheTvsOwnOpensTheNewOneAndStopsNothing() = runTest {
- // Turning the TV's own launcher off closes it already.
- val sh = twoLaunchers()
- Actions(sh).makeHome("com.spocky.projengmenu")
- val off = sh.ran.indexOf("pm disable-user --user 0 com.google.android.tvlauncher 2>&1")
- val open = sh.ran.indexOf("am start -a android.intent.action.MAIN -c android.intent.category.HOME -n 'com.spocky.projengmenu/.ui.home.MainActivity' 2>&1")
- assertTrue(off >= 0 && open > off, sh.ran.toString())
- assertTrue(sh.ran.none { it.startsWith("am force-stop") })
+ @Test fun handOffStopsTheOldHomeThenOpensTheNewOne() = runTest {
+ val sh = FakeShell()
+ assertEquals(null, Actions(sh).handOff("com.spocky.projengmenu", "me.efesser.flauncher/.MainActivity"))
+ assertEquals(listOf("am force-stop com.spocky.projengmenu 2>&1", openFlauncher), sh.ran)
}
- @Test fun aHomeThatDoesNotOpenKeepsTheSwitch() = runTest {
- val sh = twoLaunchers()
- sh.answer.let { prev -> sh.answer = { if (it.startsWith("am start")) ok("Error: Activity not started\n") else prev(it) } }
- val r = Actions(sh).makeHome("com.spocky.projengmenu") as HomeResult.Switched
- assertTrue(r.left.orEmpty().startsWith("it didn't open"), r.left)
- assertEquals("com.spocky.projengmenu", Actions(sh).homeOptions()?.current)
- assertTrue(sh.ran.none { it.startsWith("pm enable") })
+ @Test fun handOffWithNothingToStopOnlyOpens() = runTest {
+ val sh = FakeShell()
+ Actions(sh).handOff(null, "me.efesser.flauncher/.MainActivity")
+ // Never the new one, either.
+ Actions(sh).handOff("me.efesser.flauncher", "me.efesser.flauncher/.MainActivity")
+ assertEquals(listOf(openFlauncher, openFlauncher), sh.ran)
}
- @Test fun anOldHomeThatDoesNotStopKeepsTheSwitch() = runTest {
- val sh = stockOff("com.spocky.projengmenu", "me.efesser.flauncher")
- Actions(sh).makeHome("com.spocky.projengmenu")
- sh.answer.let { prev -> sh.answer = { if (it.startsWith("am force-stop")) ShellResult("Error: denied\n", 1) else prev(it) } }
- val r = Actions(sh).makeHome("me.efesser.flauncher") as HomeResult.Switched
- assertEquals("the old home screen didn't close (Error: denied)", r.left)
- assertEquals("me.efesser.flauncher/.MainActivity", Actions(sh).currentHome())
+ @Test fun handOffSaysWhenTheNewHomeDidNotOpen() = runTest {
+ val sh = FakeShell(mapOf("am start" to ok("Error: Activity not started\n")))
+ val left = Actions(sh).handOff("com.spocky.projengmenu", "me.efesser.flauncher/.MainActivity")
+ assertTrue(left.orEmpty().startsWith("it didn't open"), left)
}
- @Test fun homeBackStopsTheLauncherItLeavesAndOpensTheTvsOwn() = runTest {
- val sh = twoLaunchers()
- Actions(sh).makeHome("com.spocky.projengmenu")
- sh.ran.clear()
- Actions(sh).homeBack("com.google.android.tvlauncher", "com.google.android.tvlauncher/.MainActivity")
- val stop = sh.ran.indexOf("am force-stop com.spocky.projengmenu 2>&1")
- val open = sh.ran.indexOf("am start -a android.intent.action.MAIN -c android.intent.category.HOME -n 'com.google.android.tvlauncher/.MainActivity' 2>&1")
- assertTrue(stop >= 0 && open > stop, sh.ran.toString())
+ @Test fun handOffSaysWhenTheOldHomeDidNotStop() = runTest {
+ val sh = FakeShell(mapOf("am force-stop" to ShellResult("Error: denied\n", 1)))
+ assertEquals("the old home screen didn't close (Error: denied)", Actions(sh).handOff("com.spocky.projengmenu", "me.efesser.flauncher/.MainActivity"))
+ assertTrue(openFlauncher in sh.ran)
}
}
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index c58e3a4..f238472 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -595,6 +595,8 @@ class AdbDeviceSource(context: Context) : DeviceSource {
// Off with Home: it would keep sending Home to [added]. Undo, a switch, turns it on again.
val service = added.takeIf { it in serviceLaunchers }?.let { Actions(shell).homeService(it) }?.takeIf { Actions(shell).serviceOn(it) }
val off = service?.let { Actions(shell).setService(it, false) }
+ // Last: with its service still on, [added] could take Home straight back.
+ val left = Actions(shell).handOff(added, r.to)
book.record(
deviceId = key,
at = System.currentTimeMillis(),
@@ -607,7 +609,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
previous = listOfNotNull(r.from?.let { "from" to it }, "to" to r.to, service?.takeIf { off == true }?.let { "service" to it }).toMap(),
).also {
if (off == false) throw IllegalStateException(serviceNotOff(labels[added] ?: AppNames.fallback(added)))
- r.left?.let { throw IllegalStateException(notShown(labels[target.pkg] ?: AppNames.fallback(target.pkg), it)) }
+ left?.let { throw IllegalStateException(notShown(labels[target.pkg] ?: AppNames.fallback(target.pkg), it)) }
}
}
is HomeResult.Refused -> throw IllegalStateException(reasonText(r.reason))
@@ -724,26 +726,25 @@ class AdbDeviceSource(context: Context) : DeviceSource {
/**
* Home back to [home]'s TV's own launcher, the added one's accessibility service off with it:
- * it would keep sending Home there. Returns what Undo needs, and why the service stayed on or
- * the hand-off to the new home screen didn't finish, if so; throws when Home didn't move. On Try again Home
- * may have moved already: then only the service is left to do, and the entry already holds the move.
+ * it would keep sending Home there; then the TV is handed to it. Returns what Undo needs, and why
+ * the service stayed on or the hand-off didn't finish, if so; throws when Home didn't move. On
+ * Try again Home may have moved already: then only the service and the hand-off are left to do,
+ * and the entry already holds the move.
*/
private suspend fun resetHome(actions: Actions, home: ResetHome): Pair<Map<String, String?>, String?> {
- var left: String? = null
val moved: Map<String, String?> = if (actions.currentHome()?.substringBefore('/') == home.to) emptyMap()
else when (val r = actions.homeBack(home.to)) {
- is HomeResult.Switched -> {
- left = r.left?.let { notShown(home.toLabel, it) }
- listOfNotNull(ResetRecord.FROM to r.from, ResetRecord.TO to r.to).toMap()
- }
+ is HomeResult.Switched -> listOfNotNull(ResetRecord.FROM to r.from, ResetRecord.TO to r.to).toMap()
is HomeResult.Refused -> throw IllegalStateException(reasonText(r.reason))
is HomeResult.Failed -> throw IllegalStateException(r.output.ifEmpty { "the TV refused" })
}
val service = home.from.takeIf { it in serviceLaunchers }?.let { actions.homeService(it) }?.takeIf { actions.serviceOn(it) }
- ?: return moved to left
+ val off = service?.let { actions.setService(it, false) }
+ // Last, on Try again too: with its service still on, the added launcher could take Home straight back.
+ val left = actions.currentHome()?.let { actions.handOff(home.from, it) }?.let { notShown(home.toLabel, it) }
// Recorded only once it's off: Undo turns back on what Reset turned off, nothing more.
- return if (actions.setService(service, false)) moved + (ResetRecord.SERVICE to service) to left
- else moved to serviceNotOff(home.fromLabel)
+ val recorded = service?.takeIf { off == true }?.let { moved + (ResetRecord.SERVICE to it) } ?: moved
+ return recorded to if (off == false) serviceNotOff(home.fromLabel) else left
}
/**
@@ -1120,6 +1121,9 @@ internal suspend fun switchHome(
val was = r.from?.substringBefore('/')?.takeIf { r.kept }
val old = was?.takeIf { it in serviceLaunchers }?.let { actions.homeService(it) }?.takeIf { actions.serviceOn(it) }
val off = old?.let { actions.setService(it, false) }
+ // Last, after both services: one still on could pull Home straight back. A launcher the
+ // TV came with is off now, which closed it already.
+ val left = actions.handOff(was, r.to)
record(
// With no launcher to replace, nothing was turned off: Undo is only for a service it turned on.
r.from?.substringBefore('/') ?: pkg,
@@ -1133,7 +1137,7 @@ internal suspend fun switchHome(
).also {
if (on == false) throw IllegalStateException(serviceNotOn(label(pkg)))
if (off == false) throw IllegalStateException(serviceNotOff(label(was!!)))
- r.left?.let { throw IllegalStateException(notShown(label(pkg), it)) }
+ left?.let { throw IllegalStateException(notShown(label(pkg), it)) }
}
}
is HomeResult.Refused -> throw IllegalStateException(reason(r.reason))
@@ -1157,7 +1161,7 @@ internal const val RESET = "reset"
private fun serviceNotOn(app: String) =
"$app is Home now, but its accessibility service didn't turn on, so the TV's Home button may still open its own launcher"
-/** Home moved, but the old launcher didn't close or [app] didn't open: [why], from [HomeResult.Switched.left]. */
+/** Home moved, but the old launcher didn't close or [app] didn't open: [why], from [Actions.handOff]. */
private fun notShown(app: String, why: String) = "$app is Home now, but $why"
private fun serviceNotOff(app: String) =
diff --git a/app/src/test/java/app/tellyscope/HomeSwitchTest.kt b/app/src/test/java/app/tellyscope/HomeSwitchTest.kt
index 1b9a4ca..d984979 100644
--- a/app/src/test/java/app/tellyscope/HomeSwitchTest.kt
+++ b/app/src/test/java/app/tellyscope/HomeSwitchTest.kt
@@ -37,8 +37,10 @@ private class LauncherTv(var home: String?) : AdbSession {
val services = mutableListOf<String>()
/** False: `am start` can't open a home screen, as on a TV that refuses it. */
var opens = true
+ val ran = mutableListOf<String>()
override suspend fun shell(command: String): ShellResult {
+ ran += command
val pkg = command.substringAfter("--user 0 ").substringBefore(' ')
val quoted = command.substringAfter("'").substringBefore("'")
return when {
@@ -143,4 +145,41 @@ class HomeSwitchTest {
assertEquals(FLAUNCHER, tv.home)
assertEquals(TV_HOME, book.entries("tv").single().packageName)
}
+
+ private val stop = { pkg: String -> "am force-stop $pkg 2>&1" }
+ private val open = { pkg: String -> "am start -a android.intent.action.MAIN -c android.intent.category.HOME -n '$pkg' 2>&1" }
+
+ @Test fun aLauncherTheUserAddedIsClosedThenTheNewOneOpens() = runTest {
+ val tv = LauncherTv(home = FLAUNCHER)
+ switch(tv, PROJECTIVY, withService = false)
+ val set = tv.ran.indexOfFirst { it.startsWith("cmd package set-home-activity --user 0 '$PROJECTIVY/") }
+ val closed = tv.ran.indexOf(stop(FLAUNCHER))
+ val opened = tv.ran.indexOf(open(tv.activities.getValue(PROJECTIVY)))
+ assertTrue(tv.ran.toString(), set >= 0 && closed > set && opened > closed)
+ assertFalse(stop(PROJECTIVY) in tv.ran)
+ }
+
+ @Test fun theTvsOwnLauncherIsNotStoppedTurningItOffClosedIt() = runTest {
+ val tv = LauncherTv(home = TV_HOME)
+ switch(tv, FLAUNCHER)
+ val off = tv.ran.indexOfFirst { it.startsWith("pm disable-user --user 0 $TV_HOME") }
+ assertTrue(tv.ran.toString(), off >= 0 && tv.ran.indexOf(open(tv.activities.getValue(FLAUNCHER))) > off)
+ assertTrue(tv.ran.none { it.startsWith("am force-stop") })
+ }
+
+ @Test fun theNewHomeOpensOnlyAfterItsServiceIsOn() = runTest {
+ val tv = LauncherTv(home = TV_HOME)
+ switch(tv, PROJECTIVY)
+ val on = tv.ran.indexOfLast { it.startsWith("settings put secure enabled_accessibility_services") }
+ assertTrue(tv.ran.toString(), on >= 0 && tv.ran.indexOf(open(tv.activities.getValue(PROJECTIVY))) > on)
+ }
+
+ @Test fun theNewHomeOpensOnlyAfterTheOldOnesServiceIsOff() = runTest {
+ // Projectivy's service still on would pull Home straight back to it.
+ val tv = LauncherTv(home = PROJECTIVY).apply { services += PROJECTIVY_SERVICE }
+ switch(tv, FLAUNCHER)
+ val off = tv.ran.indexOfLast { it.startsWith("settings delete secure enabled_accessibility_services") || it.startsWith("settings put secure enabled_accessibility_services") }
+ val opened = tv.ran.indexOf(open(tv.activities.getValue(FLAUNCHER)))
+ assertTrue(tv.ran.toString(), off >= 0 && opened > off && tv.ran.indexOf(stop(PROJECTIVY)) > off)
+ }
}