Browse Source

Handle null root window insets

Janic Duplessis 4 years ago
parent
commit
e8dedcf9ec

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

@@ -14,6 +14,8 @@ import com.facebook.react.uimanager.PixelUtil;
14 14
 
15 15
 import java.util.Map;
16 16
 
17
+import androidx.annotation.Nullable;
18
+
17 19
 /* package */ class SafeAreaUtils {
18 20
   static WritableMap edgeInsetsToJsMap(EdgeInsets insets) {
19 21
     WritableMap insetsMap = Arguments.createMap();
@@ -36,13 +38,16 @@ import java.util.Map;
36 38
         PixelUtil.toDIPFromPixel(insets.left));
37 39
   }
38 40
 
39
-  static EdgeInsets getSafeAreaInsets(WindowManager windowManager, View rootView) {
41
+  static @Nullable EdgeInsets getSafeAreaInsets(WindowManager windowManager, View rootView) {
40 42
     // Window insets are parts of the window that are covered by system views (status bar,
41 43
     // navigation bar, notches). There are no apis the get these values for android < M so we
42 44
     // do a best effort polyfill.
43 45
     EdgeInsets windowInsets;
44 46
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
45 47
       WindowInsets insets = rootView.getRootWindowInsets();
48
+      if (insets == null) {
49
+        return null;
50
+      }
46 51
       windowInsets = new EdgeInsets(
47 52
           insets.getSystemWindowInsetTop(),
48 53
           insets.getSystemWindowInsetRight(),

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

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

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

@@ -2,6 +2,7 @@ package com.th3rdwave.safeareacontext;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.content.Context;
5
+import android.view.View;
5 6
 import android.view.WindowManager;
6 7
 
7 8
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -66,9 +67,18 @@ public class SafeAreaViewManager extends ViewGroupManager<SafeAreaView> {
66 67
       return null;
67 68
     }
68 69
 
69
-    EdgeInsets insets = SafeAreaUtils.getSafeAreaInsets(mWindowManager, activity.getWindow().getDecorView());
70
+    View decorView = activity.getWindow().getDecorView();
71
+    if (decorView == null) {
72
+      return null;
73
+    }
74
+
75
+    EdgeInsets insets = SafeAreaUtils.getSafeAreaInsets(mWindowManager, decorView);
76
+    if (insets == null) {
77
+      return null;
78
+    }
70 79
     return MapBuilder.<String, Object>of(
71 80
         "initialWindowSafeAreaInsets",
72 81
         SafeAreaUtils.edgeInsetsToJavaMap(insets));
82
+
73 83
   }
74 84
 }