diff --git a/phone_agent/adb/input.py b/phone_agent/adb/input.py index 4c1c68cd8..b15ff00f1 100644 --- a/phone_agent/adb/input.py +++ b/phone_agent/adb/input.py @@ -4,6 +4,8 @@ import subprocess from typing import Optional +ADB_KEYBOARD_PACKAGE = "com.android.adbkeyboard" + def type_text(text: str, device_id: str | None = None) -> None: """ @@ -31,6 +33,8 @@ def type_text(text: str, device_id: str | None = None) -> None: "--es", "msg", encoded_text, + "-p", + ADB_KEYBOARD_PACKAGE, ], capture_output=True, text=True, @@ -47,7 +51,16 @@ def clear_text(device_id: str | None = None) -> None: adb_prefix = _get_adb_prefix(device_id) subprocess.run( - adb_prefix + ["shell", "am", "broadcast", "-a", "ADB_CLEAR_TEXT"], + adb_prefix + + [ + "shell", + "am", + "broadcast", + "-a", + "ADB_CLEAR_TEXT", + "-p", + ADB_KEYBOARD_PACKAGE, + ], capture_output=True, text=True, ) diff --git a/tests/test_adb_input.py b/tests/test_adb_input.py new file mode 100644 index 000000000..f0f9b44ba --- /dev/null +++ b/tests/test_adb_input.py @@ -0,0 +1,35 @@ +import subprocess + +from phone_agent.adb import input as adb_input + + +def test_type_text_targets_adb_keyboard_package(monkeypatch) -> None: + commands: list[list[str]] = [] + + def fake_run(command, **kwargs): + commands.append(command) + return subprocess.CompletedProcess(command, 0, stdout="", stderr="") + + monkeypatch.setattr(adb_input.subprocess, "run", fake_run) + + adb_input.type_text("你好", device_id="device-1") + + command = commands[0] + assert command[:3] == ["adb", "-s", "device-1"] + assert command[-2:] == ["-p", "com.android.adbkeyboard"] + + +def test_clear_text_targets_adb_keyboard_package(monkeypatch) -> None: + commands: list[list[str]] = [] + + def fake_run(command, **kwargs): + commands.append(command) + return subprocess.CompletedProcess(command, 0, stdout="", stderr="") + + monkeypatch.setattr(adb_input.subprocess, "run", fake_run) + + adb_input.clear_text(device_id="device-1") + + command = commands[0] + assert command[:3] == ["adb", "-s", "device-1"] + assert command[-2:] == ["-p", "com.android.adbkeyboard"]