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.

Remote volume: a waiting tap cancelled with the Remote clears its flag

jesse-agent-232a006ca772235
diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
index 6b3f5d8..97737e2 100644
--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
@@ -1833,26 +1833,33 @@ class TellyscopeViewModel(
val id = _state.value.selectedId ?: return false
val volume = key == RemoteKey.VOLUME_UP || key == RemoteKey.VOLUME_DOWN
if (volume && !volumeWaiting.compareAndSet(false, true)) return true
- return keys.withLock {
- if (volume) volumeWaiting.set(false)
- try {
- // With the picture off, any key wakes it, the way a TV's own remote does, and does nothing else.
- if (id in _state.value.screenOff) return@withLock wake(id)
- if (key == RemoteKey.DEL) {
- // Nothing typed from here to delete: the key does nothing, and a hold stops.
- val left = typed[id] ?: 0
- if (left == 0) return@withLock false
- return@withLock source.press(id, key)
- .also { if (it) typed[id] = left - 1 else _notice.value = Copy.KEY_REFUSED }
+ // Cleared once this tap has the lock, or here if it never gets it (the Remote closed while
+ // it waited), so a cancelled tap can't silence the volume keys.
+ var waiting = volume
+ try {
+ return keys.withLock {
+ if (volume) { volumeWaiting.set(false); waiting = false }
+ try {
+ // With the picture off, any key wakes it, the way a TV's own remote does, and does nothing else.
+ if (id in _state.value.screenOff) return@withLock wake(id)
+ if (key == RemoteKey.DEL) {
+ // Nothing typed from here to delete: the key does nothing, and a hold stops.
+ val left = typed[id] ?: 0
+ if (left == 0) return@withLock false
+ return@withLock source.press(id, key)
+ .also { if (it) typed[id] = left - 1 else _notice.value = Copy.KEY_REFUSED }
+ }
+ typed.remove(id)
+ source.press(id, key).also { if (!it) _notice.value = Copy.KEY_REFUSED }
+ } catch (e: CancellationException) {
+ throw e
+ } catch (_: Exception) {
+ _notice.value = Copy.REMOTE_LOST
+ false
}
- typed.remove(id)
- source.press(id, key).also { if (!it) _notice.value = Copy.KEY_REFUSED }
- } catch (e: CancellationException) {
- throw e
- } catch (_: Exception) {
- _notice.value = Copy.REMOTE_LOST
- false
}
+ } finally {
+ if (waiting) volumeWaiting.set(false)
}
}
diff --git a/app/src/test/java/app/tellyscope/VolumeTapTest.kt b/app/src/test/java/app/tellyscope/VolumeTapTest.kt
new file mode 100644
index 0000000..b6f48cc
--- /dev/null
+++ b/app/src/test/java/app/tellyscope/VolumeTapTest.kt
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-3.0-only
+
+package app.tellyscope
+
+import androidx.lifecycle.viewModelScope
+import app.tellyscope.adb.RemoteKey
+import app.tellyscope.data.Device
+import app.tellyscope.data.DeviceSource
+import app.tellyscope.data.DeviceState
+import app.tellyscope.data.FakeDeviceSource
+import app.tellyscope.data.ScanHit
+import app.tellyscope.data.ScanKind
+import app.tellyscope.data.Snapshot
+import app.tellyscope.ui.TellyscopeViewModel
+import kotlinx.coroutines.CompletableDeferred
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.flow.emptyFlow
+import kotlinx.coroutines.launch
+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.Before
+import org.junit.Test
+
+/** One slow TV: each key waits for [gate] before the TV takes it. */
+private class SlowTv : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {
+ val sent = mutableListOf<RemoteKey>()
+ var gate = CompletableDeferred<Unit>()
+
+ override suspend fun known() = listOf(
+ Device("tv", ScanHit("tv", 5555, ScanKind.Classic, "tv", null), DeviceState.Online, null, null, null, 0L, nickname = "Lounge"),
+ )
+
+ override fun live(deviceId: String) = emptyFlow<Snapshot>()
+
+ override suspend fun press(deviceId: String, key: RemoteKey): Boolean {
+ gate.await()
+ sent += key
+ return true
+ }
+}
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class VolumeTapTest {
+ private val dispatcher = StandardTestDispatcher()
+ private val test = TestScope(dispatcher)
+ private val tv = SlowTv()
+ private lateinit var vm: TellyscopeViewModel
+
+ @Before fun setUp() = Dispatchers.setMain(dispatcher)
+
+ @After fun tearDown() {
+ if (::vm.isInitialized) vm.viewModelScope.cancel()
+ Dispatchers.resetMain()
+ }
+
+ private fun start() = with(test) {
+ vm = TellyscopeViewModel(tv, clock = { 0L })
+ runCurrent()
+ }
+
+ @Test fun tapsBeyondTheOneWaitingAreDropped() = with(test) {
+ start()
+ repeat(5) { launch { vm.press(RemoteKey.VOLUME_UP) } }
+ runCurrent()
+ tv.gate.complete(Unit)
+ runCurrent()
+ // One being sent and one waiting; the other three never queue.
+ assertEquals(List(2) { RemoteKey.VOLUME_UP }, tv.sent)
+ }
+
+ @Test fun aWaitingTapCancelledWithTheRemoteDoesNotSilenceVolume() = with(test) {
+ start()
+ launch { vm.press(RemoteKey.VOLUME_UP) }
+ val waiting = launch { vm.press(RemoteKey.VOLUME_UP) }
+ runCurrent()
+ // Leaving the Remote cancels its scope while the second tap waits for the first.
+ waiting.cancel()
+ tv.gate.complete(Unit)
+ runCurrent()
+ launch { vm.press(RemoteKey.VOLUME_DOWN) }
+ runCurrent()
+ assertEquals(listOf(RemoteKey.VOLUME_UP, RemoteKey.VOLUME_DOWN), tv.sent)
+ }
+}