react-native-screenguard: production crash on some devices java.lang.NullPointerException

Still receiving

Fatal Exception: java.lang.NullPointerException java.util.Objects.requireNonNull (Objects.java:203) com.screenguard.ScreenGuardModule.lambda$deactivateShield$2$com-screenguard-ScreenGuardModule (ScreenGuardModule.java:101)

using 0.3.0-stable

Usage:

` function initScreenGuard() { try { ScreenGuardModule.register( //insert any hex color you want here, default black if null or empty ‘#FF0000’, () => { alert(“Screenshots are not allowed!”); }); } catch (e) { console.warn(“ScreenGuardModule.register”, e); } }

useEffect(() => { initScreenGuard(); console.log(“initScreenGuard()”); return () => { ScreenGuardModule.unregister(); console.log(“ScreenGuardModule.unregister()”); };
}, []); `

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Just to note: no crashes so far either on ios/android with the fixed version, using it on larger active userbase!

The fix is working, no crashes so far! Also being tested on ios in live app.

It could be somehow when calling deactivateShield at native, at the 1st time call getReactApplicationContext().getCurrentActivity(), this is still not null yet. Unluckily, at 2nd call after checking, it became null -> crash.

Relax, the problem is way a few percent and the gap between those call getReactApplicationContext().getCurrentActivity() too little, but I still provide a quick fix for you if you want to:

@ReactMethod
    public void activateShield(String hexColor) {
        try {
            if (mHandlerBlockScreenShot == null) {
                mHandlerBlockScreenShot = new Handler(Looper.getMainLooper());
            }
            if (currentReactContext == null) {
                currentReactContext = getReactApplicationContext();
            }
            Activity currentActivity = currentReactContext.getCurrentActivity();
            if (currentActivity != null) {
                mHandlerBlockScreenShot.post(() ->
                                currentActivity.getWindow().setFlags(
                        WindowManager.LayoutParams.FLAG_SECURE, 
                        WindowManager.LayoutParams.FLAG_SECURE
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

@ReactMethod
    public void deactivateShield() {
        try {
            if (mHandlerBlockScreenShot == null) {
                mHandlerBlockScreenShot = new Handler(Looper.getMainLooper());
            }
            if (currentReactContext == null) {
                currentReactContext = getReactApplicationContext();
            }
            Activity currentActivity = currentReactContext.getCurrentActivity();
                if (currentActivity != null) {
                   mHandlerBlockScreenShot.post(() ->
                           currentActivity
                     .getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE));
                    mHandlerBlockScreenShot = null;
                } else {
                    Log.w("ACTIVITY_SCREENSHOT", "handler is null");
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

No crashes! The fix definitely works!

Thank you! Seems OK in the fresh realase! Waiting for more data from firebase to be sure. I’ll push it to another app with larger user base if it looks stable for a good while. That will probably reveal any hidden bugs if there is any, hopefully none:)

yes, please do the same with the deactivateShield as well and recheck carefully before I could apply this fix for next release

@ReactMethod
    public void activateShield() {
        try {
            if (mHandlerBlockScreenShot == null) {
                mHandlerBlockScreenShot = new Handler(Looper.getMainLooper());
            }
            if (getReactApplicationContext().getCurrentActivity() != null) {
                 mHandlerBlockScreenShot.post(() -> Objects.requireNonNull(
                    getReactApplicationContext().getCurrentActivity()
            ).getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_SECURE,
                    WindowManager.LayoutParams.FLAG_SECURE
            ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }