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.

History: Undo turns off a first home's accessibility service

jesse-agent-232198ccd0d94f6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0136f7c..6a77477 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ until this file has a section for its version.
## Unreleased
+- **Fixed:** Undo in History turns off a new home screen's accessibility service, as promised.
- **Fixed:** Restore everything always ends on your TV's own home screen. [#184](../../pull/184)
- **Fixed:** The Remote's backspace no longer closes the TV app once the box is empty. [#183](../../pull/183)
- **Improved:** The Remote's dock moves to the bottom; Screen off and Restart go under ⋮. [#182](../../pull/182)
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index 2b3ad99..c24dfab 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -502,6 +502,8 @@ class AdbDeviceSource(context: Context) : DeviceSource {
return switchHome(deviceId, packageName, undoing, again ?: service)
}
if (action == Action.HomeBack) return homeBack(deviceId, packageName, undoing)
+ undoing?.let { id -> book.entries(key).firstOrNull { it.id == id } }?.firstHomeService
+ ?.let { return firstHomeServiceOff(deviceId, packageName, it, 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) {
@@ -542,10 +544,10 @@ class AdbDeviceSource(context: Context) : DeviceSource {
deviceId = key,
at = System.currentTimeMillis(),
action = Action.Home,
- // With no launcher of the TV's own to replace, nothing was turned off and there's nothing to undo.
+ // With no launcher of the TV's own to replace, nothing was turned off: Undo is for the service alone.
packageName = r.from?.substringBefore('/') ?: packageName,
label = label,
- undo = Action.Enable.takeIf { r.from != null },
+ undo = Action.Enable.takeIf { r.from != null || on == true },
undoing = undoing,
// "service" only when this switch turned it on, so Undo turns off no more than that.
previous = listOfNotNull(r.from?.let { "from" to it }, "to" to r.to, service?.takeIf { on == true }?.let { "service" to it }).toMap(),
@@ -556,6 +558,22 @@ class AdbDeviceSource(context: Context) : DeviceSource {
}
}
+ /** Undo of a first home: [launcher] stays Home, as there's none to go back to; its [service] goes off. */
+ private suspend fun firstHomeServiceOff(deviceId: String, launcher: String, service: String, undoing: Long): HistoryEntry {
+ val label = labels[launcher] ?: AppNames.fallback(launcher)
+ if (!Actions(session(deviceId)).setService(service, false)) throw IllegalStateException(serviceStillOn(label))
+ return book.record(
+ deviceId = key(deviceId),
+ at = System.currentTimeMillis(),
+ // A sentence of its own, like a setting's: nothing here for Restore or a later Undo.
+ action = Action.Change,
+ packageName = launcher,
+ label = "Turned off $label's accessibility service",
+ undo = null,
+ undoing = undoing,
+ )
+ }
+
/**
* 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.
@@ -909,6 +927,9 @@ class AdbDeviceSource(context: Context) : DeviceSource {
private fun serviceNotOff(app: String) =
"switched back, but $app's accessibility service is still on. Turn it off in the TV's Settings → Accessibility"
+ private fun serviceStillOn(app: String) =
+ "$app's accessibility service is still on. Turn it off in the TV's Settings → Accessibility"
+
private fun homeNotBack(app: String) =
"turned $app back on, but the TV kept the other home screen. Press Home on the remote to pick $app"
diff --git a/app/src/main/java/app/tellyscope/data/HistoryBook.kt b/app/src/main/java/app/tellyscope/data/HistoryBook.kt
index 23424fa..95bec8f 100644
--- a/app/src/main/java/app/tellyscope/data/HistoryBook.kt
+++ b/app/src/main/java/app/tellyscope/data/HistoryBook.kt
@@ -47,8 +47,10 @@ class HistoryBook(private val file: File? = null) {
previous: Map<String, String?>? = null,
): HistoryEntry {
val undone = undoing?.let { id -> entries.indexOfFirst { it.id == id } }?.takeIf { it >= 0 }
- // Undoing a switch makes the old launcher Home again: a home change like any switch.
- val takesHome = action == Action.Home || action == Action.HomeBack || undone?.let { entries[it].action == Action.Home } == true
+ // Undoing a switch makes the old launcher Home again: a home change like any switch. Undoing
+ // a first home only turns its service off; Home stays.
+ val takesHome = action == Action.Home || action == Action.HomeBack ||
+ undone?.let { entries[it].action == Action.Home && entries[it].firstHomeService == null } == true
undone?.let { entries[it] = entries[it].copy(undoneAt = at) }
if (takesHome) {
// An older "Switched home back" would switch to a launcher that has since lost Home: its Undo goes.
@@ -191,8 +193,15 @@ fun homeBackTarget(current: String, history: List<HistoryEntry>, builtIns: List<
return builtIns.firstOrNull { it != current }?.let { HomeBackTarget(it, null, null) }
}
-/** A home switch disables the old launcher, unless there was none to replace: then it offers no undo. */
-private val HistoryEntry.disables get() = action == Action.Disable || (action == Action.Home && undo != null)
+/**
+ * A home switch disables the old launcher, unless there was none to replace: then it offers no
+ * undo, or one for its service only.
+ */
+private val HistoryEntry.disables get() = action == Action.Disable || (action == Action.Home && undo != null && firstHomeService == null)
+
+/** The service a switch with no launcher to replace turned on: its Undo turns that off, and nothing else. */
+val HistoryEntry.firstHomeService: String?
+ get() = previous?.takeIf { action == Action.Home && it["from"] == null }?.get("service")
/**
* Turns apps back on one at a time and stops at the first refusal, saying how far it got so the
diff --git a/app/src/main/java/app/tellyscope/ui/Copy.kt b/app/src/main/java/app/tellyscope/ui/Copy.kt
index a55cbfe..fff569b 100644
--- a/app/src/main/java/app/tellyscope/ui/Copy.kt
+++ b/app/src/main/java/app/tellyscope/ui/Copy.kt
@@ -103,7 +103,7 @@ object Copy {
if (service) append("\n\nNext, you choose whether to turn on its accessibility service.")
}
- /** No default home and no launcher of the TV's own on: nothing is turned off, so there's no Undo either. */
+ /** No default home and no launcher of the TV's own on: nothing is turned off, so Undo can only turn its service off. */
fun explainFirstHome(app: String, pkg: String, service: Boolean = false) = buildString {
append("Makes $app ($pkg) open when you press Home. The TV has no default home screen now, and nothing else is turned off.")
if (service) append("\n\nNext, you choose whether to turn on its accessibility service.")
diff --git a/app/src/test/java/app/tellyscope/HistoryBookTest.kt b/app/src/test/java/app/tellyscope/HistoryBookTest.kt
index 897936e..01a8581 100644
--- a/app/src/test/java/app/tellyscope/HistoryBookTest.kt
+++ b/app/src/test/java/app/tellyscope/HistoryBookTest.kt
@@ -6,6 +6,7 @@ import app.tellyscope.data.Action
import app.tellyscope.data.FakeDeviceSource
import app.tellyscope.data.HistoryBook
import app.tellyscope.data.HistoryEntry
+import app.tellyscope.data.firstHomeService
import app.tellyscope.data.historyKey
import app.tellyscope.data.restoreEach
import kotlinx.coroutines.test.runTest
@@ -141,6 +142,25 @@ class HistoryBookTest {
assertNull(book.openDisable("tv", "me.efesser.flauncher"))
}
+ @Test fun aFirstHomesUndoIsForItsServiceAlone() {
+ val book = HistoryBook()
+ val service = "com.spocky.projengmenu/.Service"
+ val first = book.record("tv", 0L, Action.Home, "com.spocky.projengmenu", "Projectivy Launcher", undo = Action.Enable,
+ previous = mapOf("to" to "com.spocky.projengmenu/.Main", "service" to service))
+ val back = book.record("tv", 1L, Action.HomeBack, "me.efesser.flauncher", "Google TV Home", undo = Action.Home)
+ assertEquals(service, first.firstHomeService)
+ // The Undo the consent promised, and still nothing for Restore to turn on.
+ assertEquals(Action.Enable, first.undo)
+ assertEquals(emptyList<String>(), HistoryBook.leftDisabled(book.entries("tv")))
+ // Turning the service off leaves Home where it is, so an older switch back keeps its Undo.
+ book.record("tv", 2L, Action.Change, "com.spocky.projengmenu", "Turned off Projectivy Launcher's accessibility service", undo = null, undoing = first.id)
+ assertEquals(Action.Home, book.entries("tv").first { it.id == back.id }.undo)
+ // A switch that replaced a launcher is no first home, service or not.
+ val switch = book.record("tv", 3L, Action.Home, "com.google.android.tvlauncher", "Projectivy Launcher", undo = Action.Enable,
+ previous = mapOf("from" to "com.google.android.tvlauncher/.Main", "service" to service))
+ assertNull(switch.firstHomeService)
+ }
+
@Test fun restoreAllTurnsOnOnlyWhatTellyscopeDisabled() = runTest {
// The fake TV starts with TV Recommendations already disabled.
val source = FakeDeviceSource()