From 06126facadebf34469ceb26a504871e78e35ecb6 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Thu, 15 Oct 2020 17:43:13 +0200 Subject: [PATCH] Optimizing checkboxes behavior --- .../droidfs/ChangePasswordActivity.kt | 52 +++++++++---------- .../sushi/hardcore/droidfs/CreateActivity.kt | 14 +++-- .../sushi/hardcore/droidfs/OpenActivity.kt | 40 ++++++++++---- .../droidfs/adapters/SavedVolumesAdapter.kt | 6 ++- .../FingerprintPasswordHashSaver.kt | 20 ++++--- .../res/layout/activity_change_password.xml | 16 +----- app/src/main/res/layout/activity_create.xml | 16 +----- app/src/main/res/layout/activity_open.xml | 16 +----- .../main/res/layout/checkboxes_section.xml | 22 ++++++++ 9 files changed, 100 insertions(+), 102 deletions(-) create mode 100644 app/src/main/res/layout/checkboxes_section.xml diff --git a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt index 0b48ebd..f15a506 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt @@ -11,9 +11,8 @@ import android.widget.AdapterView.OnItemClickListener import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_change_password.* -import kotlinx.android.synthetic.main.activity_change_password.checkbox_remember_path -import kotlinx.android.synthetic.main.activity_change_password.checkbox_save_password import kotlinx.android.synthetic.main.activity_change_password.saved_path_listview +import kotlinx.android.synthetic.main.checkboxes_section.* import kotlinx.android.synthetic.main.toolbar.* import kotlinx.android.synthetic.main.volume_path_section.* import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter @@ -27,6 +26,7 @@ class ChangePasswordActivity : BaseActivity() { companion object { private const val PICK_DIRECTORY_REQUEST_CODE = 1 } + private lateinit var savedVolumesAdapter: SavedVolumesAdapter private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver private lateinit var rootCipherDir: String private var usf_fingerprint = false @@ -41,7 +41,7 @@ class ChangePasswordActivity : BaseActivity() { } else { WidgetUtil.hide(checkbox_save_password) } - val savedVolumesAdapter = SavedVolumesAdapter(this, sharedPrefs) + savedVolumesAdapter = SavedVolumesAdapter(this, sharedPrefs) if (savedVolumesAdapter.count > 0){ saved_path_listview.adapter = savedVolumesAdapter saved_path_listview.onItemClickListener = OnItemClickListener { _, _, position, _ -> @@ -56,13 +56,21 @@ class ChangePasswordActivity : BaseActivity() { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - if (sharedPrefs.getString(s.toString(), null) == null) { + if (savedVolumesAdapter.isPathSaved(s.toString())){ + checkbox_remember_path.isEnabled = false + checkbox_remember_path.isChecked = false + if (sharedPrefs.getString(s.toString(), null) != null){ + edit_old_password.text = null + edit_old_password.hint = getString(R.string.hash_saved_hint) + edit_old_password.isEnabled = false + } else { + edit_old_password.hint = null + edit_old_password.isEnabled = true + } + } else { + checkbox_remember_path.isEnabled = true edit_old_password.hint = null edit_old_password.isEnabled = true - } else { - edit_old_password.text = null - edit_old_password.hint = getString(R.string.hash_saved_hint) - edit_old_password.isEnabled = false } } }) @@ -161,31 +169,19 @@ class ChangePasswordActivity : BaseActivity() { returnedHash ) ) { - val editor = sharedPrefs.edit() if (sharedPrefs.getString(rootCipherDir, null) != null) { + val editor = sharedPrefs.edit() editor.remove(rootCipherDir) editor.apply() } - var continueImmediately = true if (checkbox_remember_path.isChecked) { - val oldSavedVolumesPaths = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set - val newSavedVolumesPaths = oldSavedVolumesPaths.toMutableList() - if (!oldSavedVolumesPaths.contains(rootCipherDir)) { - newSavedVolumesPaths.add(rootCipherDir) - editor.putStringSet( - ConstValues.saved_volumes_key, - newSavedVolumesPaths.toSet() - ) - editor.apply() - } - if (checkbox_save_password.isChecked && returnedHash != null) { - fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ -> - stopTask { onPasswordChanged() } - } - continueImmediately = false - } + savedVolumesAdapter.addVolumePath(rootCipherDir) } - if (continueImmediately) { + if (checkbox_save_password.isChecked && returnedHash != null) { + fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ -> + stopTask { onPasswordChanged() } + } + } else { stopTask { onPasswordChanged() } } } else { @@ -222,7 +218,7 @@ class ChangePasswordActivity : BaseActivity() { if (!fingerprintPasswordHashSaver.canAuthenticate()){ checkbox_save_password.isChecked = false } else { - checkbox_remember_path.isChecked = true + checkbox_remember_path.isChecked = checkbox_remember_path.isEnabled } } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt index 3eeac35..b97112b 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt @@ -8,6 +8,7 @@ import android.view.View import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_create.* +import kotlinx.android.synthetic.main.checkboxes_section.* import kotlinx.android.synthetic.main.toolbar.* import kotlinx.android.synthetic.main.volume_path_section.* import sushi.hardcore.droidfs.explorers.ExplorerActivity @@ -131,7 +132,6 @@ class CreateActivity : BaseActivity() { } sessionID = GocryptfsVolume.init(rootCipherDir, password, null, returnedHash) if (sessionID != -1) { - var startExplorerImmediately = true if (checkbox_remember_path.isChecked) { val oldSavedVolumesPaths = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set val editor = sharedPrefs.edit() @@ -145,14 +145,12 @@ class CreateActivity : BaseActivity() { editor.putStringSet(ConstValues.saved_volumes_key, newSavedVolumesPaths.toSet()) } editor.apply() - if (checkbox_save_password.isChecked && returnedHash != null){ - fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir){ _ -> - stopTask { startExplorer() } - } - startExplorerImmediately = false - } } - if (startExplorerImmediately){ + if (checkbox_save_password.isChecked && returnedHash != null){ + fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir){ _ -> + stopTask { startExplorer() } + } + } else { stopTask { startExplorer() } } } else { diff --git a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt index 1f9d8b2..31f0493 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt @@ -4,12 +4,16 @@ import android.app.Activity import android.content.Intent import android.os.Build import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher import android.view.MenuItem import android.view.View import android.widget.AdapterView.OnItemClickListener import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_open.* +import kotlinx.android.synthetic.main.activity_open.saved_path_listview +import kotlinx.android.synthetic.main.checkboxes_section.* import kotlinx.android.synthetic.main.toolbar.* import kotlinx.android.synthetic.main.volume_path_section.* import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter @@ -59,6 +63,27 @@ class OpenActivity : BaseActivity() { } else { WidgetUtil.hide(saved_path_listview) } + edit_volume_path.addTextChangedListener(object: TextWatcher { + override fun afterTextChanged(s: Editable?) { + } + override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { + } + override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { + if (savedVolumesAdapter.isPathSaved(s.toString())){ + checkbox_remember_path.isEnabled = false + checkbox_remember_path.isChecked = false + if (sharedPrefs.getString(s.toString(), null) != null){ + checkbox_save_password.isEnabled = false + checkbox_save_password.isChecked = false + } else { + checkbox_save_password.isEnabled = true + } + } else { + checkbox_remember_path.isEnabled = true + checkbox_save_password.isEnabled = true + } + } + }) edit_password.setOnEditorActionListener { v, _, _ -> onClickOpen(v) true @@ -145,17 +170,14 @@ class OpenActivity : BaseActivity() { } sessionID = GocryptfsVolume.init(rootCipherDir, password, null, returnedHash) if (sessionID != -1) { - var startExplorerImmediately = true if (checkbox_remember_path.isChecked) { savedVolumesAdapter.addVolumePath(rootCipherDir) - if (checkbox_save_password.isChecked && returnedHash != null){ - fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ -> - stopTask { startExplorer() } - } - startExplorerImmediately = false - } } - if (startExplorerImmediately){ + if (checkbox_save_password.isChecked && returnedHash != null){ + fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ -> + stopTask { startExplorer() } + } + } else { stopTask { startExplorer() } } } else { @@ -221,7 +243,7 @@ class OpenActivity : BaseActivity() { if (!fingerprintPasswordHashSaver.canAuthenticate()){ checkbox_save_password.isChecked = false } else { - checkbox_remember_path.isChecked = true + checkbox_remember_path.isChecked = checkbox_remember_path.isEnabled } } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/adapters/SavedVolumesAdapter.kt b/app/src/main/java/sushi/hardcore/droidfs/adapters/SavedVolumesAdapter.kt index 8e7cde2..77a0831 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/adapters/SavedVolumesAdapter.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/adapters/SavedVolumesAdapter.kt @@ -96,8 +96,12 @@ class SavedVolumesAdapter(val context: Context, private val sharedPrefs: SharedP } } + fun isPathSaved(volume_path: String): Boolean { + return savedVolumesPaths.contains(volume_path) + } + fun addVolumePath(volume_path: String) { - if (!savedVolumesPaths.contains(volume_path)) { + if (!isPathSaved(volume_path)) { savedVolumesPaths.add(volume_path) updateSharedPrefs() } diff --git a/app/src/main/java/sushi/hardcore/droidfs/fingerprint_stuff/FingerprintPasswordHashSaver.kt b/app/src/main/java/sushi/hardcore/droidfs/fingerprint_stuff/FingerprintPasswordHashSaver.kt index 6becbde..85473e6 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/fingerprint_stuff/FingerprintPasswordHashSaver.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/fingerprint_stuff/FingerprintPasswordHashSaver.kt @@ -101,18 +101,16 @@ class FingerprintPasswordHashSaver(private val activityContext: AppCompatActivit isPrepared = true } fun encryptAndSave(plainText: ByteArray, root_cipher_dir: String, onAuthenticationResult: (success: Boolean) -> Unit){ - if (shared_prefs.getString(root_cipher_dir, null) == null){ - this.rootCipherDir = root_cipher_dir - this.actionDescription = activityContext.getString(R.string.encrypt_action_description) - this.onAuthenticationResult = onAuthenticationResult - if (!isPrepared){ - prepare() - } - dataToProcess = plainText - actionMode = Cipher.ENCRYPT_MODE - cipher.init(Cipher.ENCRYPT_MODE, key) - startListening() + this.rootCipherDir = root_cipher_dir + this.actionDescription = activityContext.getString(R.string.encrypt_action_description) + this.onAuthenticationResult = onAuthenticationResult + if (!isPrepared){ + prepare() } + dataToProcess = plainText + actionMode = Cipher.ENCRYPT_MODE + cipher.init(Cipher.ENCRYPT_MODE, key) + startListening() } fun decrypt(cipherText: String, root_cipher_dir: String, onPasswordDecrypted: (password: ByteArray) -> Unit){ this.rootCipherDir = root_cipher_dir diff --git a/app/src/main/res/layout/activity_change_password.xml b/app/src/main/res/layout/activity_change_password.xml index bce890f..5d8590a 100644 --- a/app/src/main/res/layout/activity_change_password.xml +++ b/app/src/main/res/layout/activity_change_password.xml @@ -79,21 +79,7 @@ - - - + - - - + - - - + + + + + + + + \ No newline at end of file