-
-
Notifications
You must be signed in to change notification settings - Fork 955
/
RNGestureHandlerModule.java
734 lines (660 loc) · 29.3 KB
/
RNGestureHandlerModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package com.swmansion.gesturehandler.react;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.react.ReactRootView;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.common.SystemClock;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIBlock;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.swmansion.gesturehandler.FlingGestureHandler;
import com.swmansion.gesturehandler.GestureHandler;
import com.swmansion.gesturehandler.LongPressGestureHandler;
import com.swmansion.gesturehandler.NativeViewGestureHandler;
import com.swmansion.gesturehandler.OnTouchEventListener;
import com.swmansion.gesturehandler.PanGestureHandler;
import com.swmansion.gesturehandler.PinchGestureHandler;
import com.swmansion.gesturehandler.RotationGestureHandler;
import com.swmansion.gesturehandler.TapGestureHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import androidx.annotation.Nullable;
import static com.swmansion.gesturehandler.GestureHandler.HIT_SLOP_NONE;
@ReactModule(name=RNGestureHandlerModule.MODULE_NAME)
public class RNGestureHandlerModule extends ReactContextBaseJavaModule {
public static final String MODULE_NAME = "RNGestureHandlerModule";
private static final String KEY_SHOULD_CANCEL_WHEN_OUTSIDE = "shouldCancelWhenOutside";
private static final String KEY_ENABLED = "enabled";
private static final String KEY_HIT_SLOP = "hitSlop";
private static final String KEY_HIT_SLOP_LEFT = "left";
private static final String KEY_HIT_SLOP_TOP = "top";
private static final String KEY_HIT_SLOP_RIGHT = "right";
private static final String KEY_HIT_SLOP_BOTTOM = "bottom";
private static final String KEY_HIT_SLOP_VERTICAL = "vertical";
private static final String KEY_HIT_SLOP_HORIZONTAL = "horizontal";
private static final String KEY_HIT_SLOP_WIDTH = "width";
private static final String KEY_HIT_SLOP_HEIGHT = "height";
private static final String KEY_NATIVE_VIEW_SHOULD_ACTIVATE_ON_START = "shouldActivateOnStart";
private static final String KEY_NATIVE_VIEW_DISALLOW_INTERRUPTION = "disallowInterruption";
private static final String KEY_TAP_NUMBER_OF_TAPS = "numberOfTaps";
private static final String KEY_TAP_MAX_DURATION_MS = "maxDurationMs";
private static final String KEY_TAP_MAX_DELAY_MS = "maxDelayMs";
private static final String KEY_TAP_MAX_DELTA_X = "maxDeltaX";
private static final String KEY_TAP_MAX_DELTA_Y = "maxDeltaY";
private static final String KEY_TAP_MAX_DIST = "maxDist";
private static final String KEY_TAP_MIN_POINTERS = "minPointers";
private static final String KEY_LONG_PRESS_MIN_DURATION_MS = "minDurationMs";
private static final String KEY_LONG_PRESS_MAX_DIST = "maxDist";
private static final String KEY_PAN_ACTIVE_OFFSET_X_START = "activeOffsetXStart";
private static final String KEY_PAN_ACTIVE_OFFSET_X_END = "activeOffsetXEnd";
private static final String KEY_PAN_FAIL_OFFSET_RANGE_X_START = "failOffsetXStart";
private static final String KEY_PAN_FAIL_OFFSET_RANGE_X_END = "failOffsetXEnd";
private static final String KEY_PAN_ACTIVE_OFFSET_Y_START = "activeOffsetYStart";
private static final String KEY_PAN_ACTIVE_OFFSET_Y_END = "activeOffsetYEnd";
private static final String KEY_PAN_FAIL_OFFSET_RANGE_Y_START = "failOffsetYStart";
private static final String KEY_PAN_FAIL_OFFSET_RANGE_Y_END = "failOffsetYEnd";
private static final String KEY_PAN_MIN_DIST = "minDist";
private static final String KEY_PAN_MIN_VELOCITY = "minVelocity";
private static final String KEY_PAN_MIN_VELOCITY_X = "minVelocityX";
private static final String KEY_PAN_MIN_VELOCITY_Y = "minVelocityY";
private static final String KEY_PAN_MIN_POINTERS = "minPointers";
private static final String KEY_PAN_MAX_POINTERS = "maxPointers";
private static final String KEY_PAN_AVG_TOUCHES = "avgTouches";
private static final String KEY_NUMBER_OF_POINTERS = "numberOfPointers";
private static final String KEY_DIRECTION= "direction";
private abstract static class HandlerFactory<T extends GestureHandler<T>>
implements RNGestureHandlerEventDataExtractor<T> {
public abstract Class<T> getType();
public abstract String getName();
public abstract T create(Context context);
public void configure(T handler, ReadableMap config) {
handler.resetConfig();
if (config.hasKey(KEY_SHOULD_CANCEL_WHEN_OUTSIDE)) {
handler.setShouldCancelWhenOutside(config.getBoolean(KEY_SHOULD_CANCEL_WHEN_OUTSIDE));
}
if (config.hasKey(KEY_ENABLED)) {
handler.setEnabled(config.getBoolean(KEY_ENABLED));
}
if (config.hasKey(KEY_HIT_SLOP)) {
handleHitSlopProperty(handler, config);
}
}
@Override
public void extractEventData(T handler, WritableMap eventData) {
eventData.putDouble("numberOfPointers", handler.getNumberOfPointers());
}
}
private static class NativeViewGestureHandlerFactory extends
HandlerFactory<NativeViewGestureHandler> {
@Override
public Class<NativeViewGestureHandler> getType() {
return NativeViewGestureHandler.class;
}
@Override
public String getName() {
return "NativeViewGestureHandler";
}
@Override
public NativeViewGestureHandler create(Context context) {
return new NativeViewGestureHandler();
}
@Override
public void configure(NativeViewGestureHandler handler, ReadableMap config) {
super.configure(handler, config);
if (config.hasKey(KEY_NATIVE_VIEW_SHOULD_ACTIVATE_ON_START)) {
handler.setShouldActivateOnStart(
config.getBoolean(KEY_NATIVE_VIEW_SHOULD_ACTIVATE_ON_START));
}
if (config.hasKey(KEY_NATIVE_VIEW_DISALLOW_INTERRUPTION)) {
handler.setDisallowInterruption(config.getBoolean(KEY_NATIVE_VIEW_DISALLOW_INTERRUPTION));
}
}
@Override
public void extractEventData(NativeViewGestureHandler handler, WritableMap eventData) {
super.extractEventData(handler, eventData);
eventData.putBoolean("pointerInside", handler.isWithinBounds());
}
}
private static class TapGestureHandlerFactory extends HandlerFactory<TapGestureHandler> {
@Override
public Class<TapGestureHandler> getType() {
return TapGestureHandler.class;
}
@Override
public String getName() {
return "TapGestureHandler";
}
@Override
public TapGestureHandler create(Context context) {
return new TapGestureHandler();
}
@Override
public void configure(TapGestureHandler handler, ReadableMap config) {
super.configure(handler, config);
if (config.hasKey(KEY_TAP_NUMBER_OF_TAPS)) {
handler.setNumberOfTaps(config.getInt(KEY_TAP_NUMBER_OF_TAPS));
}
if (config.hasKey(KEY_TAP_MAX_DURATION_MS)) {
handler.setMaxDurationMs(config.getInt(KEY_TAP_MAX_DURATION_MS));
}
if (config.hasKey(KEY_TAP_MAX_DELAY_MS)) {
handler.setMaxDelayMs(config.getInt(KEY_TAP_MAX_DELAY_MS));
}
if (config.hasKey(KEY_TAP_MAX_DELTA_X)) {
handler.setMaxDx(PixelUtil.toPixelFromDIP(config.getDouble(KEY_TAP_MAX_DELTA_X)));
}
if (config.hasKey(KEY_TAP_MAX_DELTA_Y)) {
handler.setMaxDy(PixelUtil.toPixelFromDIP(config.getDouble(KEY_TAP_MAX_DELTA_Y)));
}
if (config.hasKey(KEY_TAP_MAX_DIST)) {
handler.setMaxDist(PixelUtil.toPixelFromDIP(config.getDouble(KEY_TAP_MAX_DIST)));
}
if (config.hasKey(KEY_TAP_MIN_POINTERS)) {
handler.setMinNumberOfPointers(config.getInt(KEY_TAP_MIN_POINTERS));
}
}
@Override
public void extractEventData(TapGestureHandler handler, WritableMap eventData) {
super.extractEventData(handler, eventData);
eventData.putDouble("x", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionX()));
eventData.putDouble("y", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionY()));
eventData.putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionX()));
eventData.putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionY()));
}
}
private static class LongPressGestureHandlerFactory extends
HandlerFactory<LongPressGestureHandler> {
@Override
public Class<LongPressGestureHandler> getType() {
return LongPressGestureHandler.class;
}
@Override
public String getName() {
return "LongPressGestureHandler";
}
@Override
public LongPressGestureHandler create(Context context) {
return new LongPressGestureHandler(context);
}
@Override
public void configure(LongPressGestureHandler handler, ReadableMap config) {
super.configure(handler, config);
if (config.hasKey(KEY_LONG_PRESS_MIN_DURATION_MS)) {
handler.setMinDurationMs(config.getInt(KEY_LONG_PRESS_MIN_DURATION_MS));
}
if (config.hasKey(KEY_LONG_PRESS_MAX_DIST)) {
handler.setMaxDist(PixelUtil.toPixelFromDIP(config.getDouble(KEY_LONG_PRESS_MAX_DIST)));
}
}
@Override
public void extractEventData(LongPressGestureHandler handler, WritableMap eventData) {
super.extractEventData(handler, eventData);
eventData.putDouble("x", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionX()));
eventData.putDouble("y", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionY()));
eventData.putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionX()));
eventData.putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionY()));
eventData.putInt("duration", handler.getDuration());
}
}
private static class PanGestureHandlerFactory extends HandlerFactory<PanGestureHandler> {
@Override
public Class<PanGestureHandler> getType() {
return PanGestureHandler.class;
}
@Override
public String getName() {
return "PanGestureHandler";
}
@Override
public PanGestureHandler create(Context context) {
return new PanGestureHandler(context);
}
@Override
public void configure(PanGestureHandler handler, ReadableMap config) {
super.configure(handler, config);
boolean hasCustomActivationCriteria = false;
if(config.hasKey(KEY_PAN_ACTIVE_OFFSET_X_START)) {
handler.setActiveOffsetXStart(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_ACTIVE_OFFSET_X_START)));
hasCustomActivationCriteria = true;
}
if(config.hasKey(KEY_PAN_ACTIVE_OFFSET_X_END)) {
handler.setActiveOffsetXEnd(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_ACTIVE_OFFSET_X_END)));
hasCustomActivationCriteria = true;
}
if(config.hasKey(KEY_PAN_FAIL_OFFSET_RANGE_X_START)) {
handler.setFailOffsetXStart(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_FAIL_OFFSET_RANGE_X_START)));
hasCustomActivationCriteria = true;
}
if(config.hasKey(KEY_PAN_FAIL_OFFSET_RANGE_X_END)) {
handler.setFailOffsetXEnd(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_FAIL_OFFSET_RANGE_X_END)));
hasCustomActivationCriteria = true;
}
if(config.hasKey(KEY_PAN_ACTIVE_OFFSET_Y_START)) {
handler.setActiveOffsetYStart(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_ACTIVE_OFFSET_Y_START)));
hasCustomActivationCriteria = true;
}
if(config.hasKey(KEY_PAN_ACTIVE_OFFSET_Y_END)) {
handler.setActiveOffsetYEnd(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_ACTIVE_OFFSET_Y_END)));
hasCustomActivationCriteria = true;
}
if(config.hasKey(KEY_PAN_FAIL_OFFSET_RANGE_Y_START)) {
handler.setFailOffsetYStart(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_FAIL_OFFSET_RANGE_Y_START)));
hasCustomActivationCriteria = true;
}
if(config.hasKey(KEY_PAN_FAIL_OFFSET_RANGE_Y_END)) {
handler.setFailOffsetYEnd(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_FAIL_OFFSET_RANGE_Y_END)));
hasCustomActivationCriteria = true;
}
if (config.hasKey(KEY_PAN_MIN_VELOCITY)) {
// This value is actually in DPs/ms, but we can use the same function as for converting
// from DPs to pixels as the unit we're converting is in the numerator
handler.setMinVelocity(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_MIN_VELOCITY)));
hasCustomActivationCriteria = true;
}
if (config.hasKey(KEY_PAN_MIN_VELOCITY_X)) {
handler.setMinVelocityX(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_MIN_VELOCITY_X)));
hasCustomActivationCriteria = true;
}
if (config.hasKey(KEY_PAN_MIN_VELOCITY_Y)) {
handler.setMinVelocityY(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_MIN_VELOCITY_Y)));
hasCustomActivationCriteria = true;
}
// PanGestureHandler sets minDist by default, if there are custom criteria specified we want
// to reset that setting and use provided criteria instead.
if (config.hasKey(KEY_PAN_MIN_DIST)) {
handler.setMinDist(PixelUtil.toPixelFromDIP(config.getDouble(KEY_PAN_MIN_DIST)));
} else if (hasCustomActivationCriteria) {
handler.setMinDist(Float.MAX_VALUE);
}
if (config.hasKey(KEY_PAN_MIN_POINTERS)) {
handler.setMinPointers(config.getInt(KEY_PAN_MIN_POINTERS));
}
if (config.hasKey(KEY_PAN_MAX_POINTERS)) {
handler.setMaxPointers(config.getInt(KEY_PAN_MAX_POINTERS));
}
if (config.hasKey(KEY_PAN_AVG_TOUCHES)) {
handler.setAverageTouches(config.getBoolean(KEY_PAN_AVG_TOUCHES));
}
}
@Override
public void extractEventData(PanGestureHandler handler, WritableMap eventData) {
super.extractEventData(handler, eventData);
eventData.putDouble("x", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionX()));
eventData.putDouble("y", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionY()));
eventData.putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionX()));
eventData.putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionY()));
eventData.putDouble("translationX", PixelUtil.toDIPFromPixel(handler.getTranslationX()));
eventData.putDouble("translationY", PixelUtil.toDIPFromPixel(handler.getTranslationY()));
eventData.putDouble("velocityX", PixelUtil.toDIPFromPixel(handler.getVelocityX()));
eventData.putDouble("velocityY", PixelUtil.toDIPFromPixel(handler.getVelocityY()));
}
}
private static class PinchGestureHandlerFactory extends HandlerFactory<PinchGestureHandler> {
@Override
public Class<PinchGestureHandler> getType() {
return PinchGestureHandler.class;
}
@Override
public String getName() {
return "PinchGestureHandler";
}
@Override
public PinchGestureHandler create(Context context) {
return new PinchGestureHandler();
}
@Override
public void extractEventData(PinchGestureHandler handler, WritableMap eventData) {
super.extractEventData(handler, eventData);
eventData.putDouble("scale", handler.getScale());
eventData.putDouble("focalX", PixelUtil.toDIPFromPixel(handler.getFocalPointX()));
eventData.putDouble("focalY", PixelUtil.toDIPFromPixel(handler.getFocalPointY()));
eventData.putDouble("velocity", handler.getVelocity());
}
}
private static class FlingGestureHandlerFactory extends HandlerFactory<FlingGestureHandler> {
@Override
public Class<FlingGestureHandler> getType() {
return FlingGestureHandler.class;
}
@Override
public String getName() {
return "FlingGestureHandler";
}
@Override
public FlingGestureHandler create(Context context) {
return new FlingGestureHandler();
}
@Override
public void configure(FlingGestureHandler handler, ReadableMap config) {
super.configure(handler, config);
if (config.hasKey(KEY_NUMBER_OF_POINTERS)) {
handler.setNumberOfPointersRequired(config.getInt(KEY_NUMBER_OF_POINTERS));
}
if (config.hasKey(KEY_DIRECTION)) {
handler.setDirection(config.getInt(KEY_DIRECTION));
}
}
@Override
public void extractEventData(FlingGestureHandler handler, WritableMap eventData) {
super.extractEventData(handler, eventData);
eventData.putDouble("x", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionX()));
eventData.putDouble("y", PixelUtil.toDIPFromPixel(handler.getLastRelativePositionY()));
eventData.putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionX()));
eventData.putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.getLastAbsolutePositionY()));
}
}
private static class RotationGestureHandlerFactory extends HandlerFactory<RotationGestureHandler> {
@Override
public Class<RotationGestureHandler> getType() {
return RotationGestureHandler.class;
}
@Override
public String getName() {
return "RotationGestureHandler";
}
@Override
public RotationGestureHandler create(Context context) {
return new RotationGestureHandler();
}
@Override
public void extractEventData(RotationGestureHandler handler, WritableMap eventData) {
super.extractEventData(handler, eventData);
eventData.putDouble("rotation", handler.getRotation());
eventData.putDouble("anchorX", PixelUtil.toDIPFromPixel(handler.getAnchorX()));
eventData.putDouble("anchorY", PixelUtil.toDIPFromPixel(handler.getAnchorY()));
eventData.putDouble("velocity", handler.getVelocity());
}
}
private OnTouchEventListener mEventListener = new OnTouchEventListener() {
@Override
public void onTouchEvent(GestureHandler handler, MotionEvent event) {
RNGestureHandlerModule.this.onTouchEvent(handler, event);
}
@Override
public void onStateChange(GestureHandler handler, int newState, int oldState) {
RNGestureHandlerModule.this.onStateChange(handler, newState, oldState);
}
};
private HandlerFactory[] mHandlerFactories = new HandlerFactory[] {
new NativeViewGestureHandlerFactory(),
new TapGestureHandlerFactory(),
new LongPressGestureHandlerFactory(),
new PanGestureHandlerFactory(),
new PinchGestureHandlerFactory(),
new RotationGestureHandlerFactory(),
new FlingGestureHandlerFactory()
};
private final RNGestureHandlerRegistry mRegistry = new RNGestureHandlerRegistry();
private RNGestureHandlerInteractionManager mInteractionManager =
new RNGestureHandlerInteractionManager();
private List<RNGestureHandlerRootHelper> mRoots = new ArrayList<>();
private List<Integer> mEnqueuedRootViewInit = new ArrayList<>();
public RNGestureHandlerModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return MODULE_NAME;
}
@ReactMethod
public void createGestureHandler(
String handlerName,
int handlerTag,
ReadableMap config) {
for (int i = 0; i < mHandlerFactories.length; i++) {
HandlerFactory handlerFactory = mHandlerFactories[i];
if (handlerFactory.getName().equals(handlerName)) {
GestureHandler handler = handlerFactory.create(getReactApplicationContext());
handler.setTag(handlerTag);
handler.setOnTouchEventListener(mEventListener);
mRegistry.registerHandler(handler);
mInteractionManager.configureInteractions(handler, config);
handlerFactory.configure(handler, config);
return;
}
}
throw new JSApplicationIllegalArgumentException("Invalid handler name " + handlerName);
}
@ReactMethod
public void attachGestureHandler(int handlerTag, int viewTag) {
tryInitializeHandlerForReactRootView(viewTag);
if (!mRegistry.attachHandlerToView(handlerTag, viewTag)) {
throw new JSApplicationIllegalArgumentException(
"Handler with tag " + handlerTag + " does not exists");
}
}
@ReactMethod
public void updateGestureHandler(
int handlerTag,
ReadableMap config) {
GestureHandler handler = mRegistry.getHandler(handlerTag);
if (handler != null) {
HandlerFactory factory = findFactoryForHandler(handler);
if (factory != null) {
mInteractionManager.dropRelationsForHandlerWithTag(handlerTag);
mInteractionManager.configureInteractions(handler, config);
factory.configure(handler, config);
}
}
}
@ReactMethod
public void dropGestureHandler(int handlerTag) {
mInteractionManager.dropRelationsForHandlerWithTag(handlerTag);
mRegistry.dropHandler(handlerTag);
}
@ReactMethod
public void handleSetJSResponder(int viewTag, boolean blockNativeResponder) {
if (mRegistry != null) {
RNGestureHandlerRootHelper rootView = findRootHelperForViewAncestor(viewTag);
if (rootView != null) {
rootView.handleSetJSResponder(viewTag, blockNativeResponder);
}
}
}
@ReactMethod
public void handleClearJSResponder() {
}
@Override
public @Nullable Map getConstants() {
return MapBuilder.of("State", MapBuilder.of(
"UNDETERMINED", GestureHandler.STATE_UNDETERMINED,
"BEGAN", GestureHandler.STATE_BEGAN,
"ACTIVE", GestureHandler.STATE_ACTIVE,
"CANCELLED", GestureHandler.STATE_CANCELLED,
"FAILED", GestureHandler.STATE_FAILED,
"END", GestureHandler.STATE_END
), "Direction", MapBuilder.of(
"RIGHT", GestureHandler.DIRECTION_RIGHT,
"LEFT", GestureHandler.DIRECTION_LEFT,
"UP", GestureHandler.DIRECTION_UP,
"DOWN", GestureHandler.DIRECTION_DOWN
));
}
public RNGestureHandlerRegistry getRegistry() {
return mRegistry;
}
@Override
public void onCatalystInstanceDestroy() {
mRegistry.dropAllHandlers();
mInteractionManager.reset();
synchronized (mRoots) {
while (!mRoots.isEmpty()) {
int sizeBefore = mRoots.size();
RNGestureHandlerRootHelper root = mRoots.get(0);
ViewGroup reactRootView = root.getRootView();
if (reactRootView instanceof RNGestureHandlerEnabledRootView) {
((RNGestureHandlerEnabledRootView) reactRootView).tearDown();
} else {
root.tearDown();
}
if (mRoots.size() >= sizeBefore) {
throw new IllegalStateException("Expected root helper to get unregistered while tearing down");
}
}
}
super.onCatalystInstanceDestroy();
}
private void tryInitializeHandlerForReactRootView(int ancestorViewTag) {
UIManagerModule uiManager = getReactApplicationContext().getNativeModule(UIManagerModule.class);
final int rootViewTag = uiManager.resolveRootTagFromReactTag(ancestorViewTag);
if (rootViewTag < 1) {
throw new JSApplicationIllegalArgumentException("Could find root view for a given ancestor with tag "
+ ancestorViewTag);
}
synchronized (mRoots) {
for (int i = 0; i < mRoots.size(); i++) {
RNGestureHandlerRootHelper root = mRoots.get(i);
ViewGroup rootView = root.getRootView();
if (rootView instanceof ReactRootView && ((ReactRootView) rootView).getRootViewTag() == rootViewTag) {
// we have found root helper registered for a given react root, we don't need to
// initialize a new one then
return;
}
}
}
synchronized (mEnqueuedRootViewInit) {
if (mEnqueuedRootViewInit.contains(rootViewTag)) {
// root view initialization already enqueued -> we skip
return;
}
mEnqueuedRootViewInit.add(rootViewTag);
}
// root helper for a given root tag has not been found, we may wat to check if the root view is
// an instance of RNGestureHandlerEnabledRootView and then initialize gesture handler with it
uiManager.addUIBlock(new UIBlock() {
@Override
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
View view = nativeViewHierarchyManager.resolveView(rootViewTag);
if (view instanceof RNGestureHandlerEnabledRootView) {
((RNGestureHandlerEnabledRootView) view).initialize();
} else {
// Seems like the root view is something else than RNGestureHandlerEnabledRootView, this
// is fine though as long as gestureHandlerRootHOC is used in JS
// FIXME: check and warn about gestureHandlerRootHOC
}
synchronized (mEnqueuedRootViewInit) {
mEnqueuedRootViewInit.remove(new Integer(rootViewTag));
}
}
});
}
public void registerRootHelper(RNGestureHandlerRootHelper root) {
synchronized (mRoots) {
if (mRoots.contains(root)) {
throw new IllegalStateException("Root helper" + root + " already registered");
}
mRoots.add(root);
}
}
public void unregisterRootHelper(RNGestureHandlerRootHelper root) {
synchronized (mRoots) {
mRoots.remove(root);
}
}
private @Nullable RNGestureHandlerRootHelper findRootHelperForViewAncestor(int viewTag) {
UIManagerModule uiManager = getReactApplicationContext().getNativeModule(UIManagerModule.class);
int rootViewTag = uiManager.resolveRootTagFromReactTag(viewTag);
if (rootViewTag < 1) {
return null;
}
synchronized (mRoots) {
for (int i = 0; i < mRoots.size(); i++) {
RNGestureHandlerRootHelper root = mRoots.get(i);
ViewGroup rootView = root.getRootView();
if (rootView instanceof ReactRootView && ((ReactRootView) rootView).getRootViewTag() == rootViewTag) {
return root;
}
}
}
return null;
}
private @Nullable HandlerFactory findFactoryForHandler(GestureHandler handler) {
for (int i = 0; i < mHandlerFactories.length; i++) {
HandlerFactory factory = mHandlerFactories[i];
if (factory.getType().equals(handler.getClass())) {
return factory;
}
}
return null;
}
private void onTouchEvent(GestureHandler handler, MotionEvent motionEvent) {
if (handler.getTag() < 0) {
// root containers use negative tags, we don't need to dispatch events for them to the JS
return;
}
if (handler.getState() == GestureHandler.STATE_ACTIVE) {
HandlerFactory handlerFactory = findFactoryForHandler(handler);
EventDispatcher eventDispatcher = getReactApplicationContext()
.getNativeModule(UIManagerModule.class)
.getEventDispatcher();
RNGestureHandlerEvent event = RNGestureHandlerEvent.obtain(handler, handlerFactory);
eventDispatcher.dispatchEvent(event);
}
}
private void onStateChange(GestureHandler handler, int newState, int oldState) {
if (handler.getTag() < 0) {
// root containers use negative tags, we don't need to dispatch events for them to the JS
return;
}
HandlerFactory handlerFactory = findFactoryForHandler(handler);
EventDispatcher eventDispatcher = getReactApplicationContext()
.getNativeModule(UIManagerModule.class)
.getEventDispatcher();
RNGestureHandlerStateChangeEvent event = RNGestureHandlerStateChangeEvent.obtain(
handler,
newState,
oldState,
handlerFactory);
eventDispatcher.dispatchEvent(event);
}
private static void handleHitSlopProperty(GestureHandler handler, ReadableMap config) {
if (config.getType(KEY_HIT_SLOP) == ReadableType.Number) {
float hitSlop = PixelUtil.toPixelFromDIP(config.getDouble(KEY_HIT_SLOP));
handler.setHitSlop(hitSlop, hitSlop, hitSlop, hitSlop, HIT_SLOP_NONE, HIT_SLOP_NONE);
} else {
ReadableMap hitSlop = config.getMap(KEY_HIT_SLOP);
float left = HIT_SLOP_NONE, top = HIT_SLOP_NONE, right = HIT_SLOP_NONE, bottom = HIT_SLOP_NONE;
float width = HIT_SLOP_NONE, height = HIT_SLOP_NONE;
if (hitSlop.hasKey(KEY_HIT_SLOP_HORIZONTAL)) {
float horizontalPad = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_HORIZONTAL));
left = right = horizontalPad;
}
if (hitSlop.hasKey(KEY_HIT_SLOP_VERTICAL)) {
float verticalPad = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_VERTICAL));
top = bottom = verticalPad;
}
if (hitSlop.hasKey(KEY_HIT_SLOP_LEFT)) {
left = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_LEFT));
}
if (hitSlop.hasKey(KEY_HIT_SLOP_TOP)) {
top = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_TOP));
}
if (hitSlop.hasKey(KEY_HIT_SLOP_RIGHT)) {
right = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_RIGHT));
}
if (hitSlop.hasKey(KEY_HIT_SLOP_BOTTOM)) {
bottom = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_BOTTOM));
}
if (hitSlop.hasKey(KEY_HIT_SLOP_WIDTH)) {
width = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_WIDTH));
}
if (hitSlop.hasKey(KEY_HIT_SLOP_HEIGHT)) {
height = PixelUtil.toPixelFromDIP(hitSlop.getDouble(KEY_HIT_SLOP_HEIGHT));
}
handler.setHitSlop(left, top, right, bottom, width, height);
}
}
}