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: backspace deletes only what it typed, never Back

jesse-agent-821e28b82e9bc91
diff --git a/app/src/main/java/app/tellyscope/ui/Copy.kt b/app/src/main/java/app/tellyscope/ui/Copy.kt
index 8c724de..a55cbfe 100644
--- a/app/src/main/java/app/tellyscope/ui/Copy.kt
+++ b/app/src/main/java/app/tellyscope/ui/Copy.kt
@@ -443,7 +443,7 @@ 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 BACKSPACE = "Backspace on the TV"
+ const val BACKSPACE = "Delete the last letter you typed on the TV"
const val SCREEN_OFF = "Screen off"
const val SCREEN_ON = "Screen on"
const val SCREEN_OFF_WAKE = "tap any key to wake"
diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
index 46f5259..3b46554 100644
--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
@@ -328,6 +328,12 @@ class TellyscopeViewModel(
/** Keys go out one at a time, in the order pressed. */
private val keys = Mutex()
+ /**
+ * Per TV, the letters the Remote typed that are still in the TV's text box, as far as the phone
+ * knows. Backspace deletes only these: on an empty box Delete acts as Back, and YouTube closes.
+ * Any other key may move the focus out of the box, and so may closing the Remote, so both forget them.
+ */
+ private val typed = mutableMapOf<String, Int>()
private var scanJob: Job? = null
private var sideloadJob: Job? = null
@@ -1480,6 +1486,14 @@ class TellyscopeViewModel(
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
@@ -1530,6 +1544,7 @@ class TellyscopeViewModel(
* it back on, so that happens first.
*/
fun closeRemote() {
+ typed.clear()
val off = _state.value.screenOff
if (off.isEmpty()) return source.closeRemote()
_state.update { it.copy(screenOff = emptySet()) }
@@ -1556,7 +1571,12 @@ class TellyscopeViewModel(
viewModelScope.launch {
keys.withLock {
_notice.value = try {
- if (source.type(id, text)) null else Copy.TYPE_REFUSED
+ if (source.type(id, text)) {
+ typed[id] = (typed[id] ?: 0) + text.length
+ null
+ } else {
+ Copy.TYPE_REFUSED
+ }
} catch (e: CancellationException) {
throw e
} catch (_: Exception) {
diff --git a/app/src/test/java/app/tellyscope/RemoteBackspaceTest.kt b/app/src/test/java/app/tellyscope/RemoteBackspaceTest.kt
new file mode 100644
index 0000000..cab287a
--- /dev/null
+++ b/app/src/test/java/app/tellyscope/RemoteBackspaceTest.kt
@@ -0,0 +1,107 @@
+// 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.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; what reached it is recorded in order. */
+private class TextBox : DeviceSource by FakeDeviceSource(FakeDeviceSource.Scenario.Empty, clock = { 0L }) {
+ val sent = mutableListOf<String>()
+
+ override suspend fun known() = listOf(
+ Device("tv", ScanHit("tv", 5555, ScanKind.Classic, "tv", null), DeviceState.Online, null, null, null, 0L),
+ )
+
+ override fun live(deviceId: String) = emptyFlow<Snapshot>()
+
+ override suspend fun type(deviceId: String, text: String): Boolean {
+ sent += "type $text"
+ return true
+ }
+
+ override suspend fun press(deviceId: String, key: RemoteKey): Boolean {
+ sent += key.name
+ return true
+ }
+}
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class RemoteBackspaceTest {
+ private val dispatcher = StandardTestDispatcher()
+ private val test = TestScope(dispatcher)
+ private val tv = TextBox()
+ private lateinit var vm: TellyscopeViewModel
+
+ @Before fun setUp() {
+ Dispatchers.setMain(dispatcher)
+ vm = TellyscopeViewModel(tv, clock = { 0L })
+ test.runCurrent()
+ }
+
+ @After fun tearDown() {
+ vm.viewModelScope.cancel()
+ Dispatchers.resetMain()
+ }
+
+ private fun press(key: RemoteKey): Boolean {
+ var took = false
+ test.launch { took = vm.press(key) }
+ test.runCurrent()
+ return took
+ }
+
+ @Test fun anEmptyBoxSendsNothing() {
+ // On the TV, Delete in an empty box is Back: YouTube closes.
+ assertFalse(press(RemoteKey.DEL))
+ assertEquals(emptyList<String>(), tv.sent)
+ }
+
+ @Test fun deletesWhatWasTypedOneLetterAPressThenStops() {
+ vm.type("cat")
+ test.runCurrent()
+ repeat(3) { assertTrue(press(RemoteKey.DEL)) }
+ // The fourth would be Back; a held key stops here.
+ assertFalse(press(RemoteKey.DEL))
+ assertEquals(listOf("type cat", "DEL", "DEL", "DEL"), tv.sent)
+ }
+
+ @Test fun anotherKeyForgetsTheTypedText() {
+ vm.type("cat")
+ test.runCurrent()
+ press(RemoteKey.DOWN)
+ assertFalse(press(RemoteKey.DEL))
+ assertEquals(listOf("type cat", "DOWN"), tv.sent)
+ }
+
+ @Test fun closingTheRemoteForgetsIt() {
+ vm.type("cat")
+ test.runCurrent()
+ vm.closeRemote()
+ assertFalse(press(RemoteKey.DEL))
+ }
+}