forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track motion events for reuse post gesture disambiguation (flutter#19484
) This change makes it so that we track all the motion events encountered by `FlutterView` and all of its subviews in the `MotionEventTracker` class, indexed by a unique `MotionEventId`. This identifier is then passed to the Flutter framework as seen in flutter/flutter#60930. Once the gestures take part in gesture disambiguation and are sent back to the engine, we look-up the original motion event using the `MotionEventId` and dispatch it to the platform. Bug: flutter/flutter#58837
- Loading branch information
1 parent
f1056b4
commit 01c8ecb
Showing
11 changed files
with
165 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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) { | ||
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 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 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 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