diff --git a/src/android/OurCodeWorldpreventscreenshots.java b/src/android/OurCodeWorldpreventscreenshots.java index 9102ce0..875a0cc 100644 --- a/src/android/OurCodeWorldpreventscreenshots.java +++ b/src/android/OurCodeWorldpreventscreenshots.java @@ -12,17 +12,41 @@ public class OurCodeWorldpreventscreenshots extends CordovaPlugin { @Override public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException { + final CallbackContext callbacks = callbackContext; + if (ACTION_ENABLE.equals(action)) { cordova.getActivity().runOnUiThread(new Runnable() { public void run() { - cordova.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, - WindowManager.LayoutParams.FLAG_SECURE); + try{ + // Allow to make screenshots removing the FLAG_SECURE + cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); + + PluginResult result = new PluginResult(PluginResult.Status.OK, ""); + result.setKeepCallback(true); + callbacks.sendPluginResult(result); + }catch(Exception e){ + PluginResult result = new PluginResult(PluginResult.Status.ERROR, ""); + result.setKeepCallback(true); + callbacks.sendPluginResult(result); + } } }); }else if(ACTION_DISABLE.equals(action)){ cordova.getActivity().runOnUiThread(new Runnable() { public void run() { - cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); + try{ + // Disable the creation of screenshots adding the FLAG_SECURE to the window + cordova.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, + WindowManager.LayoutParams.FLAG_SECURE); + + PluginResult result = new PluginResult(PluginResult.Status.OK, ""); + result.setKeepCallback(true); + callbacks.sendPluginResult(result); + }catch(Exception e){ + PluginResult result = new PluginResult(PluginResult.Status.ERROR, ""); + result.setKeepCallback(true); + callbacks.sendPluginResult(result); + } } }); } diff --git a/www/ourcodeworldpreventscreenshots.js b/www/ourcodeworldpreventscreenshots.js index 51d7a2c..e5df38a 100644 --- a/www/ourcodeworldpreventscreenshots.js +++ b/www/ourcodeworldpreventscreenshots.js @@ -1,18 +1,43 @@ /*global cordova, module*/ +(function(module){ + function PreventScreenshots(){ + var core = {}; + var isEnabled = false; -module.exports = { - enableScreenshots: function(){ - cordova.exec(function(data){ - console.info(data); - }, function(err){ - console.error(err); - }, "OurCodeWorldpreventscreenshots", "enable", []); - }, - disableScreenshots: function(){ - cordova.exec(function(data){ - console.info(data); - }, function(err){ - console.error(err); - }, "OurCodeWorldpreventscreenshots", "disable", []); + var callFunctionIfExists = function(fn,params){ + if(typeof(fn) !== "function"){ + return false; + } + + fn.call(); + return true; + }; + + core.enable = function(success,error){ + cordova.exec(function(data){ + isEnabled = true; + callFunctionIfExists(success); + }, function(err){ + callFunctionIfExists(error); + }, "OurCodeWorldpreventscreenshots", "enable", []); + }; + + core.disable = function(success,error){ + cordova.exec(function(data){ + isEnabled = false; + callFunctionIfExists(success); + }, function(err){ + callFunctionIfExists(error); + }, "OurCodeWorldpreventscreenshots", "disable", []); + }; + + core.isEnabled = function(){ + return isEnabled; + }; + + + return core; } -}; + + module.exports = new PreventScreenshots(); +})(module);