-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track motion events for reuse post gesture disambiguation #19484
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.flutter.embedding.android; | ||
|
||
import android.util.LongSparseArray; | ||
import android.view.MotionEvent; | ||
import androidx.annotation.Nullable; | ||
import java.util.PriorityQueue; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** Tracks the motion events received by the FlutterView. */ | ||
public final class MotionEventTracker { | ||
|
||
/** Represents a unique identifier corresponding to a motion event. */ | ||
public static class MotionEventId { | ||
private static final AtomicLong ID_COUNTER = new AtomicLong(0); | ||
private final long id; | ||
|
||
private MotionEventId(long id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would this wrapper object provide something that just the return value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not much really, except type safety. If you feel strongly about this, I can move to atomic long. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I don't feel strongly about it, so your call! |
||
this.id = id; | ||
} | ||
|
||
public static MotionEventId from(long id) { | ||
return new MotionEventId(id); | ||
} | ||
|
||
public static MotionEventId createUnique() { | ||
return MotionEventId.from(ID_COUNTER.incrementAndGet()); | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
} | ||
|
||
private final LongSparseArray<MotionEvent> eventById; | ||
private final PriorityQueue<Long> unusedEvents; | ||
private static MotionEventTracker INSTANCE; | ||
|
||
public static MotionEventTracker getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new MotionEventTracker(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
private MotionEventTracker() { | ||
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; | ||
} | ||
|
||
/** | ||
* Returns the MotionEvent corresponding to the eventId while discarding all the motion events | ||
* that occured prior to the event represented by the eventId. Returns null if this event was | ||
* popped or discarded. | ||
*/ | ||
@Nullable | ||
public MotionEvent pop(MotionEventId eventId) { | ||
// 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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you know if the other embeddings have to be updated as well? I wonder if adding the field to the end is safer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, this field will be set to 0 for other embeddings and nothing else needs to change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SG