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.