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.

Show every action's result right after it ends

jesse-agent-232b6e1dc19af3f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b4b8ee0..ebdc270 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,11 @@ until this file has a section for its version.
## Unreleased
-- **Fixed:** A stopped or turned-off app leaves Running right away, not seconds later. [#202](../../pull/202)
+- **Fixed:** The TV's page shows the result of a change right away. [#202](../../pull/202)
+ - A stopped or turned-off app leaves Running at once, and an opened one joins it.
+ - Free storage updates right after clearing caches, installing, uninstalling or uploading.
+ - A change that fails partway shows what the TV has now.
+ - A device you allow shows under access straight away.
## 0.11.2 — 2026-10-28
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
index 48ab270..2a69a17 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
@@ -78,7 +78,7 @@ data class StatsProfile(val fastEvery: Duration, val processes: ProcessLevel, va
* never competes with `pm`, an install or a Home switch; [profile] is the open screen's refresh
* rates. Until a screen sets one, everything is read every 2 s. [paused] is the app in the
* background: no stats are read and a dropped link doesn't retry until it's false again.
- * [appsChanged] counts the changes to apps that have ended, so the process list is read again right after one.
+ * [changed] counts the ended changes to apps or storage, so what they touch is read again right after one.
*/
class StatsGate {
private val running = MutableStateFlow(0)
@@ -87,18 +87,21 @@ class StatsGate {
val paused = MutableStateFlow(false)
- val appsChanged = MutableStateFlow(0)
+ val changed = MutableStateFlow(0)
val busy: Boolean get() = running.value > 0
- /** [apps]: the change stops, starts, disables or removes an app, so what's running may differ after it. */
- suspend fun <T> during(apps: Boolean = false, block: suspend () -> T): T {
+ /**
+ * [reread]: the change stops, starts, disables, installs or removes an app, or frees or fills storage,
+ * so the process list, storage and the package list are read again once it ends.
+ */
+ suspend fun <T> during(reread: Boolean = false, block: suspend () -> T): T {
running.update { it + 1 }
try {
return block()
} finally {
running.update { it - 1 }
- if (apps) appsChanged.update { it + 1 }
+ if (reread) changed.update { it + 1 }
}
}
@@ -138,7 +141,7 @@ class DeviceMonitor(
var procStats: Pair<List<AppProcess>, Boolean?> = emptyList<AppProcess>() to null
var lastProcs: TimeMark? = null
var lastLevel = ProcessLevel.NONE
- var seenChanged = gate.appsChanged.value
+ var seenChanged = gate.changed.value
var prev: Fast? = null
while (true) {
@@ -150,20 +153,22 @@ class DeviceMonitor(
val profile = gate.profile.value
// Tiers are timed from the start of the round they ran in, so a 2 s tier runs every round.
val started = clock.markNow()
+ // A change just ended (an app stopped, installed, uninstalled; caches cleared): what it touched
+ // can't wait out its interval. Storage and packages first, so a new app's processes are named.
+ val changed = gate.changed.value
+ val fresh = changed != seenChanged
+ seenChanged = changed
// A skipped tier waits its whole interval again, so a TV that is always slow isn't asked every round.
- if (lastSlow.due(slow)) {
+ if (lastSlow.due(slow) || fresh) {
readSlow()?.let { slowStats = it }
lastSlow = started
}
val level = profile.processes
- // A change just ended (an app stopped, disabled, uninstalled): the Running list can't wait out the interval.
- val changed = gate.appsChanged.value
- if (level != ProcessLevel.NONE && (level != lastLevel || lastProcs.due(profile.processEvery) || changed != seenChanged)) {
+ if (level != ProcessLevel.NONE && (level != lastLevel || lastProcs.due(profile.processEvery) || fresh)) {
readProcesses(level, slowStats.packages, procStats.second)?.let { procStats = it }
lastProcs = started
lastLevel = level
}
- seenChanged = changed
val now = readFast()
val last = prev
if (now != null && last != null) {
@@ -182,7 +187,7 @@ class DeviceMonitor(
val wait = profile.fastEvery - started.elapsedNow()
if (wait.isPositive()) {
withTimeoutOrNull(wait) {
- combine(gate.profile, gate.appsChanged) { p, c -> p != profile || (level != ProcessLevel.NONE && c != seenChanged) }.first { it }
+ combine(gate.profile, gate.changed) { p, c -> p != profile || c != seenChanged }.first { it }
}
}
}
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
index 145448b..7469c1f 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
@@ -256,11 +256,29 @@ class DeviceMonitorTest {
assertEquals(listOf(2201), readings.last().processes.map { it.pid })
advanceTimeBy(1_000)
- gate.during(apps = true) { tv.ps = "PID USER RSS NAME\n" }
+ gate.during(reread = true) { tv.ps = "PID USER RSS NAME\n" }
runCurrent()
assertEquals(emptyList(), readings.last().processes)
}
+ @Test fun clearingCachesShowsTheFreedStorageAtOnce() = runTest {
+ // Storage is read once a minute; on Files or Tweaks, which read no processes, a change still brings it.
+ val gate = StatsGate().apply { profile.value = StatsProfile.OTHER }
+ var used = 9800
+ val tv = AdbShell { cmd ->
+ if (cmd.startsWith("cat /proc/stat")) ok("cpu 25 0 0 75 0 0 0 0\n${sep}MemTotal: 2000 kB\n")
+ else ok("$sep" + "Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/x 16000 $used 0 0% /data\n$sep$sep$sep")
+ }
+ val readings = mutableListOf<Stats>()
+ backgroundScope.launch { DeviceMonitor(tv, gate = gate, clock = testScheduler.timeSource).stats().collect { readings += it } }
+ advanceTimeBy(10_001)
+ assertEquals(Parsers.Storage(16000, 9800), readings.last().storage)
+
+ gate.during(reread = true) { used = 7000 }
+ runCurrent()
+ assertEquals(Parsers.Storage(16000, 7000), readings.last().storage)
+ }
+
@Test fun aKeyPressDoesNotReadProcessesAgain() = runTest {
val gate = StatsGate().apply { profile.value = StatsProfile.APPS }
val tv = PsTv("PID USER RSS NAME\n")
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index c4f69f7..005fb9f 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -364,10 +364,10 @@ class AdbDeviceSource(context: Context) : DeviceSource {
/**
* Runs a change with that TV's stats paused, so a slow stats read can't compete with it or cut it off.
- * [apps]: it stops, starts, disables or removes an app, so the Running list is read again once it ends.
+ * [reread]: it changes apps or storage, so the stats it touches are read again once it ends.
*/
- private suspend fun <T> busy(deviceId: String, apps: Boolean = false, block: suspend () -> T): T =
- links[deviceId]?.gate?.during(apps, block) ?: block()
+ private suspend fun <T> busy(deviceId: String, reread: Boolean = false, block: suspend () -> T): T =
+ links[deviceId]?.gate?.during(reread, block) ?: block()
override suspend fun apps(deviceId: String): List<AppInfo> {
val session = session(deviceId)
@@ -470,12 +470,12 @@ class AdbDeviceSource(context: Context) : DeviceSource {
}
override suspend fun act(deviceId: String, action: Action, packageName: String, undoing: Long?): HistoryEntry =
- busy(deviceId, apps = true) { actNow(deviceId, action, packageName, undoing) }
+ busy(deviceId, reread = true) { actNow(deviceId, action, packageName, undoing) }
override suspend fun makeHome(deviceId: String, packageName: String, service: Boolean): HistoryEntry =
- busy(deviceId, apps = true) { actNow(deviceId, Action.Home, packageName, null, service) }
+ busy(deviceId, reread = true) { actNow(deviceId, Action.Home, packageName, null, service) }
- override suspend fun homeServiceOn(deviceId: String, packageName: String): Unit = busy(deviceId) {
+ override suspend fun homeServiceOn(deviceId: String, packageName: String): Unit = busy(deviceId, reread = true) {
val actions = Actions(session(deviceId))
val label = labels[packageName] ?: AppNames.fallback(packageName)
val service = packageName.takeIf { it in serviceLaunchers }?.let { actions.homeService(it) }
@@ -738,7 +738,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
override suspend fun openUpdates(deviceId: String) = Remote(session(deviceId)).openUpdates()
override suspend fun launch(deviceId: String, packageName: String) =
- busy(deviceId, apps = true) { Actions(session(deviceId)).run(AdbAction.OPEN, packageName) is ActionResult.Done }
+ busy(deviceId, reread = true) { Actions(session(deviceId)).run(AdbAction.OPEN, packageName) is ActionResult.Done }
override suspend fun dock(deviceId: String): List<Docked> {
restored.await()
@@ -779,7 +779,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
}
override suspend fun clearCaches(deviceId: String): Long? {
- val freed = busy(deviceId) { Tweaks(session(deviceId)).clearCaches() }
+ val freed = busy(deviceId, reread = true) { Tweaks(session(deviceId)).clearCaches() }
book.record(key(deviceId), System.currentTimeMillis(), Action.ClearCaches, "", TvSettings.CACHES, undo = null)
return freed
}
@@ -861,12 +861,12 @@ class AdbDeviceSource(context: Context) : DeviceSource {
apk: ApkInfo,
allowDowngrade: Boolean,
onProgress: (Progress) -> Unit,
- ): InstallResult = busy(deviceId) { Sideload(session(deviceId)).install(apks, apk, allowDowngrade, onProgress) }
+ ): InstallResult = busy(deviceId, reread = true) { Sideload(session(deviceId)).install(apks, apk, allowDowngrade, onProgress) }
override suspend fun files(deviceId: String, folder: TvFolder) = Files(session(deviceId)).list(folder)
override suspend fun push(deviceId: String, folder: TvFolder, name: String, size: Long?, source: Source, onProgress: (Progress) -> Unit) =
- busy(deviceId) { Files(session(deviceId)).push(source, size, name, folder, onProgress) }
+ busy(deviceId, reread = true) { Files(session(deviceId)).push(source, size, name, folder, onProgress) }
override suspend fun pull(deviceId: String, folder: TvFolder, file: TvFile, sink: Sink, onProgress: (Progress) -> Unit) =
busy(deviceId) { Files(session(deviceId)).pull(folder, file, sink, onProgress) }
diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
index d5925df..60234d8 100644
--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
@@ -701,8 +701,8 @@ class TellyscopeViewModel(
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
- updateDevice(id) { it.copy(busy = null) }
_notice.value = e.message?.replaceFirstChar(Char::uppercase) ?: "That didn't work."
+ rereadAfterFailure(id)
return@launch
}
val history = source.history(id)
@@ -949,9 +949,9 @@ class TellyscopeViewModel(
throw e
} catch (e: Exception) {
// Anything else (a dropped socket) still must not crash the app.
- updateDevice(id) { it.copy(busy = null) }
undoing?.let(undoInFlight::remove) // a failed Undo can be tried again
_notice.value = e.message?.replaceFirstChar(Char::uppercase) ?: "That didn't work."
+ rereadAfterFailure(id)
return@launch
}
val history = source.history(id)
@@ -961,6 +961,16 @@ class TellyscopeViewModel(
}
}
+ /**
+ * A change can fail halfway (a Home switch whose old launcher didn't come back, recorded first),
+ * so the rows and History show what the TV has now. A TV that dropped keeps what was shown.
+ */
+ private suspend fun rereadAfterFailure(id: String) {
+ val apps = runCatching { source.apps(id) }.getOrNull()
+ val history = runCatching { source.history(id) }.getOrNull()
+ updateDevice(id) { it.copy(apps = apps ?: it.apps, busy = null, history = history ?: it.history) }
+ }
+
/** The user picked one or more files to install. Reads them, then asks the TV what it has. */
fun pickedApp(uris: List<Uri>) {
val id = _state.value.selectedId ?: return
@@ -1251,6 +1261,8 @@ class TellyscopeViewModel(
else -> Copy.REQUEST_DENIED
}
updateDevice(id) { it.copy(busy = null, request = null) }
+ // An allowed device joins who has access now, not at the next 10 s read.
+ if (ok) readAccess(id)
}
}
diff --git a/app/src/test/java/app/tellyscope/StateAfterActionTest.kt b/app/src/test/java/app/tellyscope/StateAfterActionTest.kt
new file mode 100644
index 0000000..5d2a35b
--- /dev/null
+++ b/app/src/test/java/app/tellyscope/StateAfterActionTest.kt
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-3.0-only
+
+package app.tellyscope
+
+import androidx.lifecycle.viewModelScope
+import app.tellyscope.adb.ConnectionRequest
+import app.tellyscope.data.AccessKey
+import app.tellyscope.data.Action
+import app.tellyscope.data.AppInfo
+import app.tellyscope.data.Device
+import app.tellyscope.data.DeviceSource
+import app.tellyscope.data.DeviceState
+import app.tellyscope.data.FakeDeviceSource
+import app.tellyscope.data.HistoryEntry
+import app.tellyscope.data.ScanHit
+import app.tellyscope.data.ScanKind
+import app.tellyscope.data.Snapshot
+import app.tellyscope.data.TvAccess
+import app.tellyscope.ui.TellyscopeViewModel
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.emptyFlow
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.TestScope
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runCurrent
+import kotlinx.coroutines.test.setMain
+import org.junit.After
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Before
+import org.junit.Test
+
+/** One online TV whose app is turned off by a change that then fails, and that asks to allow a device. */
+private class HalfwayTv : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {
+ var appOn = true
+ val history = mutableListOf<HistoryEntry>()
+ var request: ConnectionRequest? = null
+ var keys = emptyList<AccessKey>()
+
+ override suspend fun known() = listOf(Device("tv", ScanHit("tv", 5555, ScanKind.Classic, "tv", null), DeviceState.Online, null, null, null, 0L))
+
+ override fun live(deviceId: String): Flow<Snapshot> = emptyFlow()
+
+ override suspend fun apps(deviceId: String) = listOf(AppInfo("com.example", "App", null, false, appOn, null, ""))
+
+ override suspend fun act(deviceId: String, action: Action, packageName: String, undoing: Long?): HistoryEntry {
+ // Recorded and done on the TV, then a later step fails, as a Home switch's way back can.
+ appOn = false
+ history += HistoryEntry(1, deviceId, 0L, action, packageName, "App", Action.Enable, null)
+ throw IllegalStateException("the old launcher didn't come back")
+ }
+
+ override suspend fun history(deviceId: String) = history.toList()
+
+ override suspend fun connectionRequest(deviceId: String) = request
+
+ override suspend fun answerRequest(deviceId: String, request: ConnectionRequest, allow: Boolean, always: Boolean): Boolean {
+ this.request = null
+ keys = listOf(AccessKey(request.fingerprint, "laptop", null))
+ return true
+ }
+
+ override suspend fun access(deviceId: String) = TvAccess(allowed = keys)
+}
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class StateAfterActionTest {
+ private val dispatcher = StandardTestDispatcher()
+ private val test = TestScope(dispatcher)
+ private val source = HalfwayTv()
+ private lateinit var vm: TellyscopeViewModel
+
+ @Before fun setUp() = Dispatchers.setMain(dispatcher)
+
+ @After fun tearDown() {
+ vm.viewModelScope.cancel()
+ Dispatchers.resetMain()
+ }
+
+ @Test fun aChangeThatFailsHalfwayShowsWhatTheTvHasNow() = with(test) {
+ vm = TellyscopeViewModel(source, clock = { 0L })
+ runCurrent()
+ vm.propose("com.example", Action.Disable)
+ vm.confirm()
+ runCurrent()
+
+ val page = vm.state.value.device
+ assertEquals(listOf(false), page.apps.map { it.enabled })
+ assertEquals(1, page.history.size)
+ assertNull(page.busy)
+ }
+
+ @Test fun anAllowedDeviceShowsInAccessAtOnce() = with(test) {
+ source.request = ConnectionRequest("AA:BB")
+ vm = TellyscopeViewModel(source, clock = { 0L })
+ runCurrent()
+ vm.answerRequest(allow = true, always = true)
+ runCurrent()
+ // Well before the next 10 s read of who has access.
+ assertEquals(listOf("AA:BB"), vm.state.value.device.access?.allowed?.map { it.fingerprint })
+ }
+}