23
23
import android .graphics .Rect ;
24
24
import android .os .Bundle ;
25
25
import android .os .Handler ;
26
+ import android .os .Message ;
26
27
import android .support .annotation .RequiresApi ;
27
28
import android .util .DisplayMetrics ;
28
29
import android .util .Log ;
@@ -76,6 +77,20 @@ public static Intent createIntent(Context context, String list) {
76
77
return new Intent (context , UIAutoActivity .class ).putExtra (INTENT_TOUCH_LIST , list );
77
78
}
78
79
80
+
81
+ private boolean isRecovering = false ;
82
+ private Handler handler = new Handler () {
83
+ @ Override
84
+ public void handleMessage (Message msg ) {
85
+ super .handleMessage (msg );
86
+
87
+ if (isRecovering ) {
88
+ MotionEvent event = (MotionEvent ) msg .obj ;
89
+ dispatchEventToCurrentActivity (event );
90
+ }
91
+ }
92
+ };
93
+
79
94
private Activity context ;
80
95
int screenWidth ;
81
96
int screenHeight ;
@@ -108,11 +123,6 @@ protected void onCreate(Bundle savedInstanceState) {
108
123
flowId = getIntent ().getLongExtra (INTENT_FLOW_ID , flowId );
109
124
touchList = JSON .parseArray (getIntent ().getStringExtra (INTENT_TOUCH_LIST ));
110
125
111
- if (touchList != null && touchList .isEmpty () == false ) { //TODO 回放操作
112
-
113
- } else { //TODO 录制操作
114
-
115
- }
116
126
117
127
DisplayMetrics outMetrics = new DisplayMetrics ();
118
128
Display display = getWindowManager ().getDefaultDisplay ();
@@ -126,6 +136,12 @@ protected void onCreate(Bundle savedInstanceState) {
126
136
cache = getSharedPreferences (TAG , Context .MODE_PRIVATE );
127
137
dividerY = cache .getFloat (DIVIDER_Y , screenHeight - dip2px (30 ));
128
138
139
+ if (touchList != null && touchList .isEmpty () == false ) { //TODO 回放操作
140
+ recover (touchList );
141
+ return ;
142
+ }
143
+
144
+
129
145
ViewGroup root = (ViewGroup ) getWindow ().getDecorView ();
130
146
cover = (ViewGroup ) getLayoutInflater ().inflate (R .layout .unit_auto_cover_layout , null );
131
147
divider = (ViewGroup ) getLayoutInflater ().inflate (R .layout .unit_auto_divider_layout , null );
@@ -166,6 +182,7 @@ protected void onCreate(Bundle savedInstanceState) {
166
182
ivUnitAutoMenu .setOnClickListener (new View .OnClickListener () {
167
183
@ Override
168
184
public void onClick (View v ) {
185
+ isRecovering = false ;
169
186
// ((ViewGroup) v.getParent()).removeView(v);
170
187
171
188
String cacheKey = UIAutoListActivity .CACHE_TOUCH ;
@@ -181,7 +198,7 @@ public void onClick(View v) {
181
198
cache .edit ().remove (cacheKey ).putString (cacheKey , JSON .toJSONString (allList )).commit ();
182
199
183
200
// startActivity(UIAutoListActivity.createIntent(DemoApplication.getInstance(), flowId)); // touchList == null ? null : touchList.toJSONString()));
184
- startActivity (UIAutoListActivity .createIntent (DemoApplication .getInstance (), touchList == null ? null : touchList .toJSONString ()));
201
+ startActivityForResult (UIAutoListActivity .createIntent (DemoApplication .getInstance (), touchList == null ? null : touchList .toJSONString ()), REQUEST_UI_AUTO_LIST );
185
202
186
203
FloatWindow .destroy ("v" );
187
204
FloatWindow .destroy ("v_ball" );
@@ -232,37 +249,7 @@ else if (event.getAction() == MotionEvent.ACTION_UP) {
232
249
public boolean onTouch (View v , MotionEvent event ) {
233
250
Log .d (TAG , "onTouchEvent " + Calendar .getInstance ().getTime ().toLocaleString () + " action:" + (event .getAction ()) + "; x:" + event .getX () + "; y:" + event .getY ());
234
251
235
- Activity a = DemoApplication .getInstance ().getCurrentActivity ();
236
- if (a != null ) {
237
- View decorView = a .getWindow ().getDecorView ();
238
- float y = decorView .getY ();
239
- float top = decorView .getTop ();
240
- event .offsetLocation (0 , decorView .getTop ());
241
- View content = decorView .findViewById (android .R .id .content );
242
- float cy = content .getY ();
243
- float ctop = content .getTop ();
244
-
245
- Rect rectangle = new Rect ();
246
- decorView .getWindowVisibleDisplayFrame (rectangle );
247
-
248
- // event.offsetLocation(0, a.getWindow().getDecorView().findViewById(android.R.id.content).getTop());
249
-
250
- if (rectangle .top > 0 ) {
251
- event = MotionEvent .obtain (event );
252
- event .offsetLocation (0 , rectangle .top );
253
- }
254
- a .dispatchTouchEvent (event );
255
-
256
- //放到 Application 中 have already added to window manager
257
- new Handler ().postDelayed (new Runnable () {
258
- @ Override
259
- public void run () {
260
- showCover (true , a );
261
- }
262
- }, 1000 );
263
- } else {
264
- //TODO 不是本 APP 的界面
265
- }
252
+ dispatchEventToCurrentActivity (event );
266
253
267
254
if (touchList == null ) {
268
255
touchList = new JSONArray ();
@@ -279,6 +266,9 @@ public void run() {
279
266
obj .put ("y" , (int ) relativeY );
280
267
obj .put ("dividerY" , (int ) dividerY );
281
268
obj .put ("time" , System .currentTimeMillis ());
269
+ obj .put ("downTime" , event .getDownTime ());
270
+ obj .put ("eventTime" , event .getEventTime ());
271
+ obj .put ("metaState" , event .getMetaState ());
282
272
touchList .add (obj );
283
273
284
274
if (isFinishing () || isDestroyed ()) {
@@ -476,6 +466,38 @@ private void showCover(boolean show, Activity activity) {
476
466
}
477
467
478
468
469
+ public boolean dispatchEventToCurrentActivity (MotionEvent event ) {
470
+ Activity a = DemoApplication .getInstance ().getCurrentActivity ();
471
+ if (a != null ) {
472
+ View decorView = a .getWindow ().getDecorView ();
473
+ float y = decorView .getY ();
474
+ float top = decorView .getTop ();
475
+ event .offsetLocation (0 , decorView .getTop ());
476
+ View content = decorView .findViewById (android .R .id .content );
477
+ float cy = content .getY ();
478
+ float ctop = content .getTop ();
479
+
480
+ Rect rectangle = new Rect ();
481
+ decorView .getWindowVisibleDisplayFrame (rectangle );
482
+
483
+ // event.offsetLocation(0, a.getWindow().getDecorView().findViewById(android.R.id.content).getTop());
484
+
485
+ if (rectangle .top > 0 ) {
486
+ event = MotionEvent .obtain (event );
487
+ event .offsetLocation (0 , rectangle .top );
488
+ }
489
+ a .dispatchTouchEvent (event );
490
+
491
+ return true ;
492
+ } else {
493
+ //TODO 不是本 APP 的界面
494
+ }
495
+
496
+ return false ;
497
+ }
498
+
499
+
500
+
479
501
/**
480
502
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
481
503
*/
@@ -511,6 +533,24 @@ protected void onDestroy() {
511
533
// }
512
534
513
535
536
+ public void recover (JSONArray touchList ) {
537
+ isRecovering = true ;
538
+
539
+ JSONObject last = null ;
540
+ for (int i = 0 ; i < touchList .size (); i ++) {
541
+ JSONObject obj = touchList .getJSONObject (i );
542
+
543
+ MotionEvent event = MotionEvent .obtain (obj .getIntValue ("downTime" ), obj .getIntValue ("eventTime" ),
544
+ obj .getIntValue ("action" ), obj .getIntValue ("x" ), obj .getIntValue ("y" ), obj .getIntValue ("metaState" ));
545
+
546
+ Message msg = handler .obtainMessage ();
547
+ msg .obj = event ;
548
+ handler .sendMessageDelayed (msg , last == null ? 0 : obj .getIntValue ("eventTime" ) - last .getIntValue ("eventTime" ));
549
+
550
+ last = obj ;
551
+ }
552
+ }
553
+
514
554
public static final int REQUEST_UI_AUTO_LIST = 1 ;
515
555
516
556
@ Override
@@ -523,6 +563,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
523
563
524
564
if (requestCode == REQUEST_UI_AUTO_LIST ) {
525
565
JSONArray array = data == null ? null : JSON .parseArray (data .getStringExtra (UIAutoListActivity .RESULT_LIST ));
566
+ recover (array );
526
567
527
568
Toast .makeText (context , "onActivityResult array = " + JSON .toJSONString (array ), Toast .LENGTH_LONG ).show ();
528
569
//TODO 恢复
0 commit comments