From a9adee648f812b9e32ba39b44062f71531530a49 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Mon, 6 Jul 2020 15:16:14 -0700 Subject: [PATCH] fix lint problems --- .../embedding/android/MotionEventTracker.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java b/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java index b733ed5da0af3..27d3dcff20622 100644 --- a/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java +++ b/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java @@ -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. */ @@ -31,7 +31,8 @@ public long getId() { } } - private final Map eventById; + private final LongSparseArray eventById; + private final PriorityQueue unusedEvents; private static MotionEventTracker INSTANCE; public static MotionEventTracker getInstance() { @@ -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; } @@ -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; } }