From 04f746d372e94496dc6434c987c461a3a3237e02 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 24 Sep 2021 18:09:46 -0700 Subject: [PATCH] Touch events should have same timestmap as native Android MotionEvent Summary: When TouchEvent is created in RN, we're currently using System.currentTimeMillis - but this can differ from the MotionEvent timestamp by a few milliseconds. This difference is very minor but makes it challenging to implement touch telemetry. It's easy and should be zero-impact otherwise to align the timestamps. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D31183733 fbshipit-source-id: 5b275ee534658dc429beb1d3cec0c83a779b5ea3 --- .../com/facebook/react/uimanager/events/Event.java | 12 ++++++++++-- .../facebook/react/uimanager/events/TouchEvent.java | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java index b045cd8984f..909a5644337 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java @@ -56,8 +56,16 @@ public abstract class Event { init(-1, viewTag); } - /** This method needs to be called before event is sent to event dispatcher. */ protected void init(int surfaceId, int viewTag) { + init(surfaceId, viewTag, SystemClock.uptimeMillis()); + } + + /** + * This method needs to be called before event is sent to event dispatcher. Event timestamps can + * optionally be dated/backdated to a custom time: for example, touch events should be dated with + * the system event time. + */ + protected void init(int surfaceId, int viewTag, long timestampMs) { mSurfaceId = surfaceId; mViewTag = viewTag; @@ -73,7 +81,7 @@ public abstract class Event { // At some point it would be great to pass the SurfaceContext here instead. mUIManagerType = (surfaceId == -1 ? UIManagerType.DEFAULT : UIManagerType.FABRIC); - mTimestampMs = SystemClock.uptimeMillis(); + mTimestampMs = timestampMs; mInitialized = true; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java index 554456ccccc..de64e8c8f90 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchEvent.java @@ -96,7 +96,7 @@ public class TouchEvent extends Event { float viewX, float viewY, TouchEventCoalescingKeyHelper touchEventCoalescingKeyHelper) { - super.init(surfaceId, viewTag); + super.init(surfaceId, viewTag, motionEventToCopy.getEventTime()); SoftAssertions.assertCondition( gestureStartTime != UNSET, "Gesture start time must be initialized");