fix(ctl): the broadcast fallback only stands in for a missing content command

This commit is contained in:
Kurt Himebauch committed 2026-11-09 12:26:37 -05:00
1 parent 05f50c3232
commit 941cfe9c44
2 files changed
+49 -1

No files matched your search

+6 -1
View File
@@ -100,6 +100,10 @@ def encode_request(request: dict) -> str:
def receiver_call(adb: str, serial: str, method: str, request: dict | None, timeout: float) -> dict:
"""The agent bridge through a broadcast, for shells without the `content` command."""
# Its receiver is registered by the running app; a broadcast does not start the app the way `content call` does
running = run_adb(adb, serial, "shell", f"pidof {PACKAGE}", timeout=30)
if not running.stdout.decode(errors="replace").strip():
raise ControlError(f"{PACKAGE} is not running and this shell has no `content` command to start it; open DroidDeck once", "APP_NOT_RUNNING")
call_id = uuid.uuid4().hex[:16]
remote = f"/sdcard/Android/data/{PACKAGE}/files/agent-calls/{call_id}.json"
command = ["shell", "am", "broadcast", "-a", f"{PACKAGE}.AGENT_CALL", "-p", PACKAGE,
@@ -129,7 +133,8 @@ def provider_call(adb: str, serial: str, method: str, arg: str | None = None, re
command.extend(["--extra", f"request:s:{encoded}"])
result = run_adb(adb, serial, *command, timeout=timeout)
output = (result.stdout + result.stderr).decode(errors="replace")
if "content: inaccessible or not found" in output:
# Only the shell's own answer for a missing `content` binary, never a command's output that quotes it
if result.returncode == 127 and "Result:" not in output and "content: inaccessible or not found" in result.stderr.decode(errors="replace") + output:
return receiver_call(adb, serial, method, request, timeout)
response = _content_result(output)
if result.returncode != 0 and response.get("ok") is not False:
+43
View File
@@ -792,5 +792,48 @@ class ScenarioTest(FakeAdbTest):
self.assertEqual([{"appId": 7, "timeout": 15.0}] * 2, [c["request"] for c in self.calls("quit")])
class ProviderFallbackTest(unittest.TestCase):
MISSING = "/system/bin/sh: content: inaccessible or not found\n"
def adb(self, answers):
calls = []
def run(adb, serial, *args, timeout=None):
calls.append(args)
for prefix, (code, out, err) in answers:
if " ".join(args).startswith(" ".join(prefix)):
return unittest.mock.Mock(returncode=code, stdout=out.encode(), stderr=err.encode())
raise AssertionError(f"unexpected adb call {args}")
return calls, unittest.mock.patch.object(ctl, "run_adb", run)
def test_a_command_that_prints_the_shell_error_is_not_run_again(self):
result = 'Result: Bundle[{json={"ok":true,"stdout":{"text":"' + self.MISSING.strip() + '"}}}]'
calls, patch = self.adb([(["shell", "content"], (0, result, ""))])
with patch:
self.assertTrue(ctl.provider_call("adb", "s", "native", request={"argv": ["x"]})["ok"])
self.assertEqual(1, len(calls))
def test_a_shell_without_content_uses_the_broadcast(self):
calls, patch = self.adb([
(["shell", "content"], (127, "", self.MISSING)),
(["shell", "pidof"], (0, "4940\n", "")),
(["shell", "am", "broadcast"], (0, "Broadcast completed", "")),
(["shell"], (0, '{"ok": true}', "")),
])
with patch:
self.assertTrue(ctl.provider_call("adb", "s", "state")["ok"])
self.assertEqual(["shell", "am", "broadcast"], list(calls[2][:3]))
def test_the_broadcast_needs_the_app_running(self):
calls, patch = self.adb([
(["shell", "content"], (127, "", self.MISSING)),
(["shell", "pidof"], (1, "", "")),
])
with patch, self.assertRaises(ctl.ControlError) as raised:
ctl.provider_call("adb", "s", "state")
self.assertEqual("APP_NOT_RUNNING", raised.exception.code)
self.assertFalse(any(c[:3] == ("shell", "am", "broadcast") for c in calls))
if __name__ == "__main__":
unittest.main()