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.

Re-read running apps as soon as an app action ends

jesse-agent-23253149f41d071
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85a2c7c..b1a2dbb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ until this file has a section for its version.
## Unreleased
+- **Fixed:** A stopped or turned-off app leaves Running right away, not seconds later. [#PR](../../pull/PR)
+
## 0.11.2 — 2026-10-28
- **Fixed:** An unpaired TV on wireless debugging asks for the pairing code, not "didn't answer". [#200](../../pull/200)
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
index 7eaf6fb..48ab270 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
@@ -3,6 +3,7 @@ package app.tellyscope.adb
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.update
@@ -77,6 +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.
*/
class StatsGate {
private val running = MutableStateFlow(0)
@@ -85,14 +87,18 @@ class StatsGate {
val paused = MutableStateFlow(false)
+ val appsChanged = MutableStateFlow(0)
+
val busy: Boolean get() = running.value > 0
- suspend fun <T> during(block: suspend () -> T): T {
+ /** [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 {
running.update { it + 1 }
try {
return block()
} finally {
running.update { it - 1 }
+ if (apps) appsChanged.update { it + 1 }
}
}
@@ -132,6 +138,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 prev: Fast? = null
while (true) {
@@ -149,11 +156,14 @@ class DeviceMonitor(
lastSlow = started
}
val level = profile.processes
- if (level != ProcessLevel.NONE && (level != lastLevel || lastProcs.due(profile.processEvery))) {
+ // 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)) {
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) {
@@ -170,7 +180,11 @@ class DeviceMonitor(
}
if (now != null) prev = now
val wait = profile.fastEvery - started.elapsedNow()
- if (wait.isPositive()) withTimeoutOrNull(wait) { gate.profile.first { it != profile } }
+ if (wait.isPositive()) {
+ withTimeoutOrNull(wait) {
+ combine(gate.profile, gate.appsChanged) { p, c -> p != profile || (level != ProcessLevel.NONE && 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 0bbd2a6..145448b 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
@@ -246,6 +246,32 @@ class DeviceMonitorTest {
assertTrue(tv.ran.any { it.startsWith("top") }, "ran: ${tv.ran}")
}
+ @Test fun anAppStoppedOnAppsLeavesTheListAtOnce() = runTest {
+ // Apps reads processes every 10 s: a stopped app must not linger in Running until the next one.
+ val gate = StatsGate().apply { profile.value = StatsProfile.APPS }
+ val tv = PsTv("PID USER RSS NAME\n2201 u0_a87 319488 com.google.android.youtube.tv\n")
+ val readings = mutableListOf<Stats>()
+ backgroundScope.launch { DeviceMonitor(tv, gate = gate, clock = testScheduler.timeSource).stats().collect { readings += it } }
+ advanceTimeBy(10_001)
+ assertEquals(listOf(2201), readings.last().processes.map { it.pid })
+
+ advanceTimeBy(1_000)
+ gate.during(apps = true) { tv.ps = "PID USER RSS NAME\n" }
+ runCurrent()
+ assertEquals(emptyList(), readings.last().processes)
+ }
+
+ @Test fun aKeyPressDoesNotReadProcessesAgain() = runTest {
+ val gate = StatsGate().apply { profile.value = StatsProfile.APPS }
+ val tv = PsTv("PID USER RSS NAME\n")
+ backgroundScope.launch { DeviceMonitor(tv, gate = gate, clock = testScheduler.timeSource).stats().collect {} }
+ advanceTimeBy(11_000)
+ val before = tv.ran.count { it.startsWith("ps") }
+ gate.during {}
+ runCurrent()
+ assertEquals(before, tv.ran.count { it.startsWith("ps") })
+ }
+
/** Commands each screen's profile runs in its first 20 s: (fast reads, ps, top). */
private fun TestScope.reads(profile: StatsProfile): Triple<Int, Int, Int> {
val gate = StatsGate().apply { this.profile.value = profile }
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index 9004642..c4f69f7 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -362,9 +362,12 @@ class AdbDeviceSource(context: Context) : DeviceSource {
links.values.forEach { it.gate.paused.value = paused }
}
- /** Runs a change with that TV's stats paused, so a slow stats read can't compete with it or cut it off. */
- private suspend fun <T> busy(deviceId: String, block: suspend () -> T): T =
- links[deviceId]?.gate?.during(block) ?: block()
+ /**
+ * 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.
+ */
+ private suspend fun <T> busy(deviceId: String, apps: Boolean = false, block: suspend () -> T): T =
+ links[deviceId]?.gate?.during(apps, block) ?: block()
override suspend fun apps(deviceId: String): List<AppInfo> {
val session = session(deviceId)
@@ -467,10 +470,10 @@ class AdbDeviceSource(context: Context) : DeviceSource {
}
override suspend fun act(deviceId: String, action: Action, packageName: String, undoing: Long?): HistoryEntry =
- busy(deviceId) { actNow(deviceId, action, packageName, undoing) }
+ busy(deviceId, apps = true) { actNow(deviceId, action, packageName, undoing) }
override suspend fun makeHome(deviceId: String, packageName: String, service: Boolean): HistoryEntry =
- busy(deviceId) { actNow(deviceId, Action.Home, packageName, null, service) }
+ busy(deviceId, apps = true) { actNow(deviceId, Action.Home, packageName, null, service) }
override suspend fun homeServiceOn(deviceId: String, packageName: String): Unit = busy(deviceId) {
val actions = Actions(session(deviceId))
@@ -735,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) { Actions(session(deviceId)).run(AdbAction.OPEN, packageName) is ActionResult.Done }
+ busy(deviceId, apps = true) { Actions(session(deviceId)).run(AdbAction.OPEN, packageName) is ActionResult.Done }
override suspend fun dock(deviceId: String): List<Docked> {
restored.await()