Browse Source

Fix crash in SafeAreaUtils.getFrame when view parent is null

Janic Duplessis 4 years ago
parent
commit
98ddd6c65e

+ 13
- 2
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaUtils.java View File

63
     return windowInsets;
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
     Rect offset = new Rect();
71
     Rect offset = new Rect();
68
     view.getDrawingRect(offset);
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
     return new com.th3rdwave.safeareacontext.Rect(offset.left, offset.top, view.getWidth(), view.getHeight());
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 View File

31
   private void maybeUpdateInsets() {
31
   private void maybeUpdateInsets() {
32
     EdgeInsets edgeInsets = SafeAreaUtils.getSafeAreaInsets(mWindowManager, getRootView(), this);
32
     EdgeInsets edgeInsets = SafeAreaUtils.getSafeAreaInsets(mWindowManager, getRootView(), this);
33
     Rect frame = SafeAreaUtils.getFrame((ViewGroup) getRootView(), this);
33
     Rect frame = SafeAreaUtils.getFrame((ViewGroup) getRootView(), this);
34
-    if (edgeInsets != null &&
34
+    if (edgeInsets != null && frame != null &&
35
         (mLastInsets == null ||
35
         (mLastInsets == null ||
36
             mLastFrame == null ||
36
             mLastFrame == null ||
37
             !mLastInsets.equalsToEdgeInsets(edgeInsets) ||
37
             !mLastInsets.equalsToEdgeInsets(edgeInsets) ||

+ 1
- 1
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewManager.java View File

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