how to disable screenshot in capacitor plugin only for few pages not for entire app
Asked Answered
F

2

6

it's an ionic capacitor angular project in which I am trying to build a capacitor plugin so that I can disable screenshots only for required pages/screens.

I have used getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); within MainActivity inside onCreate() method, it works but it disables the screenshot for entire application which is not the desired outcome.

MainAcitivity.java:

public class MainActivity extends BridgeActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(Contacts.class);
      add(Screenshot.class);
    }});
  }
}

Now I have capacitor plugin "screenshot-plugin" in which I have 2 methods to "SET" and "CLEAR" the flag for particular pages/screen in project.

Screenshot.java:

@NativePlugin
public class Screenshot extends Plugin {

    @PluginMethod
    public void echo(PluginCall call) {
        String value = call.getString("value");

        JSObject ret = new JSObject();
        ret.put("value", value);
        call.success(ret);
    }

    @PluginMethod
    public void enableScreenshot(PluginCall call) {
        try {
            Activity activity = getBridge().getActivity();
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
        } catch (Exception e) {
            Log.e("ABC", "Exception in enableScreenshot", e);
        }
        JSObject ret = new JSObject();
        ret.put("status", true);
        call.success(ret);
    }

    @PluginMethod
    public void disableScreenshot(PluginCall call) {
        try {
            Activity activity = getBridge().getActivity();
            activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        } catch (Exception e) {
            Log.e("ABC", "Exception in disableScreenshot", e);
        }
        JSObject ret = new JSObject();
        ret.put("status", true);
        call.success(ret);
    }

}

Here I get an exception that only thread which created the view can modify it.

So I tried using MainActivity activity = new MainActivity() so that I can call getWindow() method on this activity but this gives an error cannot resolve symbol even when package is imported com.abc.myapp.

Also, when I try to use getWindow() without activity inside screenshot plugin then AndroidStudio gives compilation error, using getBridge().getActivity() and then calling getWindow() method (as in code) removes compilation error but gives above exception of only MainActivity can do so.

I could write these 2 methods in MainActivity itself but then not sure how to access these methods in ionic project component.

Using plugin I can call these methods inside my component but how to make this work for only few components/pages/screens and not for the entire android application.

Please help, Thanks in advance.

I know similar questions are already there but their use case and mine are different.

Favour answered 1/3, 2021 at 12:9 Comment(2)
in this case pass flag for 'screenshot' enable and disableHeavierthanair
Hi @RaviAshara, I couldn't understand what you are trying to say. I know how to set and clear the flag but it's about making it work properly through MainActivity class or via plugin class. If you could give any code reference or examples that would help. Thanks.Favour
D
0

You can do like this in page where you want to disable:

  getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, 
  WindowManager.LayoutParams.FLAG_SECURE);

And remove flag from page where you don't want:

  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); 

Note: Not 100% sure but may be it give some idea.

Davidadavidde answered 1/3, 2021 at 17:53 Comment(4)
i have used this already in my code, please check out.Favour
do you 100% sure this line working properly? Activity activity = getBridge().getActivity();Davidadavidde
see your exception saying only activity which created the view can modify it. That means you can only change flag from your main activity.Davidadavidde
Hi, yes am 100% sure, it works fine, actually it was thread issue not the activity, I have edited my question now.Favour
F
0

found the answer to this, I built my own capacitor plugin and it works fine, but there is another plugin that is already published in the capacitor community and it supports IOS too, so check this out https://github.com/capacitor-community/privacy-screen

for those of you who want to know how it worked successfully, you have to put your code like this:

@PluginMethod
    public void enableScreenshot(PluginCall call) {
        final Activity activity = getBridge().getActivity();
        final JSObject ret = new JSObject();
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
                } catch (Exception e) {
                    Log.e("ABC", "Exception in enableScreenshot", e);
                    ret.put("status", false);
                }
            }
        });
        if (!ret.has("status")) {
            ret.put("status", true);
        } else {
            call.reject("Exception in enableScreenshot");
        }
        call.success(ret);
    }

@PluginMethod
    public void disableScreenshot(PluginCall call) {
        final Activity activity = getBridge().getActivity();
        final JSObject ret = new JSObject();
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
                    // Toast.makeText(activity, "Screenshot not allowed on this page.", Toast.LENGTH_SHORT);
                } catch (Exception e) {
                    Log.e("ABC", "Exception in disableScreenshot", e);
                    ret.put("status", false);
                }
            }
        });
        if (!ret.has("status")) {
            ret.put("status", true);
        } else {
            call.reject("Exception in enableScreenshot");
        }
        call.success(ret);
    }

activity.runOnUiThread() is the hero.

Favour answered 29/4, 2021 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.