Skip to content

Commit 69feed5

Browse files
JoshuaGrossfacebook-github-bot
authored andcommitted
Send UNIX timestamp along with JS touch events, instead of systemUptime
Summary: We want to be able to instrument touch processing delays in JS, which does not have access to systemUptime; therefore we want a UNIX timestamp, which JS has access to and can compare to the touch time. It only matters that there is relative consistency between multiple touch events in JS, which is still the case; so this should have no impact on product code. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D26705429 fbshipit-source-id: 0f2db726048fcab9a30e830970d7d8a8d2eae446
1 parent f276214 commit 69feed5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public abstract class Event<T extends Event> {
3838
private long mTimestampMs;
3939
private int mUniqueID = sUniqueID++;
4040

41+
// Android native Event times use 'uptimeMillis', and historically we've used `uptimeMillis`
42+
// throughout this Event class as the coalescing key for events, and for other purposes.
43+
// To get an accurate(ish) absolute UNIX time for the event, we store the initial clock time here.
44+
// uptimeMillis can then be added to this to get an accurate UNIX time.
45+
// However, we still default to uptimeMillis: you must explicitly request UNIX time if you want
46+
// that; see `getUnixTimestampMs`.
47+
public static final long sInitialClockTimeUnixOffset =
48+
SystemClock.currentTimeMillis() - SystemClock.uptimeMillis();
49+
4150
protected Event() {}
4251

4352
@Deprecated
@@ -58,8 +67,10 @@ protected void init(int viewTag) {
5867
protected void init(int surfaceId, int viewTag) {
5968
mSurfaceId = surfaceId;
6069
mViewTag = viewTag;
61-
mTimestampMs = SystemClock.uptimeMillis();
6270
mInitialized = true;
71+
72+
// This is a *relative* time. See `getUnixTimestampMs`.
73+
mTimestampMs = SystemClock.uptimeMillis();
6374
}
6475

6576
/** @return the view id for the view that generated this event */
@@ -80,6 +91,11 @@ public final long getTimestampMs() {
8091
return mTimestampMs;
8192
}
8293

94+
/** @return the time at which the event happened as a UNIX timestamp, in milliseconds. */
95+
public final long getUnixTimestampMs() {
96+
return sInitialClockTimeUnixOffset + mTimestampMs;
97+
}
98+
8399
/** @return false if this Event can *never* be coalesced */
84100
public boolean canCoalesce() {
85101
return true;

ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchesHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static WritableArray createsPointersArray(
6464
touch.putDouble(LOCATION_Y_KEY, PixelUtil.toDIPFromPixel(locationY));
6565
touch.putInt(TARGET_SURFACE_KEY, surfaceId);
6666
touch.putInt(TARGET_KEY, reactTarget);
67-
touch.putDouble(TIMESTAMP_KEY, event.getTimestampMs());
67+
touch.putDouble(TIMESTAMP_KEY, event.getUnixTimestampMs());
6868
touch.putDouble(POINTER_IDENTIFIER_KEY, motionEvent.getPointerId(index));
6969
touches.pushMap(touch);
7070
}

0 commit comments

Comments
 (0)