Skip to content

Type skips input-method restoration after an exception or interruption #429

Open

Description

@LingYi-Liang

If Type raises an exception or is interrupted after keyboard detection,
Android can be left using ADB Keyboard. _handle_type only calls
restore_keyboard() after clearing, typing and all delays have finished.

Reproduction

Tested on Windows with Python 3.11.9 at
86f55382982fb054e8fc98ca80609dff8a2cdc3c.
Run this from the repository with its Python dependencies installed. It uses the
real handler, device factory and ADB input functions, with subprocess responses
and delays mocked. The text triggers a UTF-8 encoding error inside type_text().
No phone, ADB executable or model API is needed.

import subprocess
from unittest.mock import patch

from phone_agent.actions import handler

commands = []


def simulated_adb(command, **kwargs):
    commands.append(command)
    stdout = "vendor.keyboard/.IME\n" if command[-1] == "default_input_method" else ""
    return subprocess.CompletedProcess(command, 0, stdout=stdout, stderr="")


with patch("subprocess.run", side_effect=simulated_adb), patch.object(handler.time, "sleep"):
    result = handler.ActionHandler(device_id="mock-device").execute(
        {"_metadata": "do", "action": "Type", "text": "\ud800"}, 100, 200
    )

restores = [c for c in commands if c[-3:] == ["ime", "set", "vendor.keyboard/.IME"]]
print("success:", result.success, "restore commands:", len(restores))

The original code prints success: False restore commands: 0. It should still
report the input error, but also attempt to restore the saved input method.
With the proposed fix it prints success: False restore commands: 1.

Cancellation has the same problem. The regression tests also cover cancellation
followed by an ordinary exception during restoration, so cleanup doesn't hide the interrupt.

This covers cleanup after detection returns. Failures during detection's warm-up
and ADB return-code handling need separate changes. Restoration on a physical
device hasn't been tested. #421 addresses a different cause: stderr becoming part
of the saved input-method identifier.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions