aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/view
diff options
context:
space:
mode:
authorKevin T. Berstene <kberstene@gmail.com>2019-04-01 14:30:43 -0400
committerKevin T. Berstene <kberstene@gmail.com>2019-04-01 14:30:43 -0400
commit0e5c95e5cb6f7db5cc1c3ae711f622378d8ef786 (patch)
tree252846940f1e75a34b8da511d1975c68fa00c1f1 /app/src/main/java/github/daneren2005/dsub/view
parent2b26df335ccff17e3970ac94f4c0abfbd6898a47 (diff)
downloaddsub-0e5c95e5cb6f7db5cc1c3ae711f622378d8ef786.tar.gz
dsub-0e5c95e5cb6f7db5cc1c3ae711f622378d8ef786.tar.bz2
dsub-0e5c95e5cb6f7db5cc1c3ae711f622378d8ef786.zip
Added password encryption for SDK 23 and higher
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/view')
-rw-r--r--app/src/main/java/github/daneren2005/dsub/view/EditPasswordPreference.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/view/EditPasswordPreference.java b/app/src/main/java/github/daneren2005/dsub/view/EditPasswordPreference.java
new file mode 100644
index 00000000..718fc22d
--- /dev/null
+++ b/app/src/main/java/github/daneren2005/dsub/view/EditPasswordPreference.java
@@ -0,0 +1,77 @@
+package github.daneren2005.dsub.view;
+
+import android.content.Context;
+import android.os.Build;
+import android.preference.EditTextPreference;
+import android.preference.Preference;
+
+import github.daneren2005.dsub.util.Constants;
+import github.daneren2005.dsub.util.KeyStoreUtil;
+import github.daneren2005.dsub.util.Util;
+
+public class EditPasswordPreference extends EditTextPreference {
+ final private String TAG = EditPasswordPreference.class.getSimpleName();
+
+ private int instance;
+ private boolean passwordDecrypted;
+
+ public EditPasswordPreference(Context context, int instance) {
+ super(context);
+
+ final EditPasswordPreference editPassPref = this;
+ this.instance = instance;
+
+ if (Build.VERSION.SDK_INT >= 23) {
+ this.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ return editPassPref.onPreferenceClick();
+ }
+ });
+ }
+ }
+
+ private boolean onPreferenceClick() {
+ Context context = this.getContext();
+
+ // If password is encrypted, attempt to decrypt it to list actual password in masked box
+ // It could be that we should fill in nonsense in here instead, but if we do and the user clicks OK,
+ // the nonsense will be encrypted and the server connection will fail
+ // Checks first to see if the password has already been decrypted - if the user clicks on the preference a second time
+ // before the box has loaded, but after the password has already been encrypted
+ if (!(passwordDecrypted) && (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_ENCRYPTED_PASSWORD + this.instance, false))) {
+ String decryptedPassword = KeyStoreUtil.decrypt(this.getEditText().getText().toString());
+ if (decryptedPassword != null) {
+ this.getEditText().setText(decryptedPassword);
+ this.passwordDecrypted = true;
+ } else {
+ Util.toast(context, "Password Decryption Failed");
+ }
+ }
+
+ // Let the click action continue as normal
+ return false;
+ }
+
+ @Override
+ protected void onDialogClosed(boolean positiveResult) {
+ if ((positiveResult) && (Build.VERSION.SDK_INT >= 23)) {
+ Context context = this.getContext();
+
+ String encryptedString = KeyStoreUtil.encrypt(this.getEditText().getText().toString());
+ if (encryptedString != null) {
+ this.getEditText().setText(encryptedString);
+ Util.getPreferences(context).edit().putBoolean(Constants.PREFERENCES_KEY_ENCRYPTED_PASSWORD + instance, true).commit();
+ } else {
+ Util.toast(context, "Password encryption failed");
+ Util.getPreferences(context).edit().putBoolean(Constants.PREFERENCES_KEY_ENCRYPTED_PASSWORD + instance, false).commit();
+ }
+ }
+
+ // Reset this flag so it decrypts if applicable next time the dialog is opened
+ this.passwordDecrypted = false;
+
+ // Continue the dialog closing process
+ super.onDialogClosed(positiveResult);
+ }
+}