Optimizing checkboxes behavior

This commit is contained in:
Hardcore Sushi 2020-10-15 17:43:13 +02:00
parent b482b96322
commit 06126facad
9 changed files with 100 additions and 102 deletions

View File

@ -11,9 +11,8 @@ import android.widget.AdapterView.OnItemClickListener
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_change_password.* 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.activity_change_password.saved_path_listview
import kotlinx.android.synthetic.main.checkboxes_section.*
import kotlinx.android.synthetic.main.toolbar.* import kotlinx.android.synthetic.main.toolbar.*
import kotlinx.android.synthetic.main.volume_path_section.* import kotlinx.android.synthetic.main.volume_path_section.*
import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter
@ -27,6 +26,7 @@ class ChangePasswordActivity : BaseActivity() {
companion object { companion object {
private const val PICK_DIRECTORY_REQUEST_CODE = 1 private const val PICK_DIRECTORY_REQUEST_CODE = 1
} }
private lateinit var savedVolumesAdapter: SavedVolumesAdapter
private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver
private lateinit var rootCipherDir: String private lateinit var rootCipherDir: String
private var usf_fingerprint = false private var usf_fingerprint = false
@ -41,7 +41,7 @@ class ChangePasswordActivity : BaseActivity() {
} else { } else {
WidgetUtil.hide(checkbox_save_password) WidgetUtil.hide(checkbox_save_password)
} }
val savedVolumesAdapter = SavedVolumesAdapter(this, sharedPrefs) savedVolumesAdapter = SavedVolumesAdapter(this, sharedPrefs)
if (savedVolumesAdapter.count > 0){ if (savedVolumesAdapter.count > 0){
saved_path_listview.adapter = savedVolumesAdapter saved_path_listview.adapter = savedVolumesAdapter
saved_path_listview.onItemClickListener = OnItemClickListener { _, _, position, _ -> 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 beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
} }
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (sharedPrefs.getString(s.toString(), null) == null) { if (savedVolumesAdapter.isPathSaved(s.toString())){
edit_old_password.hint = null checkbox_remember_path.isEnabled = false
edit_old_password.isEnabled = true checkbox_remember_path.isChecked = false
} else { if (sharedPrefs.getString(s.toString(), null) != null){
edit_old_password.text = null edit_old_password.text = null
edit_old_password.hint = getString(R.string.hash_saved_hint) edit_old_password.hint = getString(R.string.hash_saved_hint)
edit_old_password.isEnabled = false 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
} }
} }
}) })
@ -161,31 +169,19 @@ class ChangePasswordActivity : BaseActivity() {
returnedHash returnedHash
) )
) { ) {
val editor = sharedPrefs.edit()
if (sharedPrefs.getString(rootCipherDir, null) != null) { if (sharedPrefs.getString(rootCipherDir, null) != null) {
val editor = sharedPrefs.edit()
editor.remove(rootCipherDir) editor.remove(rootCipherDir)
editor.apply() editor.apply()
} }
var continueImmediately = true
if (checkbox_remember_path.isChecked) { if (checkbox_remember_path.isChecked) {
val oldSavedVolumesPaths = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set<String> savedVolumesAdapter.addVolumePath(rootCipherDir)
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) { if (checkbox_save_password.isChecked && returnedHash != null) {
fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ -> fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ ->
stopTask { onPasswordChanged() } stopTask { onPasswordChanged() }
} }
continueImmediately = false } else {
}
}
if (continueImmediately) {
stopTask { onPasswordChanged() } stopTask { onPasswordChanged() }
} }
} else { } else {
@ -222,7 +218,7 @@ class ChangePasswordActivity : BaseActivity() {
if (!fingerprintPasswordHashSaver.canAuthenticate()){ if (!fingerprintPasswordHashSaver.canAuthenticate()){
checkbox_save_password.isChecked = false checkbox_save_password.isChecked = false
} else { } else {
checkbox_remember_path.isChecked = true checkbox_remember_path.isChecked = checkbox_remember_path.isEnabled
} }
} }
} }

View File

@ -8,6 +8,7 @@ import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_create.* 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.toolbar.*
import kotlinx.android.synthetic.main.volume_path_section.* import kotlinx.android.synthetic.main.volume_path_section.*
import sushi.hardcore.droidfs.explorers.ExplorerActivity import sushi.hardcore.droidfs.explorers.ExplorerActivity
@ -131,7 +132,6 @@ class CreateActivity : BaseActivity() {
} }
sessionID = GocryptfsVolume.init(rootCipherDir, password, null, returnedHash) sessionID = GocryptfsVolume.init(rootCipherDir, password, null, returnedHash)
if (sessionID != -1) { if (sessionID != -1) {
var startExplorerImmediately = true
if (checkbox_remember_path.isChecked) { if (checkbox_remember_path.isChecked) {
val oldSavedVolumesPaths = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set<String> val oldSavedVolumesPaths = sharedPrefs.getStringSet(ConstValues.saved_volumes_key, HashSet()) as Set<String>
val editor = sharedPrefs.edit() val editor = sharedPrefs.edit()
@ -145,14 +145,12 @@ class CreateActivity : BaseActivity() {
editor.putStringSet(ConstValues.saved_volumes_key, newSavedVolumesPaths.toSet()) editor.putStringSet(ConstValues.saved_volumes_key, newSavedVolumesPaths.toSet())
} }
editor.apply() editor.apply()
}
if (checkbox_save_password.isChecked && returnedHash != null){ if (checkbox_save_password.isChecked && returnedHash != null){
fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir){ _ -> fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir){ _ ->
stopTask { startExplorer() } stopTask { startExplorer() }
} }
startExplorerImmediately = false } else {
}
}
if (startExplorerImmediately){
stopTask { startExplorer() } stopTask { startExplorer() }
} }
} else { } else {

View File

@ -4,12 +4,16 @@ import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.AdapterView.OnItemClickListener import android.widget.AdapterView.OnItemClickListener
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_open.* 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.toolbar.*
import kotlinx.android.synthetic.main.volume_path_section.* import kotlinx.android.synthetic.main.volume_path_section.*
import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter
@ -59,6 +63,27 @@ class OpenActivity : BaseActivity() {
} else { } else {
WidgetUtil.hide(saved_path_listview) 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, _, _ -> edit_password.setOnEditorActionListener { v, _, _ ->
onClickOpen(v) onClickOpen(v)
true true
@ -145,17 +170,14 @@ class OpenActivity : BaseActivity() {
} }
sessionID = GocryptfsVolume.init(rootCipherDir, password, null, returnedHash) sessionID = GocryptfsVolume.init(rootCipherDir, password, null, returnedHash)
if (sessionID != -1) { if (sessionID != -1) {
var startExplorerImmediately = true
if (checkbox_remember_path.isChecked) { if (checkbox_remember_path.isChecked) {
savedVolumesAdapter.addVolumePath(rootCipherDir) savedVolumesAdapter.addVolumePath(rootCipherDir)
}
if (checkbox_save_password.isChecked && returnedHash != null){ if (checkbox_save_password.isChecked && returnedHash != null){
fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ -> fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { _ ->
stopTask { startExplorer() } stopTask { startExplorer() }
} }
startExplorerImmediately = false } else {
}
}
if (startExplorerImmediately){
stopTask { startExplorer() } stopTask { startExplorer() }
} }
} else { } else {
@ -221,7 +243,7 @@ class OpenActivity : BaseActivity() {
if (!fingerprintPasswordHashSaver.canAuthenticate()){ if (!fingerprintPasswordHashSaver.canAuthenticate()){
checkbox_save_password.isChecked = false checkbox_save_password.isChecked = false
} else { } else {
checkbox_remember_path.isChecked = true checkbox_remember_path.isChecked = checkbox_remember_path.isEnabled
} }
} }
} }

View File

@ -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) { fun addVolumePath(volume_path: String) {
if (!savedVolumesPaths.contains(volume_path)) { if (!isPathSaved(volume_path)) {
savedVolumesPaths.add(volume_path) savedVolumesPaths.add(volume_path)
updateSharedPrefs() updateSharedPrefs()
} }

View File

@ -101,7 +101,6 @@ class FingerprintPasswordHashSaver(private val activityContext: AppCompatActivit
isPrepared = true isPrepared = true
} }
fun encryptAndSave(plainText: ByteArray, root_cipher_dir: String, onAuthenticationResult: (success: Boolean) -> Unit){ 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.rootCipherDir = root_cipher_dir
this.actionDescription = activityContext.getString(R.string.encrypt_action_description) this.actionDescription = activityContext.getString(R.string.encrypt_action_description)
this.onAuthenticationResult = onAuthenticationResult this.onAuthenticationResult = onAuthenticationResult
@ -113,7 +112,6 @@ class FingerprintPasswordHashSaver(private val activityContext: AppCompatActivit
cipher.init(Cipher.ENCRYPT_MODE, key) cipher.init(Cipher.ENCRYPT_MODE, key)
startListening() startListening()
} }
}
fun decrypt(cipherText: String, root_cipher_dir: String, onPasswordDecrypted: (password: ByteArray) -> Unit){ fun decrypt(cipherText: String, root_cipher_dir: String, onPasswordDecrypted: (password: ByteArray) -> Unit){
this.rootCipherDir = root_cipher_dir this.rootCipherDir = root_cipher_dir
this.actionDescription = activityContext.getString(R.string.decrypt_action_description) this.actionDescription = activityContext.getString(R.string.decrypt_action_description)

View File

@ -79,21 +79,7 @@
</LinearLayout> </LinearLayout>
<sushi.hardcore.droidfs.widgets.ColoredCheckBox <include layout="@layout/checkboxes_section"/>
android:id="@+id/checkbox_remember_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/remember_volume_path"
android:onClick="onClickRememberPath"/>
<sushi.hardcore.droidfs.widgets.ColoredCheckBox
android:id="@+id/checkbox_save_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/fingerprint_save_checkbox_text"
android:onClick="onClickSavePasswordHash"/>
<sushi.hardcore.droidfs.widgets.NonScrollableColoredBorderListView <sushi.hardcore.droidfs.widgets.NonScrollableColoredBorderListView
android:id="@+id/saved_path_listview" android:id="@+id/saved_path_listview"

View File

@ -59,21 +59,7 @@
</LinearLayout> </LinearLayout>
<sushi.hardcore.droidfs.widgets.ColoredCheckBox <include layout="@layout/checkboxes_section"/>
android:id="@+id/checkbox_remember_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/remember_volume_path"
android:onClick="onClickRememberPath"/>
<sushi.hardcore.droidfs.widgets.ColoredCheckBox
android:id="@+id/checkbox_save_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/fingerprint_save_checkbox_text"
android:onClick="onClickSavePasswordHash"/>
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -39,21 +39,7 @@
</LinearLayout> </LinearLayout>
<sushi.hardcore.droidfs.widgets.ColoredCheckBox <include layout="@layout/checkboxes_section"/>
android:id="@+id/checkbox_remember_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/remember_volume_path"
android:onClick="onClickRememberPath"/>
<sushi.hardcore.droidfs.widgets.ColoredCheckBox
android:id="@+id/checkbox_save_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/fingerprint_save_checkbox_text"
android:onClick="onClickSavePasswordHash"/>
<sushi.hardcore.droidfs.widgets.NonScrollableColoredBorderListView <sushi.hardcore.droidfs.widgets.NonScrollableColoredBorderListView
android:id="@+id/saved_path_listview" android:id="@+id/saved_path_listview"

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<sushi.hardcore.droidfs.widgets.ColoredCheckBox
android:id="@+id/checkbox_remember_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/remember_volume_path"
android:onClick="onClickRememberPath"/>
<sushi.hardcore.droidfs.widgets.ColoredCheckBox
android:id="@+id/checkbox_save_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/fingerprint_save_checkbox_text"
android:onClick="onClickSavePasswordHash"/>
</LinearLayout>