wn-na / react-native-capture-protection

It’s a library for React Native to control simple capture events(i.e. Screenshot or Screen record)
https://www.npmjs.com/package/react-native-capture-protection
MIT License
102 stars 10 forks source link

issue: Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference #45

Closed rokkoo closed 3 months ago

rokkoo commented 4 months ago

Hi! Im receiving the following error on sentry:

Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference

The error trace is the following:

Error: Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference
  at promiseMethodWrapper(app:///index.android.bundle:1:414478)
  at ?anon_0_(app:///index.android.bundle:1:4668007)
  at next(native)
  at asyncGeneratorStep(app:///index.android.bundle:1:223136)
  at _next(app:///index.android.bundle:1:223392)
  at anonymous(app:///index.android.bundle:1:223344)
  at tryCallTwo(app:///index.android.bundle:1:519844)
  at doResolve(app:///index.android.bundle:1:521169)
  at Promise(app:///index.android.bundle:1:519940)
  at anonymous(app:///index.android.bundle:1:223265)
  at apply(native)
  at allowScreenshot(app:///index.android.bundle:1:4668055)
  at anonymous(app:///index.android.bundle:1:4663856)
  at commitHookEffectListMount(app:///index.android.bundle:1:730431)
  at flushPassiveEffects(app:///index.android.bundle:1:744349)
  at anonymous(app:///index.android.bundle:1:743464)

Checking the error I think the problem comes on the following code on the android/src/main/java/com/captureprotection/CaptureProtectionModule.java

  @ReactMethod
  public void preventScreenshot(Promise promise) {
    runOnUiThread(() -> {
      try {
       // Do we need check is the activity is null?
        getCurrentActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        sendEvent(CaptureProtectionConstant.LISTENER_EVENT_NAME, true, true,
            CaptureProtectionConstant.CaptureProtectionModuleStatus.UNKNOWN.ordinal());
        addListener();
        promise.resolve(true);
      } catch (Exception e) {
        promise.reject("preventScreenshot", e);
      }
    });
  }

In order to avoid this error I think we need to check is the current activity is available an then do the rest, but Im not sure if this implies more changes

Info

"react": "18.2.0",
"react-native": "0.73.5",
"react-native-capture-protection": "1.9.6"
wn-na commented 3 months ago

@rokkoo thanx to report this. is this issue a frequent occurrence? Anyway, i will fix this issue!

rokkoo commented 3 months ago

@rokkoo thanx to report this. is this issue a frequent occurrence? Anyway, i will fix this issue!

Hi @wn-na ! Yes is very frequent on my side, let me know if I can do something for help you, or if I can test it when it is done :)

Thanks!

wn-na commented 3 months ago

hi @rokkoo! I've fixed the above issue and deployed to 1.9.11, can you check it out?

P.S It seems related to this issue https://github.com/facebook/react-native/issues/37518

rokkoo commented 3 months ago

Hi @wn-na thanks for fixing it so quickly, I tested in my devices and it works fine, but this error I couldn't replicate by my self, Im gonna release this upgrade in my next release If it still there I can reopen this issue if you want :)

thanks!

wn-na commented 3 months ago

hi @rokkoo thanx to report. As always, if you have any issues with this, please open an issue again!