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.

Read the TV again once per Debloat, not once per app

jesse-agent-23218328caa30b6
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
index 2a69a17..abf35f3 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/DeviceMonitor.kt
@@ -12,6 +12,7 @@ import java.io.IOException
import java.time.YearMonth
import kotlin.time.ComparableTimeMark
import kotlin.time.Duration
+import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeMark
import kotlin.time.TimeSource
@@ -81,7 +82,7 @@ data class StatsProfile(val fastEvery: Duration, val processes: ProcessLevel, va
* [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)
+ internal val running = MutableStateFlow(0)
val profile = MutableStateFlow(StatsProfile.ADVANCED)
@@ -131,6 +132,8 @@ class DeviceMonitor(
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,
+ /** How long changes must stay quiet, none running, before what they touched is read again. */
+ private val settle: Duration = 300.milliseconds,
private val gate: StatsGate = StatsGate(),
/** Times the tiers and network speed; tests pass the test scheduler's clock. */
private val clock: TimeSource.WithComparableMarks = TimeSource.Monotonic,
@@ -155,9 +158,8 @@ class DeviceMonitor(
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
+ val fresh = gate.changed.value != seenChanged
+ if (fresh) seenChanged = settled()
// A skipped tier waits its whole interval again, so a TV that is always slow isn't asked every round.
if (lastSlow.due(slow) || fresh) {
readSlow()?.let { slowStats = it }
@@ -195,13 +197,33 @@ class DeviceMonitor(
private fun TimeMark?.due(every: Duration) = this == null || elapsedNow() >= every
+ /**
+ * Waits until no change runs and none has ended for [settle], and returns the count then: a
+ * Debloat, or taps on several rows, are read again once at the end rather than after each app.
+ */
+ private suspend fun settled(): Int {
+ while (true) {
+ awaitIdle()
+ val count = gate.changed.value
+ val stirred = withTimeoutOrNull(settle) {
+ combine(gate.changed, gate.running) { c, r -> c != count || r > 0 }.first { it }
+ }
+ if (stirred == null) return count
+ }
+ }
+
+ /** Pings while a change runs, so one that dies mid-change still ends instead of hanging. */
+ private suspend fun awaitIdle() {
+ while (withTimeoutOrNull(pingEvery) { gate.awaitIdle() } == null) ping()
+ }
+
/**
* 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 [pingEvery], so one that dies mid-change still ends the change instead of hanging it.
*/
private suspend fun run(command: String): String? {
- while (withTimeoutOrNull(pingEvery) { gate.awaitIdle() } == null) ping()
+ awaitIdle()
withTimeoutOrNull(readTimeout) { shell.shell(command).output }?.let { return it }
if (!gate.busy) ping()
return null
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
index 7469c1f..fc9a05b 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/DeviceMonitorTest.kt
@@ -257,10 +257,40 @@ class DeviceMonitorTest {
advanceTimeBy(1_000)
gate.during(reread = true) { tv.ps = "PID USER RSS NAME\n" }
- runCurrent()
+ advanceTimeBy(301)
assertEquals(emptyList(), readings.last().processes)
}
+ @Test fun aDebloatIsReadAgainOnceAtTheEnd() = 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(1_000)
+ val before = tv.ran.size
+ // Five apps turned off one by one, each its own change inside the batch, each slower than the settle.
+ gate.during(reread = true) { repeat(5) { gate.during(reread = true) { delay(500) } } }
+ advanceTimeBy(301)
+ val after = tv.ran.drop(before)
+ assertEquals(1, after.count { it.startsWith("getprop") }, "ran: $after")
+ assertEquals(1, after.count { it.startsWith("ps") }, "ran: $after")
+ }
+
+ @Test fun quickTapsOnSeveralRowsShareOneRead() = 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(1_000)
+ val before = tv.ran.size
+ repeat(5) {
+ gate.during(reread = true) {}
+ advanceTimeBy(50)
+ }
+ advanceTimeBy(301)
+ val after = tv.ran.drop(before)
+ assertEquals(1, after.count { it.startsWith("getprop") }, "ran: $after")
+ assertEquals(1, after.count { it.startsWith("ps") }, "ran: $after")
+ }
+
@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 }
@@ -275,7 +305,7 @@ class DeviceMonitorTest {
assertEquals(Parsers.Storage(16000, 9800), readings.last().storage)
gate.during(reread = true) { used = 7000 }
- runCurrent()
+ advanceTimeBy(301)
assertEquals(Parsers.Storage(16000, 7000), readings.last().storage)
}
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index 005fb9f..8af1cd8 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -469,6 +469,8 @@ class AdbDeviceSource(context: Context) : DeviceSource {
return labels[pkg] ?: AppNames.fallback(pkg)
}
+ override suspend fun <T> batch(deviceId: String, block: suspend () -> T): T = busy(deviceId, reread = true, block)
+
override suspend fun act(deviceId: String, action: Action, packageName: String, undoing: Long?): HistoryEntry =
busy(deviceId, reread = true) { actNow(deviceId, action, packageName, undoing) }
@@ -639,8 +641,11 @@ class AdbDeviceSource(context: Context) : DeviceSource {
// Only what is still off now: the user may have turned some back on from the TV itself.
val off = disabledPackages(deviceId)
val key = key(deviceId)
- return restoreEach(book.restorable(key).filter { it in off }) { pkg ->
- act(deviceId, Action.Enable, pkg, undoing = book.openDisable(key, pkg))
+ // One change for them all: the stats are read again once, after the last app.
+ return batch(deviceId) {
+ restoreEach(book.restorable(key).filter { it in off }) { pkg ->
+ act(deviceId, Action.Enable, pkg, undoing = book.openDisable(key, pkg))
+ }
}
}
diff --git a/app/src/main/java/app/tellyscope/data/DeviceSource.kt b/app/src/main/java/app/tellyscope/data/DeviceSource.kt
index df3f009..1c28377 100644
--- a/app/src/main/java/app/tellyscope/data/DeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/DeviceSource.kt
@@ -183,6 +183,12 @@ interface DeviceSource {
*/
suspend fun change(deviceId: String, tweak: Tweak, values: Map<String, String?>, undoing: Long? = null): HistoryEntry
+ /**
+ * Runs [block], several changes to apps, as one: the TV's stats wait for all of it, then are
+ * read again once. Debloat's loop runs inside it.
+ */
+ suspend fun <T> batch(deviceId: String, block: suspend () -> T): T = block()
+
/** Frees every app's cache, keeping app data and sign-ins, and records it. Returns KB freed, or null when the TV can't say. */
suspend fun clearCaches(deviceId: String): Long?
diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
index 60234d8..ca7abf3 100644
--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
@@ -1479,14 +1479,17 @@ class TellyscopeViewModel(
_state.update { it.copy(device = it.device.copy(busy = DISABLING)) }
viewModelScope.launch {
var done = 0
- for (pkg in packages) {
- try {
- source.act(id, Action.Disable, pkg)
- done++
- } catch (e: CancellationException) {
- throw e
- } catch (_: Exception) {
- // The TV refused this one or dropped; the count says so.
+ // One change for the whole list: the TV's stats are read again once, after the last app.
+ source.batch(id) {
+ for (pkg in packages) {
+ try {
+ source.act(id, Action.Disable, pkg)
+ done++
+ } catch (e: CancellationException) {
+ throw e
+ } catch (_: Exception) {
+ // The TV refused this one or dropped; the count says so.
+ }
}
}
_notice.value = Copy.disabledSuggested(done, packages.size)