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: Screen off turns off the TV's picture, any key wakes it
jesse-agent-232ac4271f45f00
diff --git a/README.md b/README.mdindex e90d1d6..33ea3f0 100644--- a/README.md+++ b/README.md@@ -94,7 +94,7 @@ Tellyscope is a phone app that talks to your Android TV, Google TV or Fire TV ov- **Connected devices.** The Overview lists what's paired with or plugged into the TV: Bluetooth remotes and headphones, USB keyboards and drives with their free space, HDMI-CEC devices such as a soundbar, and where the sound goes now.- **Connection requests.** When another device asks to debug the TV, Tellyscope shows the TV's "Allow debugging?" prompt on your phone, with the device's key fingerprint, so you can **Allow** or **Deny** it without the remote. **Always allow this device** appears when the TV's own prompt offers it.- **Files.** Send a file from your phone into the TV's Download or Movies folder, or save one from there to your phone. Two buttons under the TV pick the folder, and stay pinned under the bar as you scroll. An empty folder says so in the middle of the page. A file with the same name gets a " (1)" instead of being overwritten, and a cancelled copy leaves nothing half-written behind.-- **Remote.** A D-pad and icon keys for Back, Home, Menu, mute and volume, laid out like Google TV's phone remote, plus typing on the TV. Each key gives a short tick; **Vibrate on key press** in Settings turns it off. The D-pad's centre pauses a playing video. **Add to dock** puts up to four of the TV's apps under the volume keys, each one tap from opening on the TV. Long-press one to change or remove it, or keep holding and move it to reorder. An app uninstalled from the TV leaves the dock by itself, and a snackbar says so. The restart button in the top bar asks first, then waits for the TV to come back: **Hide** keeps the wait in the top bar, and after a minute **Retry** waits another one.+- **Remote.** A D-pad and icon keys for Back, Home, Menu, mute and volume, laid out like Google TV's phone remote, plus typing on the TV. Each key gives a short tick; **Vibrate on key press** in Settings turns it off. The D-pad's centre pauses a playing video. **Add to dock** puts up to four of the TV's apps under the volume keys, each one tap from opening on the TV. Long-press one to change or remove it, or keep holding and move it to reorder. An app uninstalled from the TV leaves the dock by itself, and a snackbar says so. The restart button in the top bar asks first, then waits for the TV to come back: **Hide** keeps the wait in the top bar, and after a minute **Retry** waits another one. **Screen off** in the top bar turns off the TV's picture while Tellyscope stays connected, say to listen to music; any key or closing the Remote turns it back on. It needs Android 13 or older.## Setting up your TVdiff --git a/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.kt b/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.ktindex d0789d2..4a0e757 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.kt@@ -33,6 +33,9 @@ class KeyHelper(val session: AdbSession, private val dex: ByteArray) : AutoClose/** Types [text], already checked with [Remote.typeable]; answers like [press]. */suspend fun type(text: String): Boolean? = send("t$text")+ /** Turns the TV's picture off or on; the TV stays awake and connected. Answers like [press]. */+ suspend fun screen(on: Boolean): Boolean? = send(if (on) "p2" else "p0")+private suspend fun send(line: String): Boolean? = lock.withLock {if (failed) return nullval p = pipe ?: start() ?: return nulldiff --git a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt b/adb/src/main/kotlin/app/tellyscope/adb/Remote.ktindex 91e77d9..8df3902 100644--- a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt+++ b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt@@ -43,6 +43,12 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul/** Types [text] into whatever box has focus on the TV, through [keys] like [press]. Check [typeable] first. */suspend fun type(text: String): Boolean = keys?.type(text) ?: (session.shell(typeCommand(text)).exitCode == 0)+ /**+ * Turns the TV's picture off or back on, leaving the TV awake so the link stays up. Only the+ * key helper can: false when it isn't running or the TV refused. Throws if the link is gone.+ */+ suspend fun screen(on: Boolean): Boolean = keys?.screen(on) ?: false+/*** Restarts the TV. adbd's `reboot:` service works for the shell user on stock builds; where a* build refuses it, `svc power reboot` goes through the power service instead. Returns falsediff --git a/app/src/helper/java/app/tellyscope/helper/Keys.java b/app/src/helper/java/app/tellyscope/helper/Keys.javaindex fcef720..2ff63a8 100644--- a/app/src/helper/java/app/tellyscope/helper/Keys.java+++ b/app/src/helper/java/app/tellyscope/helper/Keys.java@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-3.0-onlypackage app.tellyscope.helper;+import android.os.IBinder;import android.os.SystemClock;import android.view.InputDevice;import android.view.InputEvent;@@ -21,7 +22,9 @@ import java.lang.reflect.Method;* {@code 0} when it didn't. Prints {@code ready} first. Only key codes are read, so nothing typed* reaches a shell. The argument is the jar's own path, deleted once the helper is up; it exits at* the end of stdin. A line starting with {@code t} is text instead: the rest is typed the way- * {@code input text} types it, one key press per character.+ * {@code input text} types it, one key press per character. {@code p0} turns the TV's picture off+ * and {@code p2} back on, through SurfaceFlinger: the TV stays awake, so ADB stays connected, and+ * the picture stays off after the helper exits.*/public final class Keys {/** InputManager's INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH: answers once the focused app is done with it. */@@ -50,6 +53,11 @@ public final class Keys {for (String line; (line = in.readLine()) != null; ) {boolean ok;try {+ if (line.startsWith("p")) {+ out.print(power(Integer.parseInt(line.substring(1).trim())) ? "1\n" : "0\n");+ out.flush();+ continue;+ }if (line.startsWith("t")) {out.print(type(manager, inject, keymap, line.substring(1)) ? "1\n" : "0\n");out.flush();@@ -67,6 +75,28 @@ public final class Keys {}}+ /**+ * SurfaceControl's display power mode: 0 is off, 2 is on. Android 10 to 13 name the display by+ * getInternalDisplayToken, older builds by getBuiltInDisplay(0). Android 14 moved both to a+ * class the shell's VM can't load, so there it answers 0.+ */+ private static boolean power(int mode) {+ try {+ Class<?> sc = Class.forName("android.view.SurfaceControl");+ Object display;+ try {+ display = sc.getMethod("getInternalDisplayToken").invoke(null);+ } catch (NoSuchMethodException e) {+ display = sc.getMethod("getBuiltInDisplay", int.class).invoke(null, 0);+ }+ if (display == null) return false;+ sc.getMethod("setDisplayPowerMode", IBinder.class, int.class).invoke(null, display, mode);+ return true;+ } catch (Throwable t) {+ return false;+ }+ }+/** Null from getEvents means a character the keymap has no key for. */private static boolean type(Object manager, Method inject, KeyCharacterMap keymap, String text) {try {diff --git a/app/src/main/java/app/tellyscope/MainActivity.kt b/app/src/main/java/app/tellyscope/MainActivity.ktindex ba11cda..5540ee6 100644--- a/app/src/main/java/app/tellyscope/MainActivity.kt+++ b/app/src/main/java/app/tellyscope/MainActivity.kt@@ -226,6 +226,8 @@ fun TellyscopeApp(vm: TellyscopeViewModel) {stopWaiting = vm::stopWaiting,launch = vm::launch,setDock = vm::setDock,+ screenOff = vm::setScreenOff,+ screenOffTipSeen = vm::screenOffTipSeen,),)} else if (advanced) {diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.ktindex 3bf59f3..c8d0a38 100644--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt@@ -604,6 +604,8 @@ class AdbDeviceSource(context: Context) : DeviceSource {override suspend fun type(deviceId: String, text: String) = busy(deviceId) { remote(deviceId).type(text) }+ override suspend fun screen(deviceId: String, on: Boolean) = busy(deviceId) { remote(deviceId).screen(on) }+override suspend fun reboot(deviceId: String) = Remote(session(deviceId)).reboot()override suspend fun openUpdates(deviceId: String) = Remote(session(deviceId)).openUpdates()diff --git a/app/src/main/java/app/tellyscope/data/DeviceSource.kt b/app/src/main/java/app/tellyscope/data/DeviceSource.ktindex 697de0c..e3b9050 100644--- a/app/src/main/java/app/tellyscope/data/DeviceSource.kt+++ b/app/src/main/java/app/tellyscope/data/DeviceSource.kt@@ -121,6 +121,12 @@ interface DeviceSource {/** 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+ /**+ * Turns the TV's picture off or back on; the TV stays awake and connected. False when this TV+ * can't (no key helper, or the TV refused). Throws when it's gone.+ */+ suspend fun screen(deviceId: String, on: Boolean): Boolean+/** Restarts the TV. The link drops and reconnects on its own once the TV is back. False if the TV refused. */suspend fun reboot(deviceId: String): Booleandiff --git a/app/src/main/java/app/tellyscope/data/FakeDeviceSource.kt b/app/src/main/java/app/tellyscope/data/FakeDeviceSource.ktindex 8e138c5..56077f0 100644--- a/app/src/main/java/app/tellyscope/data/FakeDeviceSource.kt+++ b/app/src/main/java/app/tellyscope/data/FakeDeviceSource.kt@@ -313,6 +313,11 @@ class FakeDeviceSource(return true}+ override suspend fun screen(deviceId: String, on: Boolean): Boolean {+ delay(100)+ return true+ }+override suspend fun openUpdates(deviceId: String): Boolean {delay(300)return truediff --git a/app/src/main/java/app/tellyscope/data/KeyHaptics.kt b/app/src/main/java/app/tellyscope/data/KeyHaptics.ktindex 095f4e9..13ab3f6 100644--- a/app/src/main/java/app/tellyscope/data/KeyHaptics.kt+++ b/app/src/main/java/app/tellyscope/data/KeyHaptics.kt@@ -5,18 +5,28 @@ package app.tellyscope.dataimport java.io.Fileimport java.util.Properties-/** Whether a Remote key press vibrates, kept in [file]. On until the user turns it off. */+/**+ * The Remote's own settings, kept in [file]: whether a key press vibrates (on until the user turns+ * it off), and whether the Screen off tip has been shown.+ */class KeyHaptics(private val file: File) {@get:Synchronized @set:Synchronizedvar on: Booleanget() = props().getProperty("off") != "true"- set(on) {- val p = props().apply { if (on) remove("off") else setProperty("off", "true") }- runCatching {- file.parentFile?.mkdirs()- file.outputStream().use { p.store(it, null) }- }+ set(on) = store { if (on) remove("off") else setProperty("off", "true") }++ @get:Synchronized @set:Synchronized+ var screenOffTipSeen: Boolean+ get() = props().getProperty("screenOffTip") == "seen"+ set(seen) = store { if (seen) setProperty("screenOffTip", "seen") else remove("screenOffTip") }++ private fun store(change: Properties.() -> Unit) {+ val p = props().apply(change)+ runCatching {+ file.parentFile?.mkdirs()+ file.outputStream().use { p.store(it, null) }}+ }private fun props() = Properties().apply {runCatching { file.takeIf { it.exists() }?.inputStream()?.use(::load) }diff --git a/app/src/main/java/app/tellyscope/ui/Copy.kt b/app/src/main/java/app/tellyscope/ui/Copy.ktindex f14d553..ca20dde 100644--- a/app/src/main/java/app/tellyscope/ui/Copy.kt+++ b/app/src/main/java/app/tellyscope/ui/Copy.kt@@ -321,6 +321,12 @@ object Copy {const val REBOOT_IGNORED = "The TV didn't restart. Use its own remote or power button instead."const val REMOTE_OFFLINE = "The TV is offline. The remote works again once it reconnects."const val TYPE_HINT = "Select a text box on the TV, like a search box, then type here."+ const val SCREEN_OFF = "Screen off"+ const val SCREEN_ON = "Turn the screen back on"+ const val SCREEN_OFF_WAKE = "tap any key to wake"+ const val SCREEN_OFF_TIP = "Turns off the TV's picture. The app stays connected; any key wakes it."+ const val SCREEN_OFF_REFUSED = "This TV can't turn its picture off from Tellyscope."+ const val SCREEN_ON_REFUSED = "The TV's picture didn't come back on. Try again, or restart the TV."fun cantType(text: String): String {val odd = text.firstOrNull { it !in ' '..'~' }diff --git a/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt b/app/src/main/java/app/tellyscope/ui/RemoteScreen.ktindex 2105ebe..47ef92c 100644--- a/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt+++ b/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt@@ -53,8 +53,10 @@ import androidx.compose.material3.DropdownMenuimport androidx.compose.material3.DropdownMenuItemimport androidx.compose.material3.ExperimentalMaterial3Apiimport androidx.compose.material3.ExperimentalMaterial3ExpressiveApi+import androidx.compose.material3.FilledIconToggleButtonimport androidx.compose.material3.Iconimport androidx.compose.material3.IconButton+import androidx.compose.material3.IconButtonDefaultsimport androidx.compose.material3.ListItemimport androidx.compose.material3.ListItemDefaultsimport androidx.compose.material3.LoadingIndicator@@ -63,17 +65,23 @@ import androidx.compose.material3.MaterialThemeimport androidx.compose.material3.ModalBottomSheetimport androidx.compose.material3.OutlinedButtonimport androidx.compose.material3.OutlinedTextField+import androidx.compose.material3.PlainTooltipimport androidx.compose.material3.Scaffoldimport androidx.compose.material3.SnackbarHostimport androidx.compose.material3.SnackbarHostStateimport androidx.compose.material3.Surfaceimport androidx.compose.material3.Textimport androidx.compose.material3.TextButton+import androidx.compose.material3.TooltipAnchorPosition+import androidx.compose.material3.TooltipBox+import androidx.compose.material3.TooltipDefaultsimport androidx.compose.material3.TopAppBar+import androidx.compose.material3.rememberTooltipStateimport androidx.compose.material3.rippleimport androidx.compose.material3.toShapeimport androidx.compose.runtime.Composableimport androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.getValueimport androidx.compose.runtime.keyimport androidx.compose.runtime.mutableFloatStateOf@@ -138,6 +146,10 @@ data class RemoteActions(val stopWaiting: () -> Unit = {},val launch: (Docked) -> Unit = {},val setDock: (List<Docked>) -> Unit = {},+ /** Turns the TV's picture off (true) or back on (false). */+ val screenOff: (Boolean) -> Unit = {},+ /** The first-visit tip on Screen off was shown. */+ val screenOffTipSeen: () -> Unit = {},)/** A held key repeats after this, then every [REPEAT_EVERY_MS], or slower if the TV is slower. */@@ -170,6 +182,9 @@ fun RemoteScreen(val device = state.selected ?: returnval restart = state.restarting[device.id]val live = device.state == DeviceState.Online && restart == null+ 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 || screenOffvar confirmReboot by rememberSaveable { mutableStateOf(false) }if (confirmReboot) RebootDialog(onRestart = { confirmReboot = false; actions.reboot() }, onDismiss = { confirmReboot = false })// Hide keeps a restart in the top bar; the dialog comes back when the wait moves on, or on a tap there.@@ -188,12 +203,20 @@ fun RemoteScreen(snackbarHost = { SnackbarHost(snackbar) },topBar = {TopAppBar(- title = { if (restart != null && hidden == stage) RestartStatus(late = restart.phase(state.now) != Restart.Phase.Waiting, onClick = { hidden = null }) else Text("Remote") },+ title = {+ when {+ restart != null && hidden == stage -> RestartStatus(late = restart.phase(state.now) != Restart.Phase.Waiting, onClick = { hidden = null })+ // In place of the title, so the keys below never move.+ screenOff -> ScreenOffStatus()+ else -> Text("Remote")+ }+ },navigationIcon = {// A chevron, not an arrow, so it can't be mistaken for the TV's Back key.IconButton(onClick = actions.back) { Icon(Icons.Default.KeyboardArrowDown, contentDescription = "Back to the TV's page") }},actions = {+ if (screenControl) ScreenOffButton(screenOff, live, showTip = state.screenOffTip, actions)IconButton(onClick = { confirmReboot = true }, enabled = live) { Icon(RemoteIcons.RestartAlt, contentDescription = "Restart TV") }},)@@ -444,6 +467,53 @@ private fun StatusShape(icon: ImageVector, container: Color, content: Color) {/** The top bar's title while a hidden restart runs, overdue once [late]; a tap brings its dialog back. */@OptIn(ExperimentalMaterial3ExpressiveApi::class)+/** Filled while the picture is off; a tap turns it back on. Explains itself on the first visit. */+@OptIn(ExperimentalMaterial3Api::class)+@Composable+private fun ScreenOffButton(off: Boolean, live: Boolean, showTip: Boolean, actions: RemoteActions) {+ val tip = rememberTooltipState(isPersistent = true)+ LaunchedEffect(showTip) {+ if (showTip) {+ tip.show()+ actions.screenOffTipSeen()+ }+ }+ TooltipBox(+ positionProvider = TooltipDefaults.rememberTooltipPositionProvider(TooltipAnchorPosition.Below),+ tooltip = {+ PlainTooltip {+ Column(Modifier.padding(vertical = 4.dp)) {+ Text(Copy.SCREEN_OFF, style = MaterialTheme.typography.labelLarge)+ Text(Copy.SCREEN_OFF_TIP)+ }+ }+ },+ state = tip,+ enableUserInput = false,+ ) {+ FilledIconToggleButton(+ checked = off,+ onCheckedChange = actions.screenOff,+ enabled = live,+ colors = IconButtonDefaults.filledIconToggleButtonColors(containerColor = Color.Transparent),+ ) { Icon(RemoteIcons.TvOff, contentDescription = if (off) Copy.SCREEN_ON else Copy.SCREEN_OFF) }+ }+}++@Composable+private fun ScreenOffStatus() {+ Row(verticalAlignment = Alignment.CenterVertically) {+ Text(Copy.SCREEN_OFF, style = MaterialTheme.typography.titleMedium)+ Text(+ " \u00b7 ${Copy.SCREEN_OFF_WAKE}",+ style = MaterialTheme.typography.bodyMedium,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ maxLines = 1,+ overflow = TextOverflow.Ellipsis,+ )+ }+}+@Composableprivate fun RestartStatus(late: Boolean, onClick: () -> Unit) {Row(@@ -683,6 +753,12 @@ private fun DockPicker(dock: List<Docked>, apps: List<AppInfo>, tvName: String,private object RemoteIcons {// Absolute commands only: Compose's path parser misplaces a relative move after a close.+ val TvOff = icon(+ "TvOff",+ "M1 3.54L2.53 5.07C1.65 5.28 1 6.06 1 7L1 19C1 20.1 1.9 21 3 21L18.46 21L20.46 23L21.72 21.73L2.27 2.27L1 3.54Z" ++ "M3 19L3 7L4.46 7L16.46 19L3 19Z" ++ "M21 5L13.42 5L16.71 1.7L16 1L12 5L8 1L7.3 1.7L10.58 5L7.52 5L9.52 7L21 7L21 18.48L22.65 20.13C22.87 19.81 23 19.42 23 19L23 7C23 5.89 22.11 5 21 5Z",+ )val RestartAlt = icon("RestartAlt","M12 5L12 2L8 6L12 10L12 7C15.31 7 18 9.69 18 13C18 15.97 15.83 18.43 13 18.91L13 20.93C16.95 20.44 20 17.08 20 13C20 8.58 16.42 5 12 5Z" +diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.ktindex 397555a..cc9c876 100644--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt@@ -199,6 +199,10 @@ data class UiState(val update: Update? = null,/** A Remote key press vibrates. */val keyHaptics: Boolean = true,+ /** The TVs whose picture the Remote turned off. */+ val screenOff: Set<String> = emptySet(),+ /** The Remote's Screen off button explains itself, the first time only. */+ val screenOffTip: Boolean = false,/** The Apps tabs whose System section the user folded away. */val foldedSystem: Set<AppsTab> = emptySet(),/** The report screen; null when it's closed. */@@ -332,7 +336,7 @@ class TellyscopeViewModel(viewModelScope.launch(Dispatchers.IO) { _state.update { it.copy(checkUpdates = u.enabled, alphaBuild = u.alpha) } }}keyHaptics?.let { k ->- viewModelScope.launch(Dispatchers.IO) { _state.update { it.copy(keyHaptics = k.on) } }+ viewModelScope.launch(Dispatchers.IO) { _state.update { it.copy(keyHaptics = k.on, screenOffTip = !k.screenOffTipSeen) } }}folds?.let { f ->viewModelScope.launch(Dispatchers.IO) {@@ -1171,6 +1175,8 @@ class TellyscopeViewModel(val id = _state.value.selectedId ?: return falsereturn keys.withLock {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)source.press(id, key).also { if (!it) _notice.value = Copy.KEY_REFUSED }} catch (e: CancellationException) {throw e@@ -1181,7 +1187,59 @@ class TellyscopeViewModel(}}- fun closeRemote() = source.closeRemote()+ /** Turns the TV's picture off, or back on. The TV stays connected either way. */+ fun setScreenOff(off: Boolean) {+ val id = _state.value.selectedId ?: return+ viewModelScope.launch {+ keys.withLock {+ try {+ if (!off) wake(id)+ else if (source.screen(id, on = false)) _state.update { it.copy(screenOff = it.screenOff + id) }+ else _notice.value = Copy.SCREEN_OFF_REFUSED+ } catch (e: CancellationException) {+ throw e+ } catch (_: Exception) {+ _notice.value = Copy.REMOTE_LOST+ }+ }+ }+ }++ /** 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)+ if (ok) _state.update { it.copy(screenOff = it.screenOff - id) } else _notice.value = Copy.SCREEN_ON_REFUSED+ return ok+ }++ fun screenOffTipSeen() {+ _state.update { it.copy(screenOffTip = false) }+ viewModelScope.launch(Dispatchers.IO) { keyHaptics?.screenOffTipSeen = true }+ }++ /**+ * The Remote closed. A picture it turned off stays off without it, and only the Remote can turn+ * it back on, so that happens first.+ */+ fun closeRemote() {+ val off = _state.value.screenOff+ if (off.isEmpty()) return source.closeRemote()+ _state.update { it.copy(screenOff = emptySet()) }+ viewModelScope.launch {+ keys.withLock {+ for (id in off) {+ try {+ source.screen(id, on = true)+ } catch (e: CancellationException) {+ throw e+ } catch (_: Exception) {+ // Gone: a restart or power cycle brings the picture back on its own.+ }+ }+ }+ source.closeRemote()+ }+ }/** Types on the TV. Returns why it can't, or null once it's on its way. */fun type(text: String): String? {@@ -1228,7 +1286,8 @@ class TellyscopeViewModel(_notice.value = Copy.REMOTE_LOSTreturn@launch}- if (ok) _state.update { it.copy(restarting = it.restarting + (id to Restart(clock()))) }+ // A restart brings the picture back on by itself.+ if (ok) _state.update { it.copy(restarting = it.restarting + (id to Restart(clock())), screenOff = it.screenOff - id) }else _notice.value = Copy.REBOOT_REFUSED}}diff --git a/app/src/test/java/app/tellyscope/ScreenOffTest.kt b/app/src/test/java/app/tellyscope/ScreenOffTest.ktnew file mode 100644index 0000000..cd43868--- /dev/null+++ b/app/src/test/java/app/tellyscope/ScreenOffTest.kt@@ -0,0 +1,114 @@+// 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.Copy+import app.tellyscope.ui.TellyscopeViewModel+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.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Before+import org.junit.Test++/** One TV; screen and key calls are recorded in order, and [screenWorks] is its answer to them. */+private class Screen : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {+ val calls = mutableListOf<String>()+ var screenWorks = true++ override suspend fun known() = listOf(+ Device("tv", ScanHit("tv", 5555, ScanKind.Classic, "tv", null), DeviceState.Online, null, null, null, 0L, nickname = "Projector"),+ )++ override fun live(deviceId: String) = emptyFlow<Snapshot>()++ override suspend fun screen(deviceId: String, on: Boolean): Boolean {+ calls += if (on) "on" else "off"+ return screenWorks+ }++ override suspend fun press(deviceId: String, key: RemoteKey): Boolean {+ calls += key.name+ return true+ }++ override fun closeRemote() {+ calls += "close"+ }+}++@OptIn(ExperimentalCoroutinesApi::class)+class ScreenOffTest {+ private val dispatcher = StandardTestDispatcher()+ private val test = TestScope(dispatcher)+ private val tv = Screen()+ private lateinit var vm: TellyscopeViewModel++ @Before fun setUp() = Dispatchers.setMain(dispatcher)++ @After fun tearDown() {+ if (::vm.isInitialized) vm.viewModelScope.cancel()+ Dispatchers.resetMain()+ }++ private fun off() = with(test) {+ vm = TellyscopeViewModel(tv, clock = { 0L })+ runCurrent()+ vm.setScreenOff(true)+ runCurrent()+ }++ @Test fun theFirstKeyOnlyWakesTheScreenAndTheNextOneIsSent() = with(test) {+ off()+ assertEquals(setOf("tv"), vm.state.value.screenOff)+ launch { vm.press(RemoteKey.DOWN) }+ runCurrent()+ assertTrue(vm.state.value.screenOff.isEmpty())+ launch { vm.press(RemoteKey.DOWN) }+ runCurrent()+ assertEquals(listOf("off", "on", "DOWN"), tv.calls)+ }++ @Test fun closingTheRemoteTurnsThePictureBackOnFirst() = with(test) {+ off()+ vm.closeRemote()+ runCurrent()+ assertEquals(listOf("off", "on", "close"), tv.calls)+ assertTrue(vm.state.value.screenOff.isEmpty())+ }++ @Test fun aTvThatCantTurnItsPictureOffSaysSo() = with(test) {+ tv.screenWorks = false+ off()+ assertFalse("tv" in vm.state.value.screenOff)+ assertEquals(Copy.SCREEN_OFF_REFUSED, vm.notice.value)+ }++ @Test fun aPictureThatWontComeBackStaysOffAndSaysSo() = with(test) {+ off()+ tv.screenWorks = false+ vm.setScreenOff(false)+ runCurrent()+ assertEquals(setOf("tv"), vm.state.value.screenOff)+ assertEquals(Copy.SCREEN_ON_REFUSED, vm.notice.value)+ }+}diff --git a/app/src/test/java/app/tellyscope/ScreenshotTest.kt b/app/src/test/java/app/tellyscope/ScreenshotTest.ktindex 01e4afd..306317f 100644--- a/app/src/test/java/app/tellyscope/ScreenshotTest.kt+++ b/app/src/test/java/app/tellyscope/ScreenshotTest.kt@@ -106,6 +106,9 @@ import app.tellyscope.data.RELEASESimport androidx.compose.ui.platform.UriHandlerimport androidx.compose.ui.platform.LocalUriHandlerimport androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.setValueimport app.tellyscope.ui.UiStateimport app.tellyscope.ui.theme.TellyscopeThemeimport com.github.takahirom.roborazzi.ExperimentalRoborazziApi@@ -955,6 +958,27 @@ class ScreenshotTest {compose.onNodeWithText("OK").assertDoesNotExist()}+ // Screen off (0.9): the button fills in and the title says how to wake it, so nothing below moves.+ private fun screenOffState() = remoteState().let { it.copy(screenOff = setOf(it.selectedId!!)) }+ @Test fun remote_screenOff() = shot("13_remote_screen_off") { RemoteScreen(screenOffState(), RemoteActions()) }+ @Test fun remote_screenOff_dark() = shot("13_remote_screen_off", dark = true) { RemoteScreen(screenOffState(), RemoteActions()) }+ @Test fun remote_screenOffTip() = shot("13_remote_screen_off_tip", screen = true) { RemoteScreen(remoteState().copy(screenOffTip = true), RemoteActions()) }++ @Test fun screenOffSaysWhatATapDoes() {+ var off by mutableStateOf(false)+ compose.setContent { TellyscopeTheme(dynamic = false) { RemoteScreen(if (off) screenOffState() else remoteState(), RemoteActions()) } }+ compose.onNodeWithContentDescription(Copy.SCREEN_OFF).assertExists()+ off = true+ compose.onNodeWithContentDescription(Copy.SCREEN_ON).assertExists()+ compose.onNodeWithText(Copy.SCREEN_OFF_WAKE, substring = true).assertExists()+ }++ @Test fun android14HasNoScreenOff() {+ val s = remoteState().let { st -> st.copy(devices = st.devices.map { if (it.id == st.selectedId) it.copy(android = "14") else it }) }+ compose.setContent { TellyscopeTheme(dynamic = false) { RemoteScreen(s, RemoteActions()) } }+ compose.onNodeWithContentDescription(Copy.SCREEN_OFF).assertDoesNotExist()+ }+/*** The apps' own icons, for the README's Remote shot only. Test resources, so they never reach the* APK; the names and logos belong to their owners (see NOTICE).