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

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

31
 
31
 
32
   private void maybeUpdateInsets() {
32
   private void maybeUpdateInsets() {
33
     EdgeInsets edgeInsets = SafeAreaUtils.getSafeAreaInsets(mWindowManager, getRootView());
33
     EdgeInsets edgeInsets = SafeAreaUtils.getSafeAreaInsets(mWindowManager, getRootView());
34
-    if (mLastInsets == null || !mLastInsets.equalsToEdgeInsets(edgeInsets)) {
34
+    if (edgeInsets != null && (mLastInsets == null || !mLastInsets.equalsToEdgeInsets(edgeInsets))) {
35
       Assertions.assertNotNull(mInsetsChangeListener).onInsetsChange(this, edgeInsets);
35
       Assertions.assertNotNull(mInsetsChangeListener).onInsetsChange(this, edgeInsets);
36
       mLastInsets = edgeInsets;
36
       mLastInsets = edgeInsets;
37
     }
37
     }

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

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.content.Context;
4
 import android.content.Context;
5
+import android.view.View;
5
 import android.view.WindowManager;
6
 import android.view.WindowManager;
6
 
7
 
7
 import com.facebook.react.bridge.ReactApplicationContext;
8
 import com.facebook.react.bridge.ReactApplicationContext;
66
       return null;
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
     return MapBuilder.<String, Object>of(
79
     return MapBuilder.<String, Object>of(
71
         "initialWindowSafeAreaInsets",
80
         "initialWindowSafeAreaInsets",
72
         SafeAreaUtils.edgeInsetsToJavaMap(insets));
81
         SafeAreaUtils.edgeInsetsToJavaMap(insets));
82
+
73
   }
83
   }
74
 }
84
 }