1
1
package io .flutter .embedding .android ;
2
2
3
+ import android .util .LongSparseArray ;
3
4
import android .view .MotionEvent ;
4
5
import androidx .annotation .Nullable ;
5
- import java .util .HashMap ;
6
- import java .util .Map ;
6
+ import java .util .PriorityQueue ;
7
7
import java .util .concurrent .atomic .AtomicLong ;
8
8
9
9
/** Tracks the motion events received by the FlutterView. */
@@ -31,7 +31,8 @@ public long getId() {
31
31
}
32
32
}
33
33
34
- private final Map <Long , MotionEvent > eventById ;
34
+ private final LongSparseArray <MotionEvent > eventById ;
35
+ private final PriorityQueue <Long > unusedEvents ;
35
36
private static MotionEventTracker INSTANCE ;
36
37
37
38
public static MotionEventTracker getInstance () {
@@ -42,13 +43,15 @@ public static MotionEventTracker getInstance() {
42
43
}
43
44
44
45
private MotionEventTracker () {
45
- eventById = new HashMap <>();
46
+ eventById = new LongSparseArray <>();
47
+ unusedEvents = new PriorityQueue <>();
46
48
}
47
49
48
50
/** Tracks the event and returns a unique MotionEventId identifying the event. */
49
51
public MotionEventId track (MotionEvent event ) {
50
52
MotionEventId eventId = MotionEventId .createUnique ();
51
53
eventById .put (eventId .id , event );
54
+ unusedEvents .add (eventId .id );
52
55
return eventId ;
53
56
}
54
57
@@ -59,7 +62,18 @@ public MotionEventId track(MotionEvent event) {
59
62
*/
60
63
@ Nullable
61
64
public MotionEvent pop (MotionEventId eventId ) {
62
- // TODO(kaushikiska) do the actual timestamp based book-keeping.
63
- return eventById .remove (eventId .id );
65
+ // remove all the older events.
66
+ while (!unusedEvents .isEmpty () && unusedEvents .peek () < eventId .id ) {
67
+ eventById .remove (unusedEvents .poll ());
68
+ }
69
+
70
+ // remove the current event from the heap if it exists.
71
+ if (!unusedEvents .isEmpty () && unusedEvents .peek () == eventId .id ) {
72
+ unusedEvents .poll ();
73
+ }
74
+
75
+ MotionEvent event = eventById .get (eventId .id );
76
+ eventById .remove (eventId .id );
77
+ return event ;
64
78
}
65
79
}
0 commit comments