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: the clear button works where Ctrl+A doesn't

jesse-agent-8218ba5a3457f9a
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.kt b/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.kt
index 7031209..37709c0 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/KeyHelper.kt
@@ -33,7 +33,7 @@ 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")
- /** Empties the focused text box on the TV (Ctrl+A, then Delete). Answers like [press]. */
+ /** Empties the focused text box on the TV (see [Remote.clearText]). Answers like [press]. */
suspend fun clear(): Boolean? = send("c")
/** Turns the TV's picture off or on; the TV stays awake and connected. Answers like [press]. */
diff --git a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
index 1697061..6ce2310 100644
--- a/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
+++ b/adb/src/main/kotlin/app/tellyscope/adb/Remote.kt
@@ -57,10 +57,11 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
suspend fun type(text: String): Boolean = keys?.type(text) ?: (session.shell(typeCommand(text)).exitCode == 0)
/**
- * Empties the focused text box on the TV. Only the key helper can (`input` has no Ctrl+A on
- * older Android): false when it isn't running or the TV refused. Throws if the link is gone.
+ * Empties the focused text box on the TV: End, then [MAX_TEXT] Deletes, one Send's worth. The
+ * key helper selects all and deletes first, which also clears longer text where the box takes
+ * Ctrl+A; `input` can't send Ctrl+A on older Android. Throws if the link is gone.
*/
- suspend fun clearText(): Boolean = keys?.clear() ?: false
+ suspend fun clearText(): Boolean = keys?.clear() ?: (session.shell(CLEAR_COMMAND).exitCode == 0)
/**
* Turns the TV's picture off or back on, leaving the TV awake so the link stays up. Only the
@@ -95,6 +96,9 @@ class Remote(private val session: AdbSession, private val keys: KeyHelper? = nul
companion object {
const val MAX_TEXT = 200
+ /** End, then a Delete per character. Keys.DELETES matches [MAX_TEXT]. */
+ internal val CLEAR_COMMAND = "input keyevent 123" + " 67".repeat(MAX_TEXT)
+
/** `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 2d85787..f79997c 100644
--- a/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
+++ b/adb/src/test/kotlin/app/tellyscope/adb/RemoteTest.kt
@@ -45,6 +45,20 @@ class RemoteTest {
assertEquals("input keyevent 3", sh.ran.last())
}
+ @Test fun clearGoesThroughTheHelperWhileItRuns() = runBlocking { // see KeyHelperTest
+ val sh = FakeShell().apply { execOutput = "ready\n1\n" }
+ assertTrue(Remote(sh, KeyHelper(sh, byteArrayOf(1))).clearText())
+ assertEquals("c\n", sh.execInput.readUtf8())
+ assertTrue(sh.ran.none { it.startsWith("input") })
+ }
+
+ @Test fun clearFallsBackToInputWhenTheHelperCannotStart() = runBlocking { // see KeyHelperTest
+ val sh = FakeShell().apply { execOutput = "" }
+ assertTrue(Remote(sh, KeyHelper(sh, byteArrayOf(1))).clearText())
+ val codes = sh.ran.last().removePrefix("input keyevent ").split(' ')
+ assertEquals(listOf("123") + List(Remote.MAX_TEXT) { "67" }, codes) // End, then a Delete per character
+ }
+
@Test fun fireHomeStartsTheDefaultLauncher() = runTest {
val sh = FakeShell()
assertTrue(Remote(sh).home("com.spocky.projengmenu/.ui.home.MainActivity"))
diff --git a/app/src/helper/java/app/tellyscope/helper/Keys.java b/app/src/helper/java/app/tellyscope/helper/Keys.java
index e11259c..5c1c870 100644
--- a/app/src/helper/java/app/tellyscope/helper/Keys.java
+++ b/app/src/helper/java/app/tellyscope/helper/Keys.java
@@ -29,6 +29,10 @@ import java.lang.reflect.Method;
public final class Keys {
/** InputManager's INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH: answers once the focused app is done with it. */
private static final int WAIT_FOR_FINISH = 2;
+ /** INJECT_INPUT_EVENT_MODE_ASYNC: queued in order, no wait. */
+ private static final int ASYNC = 0;
+ /** Deletes after End: one Send's worth, Remote.MAX_TEXT. */
+ private static final int DELETES = 200;
public static void main(String[] args) throws Exception {
Object manager;
@@ -118,7 +122,11 @@ public final class Keys {
}
}
- /** Ctrl+A, then Delete: everything in the focused text box is selected and deleted. */
+ /**
+ * Ctrl+A and Delete, then End and {@link #DELETES} Deletes for the boxes that ignore Ctrl+A.
+ * The Deletes are queued without waiting and only the last one waits for the app: keys reach
+ * it in order, so that answer covers them all and the batch stays well inside the answer wait.
+ */
private static boolean clear(Object manager, Method inject) {
try {
int ctrl = KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON;
@@ -129,17 +137,28 @@ public final class Keys {
// Ctrl comes back up even when A wasn't taken, so the TV isn't left holding it.
ok = send(manager, inject, now, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_CTRL_LEFT, 0) && ok;
long del = SystemClock.uptimeMillis();
- return ok
+ ok = ok
&& send(manager, inject, del, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL, 0)
&& send(manager, inject, del, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL, 0);
+ for (int i = 0; ok && i <= DELETES; i++) {
+ int code = i == 0 ? KeyEvent.KEYCODE_MOVE_END : KeyEvent.KEYCODE_DEL;
+ long down = SystemClock.uptimeMillis();
+ ok = send(manager, inject, down, KeyEvent.ACTION_DOWN, code, 0, ASYNC)
+ && send(manager, inject, down, KeyEvent.ACTION_UP, code, 0, i == DELETES ? WAIT_FOR_FINISH : ASYNC);
+ }
+ return ok;
} catch (Throwable t) {
return false;
}
}
private static boolean send(Object manager, Method inject, long downTime, int action, int code, int meta) throws Exception {
+ return send(manager, inject, downTime, action, code, meta, WAIT_FOR_FINISH);
+ }
+
+ private static boolean send(Object manager, Method inject, long downTime, int action, int code, int meta, int mode) throws Exception {
KeyEvent e = new KeyEvent(downTime, SystemClock.uptimeMillis(), action, code, 0, meta,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
- return (Boolean) inject.invoke(manager, e, WAIT_FOR_FINISH);
+ return (Boolean) inject.invoke(manager, e, mode);
}
}