@@ -66,15 +66,54 @@ private void run(String[] args) {
6666 }
6767 } else if (command .equals ("tap" )) {
6868 if (args .length == 3 ) {
69- sendTap (Float .parseFloat (args [1 ]), Float .parseFloat (args [2 ]));
69+ sendTap (InputDevice . SOURCE_TOUCHSCREEN , Float .parseFloat (args [1 ]), Float .parseFloat (args [2 ]));
7070 return ;
7171 }
7272 } else if (command .equals ("swipe" )) {
7373 if (args .length == 5 ) {
74- sendSwipe (Float .parseFloat (args [1 ]), Float .parseFloat (args [2 ]),
74+ sendSwipe (InputDevice . SOURCE_TOUCHSCREEN , Float .parseFloat (args [1 ]), Float .parseFloat (args [2 ]),
7575 Float .parseFloat (args [3 ]), Float .parseFloat (args [4 ]));
7676 return ;
7777 }
78+ } else if (command .equals ("touchscreen" ) || command .equals ("touchpad" )) {
79+ // determine input source
80+ int inputSource = InputDevice .SOURCE_TOUCHSCREEN ;
81+ if (command .equals ("touchpad" )) {
82+ inputSource = InputDevice .SOURCE_TOUCHPAD ;
83+ }
84+ // determine subcommand
85+ if (args .length > 1 ) {
86+ String subcommand = args [1 ];
87+ if (subcommand .equals ("tap" )) {
88+ if (args .length == 4 ) {
89+ sendTap (inputSource , Float .parseFloat (args [2 ]),
90+ Float .parseFloat (args [3 ]));
91+ return ;
92+ }
93+ } else if (subcommand .equals ("swipe" )) {
94+ if (args .length == 6 ) {
95+ sendSwipe (inputSource , Float .parseFloat (args [2 ]),
96+ Float .parseFloat (args [3 ]), Float .parseFloat (args [4 ]),
97+ Float .parseFloat (args [5 ]));
98+ return ;
99+ }
100+ }
101+ }
102+ } else if (command .equals ("trackball" )) {
103+ // determine subcommand
104+ if (args .length > 1 ) {
105+ String subcommand = args [1 ];
106+ if (subcommand .equals ("press" )) {
107+ sendTap (InputDevice .SOURCE_TRACKBALL , 0.0f , 0.0f );
108+ return ;
109+ } else if (subcommand .equals ("roll" )) {
110+ if (args .length == 4 ) {
111+ sendMove (InputDevice .SOURCE_TRACKBALL , Float .parseFloat (args [2 ]),
112+ Float .parseFloat (args [3 ]));
113+ return ;
114+ }
115+ }
116+ }
78117 } else {
79118 System .err .println ("Error: Unknown command: " + command );
80119 showUsage ();
@@ -127,33 +166,64 @@ private void sendKeyEvent(int keyCode) {
127166 KeyCharacterMap .VIRTUAL_KEYBOARD , 0 , 0 , InputDevice .SOURCE_KEYBOARD ));
128167 }
129168
130- private void sendTap (float x , float y ) {
169+ private void sendTap (int inputSource , float x , float y ) {
131170 long now = SystemClock .uptimeMillis ();
132- injectPointerEvent ( MotionEvent . obtain ( now , now , MotionEvent .ACTION_DOWN , x , y , 0 ) );
133- injectPointerEvent ( MotionEvent . obtain ( now , now , MotionEvent .ACTION_UP , x , y , 0 ) );
171+ injectMotionEvent ( inputSource , MotionEvent .ACTION_DOWN , now , x , y , 1.0f );
172+ injectMotionEvent ( inputSource , MotionEvent .ACTION_UP , now , x , y , 0.0f );
134173 }
135174
136- private void sendSwipe (float x1 , float y1 , float x2 , float y2 ) {
175+ private void sendSwipe (int inputSource , float x1 , float y1 , float x2 , float y2 ) {
137176 final int NUM_STEPS = 11 ;
138177 long now = SystemClock .uptimeMillis ();
139- injectPointerEvent ( MotionEvent . obtain ( now , now , MotionEvent .ACTION_DOWN , x1 , y1 , 0 ) );
178+ injectMotionEvent ( inputSource , MotionEvent .ACTION_DOWN , now , x1 , y1 , 1.0f );
140179 for (int i = 1 ; i < NUM_STEPS ; i ++) {
141- float alpha = (float )i / NUM_STEPS ;
142- injectPointerEvent ( MotionEvent .obtain ( now , now , MotionEvent . ACTION_MOVE ,
143- lerp (x1 , x2 , alpha ), lerp ( y1 , y2 , alpha ), 0 ) );
180+ float alpha = (float ) i / NUM_STEPS ;
181+ injectMotionEvent ( inputSource , MotionEvent .ACTION_MOVE , now , lerp ( x1 , x2 , alpha ) ,
182+ lerp (y1 , y2 , alpha ), 1.0f );
144183 }
145- injectPointerEvent (MotionEvent .obtain (now , now , MotionEvent .ACTION_UP , x2 , y2 , 0 ));
184+ injectMotionEvent (inputSource , MotionEvent .ACTION_UP , now , x1 , y1 , 0.0f );
185+ }
186+
187+ /**
188+ * Sends a simple zero-pressure move event.
189+ *
190+ * @param inputSource the InputDevice.SOURCE_* sending the input event
191+ * @param dx change in x coordinate due to move
192+ * @param dy change in y coordinate due to move
193+ */
194+ private void sendMove (int inputSource , float dx , float dy ) {
195+ long now = SystemClock .uptimeMillis ();
196+ injectMotionEvent (inputSource , MotionEvent .ACTION_MOVE , now , dx , dy , 0.0f );
146197 }
147198
148199 private void injectKeyEvent (KeyEvent event ) {
149- Log .i (TAG , "InjectKeyEvent : " + event );
200+ Log .i (TAG , "injectKeyEvent : " + event );
150201 InputManager .getInstance ().injectInputEvent (event ,
151202 InputManager .INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH );
152203 }
153204
154- private void injectPointerEvent (MotionEvent event ) {
155- event .setSource (InputDevice .SOURCE_TOUCHSCREEN );
156- Log .i ("Input" , "InjectPointerEvent: " + event );
205+ /**
206+ * Builds a MotionEvent and injects it into the event stream.
207+ *
208+ * @param inputSource the InputDevice.SOURCE_* sending the input event
209+ * @param action the MotionEvent.ACTION_* for the event
210+ * @param when the value of SystemClock.uptimeMillis() at which the event happened
211+ * @param x x coordinate of event
212+ * @param y y coordinate of event
213+ * @param pressure pressure of event
214+ */
215+ private void injectMotionEvent (int inputSource , int action , long when , float x , float y , float pressure ) {
216+ final float DEFAULT_SIZE = 1.0f ;
217+ final int DEFAULT_META_STATE = 0 ;
218+ final float DEFAULT_PRECISION_X = 1.0f ;
219+ final float DEFAULT_PRECISION_Y = 1.0f ;
220+ final int DEFAULT_DEVICE_ID = 0 ;
221+ final int DEFAULT_EDGE_FLAGS = 0 ;
222+ MotionEvent event = MotionEvent .obtain (when , when , action , x , y , pressure , DEFAULT_SIZE ,
223+ DEFAULT_META_STATE , DEFAULT_PRECISION_X , DEFAULT_PRECISION_Y , DEFAULT_DEVICE_ID ,
224+ DEFAULT_EDGE_FLAGS );
225+ event .setSource (inputSource );
226+ Log .i ("Input" , "injectMotionEvent: " + event );
157227 InputManager .getInstance ().injectInputEvent (event ,
158228 InputManager .INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH );
159229 }
@@ -166,7 +236,9 @@ private void showUsage() {
166236 System .err .println ("usage: input ..." );
167237 System .err .println (" input text <string>" );
168238 System .err .println (" input keyevent <key code number or name>" );
169- System .err .println (" input tap <x> <y>" );
170- System .err .println (" input swipe <x1> <y1> <x2> <y2>" );
239+ System .err .println (" input [touchscreen|touchpad] tap <x> <y>" );
240+ System .err .println (" input [touchscreen|touchpad] swipe <x1> <y1> <x2> <y2>" );
241+ System .err .println (" input trackball press" );
242+ System .err .println (" input trackball roll <dx> <dy>" );
171243 }
172244}
0 commit comments