This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Track motion events for reuse post gesture disambiguation #19484
Merged
iskakaushik
merged 3 commits into
flutter:master
from
iskakaushik:android-track-motion-events
Jul 7, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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