Skip to content

Add Enable/Disable functionality to Android #36

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 105 additions & 2 deletions src/android/PrivacyScreenPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.apache.cordova.CordovaWebView;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

Expand All @@ -24,14 +27,114 @@

/**
* This class sets the FLAG_SECURE flag on the window to make the app
* private when shown in the task switcher
* private when shown in the task switcher
*/
public class PrivacyScreenPlugin extends CordovaPlugin {
public static final String KEY_PRIVACY_SCREEN_ENABLED = "org.devgeeks.privacyscreen/PrivacyScreenEnabled";
private SharedPreferences preferences;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
Activity activity = this.cordova.getActivity();
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
preferences = PreferenceManager.getDefaultSharedPreferences(activity);
boolean privacyScreenEnabled = isPrivacyScreenEnabled(true);

if (privacyScreenEnabled) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
}

/**
* Gets the value of the Privacy Screen Enabled entry stored in {@link SharedPreferences}.
* Will not attempt to correct an erroneous entry.
*
* @param defValue The default value of the preference if not readable or non-existent.
* @return Whether the privacy screen should be enabled during the next application launch.
* @see #KEY_PRIVACY_SCREEN_ENABLED
* @see #isPrivacyScreenEnabled(boolean, boolean)
*/
private boolean isPrivacyScreenEnabled(boolean defValue) {
return isPrivacyScreenEnabled(defValue, false);
}

/**
* Gets the value of the Privacy Screen Enabled entry stored in {@link SharedPreferences}.
*
* @param defValue The default value of the preference if not readable or non-existent.
* @param shouldCorrect If true, will attempt to convert String value to Boolean or replace with the default value.
* @return Whether the privacy screen should be enabled during the next application launch.
* @see #KEY_PRIVACY_SCREEN_ENABLED
*/
private boolean isPrivacyScreenEnabled(boolean defValue, boolean shouldCorrect) {
if (!preferences.contains(KEY_PRIVACY_SCREEN_ENABLED)) {
setPrivacyScreenEnabled(defValue);
return defValue;
}
try {
return preferences.getBoolean(KEY_PRIVACY_SCREEN_ENABLED, defValue);
} catch (ClassCastException e) {
Log.w("PrivacyScreen", "SharedPreference '" + KEY_PRIVACY_SCREEN_ENABLED + "' was not a Boolean value.", e);

if (shouldCorrect) {
if (convertStringEntryToBoolean()) {
return preferences.getBoolean(KEY_PRIVACY_SCREEN_ENABLED, defValue);
}
setPrivacyScreenEnabled(defValue);
}
}
return defValue;
}

/**
* Converts the entry from a {@link String} to {@link Boolean} if possible.
*
* @return true if the entry was a {@code String} value that could be resolved to a {@code Boolean} value, false otherwise.
* @see #isPrivacyScreenEnabled(boolean, boolean)
* @see #KEY_PRIVACY_SCREEN_ENABLED
*/
private boolean convertStringEntryToBoolean() {
try {
String val = preferences.getString(KEY_PRIVACY_SCREEN_ENABLED, null);
if (val == null) {
return false;
}
if (val.equalsIgnoreCase("true")) {
setPrivacyScreenEnabled(true);
return true;
} else if (val.equalsIgnoreCase("false")) {
setPrivacyScreenEnabled(false);
return true;
}
} catch (ClassCastException e) {
Log.w("PrivacyScreen", "SharedPreference '" + KEY_PRIVACY_SCREEN_ENABLED + "' was not a String value.", e);
}
return false;
}

/**
* Sets the value of the Privacy Screen Enabled entry in {@link SharedPreferences}.
*
* @param value the value to set.
* @return true if successful, false otherwise.
* @see #KEY_PRIVACY_SCREEN_ENABLED
*/
private boolean setPrivacyScreenEnabled(boolean value) {
preferences.edit().putBoolean(KEY_PRIVACY_SCREEN_ENABLED, value).apply();
return true;
}

@Override
public Object onMessage(String id, Object data) {
if (id == "isPrivacyScreenEnabled") {
boolean shouldCorrect = (data instanceof Boolean) && (data != null) && ((Boolean) data);
return isPrivacyScreenEnabled(true, shouldCorrect);
} else if (id == "setPrivacyScreenEnabled") {
if (data instanceof Boolean && data != null) {
return setPrivacyScreenEnabled((Boolean) data);
}
return false;
}
return null;
}
}