-
Notifications
You must be signed in to change notification settings - Fork 24k
/
SurfaceMountingManager.java
948 lines (822 loc) · 32.8 KB
/
SurfaceMountingManager.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.fabric.mounting;
import static com.facebook.infer.annotation.ThreadConfined.ANY;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import androidx.annotation.UiThread;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.ThreadConfined;
import com.facebook.react.bridge.ReactSoftException;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.RetryableMountingLayerException;
import com.facebook.react.bridge.SoftAssertions;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.fabric.events.EventEmitterWrapper;
import com.facebook.react.fabric.mounting.mountitems.MountItem;
import com.facebook.react.touch.JSResponderHandler;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.ReactRoot;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.RootView;
import com.facebook.react.uimanager.RootViewManager;
import com.facebook.react.uimanager.StateWrapper;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.ViewManagerRegistry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.annotation.Nullable;
public class SurfaceMountingManager {
public static final String TAG = SurfaceMountingManager.class.getSimpleName();
private static final boolean SHOW_CHANGED_VIEW_HIERARCHIES = ReactBuildConfig.DEBUG && false;
private volatile boolean mIsStopped = false;
private volatile boolean mRootViewAttached = false;
@Nullable private ThemedReactContext mThemedReactContext;
// These are all non-null, until StopSurface is called
private ConcurrentHashMap<Integer, ViewState> mTagToViewState =
new ConcurrentHashMap<>(); // any thread
private ConcurrentLinkedQueue<MountItem> mOnViewAttachItems = new ConcurrentLinkedQueue<>();
private JSResponderHandler mJSResponderHandler;
private ViewManagerRegistry mViewManagerRegistry;
private RootViewManager mRootViewManager;
private MountingManager mMountingManager;
// This is null *until* StopSurface is called.
private Set<Integer> mTagSetForStoppedSurface;
private final int mSurfaceId;
public SurfaceMountingManager(
int surfaceId,
@NonNull JSResponderHandler jsResponderHandler,
@NonNull ViewManagerRegistry viewManagerRegistry,
@NonNull RootViewManager rootViewManager,
@NonNull MountingManager mountingManager) {
mSurfaceId = surfaceId;
mJSResponderHandler = jsResponderHandler;
mViewManagerRegistry = viewManagerRegistry;
mRootViewManager = rootViewManager;
mMountingManager = mountingManager;
}
public boolean isStopped() {
return mIsStopped;
}
public void attachRootView(View rootView, ThemedReactContext themedReactContext) {
mThemedReactContext = themedReactContext;
addRootView(rootView);
}
public int getSurfaceId() {
return mSurfaceId;
}
public boolean isRootViewAttached() {
return mRootViewAttached;
}
@Nullable
public ThemedReactContext getContext() {
return mThemedReactContext;
}
private static void logViewHierarchy(ViewGroup parent, boolean recurse) {
int parentTag = parent.getId();
FLog.e(TAG, " <ViewGroup tag=" + parentTag + " class=" + parent.getClass().toString() + ">");
for (int i = 0; i < parent.getChildCount(); i++) {
FLog.e(
TAG,
" <View idx="
+ i
+ " tag="
+ parent.getChildAt(i).getId()
+ " class="
+ parent.getChildAt(i).getClass().toString()
+ ">");
}
FLog.e(TAG, " </ViewGroup tag=" + parentTag + ">");
if (recurse) {
FLog.e(TAG, "Displaying Ancestors:");
ViewParent ancestor = parent.getParent();
while (ancestor != null) {
ViewGroup ancestorViewGroup = (ancestor instanceof ViewGroup ? (ViewGroup) ancestor : null);
int ancestorId = ancestorViewGroup == null ? View.NO_ID : ancestorViewGroup.getId();
FLog.e(
TAG,
"<ViewParent tag=" + ancestorId + " class=" + ancestor.getClass().toString() + ">");
ancestor = ancestor.getParent();
}
}
}
public boolean getViewExists(int tag) {
// If Surface stopped, check if tag *was* associated with this Surface, even though it's been
// deleted. This helps distinguish between scenarios where an invalid tag is referenced, vs
// race conditions where an imperative method is called on a tag during/just after StopSurface.
if (mTagSetForStoppedSurface != null && mTagSetForStoppedSurface.contains(tag)) {
return true;
}
if (mTagToViewState == null) {
return false;
}
return mTagToViewState.containsKey(tag);
}
@AnyThread
public void executeOnViewAttach(MountItem item) {
mOnViewAttachItems.add(item);
}
@AnyThread
private void addRootView(@NonNull final View rootView) {
if (isStopped()) {
return;
}
mTagToViewState.put(mSurfaceId, new ViewState(mSurfaceId, rootView, mRootViewManager, true));
Runnable runnable =
new Runnable() {
@Override
public void run() {
// The CPU has ticked since `addRootView` was called, so the surface could technically
// have already stopped here.
if (isStopped()) {
return;
}
if (rootView.getId() == mSurfaceId) {
ReactSoftException.logSoftException(
TAG,
new IllegalViewOperationException(
"Race condition in addRootView detected. Trying to set an id of ["
+ mSurfaceId
+ "] on the RootView, but that id has already been set. "));
} else if (rootView.getId() != View.NO_ID) {
FLog.e(
TAG,
"Trying to add RootTag to RootView that already has a tag: existing tag: [%d] new tag: [%d]",
rootView.getId(),
mSurfaceId);
throw new IllegalViewOperationException(
"Trying to add a root view with an explicit id already set. React Native uses "
+ "the id field to track react tags and will overwrite this field. If that is fine, "
+ "explicitly overwrite the id field to View.NO_ID before calling addRootView.");
}
rootView.setId(mSurfaceId);
if (rootView instanceof ReactRoot) {
((ReactRoot) rootView).setRootViewTag(mSurfaceId);
}
mRootViewAttached = true;
executeViewAttachMountItems();
}
};
if (UiThreadUtil.isOnUiThread()) {
runnable.run();
} else {
UiThreadUtil.runOnUiThread(runnable);
}
}
@UiThread
private void executeViewAttachMountItems() {
while (!mOnViewAttachItems.isEmpty()) {
MountItem item = mOnViewAttachItems.poll();
item.execute(mMountingManager);
}
}
/**
* Stop surface and all operations within it. Garbage-collect Views (caller is responsible for
* removing RootView from View layer).
*
* <p>Delete rootView from cache. Since RN does not control the RootView, in a sense, the fragment
* is responsible for actually removing the RootView from the hierarchy / tearing down the
* fragment.
*
* <p>In the original version(s) of this function, we recursively went through all children of the
* View and dropped those Views as well; ad infinitum. This was before we had a
* SurfaceMountingManager, and all tags were in one global map. Doing this was particularly
* important in the case of StopSurface, where race conditions between threads meant you couldn't
* rely on DELETE instructions actually deleting all Views in the Surface.
*
* <p>Now that we have SurfaceMountingManager, we can simply drop our local reference to the View.
* Since it will be removed from the View hierarchy entirely (outside of the scope of this class),
* garbage collection will take care of destroying it and all descendents.
*/
@AnyThread
public void stopSurface() {
if (isStopped()) {
return;
}
// Prevent more views from being created, or the hierarchy from being manipulated at all. This
// causes further operations to noop.
mIsStopped = true;
// Reset all StateWrapper objects
// Since this can happen on any thread, is it possible to race between StateWrapper destruction
// and some accesses from View classes in the UI thread?
for (ViewState viewState : mTagToViewState.values()) {
if (viewState.mStateWrapper != null) {
viewState.mStateWrapper.destroyState();
viewState.mStateWrapper = null;
}
if (viewState.mEventEmitter != null) {
viewState.mEventEmitter.destroy();
viewState.mEventEmitter = null;
}
}
Runnable runnable =
new Runnable() {
@Override
public void run() {
// We must call `onDropViewInstance` on all remaining Views
for (ViewState viewState : mTagToViewState.values()) {
onViewStateDeleted(viewState);
}
// Evict all views from cache and memory
mTagSetForStoppedSurface = mTagToViewState.keySet();
mTagToViewState = null;
mJSResponderHandler = null;
mRootViewManager = null;
mMountingManager = null;
mOnViewAttachItems.clear();
}
};
if (UiThreadUtil.isOnUiThread()) {
runnable.run();
} else {
UiThreadUtil.runOnUiThread(runnable);
}
}
@UiThread
public void addViewAt(final int parentTag, final int tag, final int index) {
UiThreadUtil.assertOnUiThread();
if (isStopped()) {
return;
}
ViewState parentViewState = getViewState(parentTag);
if (!(parentViewState.mView instanceof ViewGroup)) {
String message =
"Unable to add a view into a view that is not a ViewGroup. ParentTag: "
+ parentTag
+ " - Tag: "
+ tag
+ " - Index: "
+ index;
FLog.e(TAG, message);
throw new IllegalStateException(message);
}
final ViewGroup parentView = (ViewGroup) parentViewState.mView;
ViewState viewState = getViewState(tag);
final View view = viewState.mView;
if (view == null) {
throw new IllegalStateException(
"Unable to find view for viewState " + viewState + " and tag " + tag);
}
// Display children before inserting
if (SHOW_CHANGED_VIEW_HIERARCHIES) {
FLog.e(TAG, "addViewAt: [" + tag + "] -> [" + parentTag + "] idx: " + index + " BEFORE");
logViewHierarchy(parentView, false);
}
ViewParent viewParent = view.getParent();
if (viewParent != null) {
int actualParentId =
viewParent instanceof ViewGroup ? ((ViewGroup) viewParent).getId() : View.NO_ID;
ReactSoftException.logSoftException(
TAG,
new IllegalStateException(
"addViewAt: cannot insert view ["
+ tag
+ "] into parent ["
+ parentTag
+ "]: View already has a parent: ["
+ actualParentId
+ "] "
+ viewParent.getClass().getSimpleName()));
}
try {
getViewGroupManager(parentViewState).addView(parentView, view, index);
} catch (IllegalStateException e) {
// Wrap error with more context for debugging
throw new IllegalStateException(
"addViewAt: failed to insert view ["
+ tag
+ "] into parent ["
+ parentTag
+ "] at index "
+ index,
e);
}
// Display children after inserting
if (SHOW_CHANGED_VIEW_HIERARCHIES) {
// Why are we calling `runOnUiThread`? We're already on the UI thread, right?!
// Yes - but if you get the children of the View here and display them, *it might show you
// the previous children*. Without getting too much into Android internals, basically if we
// wait a tick, everything is what we expect.
// tldr is that `parent.children == []; parent.addView(x); parent.children == []`
// and you need to wait a tick for `parent.children == [x]`.
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
FLog.e(
TAG, "addViewAt: [" + tag + "] -> [" + parentTag + "] idx: " + index + " AFTER");
logViewHierarchy(parentView, false);
}
});
}
}
@UiThread
public void removeViewAt(final int tag, final int parentTag, int index) {
if (isStopped()) {
return;
}
UiThreadUtil.assertOnUiThread();
ViewState parentViewState = getNullableViewState(parentTag);
// TODO: throw exception here?
if (parentViewState == null) {
ReactSoftException.logSoftException(
MountingManager.TAG,
new IllegalStateException(
"Unable to find viewState for tag: [" + parentTag + "] for removeViewAt"));
return;
}
if (!(parentViewState.mView instanceof ViewGroup)) {
String message =
"Unable to remove a view from a view that is not a ViewGroup. ParentTag: "
+ parentTag
+ " - Tag: "
+ tag
+ " - Index: "
+ index;
FLog.e(TAG, message);
throw new IllegalStateException(message);
}
final ViewGroup parentView = (ViewGroup) parentViewState.mView;
if (parentView == null) {
throw new IllegalStateException("Unable to find view for tag [" + parentTag + "]");
}
if (SHOW_CHANGED_VIEW_HIERARCHIES) {
// Display children before deleting any
FLog.e(TAG, "removeViewAt: [" + tag + "] -> [" + parentTag + "] idx: " + index + " BEFORE");
logViewHierarchy(parentView, false);
}
ViewGroupManager<ViewGroup> viewGroupManager = getViewGroupManager(parentViewState);
// Verify that the view we're about to remove has the same tag we expect
View view = viewGroupManager.getChildAt(parentView, index);
int actualTag = (view != null ? view.getId() : -1);
if (actualTag != tag) {
int tagActualIndex = -1;
int parentChildrenCount = parentView.getChildCount();
for (int i = 0; i < parentChildrenCount; i++) {
if (parentView.getChildAt(i).getId() == tag) {
tagActualIndex = i;
break;
}
}
// TODO T74425739: previously, we did not do this check and `removeViewAt` would be executed
// below, sometimes crashing there. *However*, interestingly enough, `removeViewAt` would not
// complain if you removed views from an already-empty parent. This seems necessary currently
// for certain ViewManagers that remove their own children - like BottomSheet?
// This workaround seems not-great, but for now, we just return here for
// backwards-compatibility. Essentially, if a view has already been removed from the
// hierarchy, we treat it as a noop.
if (tagActualIndex == -1) {
FLog.e(
TAG,
"removeViewAt: ["
+ tag
+ "] -> ["
+ parentTag
+ "] @"
+ index
+ ": view already removed from parent! Children in parent: "
+ parentChildrenCount);
return;
}
// Here we are guaranteed that the view is still in the View hierarchy, just
// at a different index. In debug mode we'll crash here; in production, we'll remove
// the child from the parent and move on.
// This is an issue that is safely recoverable 95% of the time. If this allows corruption
// of the view hierarchy and causes bugs or a crash after this point, there will be logs
// indicating that this happened.
// This is likely *only* necessary because of Fabric's LayoutAnimations implementation.
// If we can fix the bug there, or remove the need for LayoutAnimation index adjustment
// entirely, we can just throw this exception without regression user experience.
logViewHierarchy(parentView, true);
ReactSoftException.logSoftException(
TAG,
new IllegalStateException(
"Tried to remove view ["
+ tag
+ "] of parent ["
+ parentTag
+ "] at index "
+ index
+ ", but got view tag "
+ actualTag
+ " - actual index of view: "
+ tagActualIndex));
index = tagActualIndex;
}
try {
viewGroupManager.removeViewAt(parentView, index);
} catch (RuntimeException e) {
// Note: `getChildCount` may not always be accurate!
// We don't currently have a good explanation other than, in situations where you
// would empirically expect to see childCount > 0, the childCount is reported as 0.
// This is likely due to a ViewManager overriding getChildCount or some other methods
// in a way that is strictly incorrect, but potentially only visible here.
// The failure mode is actually that in `removeViewAt`, a NullPointerException is
// thrown when we try to perform an operation on a View that doesn't exist, and
// is therefore null.
// We try to add some extra diagnostics here, but we always try to remove the View
// from the hierarchy first because detecting by looking at childCount will not work.
//
// Note that the lesson here is that `getChildCount` is not /required/ to adhere to
// any invariants. If you add 9 children to a parent, the `getChildCount` of the parent
// may not be equal to 9. This apparently causes no issues with Android and is common
// enough that we shouldn't try to change this invariant, without a lot of thought.
int childCount = viewGroupManager.getChildCount(parentView);
logViewHierarchy(parentView, true);
throw new IllegalStateException(
"Cannot remove child at index "
+ index
+ " from parent ViewGroup ["
+ parentView.getId()
+ "], only "
+ childCount
+ " children in parent. Warning: childCount may be incorrect!",
e);
}
// Display children after deleting any
if (SHOW_CHANGED_VIEW_HIERARCHIES) {
final int finalIndex = index;
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
FLog.e(
TAG,
"removeViewAt: ["
+ tag
+ "] -> ["
+ parentTag
+ "] idx: "
+ finalIndex
+ " AFTER");
logViewHierarchy(parentView, false);
}
});
}
}
@UiThread
public void createView(
@NonNull String componentName,
int reactTag,
@Nullable ReadableMap props,
@Nullable StateWrapper stateWrapper,
@Nullable EventEmitterWrapper eventEmitterWrapper,
boolean isLayoutable) {
if (isStopped()) {
return;
}
if (getNullableViewState(reactTag) != null) {
return;
}
View view = null;
ViewManager viewManager = null;
ReactStylesDiffMap propsDiffMap = null;
if (props != null) {
propsDiffMap = new ReactStylesDiffMap(props);
}
if (isLayoutable) {
viewManager = mViewManagerRegistry.get(componentName);
// View Managers are responsible for dealing with initial state and props.
view =
viewManager.createView(
reactTag, mThemedReactContext, propsDiffMap, stateWrapper, mJSResponderHandler);
}
ViewState viewState = new ViewState(reactTag, view, viewManager);
viewState.mCurrentProps = propsDiffMap;
viewState.mStateWrapper = stateWrapper;
viewState.mEventEmitter = eventEmitterWrapper;
mTagToViewState.put(reactTag, viewState);
}
public void updateProps(int reactTag, ReadableMap props) {
if (isStopped()) {
return;
}
ViewState viewState = getViewState(reactTag);
viewState.mCurrentProps = new ReactStylesDiffMap(props);
View view = viewState.mView;
if (view == null) {
throw new IllegalStateException("Unable to find view for tag [" + reactTag + "]");
}
Assertions.assertNotNull(viewState.mViewManager)
.updateProperties(view, viewState.mCurrentProps);
}
@Deprecated
public void receiveCommand(int reactTag, int commandId, @Nullable ReadableArray commandArgs) {
if (isStopped()) {
return;
}
ViewState viewState = getNullableViewState(reactTag);
// It's not uncommon for JS to send events as/after a component is being removed from the
// view hierarchy. For example, TextInput may send a "blur" command in response to the view
// disappearing. Throw `ReactNoCrashSoftException` so they're logged but don't crash in dev
// for now.
if (viewState == null) {
throw new RetryableMountingLayerException(
"Unable to find viewState for tag: [" + reactTag + "] for commandId: " + commandId);
}
if (viewState.mViewManager == null) {
throw new RetryableMountingLayerException("Unable to find viewManager for tag " + reactTag);
}
if (viewState.mView == null) {
throw new RetryableMountingLayerException(
"Unable to find viewState view for tag " + reactTag);
}
viewState.mViewManager.receiveCommand(viewState.mView, commandId, commandArgs);
}
public void receiveCommand(
int reactTag, @NonNull String commandId, @Nullable ReadableArray commandArgs) {
if (isStopped()) {
return;
}
ViewState viewState = getNullableViewState(reactTag);
// It's not uncommon for JS to send events as/after a component is being removed from the
// view hierarchy. For example, TextInput may send a "blur" command in response to the view
// disappearing. Throw `ReactNoCrashSoftException` so they're logged but don't crash in dev
// for now.
if (viewState == null) {
throw new RetryableMountingLayerException(
"Unable to find viewState for tag: " + reactTag + " for commandId: " + commandId);
}
if (viewState.mViewManager == null) {
throw new RetryableMountingLayerException(
"Unable to find viewState manager for tag " + reactTag);
}
if (viewState.mView == null) {
throw new RetryableMountingLayerException(
"Unable to find viewState view for tag " + reactTag);
}
viewState.mViewManager.receiveCommand(viewState.mView, commandId, commandArgs);
}
public void sendAccessibilityEvent(int reactTag, int eventType) {
if (isStopped()) {
return;
}
ViewState viewState = getViewState(reactTag);
if (viewState.mViewManager == null) {
throw new RetryableMountingLayerException(
"Unable to find viewState manager for tag " + reactTag);
}
if (viewState.mView == null) {
throw new RetryableMountingLayerException(
"Unable to find viewState view for tag " + reactTag);
}
viewState.mView.sendAccessibilityEvent(eventType);
}
@UiThread
public void updateLayout(int reactTag, int x, int y, int width, int height, int displayType) {
if (isStopped()) {
return;
}
ViewState viewState = getViewState(reactTag);
// Do not layout Root Views
if (viewState.mIsRoot) {
return;
}
View viewToUpdate = viewState.mView;
if (viewToUpdate == null) {
throw new IllegalStateException("Unable to find View for tag: " + reactTag);
}
viewToUpdate.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
ViewParent parent = viewToUpdate.getParent();
if (parent instanceof RootView) {
parent.requestLayout();
}
// TODO: T31905686 Check if the parent of the view has to layout the view, or the child has
// to lay itself out. see NativeViewHierarchyManager.updateLayout
viewToUpdate.layout(x, y, x + width, y + height);
// displayType: 0 represents display: 'none'
int visibility = displayType == 0 ? View.INVISIBLE : View.VISIBLE;
if (viewToUpdate.getVisibility() != visibility) {
viewToUpdate.setVisibility(visibility);
}
}
@UiThread
public void updatePadding(int reactTag, int left, int top, int right, int bottom) {
UiThreadUtil.assertOnUiThread();
if (isStopped()) {
return;
}
ViewState viewState = getViewState(reactTag);
// Do not layout Root Views
if (viewState.mIsRoot) {
return;
}
View viewToUpdate = viewState.mView;
if (viewToUpdate == null) {
throw new IllegalStateException("Unable to find View for tag: " + reactTag);
}
ViewManager viewManager = viewState.mViewManager;
if (viewManager == null) {
throw new IllegalStateException("Unable to find ViewManager for view: " + viewState);
}
//noinspection unchecked
viewManager.setPadding(viewToUpdate, left, top, right, bottom);
}
@UiThread
public void updateState(final int reactTag, @Nullable StateWrapper stateWrapper) {
UiThreadUtil.assertOnUiThread();
if (isStopped()) {
return;
}
ViewState viewState = getViewState(reactTag);
StateWrapper prevStateWrapper = viewState.mStateWrapper;
viewState.mStateWrapper = stateWrapper;
ViewManager viewManager = viewState.mViewManager;
if (viewManager == null) {
throw new IllegalStateException("Unable to find ViewManager for tag: " + reactTag);
}
Object extraData =
viewManager.updateState(viewState.mView, viewState.mCurrentProps, stateWrapper);
if (extraData != null) {
viewManager.updateExtraData(viewState.mView, extraData);
}
// Immediately clear native side of previous state wrapper. This causes the State object in C++
// to be destroyed immediately instead of waiting for Java GC to kick in.
if (prevStateWrapper != null) {
prevStateWrapper.destroyState();
}
}
@UiThread
public void updateEventEmitter(int reactTag, @NonNull EventEmitterWrapper eventEmitter) {
UiThreadUtil.assertOnUiThread();
if (isStopped()) {
return;
}
ViewState viewState = mTagToViewState.get(reactTag);
if (viewState == null) {
// TODO T62717437 - Use a flag to determine that these event emitters belong to virtual nodes
// only.
viewState = new ViewState(reactTag, null, null);
mTagToViewState.put(reactTag, viewState);
}
viewState.mEventEmitter = eventEmitter;
}
@UiThread
public synchronized void setJSResponder(
int reactTag, int initialReactTag, boolean blockNativeResponder) {
UiThreadUtil.assertOnUiThread();
if (isStopped()) {
return;
}
if (!blockNativeResponder) {
mJSResponderHandler.setJSResponder(initialReactTag, null);
return;
}
ViewState viewState = getViewState(reactTag);
View view = viewState.mView;
if (initialReactTag != reactTag && view instanceof ViewParent) {
// In this case, initialReactTag corresponds to a virtual/layout-only View, and we already
// have a parent of that View in reactTag, so we can use it.
mJSResponderHandler.setJSResponder(initialReactTag, (ViewParent) view);
return;
} else if (view == null) {
SoftAssertions.assertUnreachable("Cannot find view for tag [" + reactTag + "].");
return;
}
if (viewState.mIsRoot) {
SoftAssertions.assertUnreachable(
"Cannot block native responder on [" + reactTag + "] that is a root view");
}
mJSResponderHandler.setJSResponder(initialReactTag, view.getParent());
}
@UiThread
private void onViewStateDeleted(ViewState viewState) {
// Destroy state immediately instead of waiting for Java GC.
if (viewState.mStateWrapper != null) {
viewState.mStateWrapper.destroyState();
viewState.mStateWrapper = null;
}
// Destroy EventEmitterWrapper immediately instead of waiting for Java GC.
// Notably, this is also required to ensure that the EventEmitterWrapper is deallocated
// before the JS VM is deallocated, since it holds onto a JSI::Pointer.
if (viewState.mEventEmitter != null) {
viewState.mEventEmitter.destroy();
viewState.mEventEmitter = null;
}
// For non-root views we notify viewmanager with {@link ViewManager#onDropInstance}
ViewManager viewManager = viewState.mViewManager;
if (!viewState.mIsRoot && viewManager != null) {
viewManager.onDropViewInstance(viewState.mView);
}
}
@UiThread
public void deleteView(int reactTag) {
UiThreadUtil.assertOnUiThread();
if (isStopped()) {
return;
}
ViewState viewState = getNullableViewState(reactTag);
if (viewState == null) {
ReactSoftException.logSoftException(
MountingManager.TAG,
new IllegalStateException(
"Unable to find viewState for tag: " + reactTag + " for deleteView"));
return;
}
// To delete we simply remove the tag from the registry.
// We want to rely on the correct set of MountInstructions being sent to the platform,
// or StopSurface being called, so we do not handle deleting descendents of the View.
mTagToViewState.remove(reactTag);
onViewStateDeleted(viewState);
}
@UiThread
public void preallocateView(
String componentName,
int reactTag,
@Nullable ReadableMap props,
@Nullable StateWrapper stateWrapper,
@Nullable EventEmitterWrapper eventEmitterWrapper,
boolean isLayoutable) {
UiThreadUtil.assertOnUiThread();
if (isStopped()) {
return;
}
if (getNullableViewState(reactTag) != null) {
throw new IllegalStateException(
"View for component " + componentName + " with tag " + reactTag + " already exists.");
}
createView(componentName, reactTag, props, stateWrapper, eventEmitterWrapper, isLayoutable);
}
@AnyThread
@ThreadConfined(ANY)
public @Nullable EventEmitterWrapper getEventEmitter(int reactTag) {
ViewState viewState = getNullableViewState(reactTag);
return viewState == null ? null : viewState.mEventEmitter;
}
@NonNull
private ViewState getViewState(int tag) {
ViewState viewState = mTagToViewState.get(tag);
if (viewState == null) {
throw new RetryableMountingLayerException("Unable to find viewState view for tag " + tag);
}
return viewState;
}
private @Nullable ViewState getNullableViewState(int tag) {
if (mTagToViewState == null) {
return null;
}
return mTagToViewState.get(tag);
}
@SuppressWarnings("unchecked") // prevents unchecked conversion warn of the <ViewGroup> type
private static @NonNull ViewGroupManager<ViewGroup> getViewGroupManager(
@NonNull ViewState viewState) {
if (viewState.mViewManager == null) {
throw new IllegalStateException("Unable to find ViewManager for view: " + viewState);
}
return (ViewGroupManager<ViewGroup>) viewState.mViewManager;
}
/**
* This class holds view state for react tags. Objects of this class are stored into the {@link
* #mTagToViewState}, and they should be updated in the same thread.
*/
private static class ViewState {
@Nullable final View mView;
final int mReactTag;
final boolean mIsRoot;
@Nullable final ViewManager mViewManager;
@Nullable public ReactStylesDiffMap mCurrentProps = null;
@Nullable public ReadableMap mCurrentLocalData = null;
@Nullable public StateWrapper mStateWrapper = null;
@Nullable public EventEmitterWrapper mEventEmitter = null;
private ViewState(int reactTag, @Nullable View view, @Nullable ViewManager viewManager) {
this(reactTag, view, viewManager, false);
}
private ViewState(int reactTag, @Nullable View view, ViewManager viewManager, boolean isRoot) {
mReactTag = reactTag;
mView = view;
mIsRoot = isRoot;
mViewManager = viewManager;
}
@Override
public String toString() {
boolean isLayoutOnly = mViewManager == null;
return "ViewState ["
+ mReactTag
+ "] - isRoot: "
+ mIsRoot
+ " - props: "
+ mCurrentProps
+ " - localData: "
+ mCurrentLocalData
+ " - viewManager: "
+ mViewManager
+ " - isLayoutOnly: "
+ isLayoutOnly;
}
}
}