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
This commit is contained in:
Joshua Gross
2021-09-24 18:11:33 -07:00
committed by Facebook GitHub Bot
parent f58a3b9ef4
commit 04f746d372
2 changed files with 11 additions and 3 deletions
@@ -56,8 +56,16 @@ public abstract class Event<T extends 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<T extends 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;
}
@@ -96,7 +96,7 @@ public class TouchEvent extends Event<TouchEvent> {
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");