1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.webkit;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
/**
* Perform asynchronous dispatch of input events in a {@link WebView}.
*
* This dispatcher is shared by the UI thread ({@link WebViewClassic}) and web kit
* thread ({@link WebViewCore}). The UI thread enqueues events for
* processing, waits for the web kit thread to handle them, and then performs
* additional processing depending on the outcome.
*
* How it works:
*
* 1. The web view thread receives an input event from the input system on the UI
* thread in its {@link WebViewClassic#onTouchEvent} handler. It sends the input event
* to the dispatcher, then immediately returns true to the input system to indicate that
* it will handle the event.
*
* 2. The web kit thread is notified that an event has been enqueued. Meanwhile additional
* events may be enqueued from the UI thread. In some cases, the dispatcher may decide to
* coalesce motion events into larger batches or to cancel events that have been
* sitting in the queue for too long.
*
* 3. The web kit thread wakes up and handles all input events that are waiting for it.
* After processing each input event, it informs the dispatcher whether the web application
* has decided to handle the event itself and to prevent default event handling.
*
* 4. If web kit indicates that it wants to prevent default event handling, then web kit
* consumes the remainder of the gesture and web view receives a cancel event if
* needed. Otherwise, the web view handles the gesture on the UI thread normally.
*
* 5. If the web kit thread takes too long to handle an input event, then it loses the
* right to handle it. The dispatcher synthesizes a cancellation event for web kit and
* then tells the web view on the UI thread to handle the event that timed out along
* with the rest of the gesture.
*
* One thing to keep in mind about the dispatcher is that what goes into the dispatcher
* is not necessarily what the web kit or UI thread will see. As mentioned above, the
* dispatcher may tweak the input event stream to improve responsiveness. Both web view and
* web kit are guaranteed to perceive a consistent stream of input events but
* they might not always see the same events (especially if one decides
* to prevent the other from handling a particular gesture).
*
* This implementation very deliberately does not refer to the {@link WebViewClassic}
* or {@link WebViewCore} classes, preferring to communicate with them only via
* interfaces to avoid unintentional coupling to their implementation details.
*
* Currently, the input dispatcher only handles pointer events (includes touch,
* hover and scroll events). In principle, it could be extended to handle trackball
* and key events if needed.
*
* @hide
*/
final class WebViewInputDispatcher {
private static final String TAG = "WebViewInputDispatcher";
private static final boolean DEBUG = false;
// This enables batching of MotionEvents. It will combine multiple MotionEvents
// together into a single MotionEvent if more events come in while we are
// still waiting on the processing of a previous event.
// If this is set to false, we will instead opt to drop ACTION_MOVE
// events we cannot keep up with.
// TODO: If batching proves to be working well, remove this
private static final boolean ENABLE_EVENT_BATCHING = true;
private final Object mLock = new Object();
// Pool of queued input events. (guarded by mLock)
private static final int MAX_DISPATCH_EVENT_POOL_SIZE = 10;
private DispatchEvent mDispatchEventPool;
private int mDispatchEventPoolSize;
// Posted state, tracks events posted to the dispatcher. (guarded by mLock)
private final TouchStream mPostTouchStream = new TouchStream();
private boolean mPostSendTouchEventsToWebKit;
private boolean mPostDoNotSendTouchEventsToWebKitUntilNextGesture;
private boolean mPostLongPressScheduled;
private boolean mPostClickScheduled;
private boolean mPostShowTapHighlightScheduled;
private boolean mPostHideTapHighlightScheduled;
private int mPostLastWebKitXOffset;
private int mPostLastWebKitYOffset;
private float mPostLastWebKitScale;
// State for event tracking (click, longpress, double tap, etc..)
private boolean mIsDoubleTapCandidate;
private boolean mIsTapCandidate;
private float mInitialDownX;
private float mInitialDownY;
private float mTouchSlopSquared;
private float mDoubleTapSlopSquared;
// Web kit state, tracks events observed by web kit. (guarded by mLock)
private final DispatchEventQueue mWebKitDispatchEventQueue = new DispatchEventQueue();
private final TouchStream mWebKitTouchStream = new TouchStream();
private final WebKitCallbacks mWebKitCallbacks;
private final WebKitHandler mWebKitHandler;
private boolean mWebKitDispatchScheduled;
private boolean mWebKitTimeoutScheduled;
private long mWebKitTimeoutTime;
// UI state, tracks events observed by the UI. (guarded by mLock)
private final DispatchEventQueue mUiDispatchEventQueue = new DispatchEventQueue();
private final TouchStream mUiTouchStream = new TouchStream();
private final UiCallbacks mUiCallbacks;
private final UiHandler mUiHandler;
private boolean mUiDispatchScheduled;
// Give up on web kit handling of input events when this timeout expires.
private static final long WEBKIT_TIMEOUT_MILLIS = 200;
private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout();
private static final int LONG_PRESS_TIMEOUT =
ViewConfiguration.getLongPressTimeout() + TAP_TIMEOUT;
private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout();
private static final int PRESSED_STATE_DURATION = ViewConfiguration.getPressedStateDuration();
/**
* Event type: Indicates a touch event type.
*
* This event is delivered together with a {@link MotionEvent} with one of the
* following actions: {@link MotionEvent#ACTION_DOWN}, {@link MotionEvent#ACTION_MOVE},
* {@link MotionEvent#ACTION_UP}, {@link MotionEvent#ACTION_POINTER_DOWN},
* {@link MotionEvent#ACTION_POINTER_UP}, {@link MotionEvent#ACTION_CANCEL}.
*/
public static final int EVENT_TYPE_TOUCH = 0;
/**
* Event type: Indicates a hover event type.
*
* This event is delivered together with a {@link MotionEvent} with one of the
* following actions: {@link MotionEvent#ACTION_HOVER_ENTER},
* {@link MotionEvent#ACTION_HOVER_MOVE}, {@link MotionEvent#ACTION_HOVER_MOVE}.
*/
public static final int EVENT_TYPE_HOVER = 1;
/**
* Event type: Indicates a scroll event type.
*
* This event is delivered together with a {@link MotionEvent} with action
* {@link MotionEvent#ACTION_SCROLL}.
*/
public static final int EVENT_TYPE_SCROLL = 2;
/**
* Event type: Indicates a long-press event type.
*
* This event is delivered in the middle of a sequence of {@link #EVENT_TYPE_TOUCH} events.
* It includes a {@link MotionEvent} with action {@link MotionEvent#ACTION_MOVE}
* that indicates the current touch coordinates of the long-press.
*
* This event is sent when the current touch gesture has been held longer than
* the long-press interval.
*/
public static final int EVENT_TYPE_LONG_PRESS = 3;
/**
* Event type: Indicates a click event type.
*
* This event is delivered after a sequence of {@link #EVENT_TYPE_TOUCH} events that
* comprise a complete gesture ending with {@link MotionEvent#ACTION_UP}.
* It includes a {@link MotionEvent} with action {@link MotionEvent#ACTION_UP}
* that indicates the location of the click.
*
* This event is sent shortly after the end of a touch after the double-tap
* interval has expired to indicate a click.
*/
public static final int EVENT_TYPE_CLICK = 4;
/**
* Event type: Indicates a double-tap event type.
*
* This event is delivered after a sequence of {@link #EVENT_TYPE_TOUCH} events that
* comprise a complete gesture ending with {@link MotionEvent#ACTION_UP}.
* It includes a {@link MotionEvent} with action {@link MotionEvent#ACTION_UP}
* that indicates the location of the double-tap.
*
* This event is sent immediately after a sequence of two touches separated
* in time by no more than the double-tap interval and separated in space
* by no more than the double-tap slop.
*/
public static final int EVENT_TYPE_DOUBLE_TAP = 5;
/**
* Event type: Indicates that a hit test should be performed
*/
public static final int EVENT_TYPE_HIT_TEST = 6;
/**
* Flag: This event is private to this queue. Do not forward it.
*/
public static final int FLAG_PRIVATE = 1 << 0;
/**
* Flag: This event is currently being processed by web kit.
* If a timeout occurs, make a copy of it before forwarding the event to another queue.
*/
public static final int FLAG_WEBKIT_IN_PROGRESS = 1 << 1;
/**
* Flag: A timeout occurred while waiting for web kit to process this input event.
*/
public static final int FLAG_WEBKIT_TIMEOUT = 1 << 2;
/**
* Flag: Indicates that the event was transformed for delivery to web kit.
* The event must be transformed back before being delivered to the UI.
*/
public static final int FLAG_WEBKIT_TRANSFORMED_EVENT = 1 << 3;
public WebViewInputDispatcher(UiCallbacks uiCallbacks, WebKitCallbacks webKitCallbacks) {
this.mUiCallbacks = uiCallbacks;
mUiHandler = new UiHandler(uiCallbacks.getUiLooper());
this.mWebKitCallbacks = webKitCallbacks;
mWebKitHandler = new WebKitHandler(webKitCallbacks.getWebKitLooper());
ViewConfiguration config = ViewConfiguration.get(mUiCallbacks.getContext());
mDoubleTapSlopSquared = config.getScaledDoubleTapSlop();
mDoubleTapSlopSquared = (mDoubleTapSlopSquared * mDoubleTapSlopSquared);
mTouchSlopSquared = config.getScaledTouchSlop();
mTouchSlopSquared = (mTouchSlopSquared * mTouchSlopSquared);
}
/**
* Sets whether web kit wants to receive touch events.
*
* @param enable True to enable dispatching of touch events to web kit, otherwise
* web kit will be skipped.
*/
public void setWebKitWantsTouchEvents(boolean enable) {
if (DEBUG) {
Log.d(TAG, "webkitWantsTouchEvents: " + enable);
}
synchronized (mLock) {
if (mPostSendTouchEventsToWebKit != enable) {
if (!enable) {
enqueueWebKitCancelTouchEventIfNeededLocked();
}
mPostSendTouchEventsToWebKit = enable;
}
}
}
/**
* Posts a pointer event to the dispatch queue.
*
* @param event The event to post.
* @param webKitXOffset X offset to apply to events before dispatching them to web kit.
* @param webKitYOffset Y offset to apply to events before dispatching them to web kit.
* @param webKitScale The scale factor to apply to translated events before dispatching
* them to web kit.
* @return True if the dispatcher will handle the event, false if the event is unsupported.
*/
public boolean postPointerEvent(MotionEvent event,
int webKitXOffset, int webKitYOffset, float webKitScale) {
if (event == null) {
throw new IllegalArgumentException("event cannot be null");
}
if (DEBUG) {
Log.d(TAG, "postPointerEvent: " + event);
}
final int action = event.getActionMasked();
final int eventType;
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
eventType = EVENT_TYPE_TOUCH;
break;
case MotionEvent.ACTION_SCROLL:
eventType = EVENT_TYPE_SCROLL;
break;
case MotionEvent.ACTION_HOVER_ENTER:
case MotionEvent.ACTION_HOVER_MOVE:
case MotionEvent.ACTION_HOVER_EXIT:
eventType = EVENT_TYPE_HOVER;
break;
default:
return false; // currently unsupported event type
}
synchronized (mLock) {
// Ensure that the event is consistent and should be delivered.
MotionEvent eventToEnqueue = event;
if (eventType == EVENT_TYPE_TOUCH) {
eventToEnqueue = mPostTouchStream.update(event);
if (eventToEnqueue == null) {
if (DEBUG) {
Log.d(TAG, "postPointerEvent: dropped event " + event);
}
unscheduleLongPressLocked();
unscheduleClickLocked();
hideTapCandidateLocked();
return false;
}
if (action == MotionEvent.ACTION_DOWN && mPostSendTouchEventsToWebKit) {
if (mUiCallbacks.shouldInterceptTouchEvent(eventToEnqueue)) {
mPostDoNotSendTouchEventsToWebKitUntilNextGesture = true;
} else if (mPostDoNotSendTouchEventsToWebKitUntilNextGesture) {
// Recover from a previous web kit timeout.
mPostDoNotSendTouchEventsToWebKitUntilNextGesture = false;
}
}
}
// Copy the event because we need to retain ownership.
if (eventToEnqueue == event) {
eventToEnqueue = event.copy();
}
DispatchEvent d = obtainDispatchEventLocked(eventToEnqueue, eventType, 0,
webKitXOffset, webKitYOffset, webKitScale);
updateStateTrackersLocked(d, event);
enqueueEventLocked(d);
}
return true;
}
private void scheduleLongPressLocked() {
unscheduleLongPressLocked();
mPostLongPressScheduled = true;
mUiHandler.sendEmptyMessageDelayed(UiHandler.MSG_LONG_PRESS,
LONG_PRESS_TIMEOUT);
}
private void unscheduleLongPressLocked() {
if (mPostLongPressScheduled) {
mPostLongPressScheduled = false;
mUiHandler.removeMessages(UiHandler.MSG_LONG_PRESS);
}
}
private void postLongPress() {
synchronized (mLock) {
if (!mPostLongPressScheduled) {
return;
}
mPostLongPressScheduled = false;
MotionEvent event = mPostTouchStream.getLastEvent();
if (event == null) {
return;
}
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
break;
default:
return;
}
MotionEvent eventToEnqueue = MotionEvent.obtainNoHistory(event);
eventToEnqueue.setAction(MotionEvent.ACTION_MOVE);
DispatchEvent d = obtainDispatchEventLocked(eventToEnqueue, EVENT_TYPE_LONG_PRESS, 0,
mPostLastWebKitXOffset, mPostLastWebKitYOffset, mPostLastWebKitScale);
enqueueEventLocked(d);
}
}
private void hideTapCandidateLocked() {
unscheduleHideTapHighlightLocked();
unscheduleShowTapHighlightLocked();
mUiCallbacks.showTapHighlight(false);
}
private void showTapCandidateLocked() {
unscheduleHideTapHighlightLocked();
unscheduleShowTapHighlightLocked();
mUiCallbacks.showTapHighlight(true);
}
private void scheduleShowTapHighlightLocked() {
unscheduleShowTapHighlightLocked();
mPostShowTapHighlightScheduled = true;
mUiHandler.sendEmptyMessageDelayed(UiHandler.MSG_SHOW_TAP_HIGHLIGHT,
TAP_TIMEOUT);
}
private void unscheduleShowTapHighlightLocked() {
if (mPostShowTapHighlightScheduled) {
mPostShowTapHighlightScheduled = false;
mUiHandler.removeMessages(UiHandler.MSG_SHOW_TAP_HIGHLIGHT);
}
}
private void scheduleHideTapHighlightLocked() {
unscheduleHideTapHighlightLocked();
mPostHideTapHighlightScheduled = true;
mUiHandler.sendEmptyMessageDelayed(UiHandler.MSG_HIDE_TAP_HIGHLIGHT,
PRESSED_STATE_DURATION);
}
private void unscheduleHideTapHighlightLocked() {
if (mPostHideTapHighlightScheduled) {
mPostHideTapHighlightScheduled = false;
mUiHandler.removeMessages(UiHandler.MSG_HIDE_TAP_HIGHLIGHT);
}
}
private void postShowTapHighlight(boolean show) {
synchronized (mLock) {
if (show) {
if (!mPostShowTapHighlightScheduled) {
return;
}
mPostShowTapHighlightScheduled = false;
} else {
if (!mPostHideTapHighlightScheduled) {
return;
}
mPostHideTapHighlightScheduled = false;
}
mUiCallbacks.showTapHighlight(show);
}
}
private void scheduleClickLocked() {
unscheduleClickLocked();
mPostClickScheduled = true;
mUiHandler.sendEmptyMessageDelayed(UiHandler.MSG_CLICK, DOUBLE_TAP_TIMEOUT);
}
private void unscheduleClickLocked() {
if (mPostClickScheduled) {
mPostClickScheduled = false;
mUiHandler.removeMessages(UiHandler.MSG_CLICK);
}
}
private void postClick() {
synchronized (mLock) {
if (!mPostClickScheduled) {
return;
}
mPostClickScheduled = false;
MotionEvent event = mPostTouchStream.getLastEvent();
if (event == null || event.getAction() != MotionEvent.ACTION_UP) {
return;
}
showTapCandidateLocked();
MotionEvent eventToEnqueue = MotionEvent.obtainNoHistory(event);
DispatchEvent d = obtainDispatchEventLocked(eventToEnqueue, EVENT_TYPE_CLICK, 0,
mPostLastWebKitXOffset, mPostLastWebKitYOffset, mPostLastWebKitScale);
enqueueEventLocked(d);
}
}
private void checkForDoubleTapOnDownLocked(MotionEvent event) {
mIsDoubleTapCandidate = false;
if (!mPostClickScheduled) {
return;
}
int deltaX = (int) mInitialDownX - (int) event.getX();
int deltaY = (int) mInitialDownY - (int) event.getY();
if ((deltaX * deltaX + deltaY * deltaY) < mDoubleTapSlopSquared) {
unscheduleClickLocked();
mIsDoubleTapCandidate = true;
}
}
private boolean isClickCandidateLocked(MotionEvent event) {
if (event == null
|| event.getActionMasked() != MotionEvent.ACTION_UP
|| !mIsTapCandidate) {
return false;
}
long downDuration = event.getEventTime() - event.getDownTime();
return downDuration < LONG_PRESS_TIMEOUT;
}
private void enqueueDoubleTapLocked(MotionEvent event) {
MotionEvent eventToEnqueue = MotionEvent.obtainNoHistory(event);
DispatchEvent d = obtainDispatchEventLocked(eventToEnqueue, EVENT_TYPE_DOUBLE_TAP, 0,
mPostLastWebKitXOffset, mPostLastWebKitYOffset, mPostLastWebKitScale);
enqueueEventLocked(d);
}
private void enqueueHitTestLocked(MotionEvent event) {
mUiCallbacks.clearPreviousHitTest();
MotionEvent eventToEnqueue = MotionEvent.obtainNoHistory(event);
DispatchEvent d = obtainDispatchEventLocked(eventToEnqueue, EVENT_TYPE_HIT_TEST, 0,
mPostLastWebKitXOffset, mPostLastWebKitYOffset, mPostLastWebKitScale);
enqueueEventLocked(d);
}
private void checkForSlopLocked(MotionEvent event) {
if (!mIsTapCandidate) {
return;
}
int deltaX = (int) mInitialDownX - (int) event.getX();
int deltaY = (int) mInitialDownY - (int) event.getY();
if ((deltaX * deltaX + deltaY * deltaY) > mTouchSlopSquared) {
unscheduleLongPressLocked();
mIsTapCandidate = false;
hideTapCandidateLocked();
}
}
private void updateStateTrackersLocked(DispatchEvent d, MotionEvent event) {
mPostLastWebKitXOffset = d.mWebKitXOffset;
mPostLastWebKitYOffset = d.mWebKitYOffset;
mPostLastWebKitScale = d.mWebKitScale;
int action = event != null ? event.getAction() : MotionEvent.ACTION_CANCEL;
if (d.mEventType != EVENT_TYPE_TOUCH) {
return;
}
if (action == MotionEvent.ACTION_CANCEL
|| event.getPointerCount() > 1) {
unscheduleLongPressLocked();
unscheduleClickLocked();
hideTapCandidateLocked();
mIsDoubleTapCandidate = false;
mIsTapCandidate = false;
hideTapCandidateLocked();
} else if (action == MotionEvent.ACTION_DOWN) {
checkForDoubleTapOnDownLocked(event);
scheduleLongPressLocked();
mIsTapCandidate = true;
mInitialDownX = event.getX();
mInitialDownY = event.getY();
enqueueHitTestLocked(event);
if (mIsDoubleTapCandidate) {
hideTapCandidateLocked();
} else {
scheduleShowTapHighlightLocked();
}
} else if (action == MotionEvent.ACTION_UP) {
unscheduleLongPressLocked();
if (isClickCandidateLocked(event)) {
if (mIsDoubleTapCandidate) {
hideTapCandidateLocked();
enqueueDoubleTapLocked(event);
} else {
scheduleClickLocked();
}
} else {
hideTapCandidateLocked();
}
} else if (action == MotionEvent.ACTION_MOVE) {
checkForSlopLocked(event);
}
}
/**
* Dispatches pending web kit events.
* Must only be called from the web kit thread.
*
* This method may be used to flush the queue of pending input events
* immediately. This method may help to reduce input dispatch latency
* if called before certain expensive operations such as drawing.
*/
public void dispatchWebKitEvents() {
dispatchWebKitEvents(false);
}
private void dispatchWebKitEvents(boolean calledFromHandler) {
for (;;) {
// Get the next event, but leave it in the queue so we can move it to the UI
// queue if a timeout occurs.
DispatchEvent d;
MotionEvent event;
final int eventType;
int flags;
synchronized (mLock) {
if (!ENABLE_EVENT_BATCHING) {
drainStaleWebKitEventsLocked();
}
d = mWebKitDispatchEventQueue.mHead;
if (d == null) {
if (mWebKitDispatchScheduled) {
mWebKitDispatchScheduled = false;
if (!calledFromHandler) {
mWebKitHandler.removeMessages(
WebKitHandler.MSG_DISPATCH_WEBKIT_EVENTS);
}
}
return;
}
event = d.mEvent;
if (event != null) {
event.offsetLocation(d.mWebKitXOffset, d.mWebKitYOffset);
event.scale(d.mWebKitScale);
d.mFlags |= FLAG_WEBKIT_TRANSFORMED_EVENT;
}
eventType = d.mEventType;
if (eventType == EVENT_TYPE_TOUCH) {
event = mWebKitTouchStream.update(event);
if (DEBUG && event == null && d.mEvent != null) {
Log.d(TAG, "dispatchWebKitEvents: dropped event " + d.mEvent);
}
}
d.mFlags |= FLAG_WEBKIT_IN_PROGRESS;
flags = d.mFlags;
}
// Handle the event.
final boolean preventDefault;
if (event == null) {
preventDefault = false;
} else {
preventDefault = dispatchWebKitEvent(event, eventType, flags);
}
synchronized (mLock) {
flags = d.mFlags;
d.mFlags = flags & ~FLAG_WEBKIT_IN_PROGRESS;
boolean recycleEvent = event != d.mEvent;
if ((flags & FLAG_WEBKIT_TIMEOUT) != 0) {
// A timeout occurred!
recycleDispatchEventLocked(d);
} else {
// Web kit finished in a timely manner. Dequeue the event.
assert mWebKitDispatchEventQueue.mHead == d;
mWebKitDispatchEventQueue.dequeue();
updateWebKitTimeoutLocked();
if ((flags & FLAG_PRIVATE) != 0) {
// Event was intended for web kit only. All done.
recycleDispatchEventLocked(d);
} else if (preventDefault) {
// Web kit has decided to consume the event!
if (d.mEventType == EVENT_TYPE_TOUCH) {
enqueueUiCancelTouchEventIfNeededLocked();
unscheduleLongPressLocked();
}
} else {
// Web kit is being friendly. Pass the event to the UI.
enqueueUiEventUnbatchedLocked(d);
}
}
if (event != null && recycleEvent) {
event.recycle();
}
if (eventType == EVENT_TYPE_CLICK) {
scheduleHideTapHighlightLocked();
}
}
}
}
// Runs on web kit thread.
private boolean dispatchWebKitEvent(MotionEvent event, int eventType, int flags) {
if (DEBUG) {
Log.d(TAG, "dispatchWebKitEvent: event=" + event
+ ", eventType=" + eventType + ", flags=" + flags);
}
boolean preventDefault = mWebKitCallbacks.dispatchWebKitEvent(
this, event, eventType, flags);
if (DEBUG) {
Log.d(TAG, "dispatchWebKitEvent: preventDefault=" + preventDefault);
}
return preventDefault;
}
private boolean isMoveEventLocked(DispatchEvent d) {
return d.mEvent != null
&& d.mEvent.getActionMasked() == MotionEvent.ACTION_MOVE;
}
private void drainStaleWebKitEventsLocked() {
DispatchEvent d = mWebKitDispatchEventQueue.mHead;
while (d != null && d.mNext != null
&& isMoveEventLocked(d)
&& isMoveEventLocked(d.mNext)) {
DispatchEvent next = d.mNext;
skipWebKitEventLocked(d);
d = next;
}
mWebKitDispatchEventQueue.mHead = d;
}
// Called by WebKit when it doesn't care about the rest of the touch stream
public void skipWebkitForRemainingTouchStream() {
// Just treat this like a timeout
handleWebKitTimeout();
}
// Runs on UI thread in response to the web kit thread appearing to be unresponsive.
private void handleWebKitTimeout() {
synchronized (mLock) {
if (!mWebKitTimeoutScheduled) {
return;
}
mWebKitTimeoutScheduled = false;
if (DEBUG) {
Log.d(TAG, "handleWebKitTimeout: timeout occurred!");
}
// Drain the web kit event queue.
DispatchEvent d = mWebKitDispatchEventQueue.dequeueList();
// If web kit was processing an event (must be at the head of the list because
// it can only do one at a time), then clone it or ignore it.
if ((d.mFlags & FLAG_WEBKIT_IN_PROGRESS) != 0) {
d.mFlags |= FLAG_WEBKIT_TIMEOUT;
if ((d.mFlags & FLAG_PRIVATE) != 0) {
d = d.mNext; // the event is private to web kit, ignore it
} else {
d = copyDispatchEventLocked(d);
d.mFlags &= ~FLAG_WEBKIT_IN_PROGRESS;
}
}
// Enqueue all non-private events for handling by the UI thread.
while (d != null) {
DispatchEvent next = d.mNext;
skipWebKitEventLocked(d);
d = next;
}
// Tell web kit to cancel all pending touches.
// This also prevents us from sending web kit any more touches until the
// next gesture begins. (As required to ensure touch event stream consistency.)
enqueueWebKitCancelTouchEventIfNeededLocked();
}
}
private void skipWebKitEventLocked(DispatchEvent d) {
d.mNext = null;
if ((d.mFlags & FLAG_PRIVATE) != 0) {
recycleDispatchEventLocked(d);
} else {
d.mFlags |= FLAG_WEBKIT_TIMEOUT;
enqueueUiEventUnbatchedLocked(d);
}
}
/**
* Dispatches pending UI events.
* Must only be called from the UI thread.
*
* This method may be used to flush the queue of pending input events
* immediately. This method may help to reduce input dispatch latency
* if called before certain expensive operations such as drawing.
*/
public void dispatchUiEvents() {
dispatchUiEvents(false);
}
private void dispatchUiEvents(boolean calledFromHandler) {
for (;;) {
MotionEvent event;
final int eventType;
final int flags;
synchronized (mLock) {
DispatchEvent d = mUiDispatchEventQueue.dequeue();
if (d == null) {
if (mUiDispatchScheduled) {
mUiDispatchScheduled = false;
if (!calledFromHandler) {
mUiHandler.removeMessages(UiHandler.MSG_DISPATCH_UI_EVENTS);
}
}
return;
}
event = d.mEvent;
if (event != null && (d.mFlags & FLAG_WEBKIT_TRANSFORMED_EVENT) != 0) {
event.scale(1.0f / d.mWebKitScale);
event.offsetLocation(-d.mWebKitXOffset, -d.mWebKitYOffset);
d.mFlags &= ~FLAG_WEBKIT_TRANSFORMED_EVENT;
}
eventType = d.mEventType;
if (eventType == EVENT_TYPE_TOUCH) {
event = mUiTouchStream.update(event);
if (DEBUG && event == null && d.mEvent != null) {
Log.d(TAG, "dispatchUiEvents: dropped event " + d.mEvent);
}
}
flags = d.mFlags;
if (event == d.mEvent) {
d.mEvent = null; // retain ownership of event, don't recycle it yet
}
recycleDispatchEventLocked(d);
if (eventType == EVENT_TYPE_CLICK) {
scheduleHideTapHighlightLocked();
}
}
// Handle the event.
if (event != null) {
dispatchUiEvent(event, eventType, flags);
event.recycle();
}
}
}
// Runs on UI thread.
private void dispatchUiEvent(MotionEvent event, int eventType, int flags) {
if (DEBUG) {
Log.d(TAG, "dispatchUiEvent: event=" + event
+ ", eventType=" + eventType + ", flags=" + flags);
}
mUiCallbacks.dispatchUiEvent(event, eventType, flags);
}
private void enqueueEventLocked(DispatchEvent d) {
if (!shouldSkipWebKit(d)) {
enqueueWebKitEventLocked(d);
} else {
enqueueUiEventLocked(d);
}
}
private boolean shouldSkipWebKit(DispatchEvent d) {
switch (d.mEventType) {
case EVENT_TYPE_CLICK:
case EVENT_TYPE_HOVER:
case EVENT_TYPE_SCROLL:
case EVENT_TYPE_HIT_TEST:
return false;
case EVENT_TYPE_TOUCH:
// TODO: This should be cleaned up. We now have WebViewInputDispatcher
// and WebViewClassic both checking for slop and doing their own
// thing - they should be consolidated. And by consolidated, I mean
// WebViewClassic's version should just be deleted.
// The reason this is done is because webpages seem to expect
// that they only get an ontouchmove if the slop has been exceeded.
if (mIsTapCandidate && d.mEvent != null
&& d.mEvent.getActionMasked() == MotionEvent.ACTION_MOVE) {
return true;
}
return !mPostSendTouchEventsToWebKit
|| mPostDoNotSendTouchEventsToWebKitUntilNextGesture;
}
return true;
}
private void enqueueWebKitCancelTouchEventIfNeededLocked() {
// We want to cancel touch events that were delivered to web kit.
// Enqueue a null event at the end of the queue if needed.
if (mWebKitTouchStream.isCancelNeeded() || !mWebKitDispatchEventQueue.isEmpty()) {
DispatchEvent d = obtainDispatchEventLocked(null, EVENT_TYPE_TOUCH, FLAG_PRIVATE,
0, 0, 1.0f);
enqueueWebKitEventUnbatchedLocked(d);
mPostDoNotSendTouchEventsToWebKitUntilNextGesture = true;
}
}
private void enqueueWebKitEventLocked(DispatchEvent d) {
if (batchEventLocked(d, mWebKitDispatchEventQueue.mTail)) {
if (DEBUG) {
Log.d(TAG, "enqueueWebKitEventLocked: batched event " + d.mEvent);
}
recycleDispatchEventLocked(d);
} else {
enqueueWebKitEventUnbatchedLocked(d);
}
}
private void enqueueWebKitEventUnbatchedLocked(DispatchEvent d) {
if (DEBUG) {
Log.d(TAG, "enqueueWebKitEventUnbatchedLocked: enqueued event " + d.mEvent);
}
mWebKitDispatchEventQueue.enqueue(d);
scheduleWebKitDispatchLocked();
updateWebKitTimeoutLocked();
}
private void scheduleWebKitDispatchLocked() {
if (!mWebKitDispatchScheduled) {
mWebKitHandler.sendEmptyMessage(WebKitHandler.MSG_DISPATCH_WEBKIT_EVENTS);
mWebKitDispatchScheduled = true;
}
}
private void updateWebKitTimeoutLocked() {
DispatchEvent d = mWebKitDispatchEventQueue.mHead;
if (d != null && mWebKitTimeoutScheduled && mWebKitTimeoutTime == d.mTimeoutTime) {
return;
}
if (mWebKitTimeoutScheduled) {
mUiHandler.removeMessages(UiHandler.MSG_WEBKIT_TIMEOUT);
mWebKitTimeoutScheduled = false;
}
if (d != null) {
mUiHandler.sendEmptyMessageAtTime(UiHandler.MSG_WEBKIT_TIMEOUT, d.mTimeoutTime);
mWebKitTimeoutScheduled = true;
mWebKitTimeoutTime = d.mTimeoutTime;
}
}
private void enqueueUiCancelTouchEventIfNeededLocked() {
// We want to cancel touch events that were delivered to the UI.
// Enqueue a null event at the end of the queue if needed.
if (mUiTouchStream.isCancelNeeded() || !mUiDispatchEventQueue.isEmpty()) {
DispatchEvent d = obtainDispatchEventLocked(null, EVENT_TYPE_TOUCH, FLAG_PRIVATE,
0, 0, 1.0f);
enqueueUiEventUnbatchedLocked(d);
}
}
private void enqueueUiEventLocked(DispatchEvent d) {
if (batchEventLocked(d, mUiDispatchEventQueue.mTail)) {
if (DEBUG) {
Log.d(TAG, "enqueueUiEventLocked: batched event " + d.mEvent);
}
recycleDispatchEventLocked(d);
} else {
enqueueUiEventUnbatchedLocked(d);
}
}
private void enqueueUiEventUnbatchedLocked(DispatchEvent d) {
if (DEBUG) {
Log.d(TAG, "enqueueUiEventUnbatchedLocked: enqueued event " + d.mEvent);
}
mUiDispatchEventQueue.enqueue(d);
scheduleUiDispatchLocked();
}
private void scheduleUiDispatchLocked() {
if (!mUiDispatchScheduled) {
mUiHandler.sendEmptyMessage(UiHandler.MSG_DISPATCH_UI_EVENTS);
mUiDispatchScheduled = true;
}
}
private boolean batchEventLocked(DispatchEvent in, DispatchEvent tail) {
if (!ENABLE_EVENT_BATCHING) {
return false;
}
if (tail != null && tail.mEvent != null && in.mEvent != null
&& in.mEventType == tail.mEventType
&& in.mFlags == tail.mFlags
&& in.mWebKitXOffset == tail.mWebKitXOffset
&& in.mWebKitYOffset == tail.mWebKitYOffset
&& in.mWebKitScale == tail.mWebKitScale) {
return tail.mEvent.addBatch(in.mEvent);
}
return false;
}
private DispatchEvent obtainDispatchEventLocked(MotionEvent event,
int eventType, int flags, int webKitXOffset, int webKitYOffset, float webKitScale) {
DispatchEvent d = obtainUninitializedDispatchEventLocked();
d.mEvent = event;
d.mEventType = eventType;
d.mFlags = flags;
d.mTimeoutTime = SystemClock.uptimeMillis() + WEBKIT_TIMEOUT_MILLIS;
d.mWebKitXOffset = webKitXOffset;
d.mWebKitYOffset = webKitYOffset;
d.mWebKitScale = webKitScale;
if (DEBUG) {
Log.d(TAG, "Timeout time: " + (d.mTimeoutTime - SystemClock.uptimeMillis()));
}
return d;
}
private DispatchEvent copyDispatchEventLocked(DispatchEvent d) {
DispatchEvent copy = obtainUninitializedDispatchEventLocked();
if (d.mEvent != null) {
copy.mEvent = d.mEvent.copy();
}
copy.mEventType = d.mEventType;
copy.mFlags = d.mFlags;
copy.mTimeoutTime = d.mTimeoutTime;
copy.mWebKitXOffset = d.mWebKitXOffset;
copy.mWebKitYOffset = d.mWebKitYOffset;
copy.mWebKitScale = d.mWebKitScale;
copy.mNext = d.mNext;
return copy;
}
private DispatchEvent obtainUninitializedDispatchEventLocked() {
DispatchEvent d = mDispatchEventPool;
if (d != null) {
mDispatchEventPoolSize -= 1;
mDispatchEventPool = d.mNext;
d.mNext = null;
} else {
d = new DispatchEvent();
}
return d;
}
private void recycleDispatchEventLocked(DispatchEvent d) {
if (d.mEvent != null) {
d.mEvent.recycle();
d.mEvent = null;
}
if (mDispatchEventPoolSize < MAX_DISPATCH_EVENT_POOL_SIZE) {
mDispatchEventPoolSize += 1;
d.mNext = mDispatchEventPool;
mDispatchEventPool = d;
}
}
/* Implemented by {@link WebViewClassic} to perform operations on the UI thread. */
public static interface UiCallbacks {
/**
* Gets the UI thread's looper.
* @return The looper.
*/
public Looper getUiLooper();
/**
* Gets the UI's context
* @return The context
*/
public Context getContext();
/**
* Dispatches an event to the UI.
* @param event The event.
* @param eventType The event type.
* @param flags The event's dispatch flags.
*/
public void dispatchUiEvent(MotionEvent event, int eventType, int flags);
/**
* Asks the UI thread whether this touch event stream should be
* intercepted based on the touch down event.
* @param event The touch down event.
* @return true if the UI stream wants the touch stream without going
* through webkit or false otherwise.
*/
public boolean shouldInterceptTouchEvent(MotionEvent event);
/**
* Inform's the UI that it should show the tap highlight
* @param show True if it should show the highlight, false if it should hide it
*/
public void showTapHighlight(boolean show);
/**
* Called when we are sending a new EVENT_TYPE_HIT_TEST to WebKit, so
* previous hit tests should be cleared as they are obsolete.
*/
public void clearPreviousHitTest();
}
/* Implemented by {@link WebViewCore} to perform operations on the web kit thread. */
public static interface WebKitCallbacks {
/**
* Gets the web kit thread's looper.
* @return The looper.
*/
public Looper getWebKitLooper();
/**
* Dispatches an event to web kit.
* @param dispatcher The WebViewInputDispatcher sending the event
* @param event The event.
* @param eventType The event type.
* @param flags The event's dispatch flags.
* @return True if web kit wants to prevent default event handling.
*/
public boolean dispatchWebKitEvent(WebViewInputDispatcher dispatcher,
MotionEvent event, int eventType, int flags);
}
// Runs on UI thread.
private final class UiHandler extends Handler {
public static final int MSG_DISPATCH_UI_EVENTS = 1;
public static final int MSG_WEBKIT_TIMEOUT = 2;
public static final int MSG_LONG_PRESS = 3;
public static final int MSG_CLICK = 4;
public static final int MSG_SHOW_TAP_HIGHLIGHT = 5;
public static final int MSG_HIDE_TAP_HIGHLIGHT = 6;
public UiHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_DISPATCH_UI_EVENTS:
dispatchUiEvents(true);
break;
case MSG_WEBKIT_TIMEOUT:
handleWebKitTimeout();
break;
case MSG_LONG_PRESS:
postLongPress();
break;
case MSG_CLICK:
postClick();
break;
case MSG_SHOW_TAP_HIGHLIGHT:
postShowTapHighlight(true);
break;
case MSG_HIDE_TAP_HIGHLIGHT:
postShowTapHighlight(false);
break;
default:
throw new IllegalStateException("Unknown message type: " + msg.what);
}
}
}
// Runs on web kit thread.
private final class WebKitHandler extends Handler {
public static final int MSG_DISPATCH_WEBKIT_EVENTS = 1;
public WebKitHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_DISPATCH_WEBKIT_EVENTS:
dispatchWebKitEvents(true);
break;
default:
throw new IllegalStateException("Unknown message type: " + msg.what);
}
}
}
private static final class DispatchEvent {
public DispatchEvent mNext;
public MotionEvent mEvent;
public int mEventType;
public int mFlags;
public long mTimeoutTime;
public int mWebKitXOffset;
public int mWebKitYOffset;
public float mWebKitScale;
}
private static final class DispatchEventQueue {
public DispatchEvent mHead;
public DispatchEvent mTail;
public boolean isEmpty() {
return mHead != null;
}
public void enqueue(DispatchEvent d) {
if (mHead == null) {
mHead = d;
mTail = d;
} else {
mTail.mNext = d;
mTail = d;
}
}
public DispatchEvent dequeue() {
DispatchEvent d = mHead;
if (d != null) {
DispatchEvent next = d.mNext;
if (next == null) {
mHead = null;
mTail = null;
} else {
mHead = next;
d.mNext = null;
}
}
return d;
}
public DispatchEvent dequeueList() {
DispatchEvent d = mHead;
if (d != null) {
mHead = null;
mTail = null;
}
return d;
}
}
/**
* Keeps track of a stream of touch events so that we can discard touch
* events that would make the stream inconsistent.
*/
private static final class TouchStream {
private MotionEvent mLastEvent;
/**
* Gets the last touch event that was delivered.
* @return The last touch event, or null if none.
*/
public MotionEvent getLastEvent() {
return mLastEvent;
}
/**
* Updates the touch event stream.
* @param event The event that we intend to send, or null to cancel the
* touch event stream.
* @return The event that we should actually send, or null if no event should
* be sent because the proposed event would make the stream inconsistent.
*/
public MotionEvent update(MotionEvent event) {
if (event == null) {
if (isCancelNeeded()) {
event = mLastEvent;
if (event != null) {
event.setAction(MotionEvent.ACTION_CANCEL);
mLastEvent = null;
}
}
return event;
}
switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
if (mLastEvent == null
|| mLastEvent.getAction() == MotionEvent.ACTION_UP) {
return null;
}
updateLastEvent(event);
return event;
case MotionEvent.ACTION_DOWN:
updateLastEvent(event);
return event;
case MotionEvent.ACTION_CANCEL:
if (mLastEvent == null) {
return null;
}
updateLastEvent(null);
return event;
default:
return null;
}
}
/**
* Returns true if there is a gesture in progress that may need to be canceled.
* @return True if cancel is needed.
*/
public boolean isCancelNeeded() {
return mLastEvent != null && mLastEvent.getAction() != MotionEvent.ACTION_UP;
}
private void updateLastEvent(MotionEvent event) {
if (mLastEvent != null) {
mLastEvent.recycle();
}
mLastEvent = event != null ? MotionEvent.obtainNoHistory(event) : null;
}
}
}
|