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.
Remove access waits for a read on the new link, not for online
jesse-agent-232c45bfa0b55d9
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt b/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.ktindex 62bf53e..54ba410 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/DeviceLink.kt@@ -48,8 +48,10 @@ class DeviceLink(private var job: Job? = null+ /** Starts over. The old session is let go at once, so nothing reads through it while the new one opens. */fun connect() {job?.cancel()+ session = nulljob = scope.launch { loop() }}diff --git a/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.ktindex 2bd8867..15274ee 100644--- a/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.kt+++ b/adb/src/test/kotlin/app/tellyscope/adb/DeviceLinkTest.kt@@ -2,6 +2,7 @@package app.tellyscope.adbimport kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.delayimport kotlinx.coroutines.flow.flowimport kotlinx.coroutines.test.advanceTimeByimport kotlinx.coroutines.test.runCurrent@@ -111,6 +112,23 @@ class DeviceLinkTest {assertEquals(null, link.session)}+ @Test fun reconnectLetsTheOldSessionGoBeforeTheNewOneOpens() = runTest {+ // After a revoke the TV keeps the old session up and holds the new one at its prompt.+ val first = FakeShell()+ var opens = 0+ val link = DeviceLink("tv", 5555, backgroundScope, monitorFor = { _, _ -> fakeMonitor(failFirst = false) },+ open = { _, _ -> if (opens++ == 0) ConnectResult.Online(first) else { delay(10_000); ConnectResult.Online(FakeShell()) } })+ link.connect(); runCurrent()+ assertEquals(first, link.session)+ link.connect()+ assertEquals(null, link.session)+ advanceTimeBy(5_000)+ assertTrue(first.closed)+ assertEquals(null, link.session)+ advanceTimeBy(5_001)+ assertTrue(link.session != null && link.session != first)+ }+private fun fakeMonitor(failFirst: Boolean) = object : MonitorSource {override fun stats() = flow {emit(reading)diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.ktindex cc9c876..c5a57e9 100644--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt@@ -252,9 +252,6 @@ const val SCREENSHOT = "screenshot"/** [DeviceUi.busy] value while the TV's connection prompt is being answered. */const val ANSWERING = "request"-/** How long a removal waits to see the link drop before checking a link that stayed up. */-const val RELINK_GRACE_MS = 5_000L-/** Where a removal ends, from the re-read after it: never Done on a guess. */fun outcome(access: TvAccess?): Removal = when {access?.connected == null || access.allowed == null -> Removal(Removal.Step.Unconfirmed)@@ -1043,25 +1040,18 @@ class TellyscopeViewModel(}/**- * Reconnects so the TV asks to allow this phone, waits for it to be back, then checks. A link- * that stays up through the revoke didn't need allowing again, and is checked straight away.+ * Reconnects so the TV asks to allow this phone, then checks on the new link. The link shows+ * online all through the reconnect, so only a read that works says the TV allowed this phone.*/private suspend fun relink(id: String) {setRemoval(id, Removal(Removal.Step.Allow, fingerprint = source.phoneFingerprint()))source.reconnect(id)- suspend fun state() = source.known().firstOrNull { it.id == id }?.state- var dropped = false- var waited = 0Lwhile (true) {- val st = state()- if (st != DeviceState.Online) dropped = true- if (st == DeviceState.Online && (dropped || waited >= RELINK_GRACE_MS)) break+ val st = source.known().firstOrNull { it.id == id }?.stateif (st == DeviceState.NotAllowed) updateDevice(id) { it.copy(removal = it.removal?.copy(refused = true)) }+ if (st == DeviceState.Online) readAccess(id)?.let { setRemoval(id, outcome(it)); return }delay(1_000)- waited += 1_000}- setRemoval(id, Removal(Removal.Step.Checking))- setRemoval(id, outcome(readAccess(id)))}/** "Try again" after another computer was still there: clear again, no need to ask twice. */diff --git a/app/src/test/java/app/tellyscope/RemoveAccessTest.kt b/app/src/test/java/app/tellyscope/RemoveAccessTest.ktindex d2946e7..6e8b795 100644--- a/app/src/test/java/app/tellyscope/RemoveAccessTest.kt+++ b/app/src/test/java/app/tellyscope/RemoveAccessTest.kt@@ -33,6 +33,8 @@ import org.junit.Test/** A TV whose access, revoke answer and link state the test sets. */private class AccessTv : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {var state = DeviceState.Online+ /** False from a reconnect until the TV allows this phone: the link still shows online, with no session. */+ var readable = truevar revokes = truevar revoked = 0var reconnects = 0@@ -42,9 +44,9 @@ private class AccessTv : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenaoverride suspend fun known() = listOf(Device("tv", ScanHit("tv", 5555, ScanKind.Classic, "tv", null), state, null, null, null, 0L))override fun live(deviceId: String) = emptyFlow<Snapshot>()override suspend fun apps(deviceId: String) = emptyList<app.tellyscope.data.AppInfo>()- override suspend fun access(deviceId: String) = if (state == DeviceState.Online) after else throw IllegalStateException("offline")+ override suspend fun access(deviceId: String) = if (state == DeviceState.Online && readable) after else throw IllegalStateException("offline")override suspend fun revokeAll(deviceId: String): Boolean { revoked++; return revokes }- override suspend fun reconnect(deviceId: String) { reconnects++ }+ override suspend fun reconnect(deviceId: String) { reconnects++; readable = false }override suspend fun openDeveloperOptions(deviceId: String): Boolean { devOptions++; return true }override fun phoneFingerprint() = "AA:BB"}@@ -88,6 +90,7 @@ class RemoveAccessTest {assertEquals(Removal.Step.Allow, step)tv.after = onlyPhonetv.state = DeviceState.Online+ tv.readable = trueadvanceTimeBy(3_100)assertEquals(Removal.Step.Done, step)}@@ -123,12 +126,27 @@ class RemoveAccessTest {assertEquals(false, vm.state.value.device.removal!!.refused)}- @Test fun aLinkThatStaysUpIsCheckedAfterTheGraceAndAnotherComputerIsNamed() = with(test) {+ @Test fun onlineIsNotEnoughTheFirstReadOnTheNewLinkDecides() = with(test) {+ tv.after = onlyPhone+ start()+ vm.askRemoveAccess()+ vm.removeAccess()+ // The link stays "online" while the TV holds the new connection at its prompt.+ advanceTimeBy(30_100)+ assertEquals(Removal.Step.Allow, step)+ tv.readable = true+ advanceTimeBy(1_100)+ assertEquals(Removal.Step.Done, step)+ }++ @Test fun aComputerStillConnectedIsNamed() = with(test) {tv.after = TvAccess(onlyPhone.connected!! + app.tellyscope.data.AccessClient("192.0.2.47", 2, app.tellyscope.data.AccessClient.Who.Other), emptyList())start()vm.askRemoveAccess()vm.removeAccess()- advanceTimeBy(6_100)+ runCurrent()+ tv.readable = true+ advanceTimeBy(1_100)assertEquals(Removal.Step.StillConnected, step)vm.closeRemoval()runCurrent()