2626import java .util .concurrent .TimeoutException ;
2727
2828public final class SnapshotInstrumentation extends Instrumentation {
29- private static final String PROTOCOL = "android-snapshot-helper-v1" ;
30- private static final String OUTPUT_FORMAT = "uiautomator-xml" ;
31- private static final String HELPER_API_VERSION = "2" ;
3229 private static final long GESTURE_UI_AUTOMATION_CONNECT_TIMEOUT_MS = 8_000 ;
3330 private static final int CHUNK_SIZE = 2 * 1024 ;
3431 // Match the host default: bounded wait for microinteraction reliability without the stock
@@ -111,9 +108,9 @@ private static void putBaseMetadata(
111108 long timeoutMs ,
112109 int maxDepth ,
113110 int maxNodes ) {
114- result .putString ("agentDeviceProtocol" , PROTOCOL );
115- result .putString ("helperApiVersion" , HELPER_API_VERSION );
116- result .putString ("outputFormat" , OUTPUT_FORMAT );
111+ result .putString ("agentDeviceProtocol" , HelperProtocol . PROTOCOL );
112+ result .putString ("helperApiVersion" , HelperProtocol . HELPER_API_VERSION );
113+ result .putString ("outputFormat" , HelperProtocol . OUTPUT_FORMAT );
117114 result .putString ("waitForIdleTimeoutMs" , Long .toString (waitForIdleTimeoutMs ));
118115 result .putString ("waitForIdleQuietMs" , Long .toString (waitForIdleQuietMs ));
119116 result .putString ("timeoutMs" , Long .toString (timeoutMs ));
@@ -131,22 +128,12 @@ private static void putCaptureMetadata(Bundle result, CaptureResult capture, lon
131128 }
132129
133130 private void runOneShotViewport (Bundle result ) {
134- android .graphics .Rect viewport =
135- GestureViewportReader .read (getConnectedUiAutomationUnchecked ());
136- result .putString ("ok" , "true" );
137- result .putString ("kind" , "viewport" );
138- putViewportMetadata (result , viewport );
131+ TouchCommandHandler .populateViewport (result , getConnectedUiAutomationUnchecked ());
139132 finishSafely (0 , result );
140133 }
141134
142135 private void runOneShotGesture (Bundle result , String payloadBase64 ) throws Exception {
143- long startedAtMs = System .currentTimeMillis ();
144- TouchPlan plan = TouchPlan .parseBase64 (payloadBase64 );
145- int injectedEvents = TouchPlanInjector .inject (getConnectedUiAutomationUnchecked (), plan );
146- result .putString ("ok" , "true" );
147- result .putString ("kind" , plan .kind );
148- result .putString ("injectedEvents" , Integer .toString (injectedEvents ));
149- result .putString ("elapsedMs" , Long .toString (System .currentTimeMillis () - startedAtMs ));
136+ TouchCommandHandler .populateGesture (result , getConnectedUiAutomationUnchecked (), payloadBase64 );
150137 finishSafely (0 , result );
151138 }
152139
@@ -158,13 +145,6 @@ private UiAutomation getConnectedUiAutomationUnchecked() {
158145 }
159146 }
160147
161- private static void putViewportMetadata (Bundle result , android .graphics .Rect viewport ) {
162- result .putString ("x" , Integer .toString (viewport .left ));
163- result .putString ("y" , Integer .toString (viewport .top ));
164- result .putString ("width" , Integer .toString (viewport .width ()));
165- result .putString ("height" , Integer .toString (viewport .height ()));
166- }
167-
168148 private void runSnapshotSession (
169149 int sessionPort ,
170150 long waitForIdleQuietMs ,
@@ -189,27 +169,32 @@ private void runSnapshotSession(
189169 new InputStreamReader (socket .getInputStream (), StandardCharsets .UTF_8 ))
190170 .readLine ();
191171 if (command == null ) {
192- writeSessionError (socket .getOutputStream (), "" , "java.io.EOFException" , "empty command" );
172+ SessionResponseWriter .writeSessionError (
173+ socket .getOutputStream (), "" , "java.io.EOFException" , "empty command" );
193174 continue ;
194175 }
195176 String [] parts = command .trim ().split ("\\ s+" , 3 );
196177 String action = parts .length > 0 ? parts [0 ] : "" ;
197178 String requestId = parts .length > 1 ? parts [1 ] : "" ;
198179 if ("quit" .equals (action )) {
199- writeSessionOk (socket .getOutputStream (), requestId );
180+ SessionResponseWriter . writeSessionOk (socket .getOutputStream (), requestId );
200181 return ;
201182 }
202183 if ("viewport" .equals (action )) {
203- writeSessionViewport (socket .getOutputStream (), requestId );
184+ TouchCommandHandler .writeSessionViewport (
185+ socket .getOutputStream (), requestId , getConnectedUiAutomationUnchecked ());
204186 continue ;
205187 }
206188 if ("gesture" .equals (action )) {
207- writeSessionGesture (
208- socket .getOutputStream (), requestId , parts .length > 2 ? parts [2 ] : "" );
189+ TouchCommandHandler .writeSessionGesture (
190+ socket .getOutputStream (),
191+ requestId ,
192+ parts .length > 2 ? parts [2 ] : "" ,
193+ getConnectedUiAutomationUnchecked ());
209194 continue ;
210195 }
211196 if (!"snapshot" .equals (action )) {
212- writeSessionError (
197+ SessionResponseWriter . writeSessionError (
213198 socket .getOutputStream (),
214199 requestId ,
215200 "java.lang.IllegalArgumentException" ,
@@ -248,106 +233,16 @@ private void writeSessionSnapshot(
248233 result .putString ("ok" , "true" );
249234 putCaptureMetadata (result , capture , System .currentTimeMillis () - startedAtMs );
250235 result .putString ("byteLength" , Integer .toString (capture .xml .getBytes (StandardCharsets .UTF_8 ).length ));
251- writeSessionResponse (output , result , capture .xml );
236+ SessionResponseWriter . writeSessionResponse (output , result , capture .xml );
252237 } catch (Throwable error ) {
253- writeSessionError (
238+ SessionResponseWriter . writeSessionError (
254239 output ,
255240 requestId ,
256241 error .getClass ().getName (),
257242 error .getMessage () == null ? error .getClass ().getName () : error .getMessage ());
258243 }
259244 }
260245
261- private void writeSessionViewport (OutputStream output , String requestId ) throws IOException {
262- Bundle result = sessionResponseBundle (requestId );
263- try {
264- android .graphics .Rect viewport =
265- GestureViewportReader .read (getConnectedUiAutomationUnchecked ());
266- result .putString ("ok" , "true" );
267- result .putString ("kind" , "viewport" );
268- putViewportMetadata (result , viewport );
269- writeSessionResponse (output , result , "" );
270- } catch (Throwable error ) {
271- writeSessionError (
272- output ,
273- requestId ,
274- error .getClass ().getName (),
275- error .getMessage () == null ? error .getClass ().getName () : error .getMessage ());
276- }
277- }
278-
279- private void writeSessionGesture (OutputStream output , String requestId , String payloadBase64 )
280- throws IOException {
281- Bundle result = sessionResponseBundle (requestId );
282- try {
283- long startedAtMs = System .currentTimeMillis ();
284- TouchPlan plan = TouchPlan .parseBase64 (payloadBase64 );
285- int injectedEvents = TouchPlanInjector .inject (getConnectedUiAutomationUnchecked (), plan );
286- result .putString ("ok" , "true" );
287- result .putString ("kind" , plan .kind );
288- result .putString ("injectedEvents" , Integer .toString (injectedEvents ));
289- result .putString ("elapsedMs" , Long .toString (System .currentTimeMillis () - startedAtMs ));
290- writeSessionResponse (output , result , "" );
291- } catch (Throwable error ) {
292- writeSessionError (
293- output ,
294- requestId ,
295- error .getClass ().getName (),
296- error .getMessage () == null ? error .getClass ().getName () : error .getMessage ());
297- }
298- }
299-
300- private static Bundle sessionResponseBundle (String requestId ) {
301- Bundle result = new Bundle ();
302- result .putString ("agentDeviceProtocol" , PROTOCOL );
303- result .putString ("helperApiVersion" , HELPER_API_VERSION );
304- result .putString ("outputFormat" , OUTPUT_FORMAT );
305- result .putString ("requestId" , requestId );
306- return result ;
307- }
308-
309- private static void writeSessionOk (OutputStream output , String requestId ) throws IOException {
310- Bundle result = new Bundle ();
311- result .putString ("agentDeviceProtocol" , PROTOCOL );
312- result .putString ("helperApiVersion" , HELPER_API_VERSION );
313- result .putString ("outputFormat" , OUTPUT_FORMAT );
314- result .putString ("requestId" , requestId );
315- result .putString ("ok" , "true" );
316- writeSessionResponse (output , result , "" );
317- }
318-
319- private static void writeSessionError (
320- OutputStream output , String requestId , String errorType , String message ) throws IOException {
321- Bundle result = new Bundle ();
322- result .putString ("agentDeviceProtocol" , PROTOCOL );
323- result .putString ("helperApiVersion" , HELPER_API_VERSION );
324- result .putString ("outputFormat" , OUTPUT_FORMAT );
325- result .putString ("requestId" , requestId );
326- result .putString ("ok" , "false" );
327- result .putString ("errorType" , errorType );
328- result .putString ("message" , message );
329- writeSessionResponse (output , result , "" );
330- }
331-
332- private static void writeSessionResponse (OutputStream output , Bundle result , String body )
333- throws IOException {
334- StringBuilder headers = new StringBuilder ();
335- for (String key : result .keySet ()) {
336- Object value = result .get (key );
337- if (value != null ) {
338- headers .append (key ).append ('=' ).append (sanitizeHeaderValue (value .toString ())).append ('\n' );
339- }
340- }
341- headers .append ('\n' );
342- output .write (headers .toString ().getBytes (StandardCharsets .UTF_8 ));
343- output .write (body .getBytes (StandardCharsets .UTF_8 ));
344- output .flush ();
345- }
346-
347- private static String sanitizeHeaderValue (String value ) {
348- return value .replace ('\r' , ' ' ).replace ('\n' , ' ' );
349- }
350-
351246 private static String readStringArgument (Bundle arguments , String key ) {
352247 if (arguments == null || !arguments .containsKey (key )) {
353248 return null ;
@@ -610,9 +505,9 @@ private void emitChunks(String payload) {
610505 int start = index * CHUNK_SIZE ;
611506 int end = Math .min (bytes .length , start + CHUNK_SIZE );
612507 Bundle status = new Bundle ();
613- status .putString ("agentDeviceProtocol" , PROTOCOL );
614- status .putString ("helperApiVersion" , HELPER_API_VERSION );
615- status .putString ("outputFormat" , OUTPUT_FORMAT );
508+ status .putString ("agentDeviceProtocol" , HelperProtocol . PROTOCOL );
509+ status .putString ("helperApiVersion" , HelperProtocol . HELPER_API_VERSION );
510+ status .putString ("outputFormat" , HelperProtocol . OUTPUT_FORMAT );
616511 status .putString ("chunkIndex" , Integer .toString (index ));
617512 status .putString ("chunkCount" , Integer .toString (chunkCount ));
618513 status .putString (
0 commit comments