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: about 15 taps for the full range, no tap pile-up

jesse-agent-232d1c4a67717ba
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f033da9..18eb88b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ until this file has a section for its version.
## Unreleased
+- **Improved:** Remote volume moves the full range in about 15 taps, without lag. [#267](../../pull/267)
- **Fixed:** On Fire TV, making Projectivy Home now opens it straight away, without a Home press. [#263](../../pull/263)
- **Fixed:** On Fire TV, Reset TV now puts Amazon's launcher back when Projectivy holds Home, by turning its accessibility service off. Undo turns it back on. [#263](../../pull/263)
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
index 4c9017f..e568519 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
@@ -2,6 +2,7 @@
package app.tellyscope.adb
import kotlinx.coroutines.CancellationException
+import kotlin.math.roundToInt
/**
* The TV's remote. Keys are a fixed list of Android key codes, so nothing the user does reaches
@@ -33,6 +34,12 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
suspend fun press(key: RemoteKey): Boolean =
keys?.press(key.code) ?: (session.shell("input keyevent ${key.code}").exitCode == 0)
+ /** [press] [times] over, stopping at the first key the TV didn't take. */
+ suspend fun press(key: RemoteKey, times: Int): Boolean = (1..times).all { press(key) }
+
+ /** 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)
+
/**
* 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
@@ -91,6 +98,18 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
companion object {
const val MAX_TEXT = 200
+ /** A volume tap moves about this share of the range, whatever the TV's own step count. */
+ const val VOLUME_STEPS = 15
+
+ /** `Max: 15` in the STREAM_MUSIC block of `dumpsys audio`. */
+ fun musicMax(dump: String): Int? {
+ val block = dump.substringAfter("- STREAM_MUSIC:", "").ifEmpty { return null }
+ return Regex("""^\s*Max:\s*(\d+)""", RegexOption.MULTILINE).find(block)?.groupValues?.get(1)?.toIntOrNull()?.takeIf { it > 0 }
+ }
+
+ /** 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)
+
/** `pkg/.Activity` or `pkg/pkg.Activity`; a nested class has a `$`, which the single quotes keep. */
private val COMPONENT = Regex("[A-Za-z0-9_.]+/[A-Za-z0-9_.$]+")
diff --git a/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt b/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
index 79cdd8a..63c8453 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
@@ -146,4 +146,42 @@ class RemoteTest {
}
assertTrue(Remote(sh).reboot())
}
+
+ @Test fun volumeMaxIsReadFromTheMusicStream() {
+ val dump = """
+ - STREAM_SYSTEM:
+ Min: 0
+ Max: 7
+ - STREAM_MUSIC:
+ Muted: false
+ Muted Internally: false
+ Min: 0
+ Max: 15
+ streamVolume:15
+ Current: 2 (speaker): 3, 400 (hdmi): 15, 40000000 (default): 3
+ Devices: hdmi
+ - STREAM_ALARM:
+ Max: 7
+ """.trimIndent()
+ assertEquals(15, Remote.musicMax(dump))
+ assertEquals(null, Remote.musicMax("Error: dumpsys not allowed"))
+ }
+
+ @Test fun volumeTapsCoverAboutFifteenSteps() {
+ assertEquals(1, Remote.volumeKeys(15))
+ assertEquals(1, Remote.volumeKeys(7))
+ assertEquals(2, Remote.volumeKeys(30))
+ assertEquals(3, Remote.volumeKeys(50))
+ assertEquals(7, Remote.volumeKeys(100))
+ assertEquals(1, Remote.volumeKeys(null))
+ }
+
+ @Test fun volumeKeysRepeatAndStopAtTheFirstRefusal() = runTest {
+ val sh = FakeShell()
+ assertTrue(Remote(sh).press(RemoteKey.VOLUME_UP, 3))
+ assertEquals(List(3) { "input keyevent 24" }, sh.ran)
+ val refused = FakeShell(mapOf("input keyevent 25" to ShellResult("", 1)))
+ assertFalse(Remote(refused).press(RemoteKey.VOLUME_DOWN, 3))
+ assertEquals(listOf("input keyevent 25"), refused.ran)
+ }
}
diff --git a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
index a6712d1..4c96455 100644
--- a/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
+++ b/app/src/main/java/app/tellyscope/data/AdbDeviceSource.kt
@@ -108,6 +108,8 @@ class AdbDeviceSource(context: Context) : DeviceSource {
private val labels = ConcurrentHashMap<String, String>()
/** Per TV, the session [fireHome] read on and the launcher it found. */
private val fireHomes = ConcurrentHashMap<String, Pair<AdbSession, String?>>()
+ /** Per TV, the session [volumeKeys] read on and the keys per tap it worked out. */
+ private val volumeSteps = ConcurrentHashMap<String, Pair<AdbSession, Int>>()
/** The remote's key helper on each TV, while the remote is open. */
private val keyHelpers = ConcurrentHashMap<String, KeyHelper>()
private val helperDex by lazy { appContext.assets.open("label-helper.jar").use { it.readBytes() } }
@@ -236,6 +238,7 @@ class AdbDeviceSource(context: Context) : DeviceSource {
watchers.remove(deviceId)?.cancel()
keyHelpers.remove(deviceId)?.close()
fireHomes -= deviceId
+ volumeSteps -= deviceId
links.remove(deviceId)?.disconnect()
hits.remove(deviceId)
restoring -= deviceId
@@ -898,9 +901,20 @@ class AdbDeviceSource(context: Context) : DeviceSource {
override suspend fun press(deviceId: String, key: RemoteKey) = busy(deviceId) {
if (key == RemoteKey.HOME && deviceKind(device(deviceId)) == DeviceKind.FireTv) remote(deviceId).home(fireHome(deviceId))
+ else if (key == RemoteKey.VOLUME_UP || key == RemoteKey.VOLUME_DOWN) remote(deviceId).press(key, volumeKeys(deviceId))
else remote(deviceId).press(key)
}
+ /**
+ * Keys per volume tap, so a tap moves about 1/[Remote.VOLUME_STEPS] of the range on any TV. Read
+ * once per session; a reconnect reads it again.
+ */
+ private suspend fun volumeKeys(deviceId: String): Int {
+ val session = session(deviceId)
+ volumeSteps[deviceId]?.takeIf { it.first === session }?.let { return it.second }
+ return Remote.volumeKeys(Remote(session).volumeMax()).also { volumeSteps[deviceId] = session to it }
+ }
+
/**
* The launcher Home resolves to on a Fire TV, read once per session so the key stays fast. A
* home switch or its undo drops it; a reconnect brings a new session, which reads it again.
diff --git a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
index 2cedd3f..6b3f5d8 100644
--- a/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
+++ b/app/src/main/java/app/tellyscope/ui/TellyscopeViewModel.kt
@@ -79,6 +79,7 @@ import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.atomic.AtomicBoolean
/** Per-row connect state on the scan screen. */
sealed interface RowState {
@@ -420,6 +421,11 @@ class TellyscopeViewModel(
/** Keys go out one at a time, in the order pressed. */
private val keys = Mutex()
+ /**
+ * A volume tap is waiting for [keys]. Further taps while it waits are dropped rather than queued,
+ * so a burst of taps can't keep a slow TV changing volume after the thumb stops.
+ */
+ private val volumeWaiting = AtomicBoolean(false)
/**
* 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.
@@ -1825,7 +1831,10 @@ class TellyscopeViewModel(
*/
suspend fun press(key: RemoteKey): Boolean {
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)