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.
Per-screen refresh rates for the stats
jesse-agent-362ced332cea65e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1890c21..9334576 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,7 +13,7 @@ until this file has a section for its version.## Unreleased- **Fixed:** A slow stats read no longer drops the TV mid-change. [#99](../../pull/99)-- **Improved:** Overview and Apps read only memory per app, lighter on the TV. [#99](../../pull/99)+- **Improved:** Each screen reads only what it shows, at its own rate. [#99](../../pull/99)## 0.6.2 — 2026-10-25diff --git a/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt b/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.ktindex cecd943..62bf53e 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt@@ -38,7 +38,7 @@ class DeviceLink(private val _stats = MutableStateFlow<Stats?>(null)val stats: StateFlow<Stats?> = _stats.asStateFlow()- /** Pauses the stats while a change runs on this TV, and turns the process list on and off. */+ /** Pauses the stats while a change runs on this TV, and sets their refresh rates. */val gate = StatsGate()@Volatile var session: AdbSession? = nulldiff --git a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.ktindex 3e7544f..c1057dd 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt@@ -1,7 +1,6 @@// SPDX-License-Identifier: GPL-3.0-onlypackage app.tellyscope.adb-import kotlinx.coroutines.delayimport kotlinx.coroutines.flow.Flowimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.first@@ -58,15 +57,30 @@ enum class ProcessLevel {FULL,}+/**+ * How often one screen refreshes what it shows: the stat strip (CPU, RAM, network) every [fastEvery],+ * the process list at [processes] every [processEvery].+ */+data class StatsProfile(val fastEvery: Duration, val processes: ProcessLevel, val processEvery: Duration) {+ companion object {+ val OVERVIEW = StatsProfile(2.seconds, ProcessLevel.LIGHT, 2.seconds)+ val APPS = StatsProfile(10.seconds, ProcessLevel.LIGHT, 10.seconds)+ val ADVANCED = StatsProfile(2.seconds, ProcessLevel.FULL, 2.seconds)++ /** Files, Tweaks, Remote, Settings, Debloat, and every TV that isn't on screen. */+ val OTHER = StatsProfile(10.seconds, ProcessLevel.NONE, 10.seconds)+ }+}+/*** What the app tells one TV's stats loop. [during] pauses it while a change runs, so a slow read- * never competes with `pm`, an install or a Home switch; [processes] says how much of the process- * list the open screen shows.+ * 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.*/class StatsGate {private val running = MutableStateFlow(0)- val processes = MutableStateFlow(ProcessLevel.FULL)+ val profile = MutableStateFlow(StatsProfile.ADVANCED)val busy: Boolean get() = running.value > 0@@ -85,8 +99,10 @@ class StatsGate {}/**- * Live stats on three tiers: CPU/RAM/network speed every [fast], processes at [StatsGate.processes]'s- * level every [medium] and at once when it changes, identity/storage/uptime/temperature/packages every [slow].+ * Live stats on three tiers, at the rates of [StatsGate.profile]: CPU/RAM/network speed, the process+ * list (read at once when its level changes), and identity/storage/uptime/temperature/packages every+ * [slow]. Reads run one after another, never overlapping: a tier slower than its interval just+ * reads back to back. A new profile takes effect at once, not after the old interval.* The first emission comes after one fast interval, since CPU % needs two samples.* A read slower than [readTimeout] skips that sample. Shell failures throw, and so does a slow read* the TV then doesn't follow with an `echo` answer within [pingTimeout], so the caller can mark it@@ -94,33 +110,36 @@ class StatsGate {*/class DeviceMonitor(private val shell: AdbShell,- private val fast: Duration = 2.seconds,- private val medium: Duration = 10.seconds,private val slow: Duration = 60.seconds,private val readTimeout: Duration = 10.seconds,private val pingTimeout: Duration = 10.seconds,+ /** While a change runs, how often the TV is pinged instead of read. */+ private val pingEvery: Duration = 10.seconds,private val gate: StatsGate = StatsGate(),- /** Times network speed; tests pass the test scheduler's clock. */+ /** Times the tiers and network speed; tests pass the test scheduler's clock. */private val clock: TimeSource.WithComparableMarks = TimeSource.Monotonic,) : MonitorSource {override fun stats(): Flow<Stats> = flow {var slowStats = Stats(cpuPercent = null, memory = null)var lastSlow: TimeMark? = null- var mediumStats: Pair<List<AppProcess>, Boolean?> = emptyList<AppProcess>() to null- var lastMedium: TimeMark? = null+ var procStats: Pair<List<AppProcess>, Boolean?> = emptyList<AppProcess>() to null+ var lastProcs: TimeMark? = nullvar lastLevel = ProcessLevel.NONEvar prev: Fast? = nullwhile (true) {- // A skipped tier waits its whole interval again, so a TV that is always slow isn't asked every 2 s.+ 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 skipped tier waits its whole interval again, so a TV that is always slow isn't asked every round.if (lastSlow.due(slow)) {readSlow()?.let { slowStats = it }- lastSlow = TimeSource.Monotonic.markNow()+ lastSlow = started}- val level = gate.processes.value- if (level != ProcessLevel.NONE && (level != lastLevel || lastMedium.due(medium))) {- readProcesses(level, slowStats.packages, mediumStats.second)?.let { mediumStats = it }- lastMedium = TimeSource.Monotonic.markNow()+ val level = profile.processes+ if (level != ProcessLevel.NONE && (level != lastLevel || lastProcs.due(profile.processEvery))) {+ readProcesses(level, slowStats.packages, procStats.second)?.let { procStats = it }+ lastProcs = startedlastLevel = level}val now = readFast()@@ -131,14 +150,15 @@ class DeviceMonitor(slowStats.copy(cpuPercent = pct,memory = now.memory,- processes = mediumStats.first,- screenOn = mediumStats.second,+ processes = procStats.first,+ screenOn = procStats.second,netBytesPerSec = rate(last, now),),)}if (now != null) prev = now- delay(fast)+ val wait = profile.fastEvery - started.elapsedNow()+ if (wait.isPositive()) withTimeoutOrNull(wait) { gate.profile.first { it != profile } }}}@@ -147,10 +167,10 @@ class DeviceMonitor(/*** Null skips the sample: the read ran past [readTimeout] while a change was running, or the TV* still answered `echo` after it. While a change runs nothing is read, but the TV is pinged- * every [medium], so one that dies mid-change still ends the change instead of hanging it.+ * every [pingEvery], so one that dies mid-change still ends the change instead of hanging it.*/private suspend fun run(command: String): String? {- while (withTimeoutOrNull(medium) { gate.awaitIdle() } == null) ping()+ while (withTimeoutOrNull(pingEvery) { gate.awaitIdle() } == null) ping()withTimeoutOrNull(readTimeout) { shell.shell(command).output }?.let { return it }if (!gate.busy) ping()return nulldiff --git a/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.ktindex ec45c68..e59866d 100644--- a/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt+++ b/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt@@ -4,10 +4,12 @@ package app.tellyscope.adbimport kotlinx.coroutines.CompletableDeferredimport kotlinx.coroutines.ExperimentalCoroutinesApiimport kotlinx.coroutines.awaitCancellation+import kotlinx.coroutines.delayimport kotlinx.coroutines.flow.firstimport kotlinx.coroutines.flow.takeimport kotlinx.coroutines.flow.toListimport kotlinx.coroutines.launch+import kotlinx.coroutines.test.TestScopeimport kotlinx.coroutines.test.advanceTimeByimport kotlinx.coroutines.test.runCurrentimport kotlinx.coroutines.test.runTest@@ -153,8 +155,8 @@ class DeviceMonitorTest {assertEquals(listOf("echo"), ran)}- @Test fun noProcessLevelSkipsTheProcessRead() = runTest {- val gate = StatsGate().apply { processes.value = ProcessLevel.NONE }+ @Test fun otherScreensSkipTheProcessRead() = runTest {+ val gate = StatsGate().apply { profile.value = StatsProfile.OTHER }val tv = SlowTop()val s = DeviceMonitor(tv, gate = gate).stats().first()assertEquals(emptyList<AppProcess>(), s.processes)@@ -179,7 +181,7 @@ class DeviceMonitorTest {}@Test fun lightLevelReadsMemoryWithoutTop() = runTest {- val gate = StatsGate().apply { processes.value = ProcessLevel.LIGHT }+ val gate = StatsGate().apply { profile.value = StatsProfile.OVERVIEW }val tv = PsTv("PID USER RSS NAME\n2201 u0_a87 319488 com.google.android.youtube.tv\n512 system 122880 system_server\n")val s = DeviceMonitor(tv, gate = gate).stats().take(3).toList().last()assertEquals(@@ -195,7 +197,7 @@ class DeviceMonitorTest {@Test fun lightLevelFallsBackToPlainPs() = runTest {// A ps that ignores -o: only its header comes back, so plain ps is read instead.- val gate = StatsGate().apply { processes.value = ProcessLevel.LIGHT }+ val gate = StatsGate().apply { profile.value = StatsProfile.OVERVIEW }val tv = PsTv(ps = "USER PID PPID VSIZE RSS WCHAN PC NAME\n",plain = "USER PID PPID VSIZE RSS WCHAN PC NAME\nu0_a87 2201 300 1200000 319488 ffffffff 00000000 S com.google.android.youtube.tv\n",@@ -206,13 +208,56 @@ class DeviceMonitorTest {}@Test fun openingAdvancedReadsTheFullListAtOnce() = runTest {- val gate = StatsGate().apply { processes.value = ProcessLevel.LIGHT }+ // From Apps, whose round is 10 s: the switch doesn't wait that out.+ val gate = StatsGate().apply { profile.value = StatsProfile.APPS }val tv = PsTv("PID USER RSS NAME\n")- backgroundScope.launch { DeviceMonitor(tv, gate = gate).stats().collect {} }+ backgroundScope.launch { DeviceMonitor(tv, gate = gate, clock = testScheduler.timeSource).stats().collect {} }runCurrent()assertTrue(tv.ran.none { it.startsWith("top") })- gate.processes.value = ProcessLevel.FULL- advanceTimeBy(2_001)+ gate.profile.value = StatsProfile.ADVANCED+ runCurrent()assertTrue(tv.ran.any { it.startsWith("top") }, "ran: ${tv.ran}")}++ /** 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 }+ val tv = PsTv("PID USER RSS NAME\n2201 u0_a87 319488 com.google.android.youtube.tv\n")+ backgroundScope.launch { DeviceMonitor(tv, gate = gate, clock = testScheduler.timeSource).stats().collect {} }+ advanceTimeBy(20_001)+ return Triple(+ tv.ran.count { it.startsWith("cat /proc/stat") },+ tv.ran.count { it.startsWith("ps") },+ tv.ran.count { it.startsWith("top") },+ )+ }++ @Test fun overviewReadsStripAndMemoryEvery2s() = runTest { assertEquals(Triple(11, 11, 0), reads(StatsProfile.OVERVIEW)) }++ @Test fun appsReadsStripAndMemoryEvery10s() = runTest { assertEquals(Triple(3, 3, 0), reads(StatsProfile.APPS)) }++ @Test fun advancedReadsStripAndTopEvery2s() = runTest { assertEquals(Triple(11, 0, 11), reads(StatsProfile.ADVANCED)) }++ @Test fun otherScreensReadOnlyTheStripEvery10s() = runTest { assertEquals(Triple(3, 0, 0), reads(StatsProfile.OTHER)) }++ @Test fun aTopSlowerThanTheRoundReadsBackToBackWithoutOverlap() = runTest {+ var inFlight = 0+ var most = 0+ var tops = 0+ val tv = AdbShell { cmd ->+ inFlight+++ most = maxOf(most, inFlight)+ try {+ if (cmd.startsWith("top")) { tops++; delay(3_000) }+ if (cmd.startsWith("cat /proc/stat")) ok("cpu 25 0 0 75 0 0 0 0${SEP}MemTotal: 2000 kB\n") else ok("")+ } finally {+ inFlight--+ }+ }+ val gate = StatsGate().apply { profile.value = StatsProfile.ADVANCED }+ backgroundScope.launch { DeviceMonitor(tv, gate = gate, clock = testScheduler.timeSource).stats().collect {} }+ advanceTimeBy(12_001)+ assertEquals(1, most)+ assertEquals(5, tops) // at 0, 3, 6, 9 and 12 s: one after another, none queued up+ }}diff --git a/app/src/main/java/app/tellyscope/MainActivity.kt b/app/src/main/java/app/tellyscope/MainActivity.ktindex 26dc05a..c3c963d 100644--- a/app/src/main/java/app/tellyscope/MainActivity.kt+++ b/app/src/main/java/app/tellyscope/MainActivity.kt@@ -37,7 +37,7 @@ import androidx.lifecycle.viewmodel.compose.viewModelimport androidx.lifecycle.viewmodel.initializerimport androidx.lifecycle.viewmodel.viewModelFactoryimport app.tellyscope.adb.Connector-import app.tellyscope.adb.ProcessLevel+import app.tellyscope.adb.StatsProfileimport app.tellyscope.data.AdbDeviceSourceimport app.tellyscope.data.AndroidPhoneStorageimport app.tellyscope.data.FakeDeviceSource@@ -212,8 +212,8 @@ fun TellyscopeApp(vm: TellyscopeViewModel) {} else if (advanced) {BackHandler { advanced = false }DisposableEffect(Unit) {- vm.showProcesses(ProcessLevel.FULL)- onDispose { vm.showProcesses(ProcessLevel.NONE) }+ vm.showStats(StatsProfile.ADVANCED)+ onDispose { vm.showStats(StatsProfile.OTHER) }}AdvancedScreen(state, back = { advanced = false }, snackbar = snackbar)} else if (settings) {@@ -278,7 +278,7 @@ fun TellyscopeApp(vm: TellyscopeViewModel) {answerRequest = vm::answerRequest,dismissRequest = vm::dismissRequest,foldSystem = vm::foldSystem,- showProcesses = vm::showProcesses,+ showStats = vm::showStats,),)}diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.ktindex 09c838f..176efb3 100644--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt@@ -31,8 +31,8 @@ import app.tellyscope.adb.ProtectedReasonimport app.tellyscope.adb.Remoteimport app.tellyscope.adb.RemoteKeyimport app.tellyscope.adb.Stats+import app.tellyscope.adb.StatsProfileimport app.tellyscope.adb.PairResult-import app.tellyscope.adb.ProcessLevelimport app.tellyscope.adb.pairOnFreshPortimport app.tellyscope.adb.Tweakimport app.tellyscope.adb.Tweaks@@ -71,8 +71,8 @@ class AdbDeviceSource(context: Context) : DeviceSource {private val appContext = context.applicationContextprivate val scope = CoroutineScope(SupervisorJob())private val links = ConcurrentHashMap<String, DeviceLink>()- /** The TV whose process list is on screen, and how much of it. */- @Volatile private var processesFor: Pair<String, ProcessLevel>? = null+ /** The TV on screen and its screen's refresh rates; every other TV reads at [StatsProfile.OTHER]. */+ @Volatile private var statsFor: Pair<String, StatsProfile>? = nullprivate val hits = ConcurrentHashMap<String, ScanHit>()private val store = DeviceStore(File(appContext.filesDir, "devices.tsv"))private val saved = ConcurrentHashMap<String, SavedDevice>()@@ -114,7 +114,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {val link = DeviceLink(host, port, scope, knownGood = knownGood, open = { h, p ->pending?.also { pending = null } ?: Connector.connect(h, p)})- link.gate.processes.value = processesFor?.takeIf { it.first == host }?.second ?: ProcessLevel.NONE+ link.gate.profile.value = statsFor?.takeIf { it.first == host }?.second ?: StatsProfile.OTHERlinks.put(host, link)?.disconnect()watchers.put(host, scope.launch {// Save a fresh reading only when what's kept of it changed, not every 2 s.@@ -307,9 +307,9 @@ class AdbDeviceSource(context: Context) : DeviceSource {}}- override fun showProcesses(deviceId: String?, level: ProcessLevel) {- processesFor = deviceId?.let { it to level }- links.forEach { (host, link) -> link.gate.processes.value = if (host == deviceId) level else ProcessLevel.NONE }+ override fun showStats(deviceId: String?, profile: StatsProfile) {+ statsFor = deviceId?.let { it to profile }+ links.forEach { (host, link) -> link.gate.profile.value = if (host == deviceId) profile else StatsProfile.OTHER }}/** Runs a change with that TV's stats paused, so a slow stats read can't compete with it or cut it off. */diff --git a/app/src/main/java/app/tellyscope/data/DeviceSource.kt b/app/src/main/java/app/tellyscope/data/DeviceSource.ktindex d4a1034..53dc6a8 100644--- a/app/src/main/java/app/tellyscope/data/DeviceSource.kt+++ b/app/src/main/java/app/tellyscope/data/DeviceSource.kt@@ -2,7 +2,7 @@package app.tellyscope.data-import app.tellyscope.adb.ProcessLevel+import app.tellyscope.adb.StatsProfileimport app.tellyscope.adb.ApkInfoimport app.tellyscope.adb.ConnectionRequestimport app.tellyscope.adb.InstallResult@@ -52,8 +52,8 @@ interface DeviceSource {/** Fast-tier stats. Completes when the device goes offline. */fun live(deviceId: String): Flow<Snapshot>- /** Reads the process list at [level] only for [deviceId], while a screen shows it; null for none. */- fun showProcesses(deviceId: String?, level: ProcessLevel) {}+ /** Reads [deviceId] at its screen's [profile]; every other TV, or all of them for null, at the slowest. */+ fun showStats(deviceId: String?, profile: StatsProfile) {}suspend fun apps(deviceId: String): List<AppInfo>diff --git a/app/src/main/java/app/tellyscope/ui/DeviceScreen.kt b/app/src/main/java/app/tellyscope/ui/DeviceScreen.ktindex 0d3a02e..05e6a36 100644--- a/app/src/main/java/app/tellyscope/ui/DeviceScreen.kt+++ b/app/src/main/java/app/tellyscope/ui/DeviceScreen.kt@@ -52,7 +52,7 @@ import androidx.compose.ui.graphics.Colorimport androidx.compose.ui.graphics.asImageBitmapimport androidx.compose.ui.text.style.TextOverflowimport androidx.compose.ui.unit.dp-import app.tellyscope.adb.ProcessLevel+import app.tellyscope.adb.StatsProfileimport app.tellyscope.adb.TvFileimport app.tellyscope.adb.TvFolderimport app.tellyscope.data.Action@@ -110,8 +110,8 @@ data class DeviceActions(val dismissRequest: () -> Unit = {},/** Folds an Apps tab's System section away, or opens it; remembered across restarts. */val foldSystem: (AppsTab, Boolean) -> Unit = { _, _ -> },- /** How much of the process list the open tab shows; the TV's process read follows it. */- val showProcesses: (ProcessLevel) -> Unit = {},+ /** The open tab's refresh rates; the TV's stats reads follow them. */+ val showStats: (StatsProfile) -> Unit = {},)/** Off while the remote is made faster: the device page hides its Remote button. */diff --git a/app/src/main/java/app/tellyscope/ui/DeviceTabs.kt b/app/src/main/java/app/tellyscope/ui/DeviceTabs.ktindex 57da5d6..7aa5498 100644--- a/app/src/main/java/app/tellyscope/ui/DeviceTabs.kt+++ b/app/src/main/java/app/tellyscope/ui/DeviceTabs.kt@@ -93,7 +93,7 @@ import androidx.compose.ui.text.buildAnnotatedStringimport androidx.compose.ui.text.font.FontWeightimport androidx.compose.ui.text.withStyleimport androidx.compose.ui.unit.dp-import app.tellyscope.adb.ProcessLevel+import app.tellyscope.adb.StatsProfileimport app.tellyscope.adb.TvFolderimport app.tellyscope.data.Actionimport app.tellyscope.data.AppInfo@@ -175,10 +175,17 @@ fun DeviceTabsScreen(// "See changes" lands on Tweaks scrolled to Recent changes, the list's third item, after the hero.var toChanges by remember { mutableStateOf(false) }var shown by rememberSaveable { mutableStateOf(tab) }- // Overview and Apps (Running, sorted by RAM) need memory per app only; Files and Tweaks need no read.+ // Each tab refreshes what it shows at its own rate: Overview and Apps (Running, sorted by RAM) need+ // memory per app only, Files and Tweaks no processes at all.DisposableEffect(tab, device.id) {- actions.showProcesses(if (tab == TvTab.Overview || tab == TvTab.Apps) ProcessLevel.LIGHT else ProcessLevel.NONE)- onDispose { actions.showProcesses(ProcessLevel.NONE) }+ actions.showStats(+ when (tab) {+ TvTab.Overview -> StatsProfile.OVERVIEW+ TvTab.Apps -> StatsProfile.APPS+ TvTab.Files, TvTab.Tweaks -> StatsProfile.OTHER+ },+ )+ onDispose { actions.showStats(StatsProfile.OTHER) }}LaunchedEffect(tab) {if (tab == shown) return@LaunchedEffectdiff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.ktindex 6938add..b46c081 100644--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt@@ -7,7 +7,7 @@ import androidx.lifecycle.ViewModelimport androidx.lifecycle.viewModelScopeimport app.tellyscope.adb.ConnectionRequestimport app.tellyscope.adb.InstallKind-import app.tellyscope.adb.ProcessLevel+import app.tellyscope.adb.StatsProfileimport app.tellyscope.adb.InstallResultimport app.tellyscope.adb.InstalledVersionimport app.tellyscope.adb.Progress@@ -334,8 +334,8 @@ class TellyscopeViewModel(if (transferJobs[_state.value.selectedId]?.isActive != true) select(device.id)}- /** Only the TV on screen reads its process list, and only as much as the screen shows. */- fun showProcesses(level: ProcessLevel) = source.showProcesses(_state.value.selectedId, level)+ /** The TV on screen refreshes at the open screen's rates; the others at the slowest. */+ fun showStats(profile: StatsProfile) = source.showStats(_state.value.selectedId, profile)fun select(id: String?) {liveJob?.cancel()