Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。
feat(input): type through the accessibility service's own input method
booboosui26d0cb498ecf
diff --git a/CLAUDE.md b/CLAUDE.mdindex 64053a2..e8d6f3b 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -99,9 +99,22 @@ Ported from generic_bridge (phone.sh), whose finding is the founding rule here:- **NO_TREE policy** (empty-list hint): prefer `back`/`home` to exit and retry via a different route over blind coordinate taps — a wrong exit costs one tap, a wrong tap can cost the task.- **Stale-service note**: `ScreenController.goodDumps` records which packages have actually produced a usable element list here. When a dump comes back with *zero nodes* from a package with such a history, `reviveNote()` appends the evidence to the hint — WeChat's tree is genuinely obtainable, but the accessibility connection sometimes goes stale and only a human toggling Andee off/on in 设置 → 无障碍 restores it. It is **evidence, not a rule**: the note states the facts and the brain decides whether to interrupt the user. It is deliberately not keyed to `com.tencent.mm`, and deliberately fires only on `nodes_total == 0` — a self-drawn pane still reports its containers, so mini-programs never trigger it.-### Text input goes through ADBKeyboard, and the action names are bare+### Text input: the service types for itself, ADBKeyboard is the spare-`type_text` has exactly one channel: `ScreenController.typeViaAdbKeyboard` broadcasts to **ADBKeyboard**, which must be the device's active IME. It is an IME, so it types through `InputConnection.commitText()` — the same path a human finger takes, which an app cannot refuse without refusing all keyboards. That is why the older waterfall (`ACTION_SET_TEXT` → clipboard `ACTION_PASTE` → IME panel walking) is gone: `SET_TEXT` crossed fields on Radix forms and did nothing at all in CodeMirror.+`type_text` has two channels and `ScreenController.typeText` tries them in this order:++1. **`screen/A11yIme.kt` (API 33+, the normal one).** `accessibility_service_config.xml` asks for `flagInputMethodEditor`, which makes `AccessibilityService.getInputMethod()` hand back the focused editor's own `InputConnection` — `commitText` down the same path a keyboard takes, which an app cannot refuse without refusing all keyboards. What it is *not* is an IME: nothing to install, nothing to enable, nothing to select, and **the user's own keyboard stays active the whole time**. That is the point. It costs the user nothing to set up, where the route below needs a third-party APK and an `adb` grant a person without a computer cannot obtain.+2. **`typeViaAdbKeyboard` (API 30–32, and any editor the first channel cannot reach).** Everything in the rest of this section describes it, and all of it is still live.++`typeViaA11yIme` returns **null only when nothing was typed** — that is what makes the fallback safe, and inverting it is how you get text typed twice. A non-null result that disagrees with the readback is a real failure, not a missing channel, and must not fall through. `submitInput` has the same shape: `ACTION_IME_ENTER` first, then `A11yIme.editorAction`, which asks the field's own `EditorInfo` whether it is a Send, a Search or a Next rather than assuming Enter.++The first channel also has a session-attach race worth knowing: a field that took focus a few frames ago may have no input session yet, and returning null there would send a `WRITE_SECURE_SETTINGS`-less device to the hard error this channel exists to retire. `awaitConnection` polls to `SESSION_WAIT_MS` (400 ms) — measured right after the service rebinds. Readback comes from `getSurroundingText`, i.e. **the editor's own buffer**, so the WebViews that hide their focused node's text stop being a false negative. Two smaller wins fall out, neither of them the reason: a vault password typed this way never leaves the process, and there is no ~0.75 s IME lend-and-give-back per call.++Verified on device 2026-09-30 with Baidu IME still the default throughout: Chrome's `url_bar` (native `EditText`), an in-page web `<input>`, and a `show_html` WebView.++#### The ADBKeyboard route, and the action names are bare++`ScreenController.typeViaAdbKeyboard` broadcasts to **ADBKeyboard**, which must be the device's active IME. It is an IME, so it types through `InputConnection.commitText()` too. That is why the older waterfall (`ACTION_SET_TEXT` → clipboard `ACTION_PASTE` → IME panel walking) is gone: `SET_TEXT` crossed fields on Radix forms and did nothing at all in CodeMirror.The one thing to get right, because getting it wrong is invisible: **the broadcast actions have no package prefix.** ADBKeyboard registers its receiver at runtime with the bare strings `ADB_INPUT_TEXT` / `ADB_CLEAR_TEXT` (confirm with `adb shell dumpsys activity broadcasts | grep ADB_INPUT`). Sending `com.android.adbkeyboard.ADB_INPUT_TEXT` matches no filter, and since we also `setPackage()` the intent, it is delivered to nobody — no exception, no log, the field simply stays empty. That failure is indistinguishable from an OEM blocking the broadcast, and it cost a day: it was read as "MIUI drops app-to-app broadcasts", a hub-side relay was built to re-send the broadcast from the PC over `adb` as `shell`, and because the relay used the *correct* bare action it worked — so on a plugged-in device everything looked fine and only an unplugged one failed. If typing ever breaks again, check the action strings and the active IME before believing any theory about app identity or OEM policy.@@ -109,7 +122,7 @@ Readback is a **poll**, not a sleep: after `commitText` the WebView still has to**The keyboard belongs to the person; the brain borrows it per call.** ADBKeyboard must be the *active* IME for any of the above to work, and it is a headless IME — no keyboard UI at all. So the device **rests on the user's own IME** (they can type in any app on the tablet), and `ui/ImeSwitch.lendToBrain` / `giveBack` bracket each `type_text`: ADBKeyboard for one tool call, then straight back. Two things that follow, both measured, both easy to get wrong:- * **The switch is a `Settings.Secure.DEFAULT_INPUT_METHOD` write.** On MIUI an app calling `InputMethodManager.setInputMethod` *or* `switchToNextInputMethod` is silently refused (no exception, no log — read the setting back or you will believe it worked). The settings write needs `WRITE_SECURE_SETTINGS`, granted once at provisioning (`adb shell pm grant net.kuafuai.andee android.permission.WRITE_SECURE_SETTINGS`), and a reinstall keeps it. `lendToBrain` then waits ~350 ms before broadcasting: the setting and the *binding* are different events, and a broadcast into an IME that has not bound yet disappears without a trace. Expect ~0.75 s per `type_text`.+ * **The switch is a `Settings.Secure.DEFAULT_INPUT_METHOD` write.** On MIUI an app calling `InputMethodManager.setInputMethod` *or* `switchToNextInputMethod` is silently refused (no exception, no log — read the setting back or you will believe it worked). The settings write needs `WRITE_SECURE_SETTINGS`, granted once at provisioning (`adb shell pm grant net.kuafuai.andee android.permission.WRITE_SECURE_SETTINGS`), and a reinstall keeps it. `lendToBrain` then waits ~350 ms before broadcasting: the setting and the *binding* are different events, and a broadcast into an IME that has not bound yet disappears without a trace. Expect ~0.75 s per `type_text`. **This is the grant an ordinary user cannot give**, which is the whole reason the accessibility channel above is now first.* **The person's keyboard is raised by the brain's own tap.** The brain taps a field, which asks for a soft keyboard; with a real IME active that request is honoured — on screen, over the app being driven. Nothing puts it back down: `hideSoftInputFromWindow(null, …)` is ignored, and `performGlobalAction(GLOBAL_ACTION_BACK)` is *not* consumed by the IME the way an injected key event is (it dismissed the page instead). Don't re-try either. It clears itself when the brain touches something that takes focus.And one platform rule that shaped the ⌨ field: **a `TYPE_APPLICATION_OVERLAY` window cannot raise the soft keyboard on this ROM** — not via `showSoftInput`, not via `WindowInsetsController.show(ime())`, not even from a real finger tap. The same input inside an Activity raises it immediately. That is why `ui/TextInputActivity` is an Activity, why the card has to **fold** before it opens (a fullscreen overlay sits *above* application windows and eats every touch aimed at the field — the same reason `ui.show_html` folds), and why it handles the IME inset by hand: a translucent window is never resized for the keyboard.@@ -379,7 +392,8 @@ Five things in the design are load-bearing:- **`Fix` is a closed set of values, not a lambda.** `Screen(action, data)` / `Link(url)` / `Grant(permissions)` / `Adb(command)` / `OurSettings` / `Nothing`. `actionable` is defined from that set. Values rather than closures because the list is built off the main thread and consumed up to seconds later — an `Intent` described at 200 ms is still good at 3 s, where a captured `Context` would be a leak waiting to be filed as a bug. `Link` is its own member rather than `Screen(ACTION_VIEW, url)`: the button it produces is a different word (去下载 is not 去开启), and a Settings-labelled button that opens a browser is the small lie this list exists to avoid.- **`Fix.Adb` deliberately has no button.** `WRITE_SECURE_SETTINGS` is `signature|privileged|development`, so `adb shell pm grant` is the only way and there is no switch in Settings to find. The row shows a **selectable** command and says it is an adb errand — a user hunting Settings for a switch that does not exist there is the failure mode being prevented.- **`Fix.OurSettings` is the one door that can be missing, and the card says so instead of drawing it.** Our settings sheet is an overlay the *service* owns, so with accessibility off there is nothing for 打开设置 to open. `SelfCheckUi.serviceUp` reads that off the `accessibility` row — one source of truth, never a second test that could disagree with the red row directly above it — and when it is false the row gets **no** button and a note above the list says why. Same rule as `Fix.Adb`: a button that does nothing is worse than no button.-- **The keyboard row tests the exact component this app switches to.** `type_text` has one channel — a broadcast to `com.android.adbkeyboard`, which has to be an *enabled* IME for the `commitText` to land — so "can the assistant type into other apps" is decided by two facts on the device that had no way of announcing themselves, and the symptom is an error that blames the app being driven. One row rather than two, because the three states are three steps of one errand and the button changes as they are completed: not there → 去下载, there but off → `INPUT_METHOD_SETTINGS` (the screen with the toggle, not `showInputMethodPicker`, which `ImeSwitch` refuses on purpose), enabled → OK. A permanent red "not installed" above a green "enabled" is what two rows would buy. Installed-ness is asked through `getInputMethodList()` — "is there a keyboard *in* that package", not "does the package exist", so a truncated APK does not send the user to a keyboard list that cannot contain it — and that only works because the manifest declares `QUERY_ALL_PACKAGES`; enabled-ness is the **exact** id, because a build with a renamed service is one this app cannot switch to and a green row beside a dead `type_text` is the lie this whole page exists to prevent. [Level.WARN], deliberately the same as the `WRITE_SECURE_SETTINGS` row beside it: one broken feature must not be graded differently in two rows, or the colours stop meaning anything. The download is the **v2.5-dev release asset**, not the `master/ADBKeyboard.apk` every blog post links to — upstream itself labels that one "[Old]", and v2.5-dev exists to fix it on android-36, which is what a newly bought tablet arrives on. Both APKs were read before choosing (`com.android.adbkeyboard.AdbIME`, `ADB_INPUT_TEXT`/`ADB_CLEAR_TEXT`/`ADB_EDITOR_CODE`, `commitText` — the same component and actions `ScreenController` names). It is **not** vendored into `assets/` and installed through a `FileProvider`, which would make it one tap instead of three: redistributing someone else's APK is a licensing decision rather than a build detail.+- **The keyboard row tests the exact component this app switches to.** `typeViaAdbKeyboard` is a broadcast to `com.android.adbkeyboard`, which has to be an *enabled* IME for the `commitText` to land — so "can the assistant type into other apps *this way*" is decided by two facts on the device that had no way of announcing themselves, and the symptom is an error that blames the app being driven. One row rather than two, because the three states are three steps of one errand and the button changes as they are completed: not there → 去下载, there but off → `INPUT_METHOD_SETTINGS` (the screen with the toggle, not `showInputMethodPicker`, which `ImeSwitch` refuses on purpose), enabled → OK. A permanent red "not installed" above a green "enabled" is what two rows would buy. Installed-ness is asked through `getInputMethodList()` — "is there a keyboard *in* that package", not "does the package exist", so a truncated APK does not send the user to a keyboard list that cannot contain it — and that only works because the manifest declares `QUERY_ALL_PACKAGES`; enabled-ness is the **exact** id, because a build with a renamed service is one this app cannot switch to and a green row beside a dead `type_text` is the lie this whole page exists to prevent. The download is the **v2.5-dev release asset**, not the `master/ADBKeyboard.apk` every blog post links to — upstream itself labels that one "[Old]", and v2.5-dev exists to fix it on android-36, which is what a newly bought tablet arrives on. Both APKs were read before choosing (`com.android.adbkeyboard.AdbIME`, `ADB_INPUT_TEXT`/`ADB_CLEAR_TEXT`/`ADB_EDITOR_CODE`, `commitText` — the same component and actions `ScreenController` names). It is **not** vendored into `assets/` and installed through a `FileProvider`, which would make it one tap instead of three: redistributing someone else's APK is a licensing decision rather than a build detail.+- **The two typing rows are graded by whether the other channel is open, and `imeChannelOpen()` is the single source for that.** `adbKeyboard` and `secureSettings` are `WARN` when the accessibility IME is unavailable and **`NOTE` when it is** — the feature is not broken, the row has become a spare for Android 12 and below. They are graded identically to each other for the reason they always were: one broken feature must not be two different colours. A third row, `typing_channel`, is `NOTE` at every value and exists only to name which channel is in play; without it a user reading two grey rows about a keyboard they never installed has no way to learn that typing works anyway. `imeChannelOpen()` reads `FLAG_INPUT_METHOD_EDITOR` off the **running service's** `serviceInfo` rather than trusting the config XML, and returns true when the service is not up (pre-33 is the only hard no) — a checklist should not invent a failure it cannot observe. Note `Fix.Adb` on the `secureSettings` row stays drawn even at `NOTE`: the command is still the only way to get the spare working.- **The backend probe distinguishes three things the symptom cannot.** `GET /models` decides all of them without spending a token: URL wrong → the connection fails; key wrong → 401/403; **model name wrong → the name is not in the returned list**. That last one is the likeliest, because it is the one a person types by hand, and its error names *what was on offer* rather than a bare 400. Gateways with no `/models` (404/405) fall back to a `max_tokens: 1` + thinking-disabled chat request, and an empty list on a 200 is read as "reachable" rather than invented into a model complaint. The voice host gets a bare **TCP connect** and not a credential check — reproducing the 火山 handshake here would mean a second copy of that protocol, and what this row is for is the failure this device actually had (`ASR ws connect timeout`, network up, host unreachable), so a green row beside a failing ASR means the key, by elimination.The service's own start passes `startBrain(announce = false)`, so the no-brain state is reported by the self-check card **with all the other rows** instead of by a second card arguing about the same fact. `reportNoBrain(what, card)` still writes the `ChatHistory` row either way — that is a record, not an interruption. A factory reset still announces normally: the user just wiped the config and is owed the news that the device is mute.diff --git a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.ktindex e8b0d18..00a9256 100644--- a/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt+++ b/app/src/main/java/net/kuafuai/andee/device/SelfCheck.kt@@ -1,6 +1,7 @@package net.kuafuai.andee.deviceimport android.Manifest+import android.accessibilityservice.AccessibilityServiceInfoimport android.content.ComponentNameimport android.content.Contextimport android.content.Intent@@ -8,6 +9,7 @@ import android.content.pm.PackageManagerimport android.net.ConnectivityManagerimport android.net.NetworkCapabilitiesimport android.net.Uri+import android.os.Buildimport android.os.PowerManagerimport android.os.StatFsimport android.provider.Settings@@ -168,6 +170,7 @@ object SelfCheck {*/fun run(context: Context, probe: Boolean = true): List<Finding> {val online = isOnline(context)+ val a11yIme = imeChannelOpen()return listOf(accessibility(context),overlay(context),@@ -176,8 +179,9 @@ object SelfCheck {backend(context, probe, online),microphone(context),notificationAccess(context),- adbKeyboard(context),- secureSettings(context),+ typingChannel(context),+ adbKeyboard(context, a11yIme),+ secureSettings(context, a11yIme),voiceKey(context),voiceReach(probe, online),wakePhrase(context),@@ -449,7 +453,55 @@ object SelfCheck {}/**- * ADBKeyboard — the headless IME every `type_text` goes through.+ * Which of the two typing channels is actually in play.+ *+ * The primary one is our own accessibility service acting as an input method+ * ([net.kuafuai.andee.screen.A11yIme]) — API 33+, nothing for the user to+ * install, enable, switch or plug a cable in for. When it is there, the two+ * rows below it describe a fallback and stop being errands; when it is not,+ * they are the only way the assistant can type and go back to being warnings.+ *+ * So this row is the *reason* those two change colour, which is why it is+ * [Level.NOTE] in both directions and never an errand itself: there is+ * nothing a user can do about an Android 12 tablet, and a red row with no+ * fix is the thing this page exists to avoid.+ *+ * Asked of the **running service's** `serviceInfo` rather than of+ * `Build.VERSION`, because the flag is what the platform granted, not what+ * the manifest asked for. With the service down the question is moot — the+ * `accessibility` row above is already red and nothing can type at all — and+ * the answer defaults to the version check so the fallback rows still read+ * correctly on the launcher's copy of this page.+ */+ private fun typingChannel(context: Context): Finding {+ val on = imeChannelOpen()+ return Finding(+ id = "typing_channel",+ title = R.string.check_title_typing_channel,+ level = Level.NOTE,+ detail = if (on) R.string.check_detail_typing_a11y+ else R.string.check_detail_typing_adb,+ fix = Fix.Nothing,+ )+ }++ /**+ * Whether the accessibility input-method channel is open.+ *+ * Shared by [typingChannel] and the two fallback rows so they cannot+ * disagree about the same fact — the failure a second copy of this test+ * would have within a week.+ */+ private fun imeChannelOpen(): Boolean {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return false+ val info = runCatching {+ net.kuafuai.andee.ScreenBodyService.get()?.serviceInfo+ }.getOrNull() ?: return true+ return info.flags and AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR != 0+ }++ /**+ * ADBKeyboard — the headless IME the **fallback** `type_text` goes through.** `ScreenController.typeViaAdbKeyboard` has exactly one channel: a broadcast* to `com.android.adbkeyboard`, which must be an *enabled* IME for the@@ -464,26 +516,34 @@ object SelfCheck {* but off → enable it; enabled → [Level.OK]. Two rows would leave a* permanently red "not installed" beside a green "enabled".*- * **[Level.WARN], matching [secureSettings] beside it.** Same capability,- * same blast radius: only typing into other apps is lost, everything else on- * this page still works. The two rows have to agree about how bad the same- * broken feature is, or the list teaches the user to distrust its colours.+ * **[Level.NOTE] once [typingChannel] is open, [Level.WARN] when it is+ * not** — and that split is the whole point of [a11yIme] being a parameter.+ * This used to be an unconditional WARN, from a time when it was the only+ * channel: a user with no computer was told to go get one, for a capability+ * the device now has by itself. Downgraded rather than deleted because+ * API 30–32 and any editor the primary channel cannot reach still land here,+ * and a silently missing fallback is how typing breaks twice.** The download is [ADB_KEYBOARD_APK] and the enable step is* `INPUT_METHOD_SETTINGS`, which is the screen that lists keyboards with the* toggle that actually turns this one on — not `showInputMethodPicker`,* which `ImeSwitch` refuses on purpose.*/- private fun adbKeyboard(context: Context): Finding {+ private fun adbKeyboard(context: Context, a11yIme: Boolean): Finding {val installed = net.kuafuai.andee.ui.ImeSwitch.brainInstalled(context)val enabled = installed &&net.kuafuai.andee.ui.ImeSwitch.brainEnabled(context)return Finding(id = "adb_keyboard",title = R.string.check_title_adb_keyboard,- level = if (enabled) Level.OK else Level.WARN,+ level = when {+ enabled -> Level.OK+ a11yIme -> Level.NOTE+ else -> Level.WARN+ },detail = when {enabled -> R.string.check_detail_adb_kb_on+ a11yIme -> R.string.check_detail_adb_kb_spareinstalled -> R.string.check_detail_adb_kb_offelse -> R.string.check_detail_adb_kb_missing},@@ -496,26 +556,35 @@ object SelfCheck {}/**- * `WRITE_SECURE_SETTINGS`, which `type_text` needs.+ * `WRITE_SECURE_SETTINGS`, which the **fallback** `type_text` needs.** The one row with no button on purpose. It is- * `signature|privileged|development`: `pm grant` over adb is the only way,- * and this tablet's provisioning already does it. When it goes missing — a- * factory reset, a new tablet — the honest answer is "plug the cable in",- * not a Settings screen that does not contain the switch.- *- * [Level.WARN] rather than FAIL because everything except typing into other- * apps still works without it.+ * `signature|privileged|development`: `pm grant` over adb is the only way.+ * When it goes missing — a factory reset, a new tablet — the honest answer+ * is "plug the cable in", not a Settings screen that does not contain the+ * switch.+ *+ * [Level.NOTE] / [Level.WARN] on the same rule as [adbKeyboard], and for the+ * same reason: this grant only ever existed to let the device swap its own+ * default IME for one tool call, and with [typingChannel] open there is no+ * IME to swap.*/- private fun secureSettings(context: Context): Finding {+ private fun secureSettings(context: Context, a11yIme: Boolean): Finding {val on = context.checkSelfPermission(WRITE_SECURE_SETTINGS) ==PackageManager.PERMISSION_GRANTEDreturn Finding(id = "secure_settings",title = R.string.check_title_secure_settings,- level = if (on) Level.OK else Level.WARN,- detail = if (on) R.string.check_detail_secure_on- else R.string.check_detail_secure_missing,+ level = when {+ on -> Level.OK+ a11yIme -> Level.NOTE+ else -> Level.WARN+ },+ detail = when {+ on -> R.string.check_detail_secure_on+ a11yIme -> R.string.check_detail_secure_spare+ else -> R.string.check_detail_secure_missing+ },fix = if (on) {Fix.Nothing} else {diff --git a/app/src/main/java/net/kuafuai/andee/screen/A11yIme.kt b/app/src/main/java/net/kuafuai/andee/screen/A11yIme.ktnew file mode 100644index 0000000..3d1e48f--- /dev/null+++ b/app/src/main/java/net/kuafuai/andee/screen/A11yIme.kt@@ -0,0 +1,174 @@+package net.kuafuai.andee.screen++import android.accessibilityservice.AccessibilityService+import android.accessibilityservice.InputMethod+import android.os.Build+import android.os.SystemClock+import android.util.Log+import android.view.inputmethod.EditorInfo++/**+ * Typing through **our own accessibility service's** input connection, which is+ * the one channel on this device that costs the user nothing to set up.+ *+ * API 33 gave an `AccessibilityService` that declares `flagInputMethodEditor` a+ * real [InputMethod]: `commitText` on the focused editor's own+ * `InputConnection` — the same path a keyboard takes, and the same reason an app+ * cannot refuse it (refusing would refuse all keyboards). What it is *not* is an+ * IME: it does not have to be installed, enabled, or selected, the person keeps+ * their own keyboard active the whole time, and nothing has to be switched.+ *+ * That is the whole point of it being here. The route it replaces —+ * `ScreenController.typeViaAdbKeyboard` — needs a third-party APK sideloaded, a+ * `Settings.Secure.DEFAULT_INPUT_METHOD` write that only+ * `adb shell pm grant … WRITE_SECURE_SETTINGS` can authorise, and ~0.75 s of+ * lend-and-give-back per call. A user who does not own a computer could not type+ * at all. It stays as the fallback for API 30–32 and for any editor this channel+ * turns out not to reach, because a silent regression in typing is the one this+ * project has paid for twice.+ *+ * Two smaller wins fall out of it, neither of them the reason but both worth+ * keeping: a vault password typed this way never leaves the process (the ADB+ * route hands it to another app over a broadcast), and readback comes from the+ * editor itself via [InputMethod.AccessibilityInputConnection.getSurroundingText]+ * rather than from the accessibility tree — so the WebViews that hide their+ * focused node's text stop being a false negative.+ *+ * Every entry point returns null / false rather than throwing when the channel+ * is not there, because "not there" is an ordinary answer: nothing is focused+ * yet, the editor is self-drawn, the platform is too old.+ */+object A11yIme {++ const val VIA = "a11y_ime"++ /**+ * Replace the focused editor's contents with [text] and read back what the+ * editor now holds.+ *+ * Null means the channel is gone (nothing focused, or pre-33) and **nothing+ * was typed** — the caller may fall back. A non-null return that differs+ * from [text] means the text was committed and the editor disagrees, which+ * is a real failure and not a missing channel.+ */+ fun replace(service: AccessibilityService, text: String): String? {+ val conn = awaitConnection(service) ?: return null+ clear(conn)+ runCatching { conn.commitText(text, 1, null) }.onFailure {+ Log.w(TAG, "commitText failed", it)+ return null+ }+ // commitText is a one-way call into the editor's process, so the value+ // is not readable the instant it returns — the same reason the ADB route+ // polls. Short budget: unlike the accessibility tree, this reads the+ // editor's own buffer, so there is no layout pass to wait for.+ var got = ""+ val deadline = SystemClock.uptimeMillis() + READBACK_BUDGET_MS+ do {+ Thread.sleep(READBACK_POLL_MS)+ got = read(conn) ?: return null+ if (got == text) break+ } while (SystemClock.uptimeMillis() < deadline)+ return got+ }++ /**+ * Fire the focused editor's own IME action.+ *+ * Asked for by name (`actionId`, then `imeOptions`) rather than assuming+ * Enter: the field says whether it is a Send, a Search or a Next, and that+ * is what its listener is registered for. Returns false when there is no+ * connection or the editor declares no action, so the caller can fall back+ * to `ACTION_IME_ENTER`.+ */+ fun editorAction(service: AccessibilityService): Boolean {+ val conn = connection(service) ?: return false+ val info: EditorInfo = runCatching { service.inputMethod?.currentInputEditorInfo }+ .getOrNull() ?: return false+ val action = if (info.actionId != 0) {+ info.actionId+ } else {+ info.imeOptions and EditorInfo.IME_MASK_ACTION+ }+ if (action == 0 || action == EditorInfo.IME_ACTION_NONE) return false+ return runCatching { conn.performEditorAction(action); true }.getOrDefault(false)+ }++ // ---- internals ----++ /**+ * [connection], but give a session that is still attaching a moment to+ * arrive.+ *+ * The caller only gets here with a focused editable node already in hand,+ * so "no connection" at this instant usually means the editor took focus+ * a few frames ago and the input session has not reached us yet — measured+ * right after the service rebinds. Returning null there is not a harmless+ * miss: it sends the caller to the ADBKeyboard route, which on a device+ * with no `WRITE_SECURE_SETTINGS` is the hard error this channel exists to+ * retire.+ */+ private fun awaitConnection(service: AccessibilityService): InputMethod.AccessibilityInputConnection? {+ val deadline = SystemClock.uptimeMillis() + SESSION_WAIT_MS+ while (true) {+ connection(service)?.let { return it }+ if (SystemClock.uptimeMillis() >= deadline) return null+ Thread.sleep(SESSION_POLL_MS)+ }+ }++ private fun connection(service: AccessibilityService): InputMethod.AccessibilityInputConnection? {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return null+ val im = runCatching { service.inputMethod }.getOrNull() ?: return null+ // Started, not merely present: the object outlives the editor, and+ // committing into a finished session types into nothing at all.+ if (!runCatching { im.currentInputStarted }.getOrDefault(false)) return null+ return runCatching { im.currentInputConnection }.getOrNull()+ }++ /**+ * Empty the field, because [replace] is a replace and `commitText` inserts.+ *+ * Deletes around the cursor when the editor reports one, which is what a+ * real IME does and what every `BaseInputConnection` supports. When it does+ * not (selection `-1`, some self-drawn editors), the retrieved range is+ * selected instead and `commitText` overwrites the selection — a second+ * route rather than a guess, because appending to a field the brain believes+ * it cleared is the failure that reads as the model typing twice.+ */+ private fun clear(conn: InputMethod.AccessibilityInputConnection) {+ val st = runCatching { conn.getSurroundingText(MAX_READ, MAX_READ, 0) }.getOrNull() ?: return+ val txt = st.text?.toString().orEmpty()+ if (txt.isEmpty()) return+ val start = st.selectionStart+ val end = st.selectionEnd+ runCatching {+ if (start >= 0 && end >= 0) {+ conn.deleteSurroundingText(start, txt.length - end)+ } else {+ conn.setSelection(st.offset, st.offset + txt.length)+ }+ }+ }++ /** The editor's current contents, or null when the connection died. */+ private fun read(conn: InputMethod.AccessibilityInputConnection): String? {+ val st = runCatching { conn.getSurroundingText(MAX_READ, MAX_READ, 0) }.getOrNull()+ ?: return null+ return st.text?.toString().orEmpty()+ }++ private const val TAG = "A11yIme"++ /**+ * Both halves of the surrounding-text window. Generous because this is how+ * the field is cleared, and text left beyond the window would survive a+ * clear; a code editor's buffer can defeat it, and that is the case the+ * readback then reports honestly.+ */+ private const val MAX_READ = 8192+ private const val READBACK_BUDGET_MS = 600L+ private const val READBACK_POLL_MS = 60L+ private const val SESSION_WAIT_MS = 400L+ private const val SESSION_POLL_MS = 50L+}diff --git a/app/src/main/java/net/kuafuai/andee/screen/ScreenController.kt b/app/src/main/java/net/kuafuai/andee/screen/ScreenController.ktindex 62737ca..63774dc 100644--- a/app/src/main/java/net/kuafuai/andee/screen/ScreenController.kt+++ b/app/src/main/java/net/kuafuai/andee/screen/ScreenController.kt@@ -592,7 +592,17 @@ class ScreenController(* [typeViaAdbKeyboard].*/fun typeText(text: String, id: String?, secret: Boolean): JSONObject {- // ── 输入通道(2026-09-20 定版):ADBKeyboard IME 广播单通道 ──+ // ── 输入通道:无障碍输入法优先,ADBKeyboard 兜底(2026-09-30) ──+ //+ // 首选 [A11yIme]:API 33 起,声明了 flagInputMethodEditor 的无障碍+ // 服务自己就能 commitText 到焦点编辑器 —— 和键盘同一条 InputConnection,+ // 但**不是**一个输入法:不用装、不用启用、不用切,用户自己的键盘+ // 全程还在。它排第一位是因为下面那条路要求用户有一台电脑(见+ // [typeViaA11yIme])。兜底仍是 ADBKeyboard,给 API 30–32 和+ // 这条路够不着的编辑器 —— 打字静默失效这件事这个项目已经付过+ // 两次代价了。+ //+ // 以下是 ADBKeyboard 那条路的原始记录(2026-09-20 定版),照旧有效://// 实测依据(当天 bolt.new 两设备全量对比):// - SET_TEXT/剪贴板PASTE:Radix 表单串字段,CodeMirror 全灭,@@ -616,7 +626,47 @@ class ScreenController(throw IllegalStateException("no input focus (tap into an input field first)")}runCatching { if (focused !== root) focused.recycle() }- return typeViaAdbKeyboard(text, secret)+ return typeViaA11yIme(text, secret) ?: typeViaAdbKeyboard(text, secret)+ }++ /**+ * 首选通道:本服务自己的无障碍输入法([A11yIme])。+ *+ * 返回 null = **这条路不通、且一个字都没打**(API < 33、当前没有+ * 焦点编辑器、自绘编辑器拿不到 InputConnection),交给+ * [typeViaAdbKeyboard] 兜底。返回非 null = 已经 commit 过,成功与+ * 否都在这里定论,绝不能再兜底一次 —— 那是真正会输两遍的情况。+ *+ * 它排在 ADBKeyboard 前面的理由不是快(虽然省掉了借还输入法的+ * ~0.75s),是**用户不需要电脑**:ADB 通道要侧载一个第三方 APK,+ * 还要 `adb shell pm grant … WRITE_SECURE_SETTINGS` 才能改默认输入+ * 法。没有电脑的用户在那条路上一个字也打不了。+ */+ private fun typeViaA11yIme(text: String, secret: Boolean): JSONObject? {+ val got = A11yIme.replace(service, text) ?: return null+ val ok = got == text+ return JSONObject()+ .put("performed", ok)+ .put("via", A11yIme.VIA)+ .put("len", text.length)+ .apply {+ if (ok) {+ if (!secret) put("text", got)+ } else if (secret) {+ put(+ "error",+ "committed through the accessibility IME but the field holds " ++ "${got.length} chars instead of ${text.length} — " ++ "verify with a screenshot",+ )+ } else {+ put(+ "error",+ "committed through the accessibility IME but the field reads " ++ "'${got.take(30)}' — verify with a screenshot",+ )+ }+ }}/**@@ -802,9 +852,21 @@ class ScreenController(val performed = target.performAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_IME_ENTER.id)+ // A WebView's editor often reports the action but does not act on+ // it. [A11yIme.editorAction] asks the editor's own EditorInfo what+ // it is (Send / Search / Next) and fires that, which is what its+ // listener is registered for. Second rather than first because+ // ACTION_IME_ENTER needs no focused *input connection* and covers+ // the `id`-addressed case this method also serves.+ val viaIme = if (!performed && id == null) {+ A11yIme.editorAction(service)+ } else {+ false+ }return JSONObject()- .put("performed", performed)+ .put("performed", performed || viaIme).put("target_id", target.viewIdResourceName)+ .apply { if (viaIme) put("via", A11yIme.VIA) }} finally {if (target !== root) target.recycle()}diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xmlindex 877dc5c..e6d7018 100644--- a/app/src/main/res/values-en/strings.xml+++ b/app/src/main/res/values-en/strings.xml@@ -314,6 +314,7 @@<string name="check_title_model">Model</string><string name="check_title_microphone">Microphone</string><string name="check_title_notifications">Notification access</string>+ <string name="check_title_typing_channel">Typing channel</string><string name="check_title_adb_keyboard">ADB keyboard</string><string name="check_title_secure_settings">Typing without root</string><string name="check_title_voice_key">Voice API key</string>@@ -344,11 +345,15 @@<string name="check_detail_notif_on">Granted, and the listener is bound.</string><string name="check_detail_notif_zombie">Settings says “granted”, but the listener is not bound — notifications are being dropped silently.\nThis one only comes back if you turn it off and then on again; leaving it on does nothing.</string><string name="check_detail_notif_off">Off. Call announcements and forwarded notifications never arrive.</string>+ <string name="check_detail_typing_a11y">Through the accessibility input method: I can type into a field myself — no keyboard to install, no default input method to change, no computer to plug in. Your own keyboard stays active throughout.</string>+ <string name="check_detail_typing_adb">This Android version (below 13) has no accessibility input method, so typing goes through the ADB keyboard below — both of those rows have to be in place.</string><string name="check_detail_adb_kb_on">Enabled. It can type into other apps for you.</string>+ <string name="check_detail_adb_kb_spare">Not needed now — typing goes through the accessibility input method. It stays as the spare: a field that channel cannot reach falls back to this one, and it has to be installed by then.</string><string name="check_detail_adb_kb_off">Installed but not enabled. Turn it on in system settings — it has no keyboard UI of its own, so enabling it does not get in the way of typing yourself.</string><string name="check_detail_adb_kb_missing">Not installed. Typing and filling in forms for you go through it (ADBKeyboard), so that does not work yet. Download it with the button here — when you come back this row is a one-tap enable.</string><string name="check_detail_secure_on">Granted. Typing into other apps works.</string><string name="check_detail_secure_missing">Not granted, so typing into other apps (type_text) is unavailable. There is no switch for this in Settings — it can only be given over adb with the cable plugged in.</string>+ <string name="check_detail_secure_spare">Not granted, and typing works anyway — it goes through the accessibility input method. This is only needed by the spare channel above.</string><string name="check_detail_mic_on">Allowed.</string><string name="check_detail_mic_off">Not allowed. Tapping the ball does nothing at all — the failure that least resembles a permission problem.</string><string name="check_detail_voice_key_ok">Set. Recognition and synthesis use it to reach Volcengine.</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex 34e9add..32aa1c3 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -369,6 +369,7 @@<string name="check_title_model">模型连通</string><string name="check_title_microphone">麦克风</string><string name="check_title_notifications">通知使用权</string>+ <string name="check_title_typing_channel">打字通道</string><string name="check_title_adb_keyboard">ADB 键盘</string><string name="check_title_secure_settings">免 root 输入</string><string name="check_title_voice_key">语音密钥</string>@@ -399,11 +400,15 @@<string name="check_detail_notif_on">已授权,监听器也连着。</string><string name="check_detail_notif_zombie">系统显示「已授权」,但监听器没连上,通知会被静默丢掉。\n这条得关掉再打开一次才行 —— 只打开不生效。</string><string name="check_detail_notif_off">没开。来电播报和通知转发都收不到。</string>+ <string name="check_detail_typing_a11y">走无障碍输入法:我自己就能往输入框里打字,不用装键盘、不用改默认输入法、也不用插电脑。你自己的键盘全程照用。</string>+ <string name="check_detail_typing_adb">这个系统版本(低于 Android 13)没有无障碍输入法,打字得走下面的 ADB 键盘,所以那两条要备齐。</string><string name="check_detail_adb_kb_on">已启用,能替你往别的应用里打字。</string>+ <string name="check_detail_adb_kb_spare">现在打字走无障碍输入法,用不着它了。留着当备用:碰上它打不进去的输入框,会自动换回这条路 —— 那时候得先装上。</string><string name="check_detail_adb_kb_off">装了但没启用。去系统设置里把它打开就行 —— 它自己没有键盘界面,启用它不影响你平时打字。</string><string name="check_detail_adb_kb_missing">没装。替你打字、填表都靠它(ADBKeyboard),所以这类事现在做不了。点右边下载装上,回来这条就只剩「启用」一步。</string><string name="check_detail_secure_on">已授权,能往别的应用里打字。</string><string name="check_detail_secure_missing">没授权,往别的应用里打字(type_text)用不了。这条在设置里没有开关 —— 只能插上线用 adb 给。</string>+ <string name="check_detail_secure_spare">没授权,不过不影响打字 —— 现在走的是无障碍输入法。它只给上面那条备用通道用,没有也行。</string><string name="check_detail_mic_on">已允许。</string><string name="check_detail_mic_off">没允许。点球不会有任何反应 —— 这是最不像权限问题的一条。</string><string name="check_detail_voice_key_ok">已填。识别和合成用它连火山。</string>diff --git a/app/src/main/res/xml/accessibility_service_config.xml b/app/src/main/res/xml/accessibility_service_config.xmlindex eb03d60..0ef41e0 100644--- a/app/src/main/res/xml/accessibility_service_config.xml+++ b/app/src/main/res/xml/accessibility_service_config.xml@@ -2,7 +2,7 @@<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged|typeWindowsChanged|typeViewFocused|typeViewScrolled"android:accessibilityFeedbackType="feedbackGeneric"- android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews|flagReportViewIds|flagRetrieveInteractiveWindows|flagRequestTouchExplorationMode"+ android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews|flagReportViewIds|flagRetrieveInteractiveWindows|flagRequestTouchExplorationMode|flagInputMethodEditor"android:canPerformGestures="true"android:canRequestEnhancedWebAccessibility="true"android:canRequestTouchExplorationMode="true"diff --git a/docs/permissions.md b/docs/permissions.mdindex e029ebe..4ec40e7 100644--- a/docs/permissions.md+++ b/docs/permissions.md@@ -54,15 +54,17 @@ The "What you get" column is the copy the app actually shows (`dev_perm_feature_| Notifications<br>`POST_NOTIFICATIONS` | Remind you on time | Due-task reminders | No reminder at the due time, **but the task is not lost** || Boot<br>`RECEIVE_BOOT_COMPLETED` | Tasks survive a reboot | Rebuilding the `AlarmManager` alarms | After a reboot, due tasks stop reminding || Battery optimisation exemption<br>`REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` | Reminders arrive on time | Task scheduling | With the screen off, reminders arrive late |-| Write secure settings<br>`WRITE_SECURE_SETTINGS` | Switch the input method (needed to type) | Prerequisite step for `type_text` | **`type_text` fails outright**, and tells you exactly why. It will not pretend to succeed |+| Write secure settings<br>`WRITE_SECURE_SETTINGS` | Switch the input method, for the spare typing route | Prerequisite for the ADBKeyboard fallback | Nothing on Android 13 and above — typing goes through the accessibility input method. On Android 12 and below, **`type_text` fails outright**, and tells you exactly why. It will not pretend to succeed |+> On **Android 13 and above you do not need this.** The app types through its own accessibility service's input connection: nothing to install, no keyboard to switch to, and your own keyboard stays active throughout.+>> `WRITE_SECURE_SETTINGS` is the odd one out: it **does not take effect on install — you must grant it manually over adb**:>> ```bash> adb shell pm grant net.kuafuai.andee android.permission.WRITE_SECURE_SETTINGS> ```>-> That is a platform restriction, not a choice this app made. You can use it without granting this; you just cannot type.+> That is a platform restriction, not a choice this app made. On Android 12 and below it is what typing needs; above that it only matters for the fallback route, which is used when an editor refuses the normal channel.---diff --git a/docs/permissions.zh-CN.md b/docs/permissions.zh-CN.mdindex 1ef704c..f694fd0 100644--- a/docs/permissions.zh-CN.md+++ b/docs/permissions.zh-CN.md@@ -54,15 +54,17 @@| 通知栏<br>`POST_NOTIFICATIONS` | 到点提醒你 | 待办到期的提醒 | 到点不提醒,**但待办不会丢** || 开机自启<br>`RECEIVE_BOOT_COMPLETED` | 重启后待办还在 | 重建 `AlarmManager` 闹钟 | 重启后待办不再提醒 || 电池优化豁免<br>`REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` | 到点提醒准时 | 待办定时 | 息屏状态下提醒会迟到 |-| 改系统设置<br>`WRITE_SECURE_SETTINGS` | 换输入法(打字用) | `type_text` 前置步骤 | **`type_text` 直接失败**,并明确告诉你失败原因,不会假装成功 |+| 改系统设置<br>`WRITE_SECURE_SETTINGS` | 换输入法(备用打字通道用) | ADBKeyboard 兜底通道的前置步骤 | Android 13 及以上没影响 —— 打字走的是无障碍输入法。Android 12 及以下则 **`type_text` 直接失败**,并明确告诉你失败原因,不会假装成功 |+> **Android 13 及以上不需要这条。** 应用用自己无障碍服务的输入连接打字:不用装键盘、不用切默认输入法,你自己的键盘全程照用。+>> `WRITE_SECURE_SETTINGS` 是特殊的一个:它**装完不生效,必须用 adb 手动授予**:>> ```bash> adb shell pm grant net.kuafuai.andee android.permission.WRITE_SECURE_SETTINGS> ```>-> 这是系统限制,不是应用的选择。不授予也能用,只是打不了字。+> 这是系统限制,不是应用的选择。Android 12 及以下打字靠它;以上版本它只给兜底通道用 —— 碰上正常通道进不去的输入框才会走到。---