From fde16c60f4ce5d57a0c7d5a0186dcd532a23f0f0 Mon Sep 17 00:00:00 2001 From: Fabian Henneke Date: Wed, 25 Mar 2020 18:13:04 +0100 Subject: Make preferred directory structure for Autofill configurable (#660) Some users keep their password files in a directory structure such as: /example.org/john@doe.org.gpg while others prefer the style: /example.org/john@doe.org/password.gpg This commit adds a setting that allows to switch between the two. All Autofill operations, such as search, match, generate and save, respect this setting. Note: The first style seems to be the most widely used and is therefore kept as the default. The second style is mentioned on the official Pass website at: https://www.passwordstore.org/#organization --- .../java/com/zeapo/pwdstore/crypto/PgpActivity.kt | 46 +++++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/crypto') diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt index 90efd583..2fb85118 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpActivity.kt @@ -41,6 +41,8 @@ import com.zeapo.pwdstore.ClipboardService import com.zeapo.pwdstore.PasswordEntry import com.zeapo.pwdstore.R import com.zeapo.pwdstore.UserPreference +import com.zeapo.pwdstore.autofill.oreo.AutofillPreferences +import com.zeapo.pwdstore.autofill.oreo.DirectoryStructure import com.zeapo.pwdstore.ui.dialogs.PasswordGeneratorDialogFragment import com.zeapo.pwdstore.ui.dialogs.XkPasswordGeneratorDialogFragment import com.zeapo.pwdstore.utils.Otp @@ -157,9 +159,23 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { } title = getString(R.string.new_password_title) - crypto_password_category.text = getRelativePath(fullPath, repoPath) - suggestedName?.let { - crypto_password_file_edit.setText(it) + crypto_password_category.apply { + setText(getRelativePath(fullPath, repoPath)) + // If the activity has been provided with suggested info, we allow the user to + // edit the path, otherwise we style the EditText like a TextView. + if (suggestedName != null) { + isEnabled = true + } else { + setBackgroundColor(getColor(android.R.color.transparent)) + } + } + suggestedName?.let { crypto_password_file_edit.setText(it) } + // Allow the user to quickly switch between storing the username as the filename or + // in the encrypted extras. This only makes sense if the directory structure is + // FileBased. + if (suggestedName != null && + AutofillPreferences.directoryStructure(this) == DirectoryStructure.FileBased + ) { encrypt_username.apply { visibility = View.VISIBLE setOnClickListener { @@ -549,7 +565,20 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { val iStream = ByteArrayInputStream(content.toByteArray(Charset.forName("UTF-8"))) val oStream = ByteArrayOutputStream() - val path = if (intent.getBooleanExtra("fromDecrypt", false)) fullPath else "$fullPath/$editName.gpg" + val path = when { + intent.getBooleanExtra("fromDecrypt", false) -> fullPath + // If we allowed the user to edit the relative path, we have to consider it here instead + // of fullPath. + crypto_password_category.isEnabled -> { + val editRelativePath = crypto_password_category.text!!.toString().trim() + if (editRelativePath.isEmpty()) { + showSnackbar(resources.getString(R.string.path_toast_text)) + return + } + "$repoPath/${editRelativePath.trim('/')}/$editName.gpg" + } + else -> "$fullPath/$editName.gpg" + } lifecycleScope.launch(IO) { api?.executeApiAsync(data, iStream, oStream, object : OpenPgpApi.IOpenPgpCallback { @@ -575,9 +604,14 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { } if (shouldGeneratePassword) { + val directoryStructure = + AutofillPreferences.directoryStructure(applicationContext) val entry = PasswordEntry(content) returnIntent.putExtra("PASSWORD", entry.password) - returnIntent.putExtra("USERNAME", entry.username ?: file.nameWithoutExtension) + returnIntent.putExtra( + "USERNAME", + directoryStructure.getUsernameFor(file) + ) } setResult(RESULT_OK, returnIntent) @@ -615,7 +649,7 @@ class PgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBound { crypto_extra_edit.setText(passwordEntry?.extraContent) crypto_extra_edit.typeface = monoTypeface - crypto_password_category.text = relativeParentPath + crypto_password_category.setText(relativeParentPath) crypto_password_file_edit.setText(name) crypto_password_file_edit.isEnabled = false -- cgit v1.2.3