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.

Fire TV: the remote's Home opens the default launcher

jesse-agent-821bdd8b8961d82
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
index 249e7a4..87402f7 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Actions.kt
@@ -180,7 +180,7 @@ class Actions(private val shell: AdbShell) {
shell.shell(Commands.RESUMED).output.lineSequence().any { "ResumedActivity" in it && " ${Commands.PLAY_STORE}/" in it }
/** The activity Home opens, or null when Android would ask (its chooser lives in `android`). */
- private suspend fun currentHome(): String? =
+ suspend fun currentHome(): String? =
activitiesIn(shell.shell(Commands.HOME_ACTIVITY).output).singleOrNull()?.takeUnless { it.startsWith("android/") }
/**
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
index 17caeca..1697061 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
@@ -31,6 +31,19 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
suspend fun press(key: RemoteKey): Boolean =
keys?.press(key.code) ?: (session.shell("input keyevent ${key.code}").exitCode == 0)
+ /**
+ * Home that opens [launcher], the activity Android resolves Home to (see [Actions.currentHome]).
+ * For Fire TV, whose Home key goes to Amazon's launcher whatever the default is. Null, a name
+ * that isn't a plain component, or a TV that won't start it fall back to the Home key.
+ */
+ suspend fun home(launcher: String?): Boolean {
+ if (launcher != null && COMPONENT.matches(launcher)) {
+ val r = session.shell("am start -a android.intent.action.MAIN -c android.intent.category.HOME -n '$launcher' 2>&1")
+ if (r.exitCode == 0 && "Error" !in r.output) return true
+ }
+ return press(RemoteKey.HOME)
+ }
+
/**
* Opens the TV's own system update screen. Some builds have none, and older `am` reports a
* missing screen in its output with exit code 0, so both are checked.
@@ -82,6 +95,9 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
companion object {
const val MAX_TEXT = 200
+ /** `pkg/.Activity` or `pkg/pkg.Activity`; a nested class has a `$`, which the single quotes keep. */
+ private val COMPONENT = Regex("[A-Za-z0-9_.]+/[A-Za-z0-9_.$]+")
+
/**
* What `input text` can type: printable ASCII only (it has no key for anything else), and
* never `%s`, which it reads as a space.
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
index 8ce01d4..6fe99d7 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/ActionsTest.kt
@@ -10,6 +10,16 @@ import kotlin.test.assertTrue
class ActionsTest {
private val home = "cmd package resolve-activity" to ok("priority=0 preferredOrder=0\ncom.google.android.tvlauncher/.MainActivity\n")
+ @Test fun currentHomeIsTheDefaultLauncher() = runTest {
+ val sh = FakeShell(mapOf("cmd package resolve-activity" to ok("priority=0\ncom.spocky.projengmenu/.ui.home.MainActivity\n")))
+ assertEquals("com.spocky.projengmenu/.ui.home.MainActivity", Actions(sh).currentHome())
+ }
+
+ @Test fun currentHomeIsNullWhenAndroidWouldAsk() = runTest {
+ val sh = FakeShell(mapOf("cmd package resolve-activity" to ok("priority=0\nandroid/com.android.internal.app.ResolverActivity\n")))
+ assertEquals(null, Actions(sh).currentHome())
+ }
+
@Test fun disableReturnsEnableAsUndo() = runTest {
val sh = FakeShell(mapOf(home, "pm disable-user" to ok("Package com.foo.ads new state: disabled-user\n")))
assertEquals(ActionResult.Done(Action.ENABLE), Actions(sh).run(Action.DISABLE, "com.foo.ads"))
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
index d77f978..2d85787 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
@@ -45,6 +45,33 @@ class RemoteTest {
assertEquals("input keyevent 3", sh.ran.last())
}
+ @Test fun fireHomeStartsTheDefaultLauncher() = runTest {
+ val sh = FakeShell()
+ assertTrue(Remote(sh).home("com.spocky.projengmenu/.ui.home.MainActivity"))
+ assertEquals(
+ listOf("am start -a android.intent.action.MAIN -c android.intent.category.HOME -n 'com.spocky.projengmenu/.ui.home.MainActivity' 2>&1"),
+ sh.ran,
+ )
+ }
+
+ @Test fun fireHomeWithoutALauncherSendsTheKey() = runTest {
+ val sh = FakeShell()
+ assertTrue(Remote(sh).home(null))
+ assertEquals(listOf("input keyevent 3"), sh.ran)
+ }
+
+ @Test fun fireHomeFallsBackToTheKeyWhenTheTvWontStartIt() = runTest {
+ val sh = FakeShell(mapOf("am start" to ShellResult("Error: Activity class {x/.Gone} does not exist.", 0)))
+ assertTrue(Remote(sh).home("x/.Gone"))
+ assertEquals("input keyevent 3", sh.ran.last())
+ }
+
+ @Test fun fireHomeNeverPutsAnOddNameOnTheShellLine() = runTest {
+ val sh = FakeShell()
+ assertTrue(Remote(sh).home("x/.A'; reboot; '"))
+ assertEquals(listOf("input keyevent 3"), sh.ran)
+ }
+
@Test fun keyRefusedByTheTvIsFalse() = runTest {
val sh = FakeShell(mapOf("input keyevent" to ShellResult("Error: Unknown command", 1)))
assertFalse(Remote(sh).press(RemoteKey.HOME))
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index b594204..fa08d68 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -20,6 +20,7 @@ import app.tellyscope.adb.TvFacts
import app.tellyscope.adb.TvFile
import app.tellyscope.adb.TvFolder
import app.tellyscope.adb.Actions
+import app.tellyscope.adb.AdbSession
import app.tellyscope.adb.ConnectionPrompt
import app.tellyscope.adb.ConnectionRequest
import app.tellyscope.adb.Connector
@@ -99,6 +100,8 @@ class AdbDeviceSource(context: Context) : DeviceSource {
private val mine = MineBook(File(appContext.filesDir, "mine.tsv"))
/** Last label seen per package, so history entries read like the app list. */
private val labels = ConcurrentHashMap<String, String>()
+ /** Per TV, the session [fireHome] read on and the launcher it found. */
+ private val fireHomes = ConcurrentHashMap<String, Pair<AdbSession, String?>>()
/** The remote's key helper on each TV, while the remote is open. */
private val keyHelpers = ConcurrentHashMap<String, KeyHelper>()
private val helperDex by lazy { appContext.assets.open("label-helper.jar").use { it.readBytes() } }
@@ -224,6 +227,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
restored.await()
watchers.remove(deviceId)?.cancel()
keyHelpers.remove(deviceId)?.close()
+ fireHomes -= deviceId
links.remove(deviceId)?.disconnect()
hits.remove(deviceId)
restoring -= deviceId
@@ -472,6 +476,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
.firstOrNull { it.action == Action.Home && it.undoneAt == null && it.previous?.get("to")?.substringBefore('/') == pkg }?.id
private suspend fun actNow(deviceId: String, action: Action, packageName: String, undoing: Long?, service: Boolean = true): HistoryEntry {
+ fireHomes -= deviceId // undoing a home switch moves Home back
val shell = session(deviceId)
// Before the first change on a TV, note what was already off: Restore must leave those alone.
val key = key(deviceId)
@@ -509,6 +514,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
/** [withService] false leaves the new launcher's accessibility service as it is: the user said no. */
private suspend fun switchHome(deviceId: String, packageName: String, undoing: Long?, withService: Boolean): HistoryEntry {
+ fireHomes -= deviceId
val key = key(deviceId)
val actions = Actions(session(deviceId))
return when (val r = actions.makeHome(packageName)) {
@@ -539,6 +545,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
* else the TV's own. [added] stays on; the entry's undo makes it Home again.
*/
private suspend fun homeBack(deviceId: String, added: String, undoing: Long?): HistoryEntry {
+ fireHomes -= deviceId
val key = key(deviceId)
val shell = session(deviceId)
val system = Parsers.packages(shell.shell("pm list packages -s").output).map { it.name }.toSet()
@@ -663,7 +670,20 @@ class AdbDeviceSource(context: Context) : DeviceSource {
return Resolved(index, match, list, out.getOrNull(MATCH_KEYS.size)?.toIntOrNull())
}
- override suspend fun press(deviceId: String, key: RemoteKey) = busy(deviceId) { remote(deviceId).press(key) }
+ override suspend fun press(deviceId: String, key: RemoteKey) = busy(deviceId) {
+ if (key == RemoteKey.HOME && deviceKind(device(deviceId)) == DeviceKind.FireTv) remote(deviceId).home(fireHome(deviceId))
+ else remote(deviceId).press(key)
+ }
+
+ /**
+ * The launcher Home resolves to on a Fire TV, read once per session so the key stays fast. A
+ * home switch or its undo drops it; a reconnect brings a new session, which reads it again.
+ */
+ private suspend fun fireHome(deviceId: String): String? {
+ val session = session(deviceId)
+ fireHomes[deviceId]?.takeIf { it.first === session }?.let { return it.second }
+ return Actions(session).currentHome().also { fireHomes[deviceId] = session to it }
+ }
/** One helper per link: a reconnect brings a new session, and the old helper went with the old one. */
private suspend fun remote(deviceId: String): Remote {