@@ -26,13 +26,19 @@ import {
2626} from './fill-verification.ts' ;
2727import {
2828 clearAndroidImeHelperText ,
29+ isAndroidImeHelperPackage ,
2930 selectAndroidImeHelperArtifact ,
3031 sendAndroidImeHelperText ,
3132} from './ime-helper.ts' ;
3233import { isAndroidTestImeActive } from './ime-lifecycle.ts' ;
3334import { focusAndroid } from './input-actions.ts' ;
3435import type { AndroidHelperSessionOptions } from './snapshot-helper-types.ts' ;
3536
37+ /**
38+ * `input text` truncates long strings in some app/IME states (#531), so ASCII is written in short
39+ * chunks rather than one command. Do not raise this to buy back spawns — the IME helper's batch
40+ * broadcast is the lever for that, and it does not truncate.
41+ */
3642const ANDROID_INPUT_TEXT_CHUNK_SIZE = 8 ;
3743
3844export async function typeAndroid ( device : DeviceInfo , text : string , delayMs = 0 ) : Promise < void > {
@@ -43,11 +49,25 @@ export async function typeAndroid(device: DeviceInfo, text: string, delayMs = 0)
4349 return ;
4450 }
4551 if ( isAndroidTestImeActive ( device ) ) {
46- await typeAndroidTestIme ( device , text , delayMs ) ;
52+ await typeAndroidImeHelper (
53+ device ,
54+ await activatedAndroidImeHelperPackage ( device ) ,
55+ text ,
56+ delayMs ,
57+ ) ;
4758 return ;
4859 }
4960 assertAndroidShellTextSupported ( text ) ;
50- await assertAndroidShellInputIsAppOwned ( device , 'type' ) ;
61+ // The shell path needs the input-method read anyway, and it also names the device's active IME.
62+ // If that is the helper, its batch channel writes the whole string in one broadcast instead of
63+ // ceil(n/8) `input text` spawns — no flag, no IME switch, nothing this process had to arrange.
64+ const inputState = await readAndroidShellTextInputState ( device , 'type' ) ;
65+ const helperPackage = androidImeHelperInputMethod ( inputState ) ;
66+ if ( helperPackage ) {
67+ await typeAndroidImeHelper ( device , helperPackage , text , delayMs ) ;
68+ return ;
69+ }
70+ assertAndroidShellInputIsAppOwned ( inputState , 'type' ) ;
5171 if ( delayMs > 0 && Array . from ( text ) . length > 1 ) {
5272 await typeAndroidShell ( device , { action : 'type' , text, chunkSize : 1 , delayMs } ) ;
5373 return ;
@@ -77,7 +97,15 @@ export async function fillAndroid(
7797 return completeAndroidFillVerification ( text , beforeTarget , verification ) ;
7898 }
7999 if ( isAndroidTestImeActive ( device ) ) {
80- const verification = await fillAndroidTestIme ( device , x , y , text , beforeTarget , helper ) ;
100+ const verification = await fillAndroidImeHelper (
101+ device ,
102+ await activatedAndroidImeHelperPackage ( device ) ,
103+ x ,
104+ y ,
105+ text ,
106+ beforeTarget ,
107+ helper ,
108+ ) ;
81109 return completeAndroidFillVerification ( text , beforeTarget , verification ) ;
82110 }
83111 assertAndroidShellTextSupported ( text ) ;
@@ -110,7 +138,23 @@ export async function fillAndroid(
110138
111139 for ( const attempt of attempts ) {
112140 await focusAndroid ( device , x , y ) ;
113- await assertAndroidShellInputIsAppOwned ( device , 'fill' ) ;
141+ // Same read, same reason as `typeAndroid`: when the helper is the active IME its channel
142+ // replaces both the delete-key clear and the chunked write for the rest of this fill.
143+ const inputState = await readAndroidShellTextInputState ( device , 'fill' ) ;
144+ const helperPackage = androidImeHelperInputMethod ( inputState ) ;
145+ if ( helperPackage ) {
146+ const verification = await fillAndroidImeHelper (
147+ device ,
148+ helperPackage ,
149+ x ,
150+ y ,
151+ text ,
152+ beforeTarget ,
153+ helper ,
154+ ) ;
155+ return completeAndroidFillVerification ( text , beforeTarget , verification ) ;
156+ }
157+ assertAndroidShellInputIsAppOwned ( inputState , 'fill' ) ;
114158 const clearCount = clampCount (
115159 textCodePointLength + attempt . clearPadding ,
116160 attempt . minClear ,
@@ -136,14 +180,27 @@ export async function fillAndroid(
136180 return completeAndroidFillVerification ( text , beforeTarget , lastVerification ) ;
137181}
138182
139- async function typeAndroidTestIme (
183+ /**
184+ * The helper package this process switched the device to. The observed-IME route reads the package
185+ * off the device instead, so it never depends on a packaged artifact being present.
186+ */
187+ async function activatedAndroidImeHelperPackage ( device : DeviceInfo ) : Promise < string > {
188+ const artifact = await selectAndroidImeHelperArtifact ( resolveAndroidAdbProvider ( device ) ) ;
189+ return artifact . manifest . packageName ;
190+ }
191+
192+ function androidImeHelperInputMethod ( state : AndroidKeyboardState | null ) : string | undefined {
193+ const packageName = state ?. inputMethodPackage ;
194+ return isAndroidImeHelperPackage ( packageName ) ? packageName : undefined ;
195+ }
196+
197+ async function typeAndroidImeHelper (
140198 device : DeviceInfo ,
199+ packageName : string ,
141200 text : string ,
142201 delayMs : number ,
143202) : Promise < void > {
144203 const adb = resolveAndroidAdbExecutor ( device ) ;
145- const artifact = await selectAndroidImeHelperArtifact ( resolveAndroidAdbProvider ( device ) ) ;
146- const packageName = artifact . manifest . packageName ;
147204 const parts = text . split ( '\n' ) ;
148205 for ( const [ partIndex , part ] of parts . entries ( ) ) {
149206 const chunks = delayMs > 0 ? chunkAndroidInputText ( part , 1 ) : [ part ] ;
@@ -160,17 +217,16 @@ async function typeAndroidTestIme(
160217 emitAndroidTextDiagnostic ( 'type' , 'test-ime' , text ) ;
161218}
162219
163- async function fillAndroidTestIme (
220+ async function fillAndroidImeHelper (
164221 device : DeviceInfo ,
222+ packageName : string ,
165223 x : number ,
166224 y : number ,
167225 text : string ,
168226 beforeTarget : AndroidFillVerification [ 'targetInput' ] ,
169227 helper : AndroidHelperSessionOptions ,
170228) : Promise < AndroidFillVerification > {
171229 const adb = resolveAndroidAdbExecutor ( device ) ;
172- const artifact = await selectAndroidImeHelperArtifact ( resolveAndroidAdbProvider ( device ) ) ;
173- const packageName = artifact . manifest . packageName ;
174230 let lastVerification : AndroidFillVerification | null = null ;
175231 // One retry covers the rare not-yet-bound InputConnection right after focus.
176232 for ( let attempt = 0 ; attempt < 2 ; attempt += 1 ) {
@@ -241,13 +297,17 @@ async function clearFocusedText(device: DeviceInfo, count: number): Promise<void
241297 }
242298}
243299
244- async function assertAndroidShellInputIsAppOwned (
300+ /**
301+ * Reads the device's input-method state for a text-entry decision, or `null` when the probe itself
302+ * fails. A failed probe is not a refusal: the shell path has always continued without this evidence
303+ * rather than blocking text entry on a diagnostic read.
304+ */
305+ async function readAndroidShellTextInputState (
245306 device : DeviceInfo ,
246307 action : AndroidTextInputAction ,
247- ) : Promise < void > {
248- let state : AndroidKeyboardState ;
308+ ) : Promise < AndroidKeyboardState | null > {
249309 try {
250- state = await getAndroidKeyboardState ( device ) ;
310+ return await getAndroidKeyboardState ( device ) ;
251311 } catch ( error ) {
252312 emitDiagnostic ( {
253313 level : 'warn' ,
@@ -257,9 +317,15 @@ async function assertAndroidShellInputIsAppOwned(
257317 error : error instanceof Error ? error . message : String ( error ) ,
258318 } ,
259319 } ) ;
260- return ;
320+ return null ;
261321 }
262- if ( state . inputOwner !== 'ime' ) return ;
322+ }
323+
324+ function assertAndroidShellInputIsAppOwned (
325+ state : AndroidKeyboardState | null ,
326+ action : AndroidTextInputAction ,
327+ ) : void {
328+ if ( ! state || state . inputOwner !== 'ime' ) return ;
263329 throw new AppError (
264330 'COMMAND_FAILED' ,
265331 'KEYBOARD_OVERLAY_BLOCKING: Android text input is blocked because the focused input belongs to the active keyboard/IME.' ,
0 commit comments