From 4df108673404d0cdb3d1c928c1747ba133a14bbc Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 27 Sep 2022 18:33:43 +0200 Subject: [PATCH] More flexible password change when fingerprint is saved --- .../droidfs/ChangePasswordActivity.kt | 52 +++++++++++++------ .../sushi/hardcore/droidfs/MainActivity.kt | 4 +- .../res/layout/activity_change_password.xml | 24 ++++++++- app/src/main/res/menu/main_activity.xml | 4 +- app/src/main/res/values-ar/strings.xml | 1 - app/src/main/res/values-es/strings.xml | 1 - app/src/main/res/values-pt-rBR/strings.xml | 1 - app/src/main/res/values-ru/strings.xml | 1 - app/src/main/res/values/strings.xml | 3 +- 9 files changed, 65 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt index 6f261b3..ab4131e 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt @@ -1,11 +1,13 @@ package sushi.hardcore.droidfs import android.annotation.SuppressLint +import android.content.Context import android.os.Build import android.os.Bundle import android.text.InputType import android.view.MenuItem import android.view.View +import android.view.inputmethod.InputMethodManager import android.widget.Toast import androidx.lifecycle.lifecycleScope import sushi.hardcore.droidfs.databinding.ActivityChangePasswordBinding @@ -24,6 +26,9 @@ class ChangePasswordActivity: BaseActivity() { private lateinit var volumeDatabase: VolumeDatabase private var fingerprintProtector: FingerprintProtector? = null private var usfFingerprint: Boolean = false + private val inputMethodManager by lazy { + getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -38,8 +43,7 @@ class ChangePasswordActivity: BaseActivity() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { fingerprintProtector = FingerprintProtector.new(this, themeValue, volumeDatabase) if (fingerprintProtector != null && volume.encryptedHash != null) { - binding.textCurrentPasswordLabel.visibility = View.GONE - binding.editCurrentPassword.visibility = View.GONE + binding.fingerprintSwitchContainer.visibility = View.VISIBLE } } if (!usfFingerprint || fingerprintProtector == null) { @@ -50,6 +54,20 @@ class ChangePasswordActivity: BaseActivity() { it.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD } } + binding.fingerprintSwitchContainer.setOnClickListener { + binding.switchUseFingerprint.toggle() + } + binding.switchUseFingerprint.setOnCheckedChangeListener { _, isChecked -> + if (isChecked && binding.editCurrentPassword.hasFocus()) { + binding.editCurrentPassword.clearFocus() + inputMethodManager.hideSoftInputFromWindow(binding.editCurrentPassword.windowToken, 0) + } + } + binding.editCurrentPassword.setOnFocusChangeListener { _, hasFocus -> + if (hasFocus) { + binding.switchUseFingerprint.isChecked = false + } + } binding.editPasswordConfirm.setOnEditorActionListener { _, _, _ -> changeVolumePassword() true @@ -81,22 +99,24 @@ class ChangePasswordActivity: BaseActivity() { volume.encryptedHash?.let { encryptedHash -> volume.iv?.let { iv -> fingerprintProtector?.let { - changeWithCurrentPassword = false - it.listener = object : FingerprintProtector.Listener { - override fun onHashStorageReset() { - showCurrentPasswordInput() - volume.encryptedHash = null - volume.iv = null - } - override fun onPasswordHashDecrypted(hash: ByteArray) { - changeVolumePassword(newPassword, hash) - } - override fun onPasswordHashSaved() {} - override fun onFailed(pending: Boolean) { - Arrays.fill(newPassword, 0) + if (binding.switchUseFingerprint.isChecked) { + changeWithCurrentPassword = false + it.listener = object : FingerprintProtector.Listener { + override fun onHashStorageReset() { + showCurrentPasswordInput() + volume.encryptedHash = null + volume.iv = null + } + override fun onPasswordHashDecrypted(hash: ByteArray) { + changeVolumePassword(newPassword, hash) + } + override fun onPasswordHashSaved() {} + override fun onFailed(pending: Boolean) { + Arrays.fill(newPassword, 0) + } } + it.loadPasswordHash(volume.name, encryptedHash, iv) } - it.loadPasswordHash(volume.name, encryptedHash, iv) } } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt index 24e6de8..03ae61e 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt @@ -283,7 +283,7 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener { removeVolumes(selectedVolumes) true } - R.id.forget_password -> { + R.id.delete_password_hash -> { for (i in volumeAdapter.selectedItems) { if (volumeDatabase.removeHash(volumeAdapter.volumes[i])) volumeAdapter.onVolumeChanged(i) @@ -349,7 +349,7 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener { val isSelecting = volumeAdapter.selectedItems.isNotEmpty() menu.findItem(R.id.select_all).isVisible = isSelecting menu.findItem(R.id.remove).isVisible = isSelecting - menu.findItem(R.id.forget_password).isVisible = + menu.findItem(R.id.delete_password_hash).isVisible = isSelecting && !volumeAdapter.selectedItems.any { i -> volumeAdapter.volumes[i].encryptedHash == null } val onlyOneSelected = volumeAdapter.selectedItems.size == 1 diff --git a/app/src/main/res/layout/activity_change_password.xml b/app/src/main/res/layout/activity_change_password.xml index ae81b53..0237e3b 100644 --- a/app/src/main/res/layout/activity_change_password.xml +++ b/app/src/main/res/layout/activity_change_password.xml @@ -31,15 +31,37 @@ android:id="@+id/edit_current_password" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginBottom="@dimen/volume_operation_vertical_gap" android:autofillHints="password" android:hint="@string/current_password_hint" android:inputType="textPassword" android:maxLines="1" /> + + + + + + + + + android:title="@string/remove_fingerprint"/> إزالة إعدادات تحديد الكل - نسيان كلمة المرور %s. فشل تحميل مفتاح التشفير. UnrecoverableKeyException %s مخفي ، هل تريد فقط نسيان مسار المجلد أو حذف كل محتوياته أيضًا? diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index f04a6da..468b335 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -186,7 +186,6 @@ Eliminar Ajustes Seleccionar todo - Olvidar la contraseña %s. No se puede cargar la clave de encriptación. UnrecoverableKeyException %s está oculto, ¿sólo quieres olvidar la ruta del volumen o también BORRAR todo su CONTENIDO? diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 829af57..298e2d2 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -180,7 +180,6 @@ Remover Configurações Selecionar tudo - Abandonar a senha %s. Não foi possível carregar a chave de criptografia. UnrecoverableKeyException %s está oculto, você só quer abandonar o caminho do volume ou também REMOVER todo o seu CONTEÚDO? diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 685ecdd..6c07c17 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -180,7 +180,6 @@ Удалить Настройки Выбрать все - Забыть пароль %s. Невозможно загрузить ключ шифрования. Том \"%s\" скрытый, вы хотите только забыть путь к нему или УДАЛИТЬ том со всем его СОДЕРЖИМЫМ? Только забыть diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dae7efd..1a569e1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -187,7 +187,7 @@ Remove Settings Select All - Forget password + Remove fingerprint %s. Can\'t load encryption key. UnrecoverableKeyException %s is hidden, do you just want to forget the path of the volume or also DELETE all its CONTENT? @@ -249,4 +249,5 @@ (%s) (%s, read-only) I/O Error. + Use fingerprint instead of current password