From 27015c8820d67438b263cd9c48064ffed30e10e8 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Mon, 6 Jul 2020 14:37:09 -0700 Subject: [PATCH] Take the events back from framework --- .../embedding/android/MotionEventTracker.java | 24 +++---------------- .../systemchannels/PlatformViewsChannel.java | 9 +++++-- .../platform/PlatformViewsController.java | 18 ++++++++++++-- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java b/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java index cacd3e18c9f06..b733ed5da0af3 100644 --- a/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java +++ b/shell/platform/android/io/flutter/embedding/android/MotionEventTracker.java @@ -26,30 +26,12 @@ public static MotionEventId createUnique() { return MotionEventId.from(ID_COUNTER.incrementAndGet()); } - public boolean equals(Object object) { - if (this == object) return true; - if (object == null || getClass() != object.getClass()) return false; - if (!super.equals(object)) return false; - - MotionEventId that = (MotionEventId) object; - - if (id != that.id) return false; - - return true; - } - - public int hashCode() { - int result = super.hashCode(); - result = 31 * result + (int) (id ^ (id >>> 32)); - return result; - } - public long getId() { return id; } } - private final Map eventById; + private final Map eventById; private static MotionEventTracker INSTANCE; public static MotionEventTracker getInstance() { @@ -66,7 +48,7 @@ private MotionEventTracker() { /** Tracks the event and returns a unique MotionEventId identifying the event. */ public MotionEventId track(MotionEvent event) { MotionEventId eventId = MotionEventId.createUnique(); - eventById.put(eventId, event); + eventById.put(eventId.id, event); return eventId; } @@ -78,6 +60,6 @@ public MotionEventId track(MotionEvent event) { @Nullable public MotionEvent pop(MotionEventId eventId) { // TODO(kaushikiska) do the actual timestamp based book-keeping. - return eventById.remove(eventId); + return eventById.remove(eventId.id); } } diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java index f5b5e51fdf873..3f238c98d99a0 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java @@ -166,7 +166,8 @@ private void touch(@NonNull MethodCall call, @NonNull MethodChannel.Result resul (int) args.get(11), (int) args.get(12), (int) args.get(13), - (int) args.get(14)); + (int) args.get(14), + ((Number) args.get(15)).longValue()); try { handler.onTouch(touch); @@ -380,6 +381,8 @@ public static class PlatformViewTouch { public final int source; /** TODO(mattcarroll): javadoc */ public final int flags; + /** TODO(iskakaushik): javadoc */ + public final long motionEventId; PlatformViewTouch( int viewId, @@ -396,7 +399,8 @@ public static class PlatformViewTouch { int deviceId, int edgeFlags, int source, - int flags) { + int flags, + long motionEventId) { this.viewId = viewId; this.downTime = downTime; this.eventTime = eventTime; @@ -412,6 +416,7 @@ public static class PlatformViewTouch { this.edgeFlags = edgeFlags; this.source = source; this.flags = flags; + this.motionEventId = motionEventId; } } } diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index 91ca791bdee94..928c89af4bb5a 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -21,6 +21,7 @@ import androidx.annotation.VisibleForTesting; import io.flutter.embedding.android.FlutterImageView; import io.flutter.embedding.android.FlutterView; +import io.flutter.embedding.android.MotionEventTracker; import io.flutter.embedding.engine.FlutterOverlaySurface; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.mutatorsstack.*; @@ -93,6 +94,9 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega // Platform view IDs that were displayed since the start of the current frame. private HashSet currentFrameUsedPlatformViewIds; + // Used to acquire the original motion events using the motionEventIds. + private final MotionEventTracker motionEventTracker; + private final PlatformViewsChannel.PlatformViewsHandler channelHandler = new PlatformViewsChannel.PlatformViewsHandler() { @@ -301,8 +305,16 @@ private void ensureValidAndroidVersion(int minSdkVersion) { } }; - private static MotionEvent toMotionEvent( - float density, PlatformViewsChannel.PlatformViewTouch touch) { + private MotionEvent toMotionEvent(float density, PlatformViewsChannel.PlatformViewTouch touch) { + MotionEventTracker.MotionEventId motionEventId = + MotionEventTracker.MotionEventId.from(touch.motionEventId); + MotionEvent trackedEvent = motionEventTracker.pop(motionEventId); + if (trackedEvent != null) { + return trackedEvent; + } + + // TODO (kaushikiska) : warn that we are potentially using an untracked + // event in the platform views. PointerProperties[] pointerProperties = parsePointerPropertiesList(touch.rawPointerPropertiesList) .toArray(new PointerProperties[touch.pointerCount]); @@ -339,6 +351,8 @@ public PlatformViewsController() { platformViewRequests = new SparseArray<>(); platformViews = new SparseArray<>(); mutatorViews = new SparseArray<>(); + + motionEventTracker = MotionEventTracker.getInstance(); } /**