Pārlūkot izejas kodu

Fix crash in SafeAreaUtils.getFrame when view parent is null

Janic Duplessis 4 gadus atpakaļ
vecāks
revīzija
98ddd6c65e

+ 13
- 2
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaUtils.java Parādīt failu

@@ -63,10 +63,21 @@ import androidx.annotation.Nullable;
63 63
     return windowInsets;
64 64
   }
65 65
 
66
-  static com.th3rdwave.safeareacontext.Rect getFrame(ViewGroup rootView, View view) {
66
+  static @Nullable com.th3rdwave.safeareacontext.Rect getFrame(ViewGroup rootView, View view) {
67
+    // This can happen while the view gets unmounted.
68
+    if (view.getParent() == null) {
69
+      return null;
70
+    }
67 71
     Rect offset = new Rect();
68 72
     view.getDrawingRect(offset);
69
-    rootView.offsetDescendantRectToMyCoords(view, offset);
73
+    try {
74
+      rootView.offsetDescendantRectToMyCoords(view, offset);
75
+    } catch (IllegalArgumentException ex) {
76
+      // This can throw if the view is not a descendant of rootView. This should not
77
+      // happen but avoid potential crashes.
78
+      ex.printStackTrace();
79
+      return null;
80
+    }
70 81
 
71 82
     return new com.th3rdwave.safeareacontext.Rect(offset.left, offset.top, view.getWidth(), view.getHeight());
72 83
   }

+ 1
- 1
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaView.java Parādīt failu

@@ -31,7 +31,7 @@ public class SafeAreaView extends ReactViewGroup implements ViewTreeObserver.OnG
31 31
   private void maybeUpdateInsets() {
32 32
     EdgeInsets edgeInsets = SafeAreaUtils.getSafeAreaInsets(mWindowManager, getRootView(), this);
33 33
     Rect frame = SafeAreaUtils.getFrame((ViewGroup) getRootView(), this);
34
-    if (edgeInsets != null &&
34
+    if (edgeInsets != null && frame != null &&
35 35
         (mLastInsets == null ||
36 36
             mLastFrame == null ||
37 37
             !mLastInsets.equalsToEdgeInsets(edgeInsets) ||

+ 1
- 1
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewManager.java Parādīt failu

@@ -79,7 +79,7 @@ public class SafeAreaViewManager extends ViewGroupManager<SafeAreaView> {
79 79
         decorView,
80 80
         contentView);
81 81
     Rect frame = SafeAreaUtils.getFrame(decorView, contentView);
82
-    if (insets == null) {
82
+    if (insets == null || frame == null) {
83 83
       return null;
84 84
     }
85 85
     return MapBuilder.<String, Object>of(