Skip to content

Commit

Permalink
fix lint problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaushik Iska committed Jul 6, 2020
1 parent a29148d commit a9adee6
Showing 1 changed file with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.flutter.embedding.android;

import android.util.LongSparseArray;
import android.view.MotionEvent;
import androidx.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.concurrent.atomic.AtomicLong;

/** Tracks the motion events received by the FlutterView. */
Expand Down Expand Up @@ -31,7 +31,8 @@ public long getId() {
}
}

private final Map<Long, MotionEvent> eventById;
private final LongSparseArray<MotionEvent> eventById;
private final PriorityQueue<Long> unusedEvents;
private static MotionEventTracker INSTANCE;

public static MotionEventTracker getInstance() {
Expand All @@ -42,13 +43,15 @@ public static MotionEventTracker getInstance() {
}

private MotionEventTracker() {
eventById = new HashMap<>();
eventById = new LongSparseArray<>();
unusedEvents = new PriorityQueue<>();
}

/** Tracks the event and returns a unique MotionEventId identifying the event. */
public MotionEventId track(MotionEvent event) {
MotionEventId eventId = MotionEventId.createUnique();
eventById.put(eventId.id, event);
unusedEvents.add(eventId.id);
return eventId;
}

Expand All @@ -59,7 +62,18 @@ public MotionEventId track(MotionEvent event) {
*/
@Nullable
public MotionEvent pop(MotionEventId eventId) {
// TODO(kaushikiska) do the actual timestamp based book-keeping.
return eventById.remove(eventId.id);
// remove all the older events.
while (!unusedEvents.isEmpty() && unusedEvents.peek() < eventId.id) {
eventById.remove(unusedEvents.poll());
}

// remove the current event from the heap if it exists.
if (!unusedEvents.isEmpty() && unusedEvents.peek() == eventId.id) {
unusedEvents.poll();
}

MotionEvent event = eventById.get(eventId.id);
eventById.remove(eventId.id);
return event;
}
}

0 comments on commit a9adee6

Please sign in to comment.