Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android shows permission on intial load #48

Open
Ahmedhamed77 opened this issue Aug 29, 2024 · 8 comments
Open

Android shows permission on intial load #48

Ahmedhamed77 opened this issue Aug 29, 2024 · 8 comments

Comments

@Ahmedhamed77
Copy link

thank you for the package
but in the latest build, I found that the package on Android asks for permissions once the app loads
in a real-world app we can't do that we can't ask users to permit photos and videos without telling them what exactly they are going to permit.

as you can see here once the app opens the permission just pop up and asks the user to give permission
is there a workaround for that?
Thank you

photo_2024-08-29_14-45-24

@wn-na
Copy link
Owner

wn-na commented Aug 30, 2024

@Ahmedhamed77 hello

In Android 13 and earlier, to detect a capture event, it detects that a capture image file is created.
To do this, it will ask for that permission.

In our example app, we require this permission to use the CaptureProtectionProvider.

@Ahmedhamed77
Copy link
Author

Ahmedhamed77 commented Aug 30, 2024

@wn-na thank you for your response
the problem is that on the first screen of the app, it always asks permission, not only in the example app but if you installed the package in a new app on the first screen the permission will popup,

so maybe we could have a workaround for Android 13 and earlier, just ask permission on the screen where we know that screenshots will be taken, there are some ways to detect screenshots so we could only ask permission in that case ? is that possible?

I was also playing with the code to check the permission problem

private boolean requestStoragePermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
      Log.d(NAME, "Permission is granted for under sdk version 23");
      return true;
    }

    if (getScreenCaptureCallback() != null) {
      return true;
    }

    try {
      String requestPermission = Build.VERSION.SDK_INT >= 33 // Build.VERSION_CODES.TIRAMISU
          ? "android.permission.READ_MEDIA_IMAGES" // Manifest.permission.READ_MEDIA_IMAGES
          : Manifest.permission.READ_EXTERNAL_STORAGE;

      if (getReactCurrentActivity() == null) {
        return false;
      }

      if (ContextCompat.checkSelfPermission(getReactCurrentActivity(),
          requestPermission) == PackageManager.PERMISSION_GRANTED) {
        Log.d(NAME, "Permission is granted");
        return true;
      } else {
        Log.d(NAME, "Permission is revoked");
        ActivityCompat.requestPermissions(getReactCurrentActivity(), new String[] { requestPermission }, 1);
        return false;
      }
    } catch (Exception e) {
      Log.e(NAME, "requestStoragePermission has raise Exception: " + e.getLocalizedMessage());
      return false;
    }
  }

  private void addListener() {
    if (getScreenCaptureCallback() == null) {
      if (contentObserver == null) {
        requestStoragePermission();
        contentObserver = new ContentObserver(Utils.MainHandler.INSTANCE) {
          @Override

and if we delete requestStoragePermission(); function from the addListener function it still works on Android 12 and 11 which I tested, no problems so far with permission !!

@wn-na
Copy link
Owner

wn-na commented Aug 30, 2024

@Ahmedhamed77 hello

I think it would be a good idea to ask for permission when calling prevent.

Even if you don't have that permission, you can still prevent screenshots, you just won't be able to detect the event that the screenshot was taken, so I don't think it's necessary to ask for the permission.

I'll give it some more thought and see how i can improve it.

@wn-na
Copy link
Owner

wn-na commented Aug 31, 2024

@Ahmedhamed77 hello
I've been thinking about it and would like to make the following changes first.

CaptureProtectionProvider no longer asks for permissions as soon as it starts.

When running preventScreenshot, preventScreenRecord, it will ask for permission.

If you have any better ideas, please let me know!

@Ahmedhamed77
Copy link
Author

Ahmedhamed77 commented Aug 31, 2024

@wn-na I think this is the best approach, I will be waiting
but there is no way to ask this while the user is taking a screenshot, right ? like when we detect that the user is already taking a screenshot, we could ask for permission, this is can't be done in Android ?
thank you

@wn-na
Copy link
Owner

wn-na commented Sep 2, 2024

@Ahmedhamed77 hi
i deployed to version 1.9.12, would you like to check it out?

@Ahmedhamed77
Copy link
Author

@wn-na
it's working now only the capture protection is called, that's nice
even though I would say it's not user-friendly
for example, I have a screen where I only have text and I try to prevent screenshots there, once the user enters the screen there is a popup of permission where they need to allow photos and videos permission, even if there is nothing related to that. so as I user, I would say that's a spam, what do you think ?

@wn-na
Copy link
Owner

wn-na commented Sep 4, 2024

@Ahmedhamed77 When asking for media permissions in a text-only app, it can feel like spam, but before Android 14, there was no way to detect screenshots other than that method, so it's unavoidable.
It could be improved by not asking for those permissions and only preventing screenshots.

i'm sure with a little more thought, i can come up with a good way to do it, but it's hard right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants