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: gray out mute and volume when a box's volume is fixed over HDMI

jesse-agent-232f0881cb25109
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
index e568519..53d4a38 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
@@ -40,6 +40,10 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
/** The top of the TV's media volume, from `dumpsys audio`; null when it doesn't say. */
suspend fun volumeMax(): Int? = musicMax(session.shell("dumpsys audio 2>/dev/null | grep -A12 '^- STREAM_MUSIC:'").output)
+ /** Media plays through a fixed-volume output, so the volume keys can't reach the TV: see [musicFixed]. */
+ suspend fun volumeFixed(): Boolean =
+ musicFixed(session.shell("dumpsys audio 2>/dev/null | grep -e mFixedVolumeDevices -e '^- STREAM_MUSIC:' -A12").output)
+
/**
* Home that opens [launcher], the activity Android resolves Home to (see [Actions.currentHome]).
* For Fire TV, whose Home key goes to Amazon's launcher whatever the default is. Null, a name
@@ -107,6 +111,22 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
return Regex("""^\s*Max:\s*(\d+)""", RegexOption.MULTILINE).find(block)?.groupValues?.get(1)?.toIntOrNull()?.takeIf { it > 0 }
}
+ /**
+ * Music plays through a fixed-volume output: `Devices: hdmi` in the STREAM_MUSIC block, whose
+ * `400 (hdmi)` code is in `mFixedVolumeDevices`. A box on HDMI (a Fire TV stick) keeps its own
+ * volume at full there and ignores the volume keys. Android 11 lists the fixed devices
+ * (`0x1000,0x400`); 10 and older print one bitmask (`0x2c400`), so a device's bits are tested.
+ */
+ fun musicFixed(dump: String): Boolean {
+ val block = dump.substringAfter("- STREAM_MUSIC:", "").ifEmpty { return false }
+ val line = Regex("""mFixedVolumeDevices=(.*)""").find(dump)?.groupValues?.get(1).orEmpty()
+ val fixed = Regex("""0x([0-9a-fA-F]+)""").findAll(line).map { it.groupValues[1].toLong(16) }.toSet()
+ val devices = Regex("""^\s*Devices:\s*(.+)$""", RegexOption.MULTILINE).find(block)?.groupValues?.get(1).orEmpty()
+ .split(Regex("""[\s,]+""")).filter { it.isNotEmpty() }
+ val codes = Regex("""([0-9a-fA-F]+) \(([^)]+)\)""").findAll(block).associate { it.groupValues[2] to it.groupValues[1].toLong(16) }
+ return devices.isNotEmpty() && devices.all { d -> codes[d]?.let { c -> c != 0L && fixed.any { (it and c) == c } } == true }
+ }
+
/** Volume keys per tap: 1 on a 15-step TV, 3 on 50, 7 on 100. Never under 1. */
fun volumeKeys(max: Int?): Int = ((max ?: VOLUME_STEPS) / VOLUME_STEPS.toDouble()).roundToInt().coerceAtLeast(1)
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
index 63c8453..7c2144d 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
@@ -167,6 +167,44 @@ class RemoteTest {
assertEquals(null, Remote.musicMax("Error: dumpsys not allowed"))
}
+ @Test fun hdmiOutputIsFixedVolume() {
+ // A Fire TV stick: on the TV over HDMI, and on its own (TV off, no HDMI) its speaker stream.
+ fun dump(devices: String) = """
+ - STREAM_MUSIC:
+ Min: 0
+ Max: 15
+ Current: 2 (speaker): 0, 400 (hdmi): 15, 40000000 (default): 3
+ Devices: $devices
+ - STREAM_ALARM:
+ Max: 7
+ --
+ mFixedVolumeDevices=0x1000,0x800,0x400,0x200000,0x40000,0x40001,0x80000
+ mExtVolumeController=null
+ """.trimIndent()
+ assertTrue(Remote.musicFixed(dump("hdmi")))
+ assertFalse(Remote.musicFixed(dump("speaker")))
+ assertFalse(Remote.musicFixed(dump("hdmi").substringBefore("--")))
+ assertFalse(Remote.musicFixed("Error: dumpsys not allowed"))
+ // Android 10 and older: one bitmask, here 0x20000|0x8000|0x4000|0x400.
+ val mask = dump("hdmi").replace("0x1000,0x800,0x400,0x200000,0x40000,0x40001,0x80000", "0x2c400")
+ assertTrue(Remote.musicFixed(mask))
+ assertFalse(Remote.musicFixed(mask.replace("0x2c400", "0x2c000")))
+ }
+
+ @Test fun volumeFixedReadsTheAudioDump() = runTest {
+ val dump = """
+ - STREAM_MUSIC:
+ Max: 15
+ Current: 2 (speaker): 0, 400 (hdmi): 15
+ Devices: hdmi
+ --
+ mFixedVolumeDevices=0x400
+ """.trimIndent()
+ val read = "dumpsys audio 2>/dev/null | grep -e mFixedVolumeDevices -e '^- STREAM_MUSIC:' -A12"
+ assertTrue(Remote(FakeShell(mapOf(read to ShellResult(dump, 0)))).volumeFixed())
+ assertFalse(Remote(FakeShell(mapOf(read to ShellResult(dump.replace("Devices: hdmi", "Devices: speaker"), 0)))).volumeFixed())
+ }
+
@Test fun volumeTapsCoverAboutFifteenSteps() {
assertEquals(1, Remote.volumeKeys(15))
assertEquals(1, Remote.volumeKeys(7))
diff --git a/app/src/main/java/app/tellyscope/MainActivity.kt b/app/src/main/java/app/tellyscope/MainActivity.kt
index cd3a4c5..372da9e 100644
--- a/app/src/main/java/app/tellyscope/MainActivity.kt
+++ b/app/src/main/java/app/tellyscope/MainActivity.kt
@@ -461,6 +461,7 @@ fun TellyscopeApp(vm: TellyscopeViewModel) {
setDock = vm::setDock,
screenOff = vm::setScreenOff,
screenOffTipSeen = vm::screenOffTipSeen,
+ checkVolume = vm::checkVolume,
),
)
}
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index 58115e5..9fb61aa 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -959,6 +959,8 @@ class AdbDeviceSource(
keyHelpers.keys.toList().forEach { keyHelpers.remove(it)?.close() }
}
+ override suspend fun volumeFixed(deviceId: String) = Remote(session(deviceId)).volumeFixed()
+
override suspend fun type(deviceId: String, text: String) = busy(deviceId) { remote(deviceId).type(text) }
diff --git a/app/src/main/java/app/tellyscope/data/DeviceSource.kt b/app/src/main/java/app/tellyscope/data/DeviceSource.kt
index 93d9c3f..96f7da2 100644
--- a/app/src/main/java/app/tellyscope/data/DeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/DeviceSource.kt
@@ -164,6 +164,12 @@ interface DeviceSource {
/** The remote screen closed: stop anything [press] left running on the TVs. */
fun closeRemote() {}
+ /**
+ * The TV can't change volume from here: a box on HDMI whose own volume is fixed, so the
+ * Remote's volume keys do nothing (see [app.tellyscope.adb.Remote.musicFixed]). Throws when it's gone.
+ */
+ suspend fun volumeFixed(deviceId: String): Boolean = false
+
/** Types into the focused box on the TV. The caller checks [app.tellyscope.adb.Remote.typeable] first. */
suspend fun type(deviceId: String, text: String): Boolean
diff --git a/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt b/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt
index 91ceb09..5c31c7a 100644
--- a/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt
+++ b/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt
@@ -41,6 +41,7 @@ import androidx.compose.material.icons.filled.KeyboardArrowLeft
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material.icons.outlined.Home
+import androidx.compose.material.icons.outlined.Info
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
@@ -181,6 +182,8 @@ data class RemoteActions(
val screenOff: (Boolean) -> Unit = {},
/** The first-visit tip on Screen off was shown. */
val screenOffTipSeen: () -> Unit = {},
+ /** Read whether the TV's volume is fixed over HDMI, into [UiState.volumeFixed]. */
+ val checkVolume: () -> Unit = {},
)
/** A held key repeats after this, then every [REPEAT_EVERY_MS], or slower if the TV is slower. */
@@ -233,6 +236,11 @@ fun RemoteScreen(
val screenOff = device.id in state.screenOff
// Android 14 moved the call that turns the picture off out of the shell's reach.
val screenControl = (device.android?.substringBefore('.')?.toIntOrNull() ?: 0) < 14 || screenOff
+ // A box on HDMI with its volume fixed ignores mute and volume: they gray out, with the reason under them.
+ LaunchedEffect(device.id, live) { if (live) actions.checkVolume() }
+ val volumeFixed = live && device.id in state.volumeFixed
+ val volume = live && !volumeFixed
+ val volumeAlpha = if (volumeFixed) Modifier.alpha(0.38f) else Modifier
var confirmReboot by rememberSaveable { mutableStateOf(false) }
if (confirmReboot) RebootDialog(onRestart = { confirmReboot = false; actions.reboot() }, onDismiss = { confirmReboot = false })
// Hide keeps a restart in the header; the dialog comes back when the wait moves on, or on a tap there.
@@ -299,7 +307,7 @@ fun RemoteScreen(
horizontalAlignment = Alignment.CenterHorizontally,
) {
Row(Modifier.weight(1f).fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) {
- if (short) RoundKey(RemoteKey.MUTE, stringResource(R.string.remote_mute), live, actions.press, size = 56.dp) { Icon(RemoteIcons.VolumeOff, null) }
+ if (short) RoundKey(RemoteKey.MUTE, stringResource(R.string.remote_mute), volume, actions.press, volumeAlpha, size = 56.dp) { Icon(RemoteIcons.VolumeOff, null) }
BoxWithConstraints(Modifier.weight(1f).fillMaxHeight(), contentAlignment = Alignment.Center) {
val room = minOf(maxWidth, maxHeight)
val cramped = room < DPAD_MIN
@@ -310,15 +318,15 @@ fun RemoteScreen(
DPad(live, actions.press, (room - 24.dp).coerceIn(DPAD_MIN, DPAD_MAX).coerceAtMost(room))
}
// Up above down, like the rocker on the side of a real remote.
- if (short) Column(Modifier.width(56.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
+ if (short) Column(Modifier.width(56.dp).then(volumeAlpha), verticalArrangement = Arrangement.spacedBy(4.dp)) {
val (outer, inner) = 28.dp to 4.dp
val tall = Modifier.fillMaxWidth().height(72.dp)
KeySurface(
- RemoteKey.VOLUME_UP, stringResource(R.string.remote_volume_up), live, actions.press, RoundedCornerShape(outer, outer, inner, inner),
+ RemoteKey.VOLUME_UP, stringResource(R.string.remote_volume_up), volume, actions.press, RoundedCornerShape(outer, outer, inner, inner),
MaterialTheme.colorScheme.secondaryContainer, tall,
) { Icon(RemoteIcons.VolumeUp, null) }
KeySurface(
- RemoteKey.VOLUME_DOWN, stringResource(R.string.remote_volume_down), live, actions.press, RoundedCornerShape(inner, inner, outer, outer),
+ RemoteKey.VOLUME_DOWN, stringResource(R.string.remote_volume_down), volume, actions.press, RoundedCornerShape(inner, inner, outer, outer),
MaterialTheme.colorScheme.secondaryContainer, tall,
) { Icon(RemoteIcons.VolumeDown, null) }
}
@@ -332,23 +340,26 @@ fun RemoteScreen(
}
RoundKey(RemoteKey.MENU, stringResource(R.string.remote_menu), live, actions.press, Modifier.align(Alignment.CenterEnd), size = side) { Icon(Icons.Default.Menu, null) }
}
+ // Beside the D-pad on a short phone, the volume keys have no row of their own: the reason goes under this one.
+ if (volumeFixed && short && !typing) VolumeFixedLine()
if (!typing && !short) {
Spacer(Modifier.height(rowGap))
- Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {
- RoundKey(RemoteKey.MUTE, stringResource(R.string.remote_mute), live, actions.press) { Icon(RemoteIcons.VolumeOff, null) }
+ Row(Modifier.fillMaxWidth().then(volumeAlpha), horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.CenterVertically) {
+ RoundKey(RemoteKey.MUTE, stringResource(R.string.remote_mute), volume, actions.press) { Icon(RemoteIcons.VolumeOff, null) }
// One split pill: two halves of the same key, so the gap stays narrow.
Row(Modifier.weight(1f), horizontalArrangement = Arrangement.spacedBy(4.dp)) {
val (outer, inner) = 32.dp to 4.dp
KeySurface(
- RemoteKey.VOLUME_DOWN, stringResource(R.string.remote_volume_down), live, actions.press, RoundedCornerShape(outer, inner, inner, outer),
+ RemoteKey.VOLUME_DOWN, stringResource(R.string.remote_volume_down), volume, actions.press, RoundedCornerShape(outer, inner, inner, outer),
MaterialTheme.colorScheme.secondaryContainer, Modifier.weight(1f).height(64.dp),
) { Icon(RemoteIcons.VolumeDown, null) }
KeySurface(
- RemoteKey.VOLUME_UP, stringResource(R.string.remote_volume_up), live, actions.press, RoundedCornerShape(inner, outer, outer, inner),
+ RemoteKey.VOLUME_UP, stringResource(R.string.remote_volume_up), volume, actions.press, RoundedCornerShape(inner, outer, outer, inner),
MaterialTheme.colorScheme.secondaryContainer, Modifier.weight(1f).height(64.dp),
) { Icon(RemoteIcons.VolumeUp, null) }
}
}
+ if (volumeFixed) VolumeFixedLine()
}
// Twice the usual gap, so a thumb aiming at a volume key doesn't land in the text box.
Spacer(Modifier.height(if (typing) rowGap else 24.dp))
@@ -398,6 +409,15 @@ private fun RemoteHeader(state: UiState, device: Device, restart: Restart?, scre
}
}
+/** Why mute and volume are gray: the box's volume is fixed over HDMI. Plain text, so TalkBack reads it after the keys. */
+@Composable
+private fun VolumeFixedLine() {
+ Row(Modifier.padding(top = 8.dp), horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically) {
+ Icon(Icons.Outlined.Info, null, Modifier.size(16.dp), tint = MaterialTheme.colorScheme.onSurfaceVariant)
+ Text(stringResource(R.string.remote_volume_fixed), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
+ }
+}
+
/** "**Screen off** · tap any key to wake": the state, then what to do about it. */
@Composable
private fun StateLine(what: String, then: String) {
diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
index fa8ea14..9a5d24e 100644
--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
@@ -309,6 +309,8 @@ data class UiState(
val screenOff: Set<String> = emptySet(),
/** The Remote's Screen off button explains itself, the first time only. */
val screenOffTip: Boolean = false,
+ /** The TVs whose volume is fixed over HDMI: the Remote grays out mute and volume, with the reason. */
+ val volumeFixed: Set<String> = emptySet(),
/** Debloat with nothing left on the list explains why it opens Apps, until Don't show again. */
val debloatGate: Boolean = true,
/** The Apps tabs whose System section the user folded away. */
@@ -1937,6 +1939,24 @@ class TellyscopeViewModel(
}
}
+ /**
+ * Reads whether the selected TV's volume is fixed over HDMI, for the Remote. Read each time it
+ * opens or the TV comes back, as the box's output changes with the TV it's plugged into.
+ */
+ fun checkVolume() {
+ val id = _state.value.selectedId ?: return
+ viewModelScope.launch {
+ val fixed = try {
+ source.volumeFixed(id)
+ } catch (e: CancellationException) {
+ throw e
+ } catch (_: Exception) {
+ return@launch
+ }
+ _state.update { it.copy(volumeFixed = if (fixed) it.volumeFixed + id else it.volumeFixed - id) }
+ }
+ }
+
/** Under [keys]. True once the picture is back on. Throws when the TV is gone. */
private suspend fun wake(id: String): Boolean {
val ok = source.screen(id, on = true)
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index 35d9131..8aac21d 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -955,6 +955,7 @@
<string name="remote_mute">Silenciar o activar sonido</string>
<string name="remote_volume_up">Subir volumen</string>
<string name="remote_volume_down">Bajar volumen</string>
+ <string name="remote_volume_fixed">El volumen es fijo por HDMI. Usa el control remoto de la TV.</string>
<string name="remote_back">Atrás</string>
<string name="remote_home">Inicio</string>
<string name="remote_menu">Menú</string>
diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml
index 676f708..9f8c7e3 100644
--- a/app/src/main/res/values-hi/strings.xml
+++ b/app/src/main/res/values-hi/strings.xml
@@ -913,6 +913,7 @@
<string name="remote_mute">म्यूट या अनम्यूट करें</string>
<string name="remote_volume_up">आवाज़ बढ़ाएं</string>
<string name="remote_volume_down">आवाज़ कम करें</string>
+ <string name="remote_volume_fixed">HDMI पर आवाज़ तय रहती है। TV का रिमोट इस्तेमाल करें।</string>
<string name="remote_back">वापस जाएं</string>
<string name="remote_home">Home</string>
<string name="remote_menu">मेन्यू</string>
diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml
index 94d9c0f..b53b915 100644
--- a/app/src/main/res/values-ja/strings.xml
+++ b/app/src/main/res/values-ja/strings.xml
@@ -871,6 +871,7 @@
<string name="remote_mute">ミュート / ミュート解除</string>
<string name="remote_volume_up">音量を上げる</string>
<string name="remote_volume_down">音量を下げる</string>
+ <string name="remote_volume_fixed">HDMI 接続では音量が固定されています。テレビのリモコンを使ってください。</string>
<string name="remote_back">戻る</string>
<string name="remote_home">ホーム</string>
<string name="remote_menu">メニュー</string>
diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml
index 2840fc7..36219c1 100644
--- a/app/src/main/res/values-ru/strings.xml
+++ b/app/src/main/res/values-ru/strings.xml
@@ -997,6 +997,7 @@
<string name="remote_mute">Включить или выключить звук</string>
<string name="remote_volume_up">Громче</string>
<string name="remote_volume_down">Тише</string>
+ <string name="remote_volume_fixed">По HDMI громкость фиксирована. Используйте пульт телевизора.</string>
<string name="remote_back">Назад</string>
<string name="remote_home">Домой</string>
<string name="remote_menu">Меню</string>
diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml
index 96476eb..bda12e0 100644
--- a/app/src/main/res/values-zh-rCN/strings.xml
+++ b/app/src/main/res/values-zh-rCN/strings.xml
@@ -871,6 +871,7 @@
<string name="remote_mute">静音或取消静音</string>
<string name="remote_volume_up">音量加</string>
<string name="remote_volume_down">音量减</string>
+ <string name="remote_volume_fixed">通过 HDMI 时音量是固定的。请使用电视的遥控器。</string>
<string name="remote_back">返回</string>
<string name="remote_home">主页</string>
<string name="remote_menu">菜单</string>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 929b072..1eb1ee5 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1138,6 +1138,7 @@
<string name="remote_mute">Mute or unmute</string>
<string name="remote_volume_up">Volume up</string>
<string name="remote_volume_down">Volume down</string>
+ <string name="remote_volume_fixed">Volume is fixed over HDMI. Use the TV\'s remote.</string>
<string name="remote_back">Back</string>
<string name="remote_home">Home</string>
<string name="remote_menu">Menu</string>
diff --git a/app/src/test/java/app/tellyscope/VolumeFixedTest.kt b/app/src/test/java/app/tellyscope/VolumeFixedTest.kt
new file mode 100644
index 0000000..d692b7c
--- /dev/null
+++ b/app/src/test/java/app/tellyscope/VolumeFixedTest.kt
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-3.0-only
+
+package app.tellyscope
+
+import androidx.lifecycle.viewModelScope
+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 java.io.IOException
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.cancel
+import kotlinx.coroutines.flow.emptyFlow
+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.Assert.assertTrue
+import org.junit.Before
+import org.junit.Test
+
+/** One TV; [fixed] is its answer to the volume read, and null makes the read throw (the TV is gone). */
+private class Box : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {
+ var fixed: Boolean? = true
+
+ override suspend fun known() = listOf(
+ Device("tv", ScanHit("tv", 5555, ScanKind.Classic, "tv", null), DeviceState.Online, null, null, null, 0L, nickname = "Fire TV"),
+ )
+
+ override fun live(deviceId: String) = emptyFlow<Snapshot>()
+
+ override suspend fun volumeFixed(deviceId: String) = fixed ?: throw IOException("gone")
+}
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class VolumeFixedTest {
+ private val dispatcher = StandardTestDispatcher()
+ private val test = TestScope(dispatcher)
+ private val tv = Box()
+ private lateinit var vm: TellyscopeViewModel
+
+ @Before fun setUp() = Dispatchers.setMain(dispatcher)
+
+ @After fun tearDown() {
+ if (::vm.isInitialized) vm.viewModelScope.cancel()
+ Dispatchers.resetMain()
+ }
+
+ private fun check() = with(test) {
+ vm.checkVolume()
+ runCurrent()
+ }
+
+ @Test fun aFixedVolumeBoxIsMarkedAndUnmarkedOnTheNextRead() = with(test) {
+ vm = TellyscopeViewModel(tv, clock = { 0L })
+ runCurrent()
+ check()
+ assertEquals(setOf("tv"), vm.state.value.volumeFixed)
+ // Plugged into another TV, or its own speaker: the keys work again.
+ tv.fixed = false
+ check()
+ assertTrue(vm.state.value.volumeFixed.isEmpty())
+ }
+
+ @Test fun aReadThatFailsLeavesTheLastAnswer() = with(test) {
+ vm = TellyscopeViewModel(tv, clock = { 0L })
+ runCurrent()
+ check()
+ tv.fixed = null
+ check()
+ assertEquals(setOf("tv"), vm.state.value.volumeFixed)
+ }
+}