From 2c620ad69a80f0a22fa00ae32c3b17defdb1c84a Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Fri, 18 Sep 2020 12:24:43 +0200 Subject: [PATCH] Add dialog for empty keyfile #679 --- .../dialogs/AssignMasterKeyDialogFragment.kt | 36 ++++++++++++++++--- app/src/main/res/values/strings.xml | 2 ++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/AssignMasterKeyDialogFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/AssignMasterKeyDialogFragment.kt index 93553d3ef..9fb98c991 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/AssignMasterKeyDialogFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/AssignMasterKeyDialogFragment.kt @@ -26,6 +26,7 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.text.Editable +import android.text.SpannableStringBuilder import android.text.TextWatcher import android.view.View import android.widget.CompoundButton @@ -35,6 +36,7 @@ import androidx.fragment.app.DialogFragment import com.google.android.material.textfield.TextInputLayout import com.kunzisoft.keepass.R import com.kunzisoft.keepass.activities.helpers.SelectFileHelper +import com.kunzisoft.keepass.utils.UriUtil import com.kunzisoft.keepass.view.KeyFileSelectionView class AssignMasterKeyDialogFragment : DialogFragment() { @@ -134,7 +136,7 @@ class AssignMasterKeyDialogFragment : DialogFragment() { mMasterPassword = "" mKeyFile = null - var error = verifyPassword() || verifyFile() + var error = verifyPassword() || verifyKeyFile() if (!passwordCheckBox!!.isChecked && !keyFileCheckBox!!.isChecked) { error = true if (allowNoMasterKey) @@ -204,7 +206,7 @@ class AssignMasterKeyDialogFragment : DialogFragment() { return error } - private fun verifyFile(): Boolean { + private fun verifyKeyFile(): Boolean { var error = false if (keyFileCheckBox != null && keyFileCheckBox!!.isChecked) { @@ -224,7 +226,7 @@ class AssignMasterKeyDialogFragment : DialogFragment() { val builder = AlertDialog.Builder(it) builder.setMessage(R.string.warning_empty_password) .setPositiveButton(android.R.string.ok) { _, _ -> - if (!verifyFile()) { + if (!verifyKeyFile()) { mListener?.onAssignKeyDialogPositiveClick( passwordCheckBox!!.isChecked, mMasterPassword, keyFileCheckBox!!.isChecked, mKeyFile) @@ -251,13 +253,37 @@ class AssignMasterKeyDialogFragment : DialogFragment() { } } + private fun showEmptyKeyFileConfirmationDialog() { + activity?.let { + val builder = AlertDialog.Builder(it) + builder.setMessage(SpannableStringBuilder().apply { + append(getString(R.string.warning_empty_keyfile)) + append("\n\n") + append(getString(R.string.warning_empty_keyfile_explanation)) + append("\n\n") + append(getString(R.string.warning_sure_add_file)) + }) + .setPositiveButton(android.R.string.ok) { _, _ -> } + .setNegativeButton(android.R.string.cancel) { _, _ -> + keyFileCheckBox?.isChecked = false + keyFileSelectionView?.uri = null + } + builder.create().show() + } + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) mSelectFileHelper?.onActivityResultCallback(requestCode, resultCode, data) { uri -> uri?.let { pathUri -> - keyFileCheckBox?.isChecked = true - keyFileSelectionView?.uri = pathUri + UriUtil.getFileData(requireContext(), uri)?.length()?.let { lengthFile -> + keyFileCheckBox?.isChecked = true + keyFileSelectionView?.uri = pathUri + if (lengthFile <= 0L) { + showEmptyKeyFileConfirmationDialog() + } + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 238cf975b..036786fd7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -262,6 +262,8 @@ Add the file anyway? Removing unlinked data may decrease the size of your database but may also delete data used for KeePass plugins. Remove this data anyway? + It is not recommended to add an empty keyfile. + The content of the keyfile should never be changed, and in the best case, should contain randomly generated data. Version %1$s Build %1$s Biometric prompt is supported, but not set up.