From 03dfe7d7345b366aaaacae7e044734c5d3640377 Mon Sep 17 00:00:00 2001 From: William Swanson Date: Tue, 29 Sep 2020 16:35:26 -0700 Subject: [PATCH 1/2] Do not crash when the main activity is unavailable The React Native application context has a method for obtaining the current Android activity, but this sometimes returns null. We believe this happens due to a race condition between the Javascript engine and the native code, but we aren't completely sure. What we do know is that this situation happens in the wild, since it shows up in our Bugsnag reports. The fix is the leave the BlurView native component uninitialized in these cases. This prevents it from rendering anything, but that's preferable to crashing the whole app. --- .../com/cmcewen/blurview/BlurViewManager.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java b/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java index b583b026..60699c42 100644 --- a/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java +++ b/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java @@ -5,12 +5,9 @@ import android.view.ViewGroup; import com.facebook.react.uimanager.ViewGroupManager; -import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.annotations.ReactProp; -import java.util.Objects; - import javax.annotation.Nonnull; import eightbitlab.com.blurview.BlurView; @@ -32,14 +29,17 @@ class BlurViewManager extends ViewGroupManager { @Override public @Nonnull BlurView createViewInstance(@Nonnull ThemedReactContext ctx) { BlurView blurView = new BlurView(ctx); - View decorView = Objects.requireNonNull(ctx.getCurrentActivity()).getWindow().getDecorView(); - ViewGroup rootView = decorView.findViewById(android.R.id.content); - Drawable windowBackground = decorView.getBackground(); - blurView.setupWith(rootView) - .setFrameClearDrawable(windowBackground) - .setBlurAlgorithm(new RenderScriptBlur(ctx)) - .setBlurRadius(defaultRadius) - .setHasFixedTransformationMatrix(false); + Activity currentActivity = ctx.getCurrentActivity(); + if (currentActivity != null) { + View decorView = currentActivity.getWindow().getDecorView(); + ViewGroup rootView = decorView.findViewById(android.R.id.content); + Drawable windowBackground = decorView.getBackground(); + blurView.setupWith(rootView) + .setFrameClearDrawable(windowBackground) + .setBlurAlgorithm(new RenderScriptBlur(ctx)) + .setBlurRadius(defaultRadius) + .setHasFixedTransformationMatrix(false); + } return blurView; } From a87cd4a30d7717ed901d791b737e4d5a63c49b9c Mon Sep 17 00:00:00 2001 From: William Swanson Date: Tue, 10 Nov 2020 16:13:54 -0800 Subject: [PATCH 2/2] Add missing `Activity` import --- android/src/main/java/com/cmcewen/blurview/BlurViewManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java b/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java index 60699c42..897920c4 100644 --- a/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java +++ b/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java @@ -1,5 +1,6 @@ package com.cmcewen.blurview; +import android.app.Activity; import android.graphics.drawable.Drawable; import android.view.View; import android.view.ViewGroup;