Skip to content

Commit 837aafd

Browse files
olinotteghemFacebook Github Bot 9
authored andcommitted
Fix touch handling crash that can when Modal is displayed as a result of touch handling code
Reviewed By: dmmiller Differential Revision: D3399325 fbshipit-source-id: 0a0bb179ef524bf35cc137d94ea3d5680a68aab2
1 parent f3fab51 commit 837aafd

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class JSTouchDispatcher {
3232
private final float[] mTargetCoordinates = new float[2];
3333
private boolean mChildIsHandlingNativeGesture = false;
3434
private final ViewGroup mRootViewGroup;
35+
private final TouchEventCoalescingKeyHelper mTouchEventCoalescingKeyHelper =
36+
new TouchEventCoalescingKeyHelper();
3537

3638
public JSTouchDispatcher(ViewGroup viewGroup) {
3739
mRootViewGroup = viewGroup;
@@ -83,7 +85,8 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
8385
TouchEventType.START,
8486
ev,
8587
mTargetCoordinates[0],
86-
mTargetCoordinates[1]));
88+
mTargetCoordinates[1],
89+
mTouchEventCoalescingKeyHelper));
8790
} else if (mChildIsHandlingNativeGesture) {
8891
// If the touch was intercepted by a child, we've already sent a cancel event to JS for this
8992
// gesture, so we shouldn't send any more touches related to it.
@@ -105,7 +108,8 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
105108
TouchEventType.END,
106109
ev,
107110
mTargetCoordinates[0],
108-
mTargetCoordinates[1]));
111+
mTargetCoordinates[1],
112+
mTouchEventCoalescingKeyHelper));
109113
mTargetTag = -1;
110114
} else if (action == MotionEvent.ACTION_MOVE) {
111115
// Update pointer position for current gesture
@@ -116,7 +120,8 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
116120
TouchEventType.MOVE,
117121
ev,
118122
mTargetCoordinates[0],
119-
mTargetCoordinates[1]));
123+
mTargetCoordinates[1],
124+
mTouchEventCoalescingKeyHelper));
120125
} else if (action == MotionEvent.ACTION_POINTER_DOWN) {
121126
// New pointer goes down, this can only happen after ACTION_DOWN is sent for the first pointer
122127
eventDispatcher.dispatchEvent(
@@ -126,7 +131,8 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
126131
TouchEventType.START,
127132
ev,
128133
mTargetCoordinates[0],
129-
mTargetCoordinates[1]));
134+
mTargetCoordinates[1],
135+
mTouchEventCoalescingKeyHelper));
130136
} else if (action == MotionEvent.ACTION_POINTER_UP) {
131137
// Exactly onw of the pointers goes up
132138
eventDispatcher.dispatchEvent(
@@ -136,9 +142,10 @@ public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
136142
TouchEventType.END,
137143
ev,
138144
mTargetCoordinates[0],
139-
mTargetCoordinates[1]));
145+
mTargetCoordinates[1],
146+
mTouchEventCoalescingKeyHelper));
140147
} else if (action == MotionEvent.ACTION_CANCEL) {
141-
if (TouchEventCoalescingKeyHelper.hasCoalescingKey(ev.getDownTime())) {
148+
if (mTouchEventCoalescingKeyHelper.hasCoalescingKey(ev.getDownTime())) {
142149
dispatchCancelEvent(ev, eventDispatcher);
143150
} else {
144151
FLog.e(
@@ -176,6 +183,7 @@ private void dispatchCancelEvent(MotionEvent androidEvent, EventDispatcher event
176183
TouchEventType.CANCEL,
177184
androidEvent,
178185
mTargetCoordinates[0],
179-
mTargetCoordinates[1]));
186+
mTargetCoordinates[1],
187+
mTouchEventCoalescingKeyHelper));
180188
}
181189
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ public static TouchEvent obtain(
3737
TouchEventType touchEventType,
3838
MotionEvent motionEventToCopy,
3939
float viewX,
40-
float viewY) {
40+
float viewY,
41+
TouchEventCoalescingKeyHelper touchEventCoalescingKeyHelper) {
4142
TouchEvent event = EVENTS_POOL.acquire();
4243
if (event == null) {
4344
event = new TouchEvent();
4445
}
45-
event.init(viewTag, timestampMs, touchEventType, motionEventToCopy, viewX, viewY);
46+
event.init(
47+
viewTag,
48+
timestampMs,
49+
touchEventType,
50+
motionEventToCopy,
51+
viewX,
52+
viewY,
53+
touchEventCoalescingKeyHelper);
4654
return event;
4755
}
4856

@@ -63,28 +71,29 @@ private void init(
6371
TouchEventType touchEventType,
6472
MotionEvent motionEventToCopy,
6573
float viewX,
66-
float viewY) {
74+
float viewY,
75+
TouchEventCoalescingKeyHelper touchEventCoalescingKeyHelper) {
6776
super.init(viewTag, timestampMs);
6877

6978
short coalescingKey = 0;
7079
int action = (motionEventToCopy.getAction() & MotionEvent.ACTION_MASK);
7180
switch (action) {
7281
case MotionEvent.ACTION_DOWN:
73-
TouchEventCoalescingKeyHelper.addCoalescingKey(motionEventToCopy.getDownTime());
82+
touchEventCoalescingKeyHelper.addCoalescingKey(motionEventToCopy.getDownTime());
7483
break;
7584
case MotionEvent.ACTION_UP:
76-
TouchEventCoalescingKeyHelper.removeCoalescingKey(motionEventToCopy.getDownTime());
85+
touchEventCoalescingKeyHelper.removeCoalescingKey(motionEventToCopy.getDownTime());
7786
break;
7887
case MotionEvent.ACTION_POINTER_DOWN:
7988
case MotionEvent.ACTION_POINTER_UP:
80-
TouchEventCoalescingKeyHelper.incrementCoalescingKey(motionEventToCopy.getDownTime());
89+
touchEventCoalescingKeyHelper.incrementCoalescingKey(motionEventToCopy.getDownTime());
8190
break;
8291
case MotionEvent.ACTION_MOVE:
8392
coalescingKey =
84-
TouchEventCoalescingKeyHelper.getCoalescingKey(motionEventToCopy.getDownTime());
93+
touchEventCoalescingKeyHelper.getCoalescingKey(motionEventToCopy.getDownTime());
8594
break;
8695
case MotionEvent.ACTION_CANCEL:
87-
TouchEventCoalescingKeyHelper.removeCoalescingKey(motionEventToCopy.getDownTime());
96+
touchEventCoalescingKeyHelper.removeCoalescingKey(motionEventToCopy.getDownTime());
8897
break;
8998
default:
9099
throw new RuntimeException("Unhandled MotionEvent action: " + action);

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,31 @@
4646
*/
4747
public class TouchEventCoalescingKeyHelper {
4848

49-
private static final SparseIntArray sDownTimeToCoalescingKey = new SparseIntArray();
49+
private final SparseIntArray mDownTimeToCoalescingKey = new SparseIntArray();
5050

5151
/**
5252
* Starts tracking a new coalescing key corresponding to the gesture with this down time.
5353
*/
54-
public static void addCoalescingKey(long downTime) {
55-
sDownTimeToCoalescingKey.put((int) downTime, 0);
54+
public void addCoalescingKey(long downTime) {
55+
mDownTimeToCoalescingKey.put((int) downTime, 0);
5656
}
5757

5858
/**
5959
* Increments the coalescing key corresponding to the gesture with this down time.
6060
*/
61-
public static void incrementCoalescingKey(long downTime) {
62-
int currentValue = sDownTimeToCoalescingKey.get((int) downTime, -1);
61+
public void incrementCoalescingKey(long downTime) {
62+
int currentValue = mDownTimeToCoalescingKey.get((int) downTime, -1);
6363
if (currentValue == -1) {
6464
throw new RuntimeException("Tried to increment non-existent cookie");
6565
}
66-
sDownTimeToCoalescingKey.put((int) downTime, currentValue + 1);
66+
mDownTimeToCoalescingKey.put((int) downTime, currentValue + 1);
6767
}
6868

6969
/**
7070
* Gets the coalescing key corresponding to the gesture with this down time.
7171
*/
72-
public static short getCoalescingKey(long downTime) {
73-
int currentValue = sDownTimeToCoalescingKey.get((int) downTime, -1);
72+
public short getCoalescingKey(long downTime) {
73+
int currentValue = mDownTimeToCoalescingKey.get((int) downTime, -1);
7474
if (currentValue == -1) {
7575
throw new RuntimeException("Tried to get non-existent cookie");
7676
}
@@ -80,12 +80,12 @@ public static short getCoalescingKey(long downTime) {
8080
/**
8181
* Stops tracking a new coalescing key corresponding to the gesture with this down time.
8282
*/
83-
public static void removeCoalescingKey(long downTime) {
84-
sDownTimeToCoalescingKey.delete((int) downTime);
83+
public void removeCoalescingKey(long downTime) {
84+
mDownTimeToCoalescingKey.delete((int) downTime);
8585
}
8686

87-
public static boolean hasCoalescingKey(long downTime) {
88-
int currentValue = sDownTimeToCoalescingKey.get((int) downTime, -1);
87+
public boolean hasCoalescingKey(long downTime) {
88+
int currentValue = mDownTimeToCoalescingKey.get((int) downTime, -1);
8989
if (currentValue == -1) {
9090
return false;
9191
}

0 commit comments

Comments
 (0)