diff options
| author | Aditya Wasan <adityawasan55@gmail.com> | 2021-02-19 13:57:57 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-19 08:27:57 +0000 |
| commit | d17ff0d9251bd2ad84778535dabe4884a4cf9d76 (patch) | |
| tree | 57c2cfd031632e8687c038bd35ff2b39f61f6d77 /app/src/main/java/dev/msfjarvis/aps/ui | |
| parent | 92ece7dbb5607258bcf954963009bf1f9411ab07 (diff) | |
| download | HardenedPass-d17ff0d9251bd2ad84778535dabe4884a4cf9d76.tar.gz HardenedPass-d17ff0d9251bd2ad84778535dabe4884a4cf9d76.zip | |
Parse extra content into key value pairs (#1321)
* ui: add skeleton recyclerview to parse extra content
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* ui: add recyclerview and update PasswordEntry to create map of key-value pairs
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* password-entry: When key-value pair is not correctly formed, display it as Extra Content
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Fix formatting
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* bug: update otp code on main thread
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Add complete string if key-value pair cannot be formed
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* test: add a few tests for key-value parsing logic
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* prefs: remove SHOW_EXTRA_CONTENT from shared preferences
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Update CHANGELOG.md
* Cleanup and refactor
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* PasswordEntryTest: silence nullability warning
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* PasswordEntry: simplify constructor
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* PasswordEntry: annotate test-enablement visibility
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* Reintroduce the catch-all field
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* update parsing logic
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* add one more test case
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Add missing newlines
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* Remove unnecessary scrollview
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* rv: do not return if hasExtraContent is false
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
* Don't anchor RV to bottom
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/dev/msfjarvis/aps/ui')
3 files changed, 124 insertions, 68 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/adapters/FieldItemAdapter.kt b/app/src/main/java/dev/msfjarvis/aps/ui/adapters/FieldItemAdapter.kt new file mode 100644 index 00000000..5ecc2888 --- /dev/null +++ b/app/src/main/java/dev/msfjarvis/aps/ui/adapters/FieldItemAdapter.kt @@ -0,0 +1,85 @@ +package dev.msfjarvis.aps.ui.adapters + +import android.text.method.PasswordTransformationMethod +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.content.ContextCompat +import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.textfield.TextInputLayout +import dev.msfjarvis.aps.R +import dev.msfjarvis.aps.data.password.FieldItem +import dev.msfjarvis.aps.databinding.ItemFieldBinding + +class FieldItemAdapter( + private var fieldItemList: List<FieldItem>, + private val showPassword: Boolean, + private val copyTextToClipBoard: (text: String?) -> Unit, +) : RecyclerView.Adapter<FieldItemAdapter.FieldItemViewHolder>() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FieldItemViewHolder { + val binding = ItemFieldBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return FieldItemViewHolder(binding.root, binding) + } + + override fun onBindViewHolder(holder: FieldItemViewHolder, position: Int) { + holder.bind(fieldItemList[position], showPassword, copyTextToClipBoard) + } + + override fun getItemCount(): Int { + return fieldItemList.size + } + + fun updateOTPCode(code: String) { + var otpItemPosition = -1; + fieldItemList = fieldItemList.mapIndexed { position, item -> + if (item.key.equals(FieldItem.ItemType.OTP.type, true)) { + otpItemPosition = position + return@mapIndexed FieldItem.createOtpField(code) + } + + return@mapIndexed item + } + + notifyItemChanged(otpItemPosition) + } + + fun updateItems(itemList: List<FieldItem>) { + fieldItemList = itemList + notifyDataSetChanged() + } + + class FieldItemViewHolder(itemView: View, val binding: ItemFieldBinding) : + RecyclerView.ViewHolder(itemView) { + + fun bind(fieldItem: FieldItem, showPassword: Boolean, copyTextToClipBoard: (String?) -> Unit) { + with(binding) { + itemText.hint = fieldItem.key + itemTextContainer.hint = fieldItem.key + itemText.setText(fieldItem.value) + + when (fieldItem.action) { + FieldItem.ActionType.COPY -> { + itemTextContainer.apply { + endIconDrawable = ContextCompat.getDrawable(itemView.context, R.drawable.ic_content_copy) + endIconMode = TextInputLayout.END_ICON_CUSTOM + setEndIconOnClickListener { copyTextToClipBoard(itemText.text.toString()) } + } + } + FieldItem.ActionType.HIDE -> { + itemTextContainer.apply { + endIconMode = TextInputLayout.END_ICON_PASSWORD_TOGGLE + setOnClickListener { copyTextToClipBoard(itemText.text.toString()) } + } + itemText.apply { + if (!showPassword) { + transformationMethod = PasswordTransformationMethod.getInstance() + } + setOnClickListener { copyTextToClipBoard(itemText.text.toString()) } + } + } + } + } + } + } +} diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt index 8e08b039..88f76161 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/crypto/DecryptActivity.kt @@ -6,9 +6,7 @@ package dev.msfjarvis.aps.ui.crypto import android.content.Intent -import android.graphics.Typeface import android.os.Bundle -import android.text.method.PasswordTransformationMethod import android.view.Menu import android.view.MenuItem import android.view.View @@ -19,8 +17,10 @@ import com.github.ajalt.timberkt.e import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.runCatching import dev.msfjarvis.aps.R +import dev.msfjarvis.aps.data.password.FieldItem import dev.msfjarvis.aps.data.password.PasswordEntry import dev.msfjarvis.aps.databinding.DecryptLayoutBinding +import dev.msfjarvis.aps.ui.adapters.FieldItemAdapter import dev.msfjarvis.aps.util.extensions.viewBinding import dev.msfjarvis.aps.util.settings.PreferenceKeys import java.io.ByteArrayOutputStream @@ -172,80 +172,56 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound { startAutoDismissTimer() runCatching { val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true) - val showExtraContent = settings.getBoolean(PreferenceKeys.SHOW_EXTRA_CONTENT, true) - val monoTypeface = Typeface.createFromAsset(assets, "fonts/sourcecodepro.ttf") val entry = PasswordEntry(outputStream) + val items = arrayListOf<FieldItem>() + val adapter = FieldItemAdapter(emptyList(), showPassword) { text -> + copyTextToClipboard(text) + } + + if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) { + copyPasswordToClipboard(entry.password) + } passwordEntry = entry invalidateOptionsMenu() - with(binding) { - if (entry.password.isEmpty()) { - passwordTextContainer.visibility = View.GONE - } else { - passwordTextContainer.visibility = View.VISIBLE - passwordText.typeface = monoTypeface - passwordText.setText(entry.password) - if (!showPassword) { - passwordText.transformationMethod = PasswordTransformationMethod.getInstance() - } - passwordTextContainer.setOnClickListener { copyPasswordToClipboard(entry.password) } - passwordText.setOnClickListener { copyPasswordToClipboard(entry.password) } - } - - if (entry.hasExtraContent()) { - if (entry.extraContentWithoutAuthData.isNotEmpty()) { - extraContentContainer.visibility = View.VISIBLE - extraContent.typeface = monoTypeface - extraContent.setText(entry.extraContentWithoutAuthData) - if (!showExtraContent) { - extraContent.transformationMethod = PasswordTransformationMethod.getInstance() - } - extraContentContainer.setOnClickListener { copyTextToClipboard(entry.extraContentWithoutAuthData) } - extraContent.setOnClickListener { copyTextToClipboard(entry.extraContentWithoutAuthData) } - } + if (entry.password.isNotEmpty()) { + items.add(FieldItem.createPasswordField(entry.password)) + } - if (entry.hasUsername()) { - usernameText.typeface = monoTypeface - usernameText.setText(entry.username) - usernameTextContainer.setEndIconOnClickListener { copyTextToClipboard(entry.username) } - usernameTextContainer.visibility = View.VISIBLE - } else { - usernameTextContainer.visibility = View.GONE + if (entry.hasTotp()) { + launch(Dispatchers.IO) { + // Calculate the actual remaining time for the first pass + // then return to the standard 30 second affair. + val remainingTime = + entry.totpPeriod - (System.currentTimeMillis() % entry.totpPeriod) + withContext(Dispatchers.Main) { + val code = entry.calculateTotpCode() ?: "Error" + items.add(FieldItem.createOtpField(code)) } - - if (entry.hasTotp()) { - otpTextContainer.visibility = View.VISIBLE - otpTextContainer.setEndIconOnClickListener { - copyTextToClipboard( - otpText.text.toString(), - snackbarTextRes = R.string.clipboard_otp_copied_text - ) - } - launch(Dispatchers.IO) { - // Calculate the actual remaining time for the first pass - // then return to the standard 30 second affair. - val remainingTime = entry.totpPeriod - (System.currentTimeMillis() % entry.totpPeriod) - withContext(Dispatchers.Main) { - otpText.setText(entry.calculateTotpCode() - ?: "Error") - } - delay(remainingTime.seconds) - repeat(Int.MAX_VALUE) { - val code = entry.calculateTotpCode() ?: "Error" - withContext(Dispatchers.Main) { - otpText.setText(code) - } - delay(30.seconds) - } + delay(remainingTime.seconds) + repeat(Int.MAX_VALUE) { + val code = entry.calculateTotpCode() ?: "Error" + withContext(Dispatchers.Main) { + adapter.updateOTPCode(code) } + delay(30.seconds) } } } - if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) { - copyPasswordToClipboard(entry.password) + if (!entry.username.isNullOrEmpty()) { + items.add(FieldItem.createUsernameField(entry.username)) } + + if (entry.hasExtraContentWithoutAuthData()) { + entry.extraContentMap.forEach { (key, value) -> + items.add(FieldItem(key, value, FieldItem.ActionType.COPY)) + } + } + + binding.recyclerView.adapter = adapter + adapter.updateItems(items) }.onFailure { e -> e(e) } diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/settings/PasswordSettings.kt b/app/src/main/java/dev/msfjarvis/aps/ui/settings/PasswordSettings.kt index 8d2536d0..922eff46 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/settings/PasswordSettings.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/settings/PasswordSettings.kt @@ -103,11 +103,6 @@ class PasswordSettings(private val activity: FragmentActivity) : SettingsProvide summaryRes = R.string.show_password_pref_summary defaultValue = true } - checkBox(PreferenceKeys.SHOW_EXTRA_CONTENT) { - titleRes = R.string.show_extra_content_pref_title - summaryRes = R.string.show_extra_content_pref_summary - defaultValue = true - } checkBox(PreferenceKeys.COPY_ON_DECRYPT) { titleRes = R.string.pref_copy_title summaryRes = R.string.pref_copy_summary |
