Skip to content

Commit d4f433b

Browse files
committed
Fix timestamps on android touch events to use milliseconds, to be consistent with iOS.
1 parent e209f2f commit d4f433b

20 files changed

Lines changed: 57 additions & 53 deletions

File tree

ReactAndroid/src/androidTest/java/com/facebook/react/tests/TextInputTestCase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void testMetionsInputColors() throws Throwable {
116116
eventDispatcher.dispatchEvent(
117117
new ReactTextChangedEvent(
118118
reactEditText.getId(),
119-
SystemClock.nanoTime(),
119+
SystemClock.elapsedRealtime(),
120120
newText.toString(),
121121
(int) PixelUtil.toDIPFromPixel(contentWidth),
122122
(int) PixelUtil.toDIPFromPixel(contentHeight),
@@ -125,7 +125,7 @@ public void testMetionsInputColors() throws Throwable {
125125
eventDispatcher.dispatchEvent(
126126
new ReactTextInputEvent(
127127
reactEditText.getId(),
128-
SystemClock.nanoTime(),
128+
SystemClock.elapsedRealtime(),
129129
newText.toString(),
130130
"",
131131
start,
@@ -150,7 +150,7 @@ public void testMetionsInputColors() throws Throwable {
150150
eventDispatcher.dispatchEvent(
151151
new ReactTextChangedEvent(
152152
reactEditText.getId(),
153-
SystemClock.nanoTime(),
153+
SystemClock.elapsedRealtime(),
154154
newText.toString(),
155155
(int) PixelUtil.toDIPFromPixel(contentWidth),
156156
(int) PixelUtil.toDIPFromPixel(contentHeight),
@@ -159,7 +159,7 @@ public void testMetionsInputColors() throws Throwable {
159159
eventDispatcher.dispatchEvent(
160160
new ReactTextInputEvent(
161161
reactEditText.getId(),
162-
SystemClock.nanoTime(),
162+
SystemClock.elapsedRealtime(),
163163
moreText,
164164
"",
165165
start,
@@ -184,7 +184,7 @@ public void testMetionsInputColors() throws Throwable {
184184
eventDispatcher.dispatchEvent(
185185
new ReactTextChangedEvent(
186186
reactEditText.getId(),
187-
SystemClock.nanoTime(),
187+
SystemClock.elapsedRealtime(),
188188
newText.toString(),
189189
(int) PixelUtil.toDIPFromPixel(contentWidth),
190190
(int) PixelUtil.toDIPFromPixel(contentHeight),
@@ -193,7 +193,7 @@ public void testMetionsInputColors() throws Throwable {
193193
eventDispatcher.dispatchEvent(
194194
new ReactTextInputEvent(
195195
reactEditText.getId(),
196-
SystemClock.nanoTime(),
196+
SystemClock.elapsedRealtime(),
197197
moreText,
198198
"",
199199
start,

ReactAndroid/src/main/java/com/facebook/react/common/SystemClock.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ public static long currentTimeMillis() {
2222
public static long nanoTime() {
2323
return System.nanoTime();
2424
}
25+
26+
public static long elapsedRealtime() {
27+
return android.os.SystemClock.elapsedRealtime();
28+
}
2529
}

ReactAndroid/src/main/java/com/facebook/react/modules/core/Timing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public void createTimer(
244244
return;
245245
}
246246

247-
long initialTargetTime = SystemClock.nanoTime() / 1000000 + adjustedDuration;
247+
long initialTargetTime = SystemClock.elapsedRealtime() + adjustedDuration;
248248
Timer timer = new Timer(executorToken, callbackID, initialTargetTime, duration, repeat);
249249
synchronized (mTimerGuard) {
250250
mTimers.add(timer);

ReactAndroid/src/main/java/com/facebook/react/uimanager/JSTouchDispatcher.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
8282
eventDispatcher.dispatchEvent(
8383
TouchEvent.obtain(
8484
mTargetTag,
85-
SystemClock.nanoTime(),
85+
SystemClock.elapsedRealtime(),
8686
TouchEventType.START,
8787
ev,
8888
mTargetCoordinates[0],
@@ -105,7 +105,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
105105
eventDispatcher.dispatchEvent(
106106
TouchEvent.obtain(
107107
mTargetTag,
108-
SystemClock.nanoTime(),
108+
SystemClock.elapsedRealtime(),
109109
TouchEventType.END,
110110
ev,
111111
mTargetCoordinates[0],
@@ -117,7 +117,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
117117
eventDispatcher.dispatchEvent(
118118
TouchEvent.obtain(
119119
mTargetTag,
120-
SystemClock.nanoTime(),
120+
SystemClock.elapsedRealtime(),
121121
TouchEventType.MOVE,
122122
ev,
123123
mTargetCoordinates[0],
@@ -128,7 +128,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
128128
eventDispatcher.dispatchEvent(
129129
TouchEvent.obtain(
130130
mTargetTag,
131-
SystemClock.nanoTime(),
131+
SystemClock.elapsedRealtime(),
132132
TouchEventType.START,
133133
ev,
134134
mTargetCoordinates[0],
@@ -139,7 +139,7 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
139139
eventDispatcher.dispatchEvent(
140140
TouchEvent.obtain(
141141
mTargetTag,
142-
SystemClock.nanoTime(),
142+
SystemClock.elapsedRealtime(),
143143
TouchEventType.END,
144144
ev,
145145
mTargetCoordinates[0],
@@ -180,7 +180,7 @@ private void dispatchCancelEvent(MotionEvent androidEvent, EventDispatcher event
180180
Assertions.assertNotNull(eventDispatcher).dispatchEvent(
181181
TouchEvent.obtain(
182182
mTargetTag,
183-
SystemClock.nanoTime(),
183+
SystemClock.elapsedRealtime(),
184184
TouchEventType.CANCEL,
185185
androidEvent,
186186
mTargetCoordinates[0],

ReactAndroid/src/main/java/com/facebook/react/uimanager/OnLayoutEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private OnLayoutEvent() {
4545
}
4646

4747
protected void init(int viewTag, int x, int y, int width, int height) {
48-
super.init(viewTag, SystemClock.nanoTime());
48+
super.init(viewTag, SystemClock.elapsedRealtime());
4949
mX = x;
5050
mY = y;
5151
mWidth = width;

ReactAndroid/src/main/java/com/facebook/react/views/drawer/ReactDrawerLayoutManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,25 @@ public DrawerEventEmitter(DrawerLayout drawerLayout, EventDispatcher eventDispat
188188
@Override
189189
public void onDrawerSlide(View view, float v) {
190190
mEventDispatcher.dispatchEvent(
191-
new DrawerSlideEvent(mDrawerLayout.getId(), SystemClock.nanoTime(), v));
191+
new DrawerSlideEvent(mDrawerLayout.getId(), SystemClock.elapsedRealtime(), v));
192192
}
193193

194194
@Override
195195
public void onDrawerOpened(View view) {
196196
mEventDispatcher.dispatchEvent(
197-
new DrawerOpenedEvent(mDrawerLayout.getId(), SystemClock.nanoTime()));
197+
new DrawerOpenedEvent(mDrawerLayout.getId(), SystemClock.elapsedRealtime()));
198198
}
199199

200200
@Override
201201
public void onDrawerClosed(View view) {
202202
mEventDispatcher.dispatchEvent(
203-
new DrawerClosedEvent(mDrawerLayout.getId(), SystemClock.nanoTime()));
203+
new DrawerClosedEvent(mDrawerLayout.getId(), SystemClock.elapsedRealtime()));
204204
}
205205

206206
@Override
207207
public void onDrawerStateChanged(int i) {
208208
mEventDispatcher.dispatchEvent(
209-
new DrawerStateChangedEvent(mDrawerLayout.getId(), SystemClock.nanoTime(), i));
209+
new DrawerStateChangedEvent(mDrawerLayout.getId(), SystemClock.elapsedRealtime(), i));
210210
}
211211
}
212212
}

ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void setShouldNotifyLoadEvents(boolean shouldNotify) {
192192
@Override
193193
public void onSubmit(String id, Object callerContext) {
194194
mEventDispatcher.dispatchEvent(
195-
new ImageLoadEvent(getId(), SystemClock.nanoTime(), ImageLoadEvent.ON_LOAD_START));
195+
new ImageLoadEvent(getId(), SystemClock.elapsedRealtime(), ImageLoadEvent.ON_LOAD_START));
196196
}
197197

198198
@Override
@@ -202,18 +202,18 @@ public void onFinalImageSet(
202202
@Nullable Animatable animatable) {
203203
if (imageInfo != null) {
204204
mEventDispatcher.dispatchEvent(
205-
new ImageLoadEvent(getId(), SystemClock.nanoTime(), ImageLoadEvent.ON_LOAD));
205+
new ImageLoadEvent(getId(), SystemClock.elapsedRealtime(), ImageLoadEvent.ON_LOAD));
206206
mEventDispatcher.dispatchEvent(
207-
new ImageLoadEvent(getId(), SystemClock.nanoTime(), ImageLoadEvent.ON_LOAD_END));
207+
new ImageLoadEvent(getId(), SystemClock.elapsedRealtime(), ImageLoadEvent.ON_LOAD_END));
208208
}
209209
}
210210

211211
@Override
212212
public void onFailure(String id, Throwable throwable) {
213213
mEventDispatcher.dispatchEvent(
214-
new ImageLoadEvent(getId(), SystemClock.nanoTime(), ImageLoadEvent.ON_ERROR));
214+
new ImageLoadEvent(getId(), SystemClock.elapsedRealtime(), ImageLoadEvent.ON_ERROR));
215215
mEventDispatcher.dispatchEvent(
216-
new ImageLoadEvent(getId(), SystemClock.nanoTime(), ImageLoadEvent.ON_LOAD_END));
216+
new ImageLoadEvent(getId(), SystemClock.elapsedRealtime(), ImageLoadEvent.ON_LOAD_END));
217217
}
218218
};
219219
}

ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ protected void addEventEmitters(
8585
new ReactModalHostView.OnRequestCloseListener() {
8686
@Override
8787
public void onRequestClose(DialogInterface dialog) {
88-
dispatcher.dispatchEvent(new RequestCloseEvent(view.getId(), SystemClock.nanoTime()));
88+
dispatcher.dispatchEvent(new RequestCloseEvent(view.getId(), SystemClock.elapsedRealtime()));
8989
}
9090
});
9191
view.setOnShowListener(
9292
new DialogInterface.OnShowListener() {
9393
@Override
9494
public void onShow(DialogInterface dialog) {
95-
dispatcher.dispatchEvent(new ShowEvent(view.getId(), SystemClock.nanoTime()));
95+
dispatcher.dispatchEvent(new ShowEvent(view.getId(), SystemClock.elapsedRealtime()));
9696
}
9797
});
9898
}

ReactAndroid/src/main/java/com/facebook/react/views/picker/ReactPickerManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public PickerEventEmitter(ReactPicker reactPicker, EventDispatcher eventDispatch
157157
@Override
158158
public void onItemSelected(int position) {
159159
mEventDispatcher.dispatchEvent( new PickerItemSelectEvent(
160-
mReactPicker.getId(), SystemClock.nanoTime(), position));
160+
mReactPicker.getId(), SystemClock.elapsedRealtime(), position));
161161
}
162162
}
163163
}

ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ protected void onScrollChanged(int l, int t, int oldl, int oldt) {
344344
((ReactContext) getContext()).getNativeModule(UIManagerModule.class).getEventDispatcher()
345345
.dispatchEvent(ScrollEvent.obtain(
346346
getId(),
347-
SystemClock.nanoTime(),
347+
SystemClock.elapsedRealtime(),
348348
ScrollEventType.SCROLL,
349349
0, /* offsetX = 0, horizontal scrolling only */
350350
calculateAbsoluteOffset(),
@@ -359,7 +359,7 @@ private void onTotalChildrenHeightChange(int newTotalChildrenHeight) {
359359
((ReactContext) getContext()).getNativeModule(UIManagerModule.class).getEventDispatcher()
360360
.dispatchEvent(new ContentSizeChangeEvent(
361361
getId(),
362-
SystemClock.nanoTime(),
362+
SystemClock.elapsedRealtime(),
363363
getWidth(),
364364
newTotalChildrenHeight));
365365
}

0 commit comments

Comments
 (0)