aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/ui
diff options
context:
space:
mode:
authorFabian Henneke <FabianHenneke@users.noreply.github.com>2020-06-18 12:04:33 +0200
committerGitHub <noreply@github.com>2020-06-18 12:04:33 +0200
commit33b3f54921bba7549933c6f889fbe7ab63b61b7c (patch)
tree0c79a1bf93a0094657fc497efc80ae2c1affb518 /app/src/main/java/com/zeapo/pwdstore/ui
parente25e0035a2e4d6227f9d8071687e421e784186a4 (diff)
downloadHardenedPass-33b3f54921bba7549933c6f889fbe7ab63b61b7c.tar.gz
HardenedPass-33b3f54921bba7549933c6f889fbe7ab63b61b7c.zip
Refactor password generation (#860)
* Refactor password generation * Update Extensions.kt * Update app/src/main/java/com/zeapo/pwdstore/pwgen/PasswordGenerator.kt Co-authored-by: Harsh Shandilya <me@msfjarvis.dev> * Address review comments Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/ui')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt111
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt2
2 files changed, 54 insertions, 59 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt
index 8f452dca..160f388b 100644
--- a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/PasswordGeneratorDialogFragment.kt
@@ -4,6 +4,8 @@
*/
package com.zeapo.pwdstore.ui.dialogs
+import android.annotation.SuppressLint
+import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.graphics.Typeface
@@ -11,94 +13,87 @@ import android.os.Bundle
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
-import androidx.appcompat.app.AlertDialog
+import androidx.annotation.IdRes
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatTextView
import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R
-import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorExeption
+import com.zeapo.pwdstore.pwgen.PasswordGenerator
+import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorException
import com.zeapo.pwdstore.pwgen.PasswordGenerator.generate
import com.zeapo.pwdstore.pwgen.PasswordGenerator.setPrefs
+import com.zeapo.pwdstore.pwgen.PasswordOption
-/** A placeholder fragment containing a simple view. */
class PasswordGeneratorDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
- val builder = MaterialAlertDialogBuilder(requireContext())
val callingActivity = requireActivity()
val inflater = callingActivity.layoutInflater
+
+ @SuppressLint("InflateParams")
val view = inflater.inflate(R.layout.fragment_pwgen, null)
val monoTypeface = Typeface.createFromAsset(callingActivity.assets, "fonts/sourcecodepro.ttf")
- builder.setView(view)
val prefs = requireActivity().applicationContext
.getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE)
- view.findViewById<CheckBox>(R.id.numerals)?.isChecked = !prefs.getBoolean("0", false)
- view.findViewById<CheckBox>(R.id.symbols)?.isChecked = prefs.getBoolean("y", false)
- view.findViewById<CheckBox>(R.id.uppercase)?.isChecked = !prefs.getBoolean("A", false)
- view.findViewById<CheckBox>(R.id.lowercase)?.isChecked = !prefs.getBoolean("L", false)
- view.findViewById<CheckBox>(R.id.ambiguous)?.isChecked = !prefs.getBoolean("B", false)
- view.findViewById<CheckBox>(R.id.pronounceable)?.isChecked = !prefs.getBoolean("s", true)
+ view.findViewById<CheckBox>(R.id.numerals)?.isChecked = !prefs.getBoolean(PasswordOption.NoDigits.key, false)
+ view.findViewById<CheckBox>(R.id.symbols)?.isChecked = prefs.getBoolean(PasswordOption.AtLeastOneSymbol.key, false)
+ view.findViewById<CheckBox>(R.id.uppercase)?.isChecked = !prefs.getBoolean(PasswordOption.NoUppercaseLetters.key, false)
+ view.findViewById<CheckBox>(R.id.lowercase)?.isChecked = !prefs.getBoolean(PasswordOption.NoLowercaseLetters.key, false)
+ view.findViewById<CheckBox>(R.id.ambiguous)?.isChecked = !prefs.getBoolean(PasswordOption.NoAmbiguousCharacters.key, false)
+ view.findViewById<CheckBox>(R.id.pronounceable)?.isChecked = !prefs.getBoolean(PasswordOption.FullyRandom.key, true)
val textView: AppCompatEditText = view.findViewById(R.id.lengthNumber)
textView.setText(prefs.getInt("length", 20).toString())
val passwordText: AppCompatTextView = view.findViewById(R.id.passwordText)
passwordText.typeface = monoTypeface
- builder.setPositiveButton(resources.getString(R.string.dialog_ok)) { _, _ ->
- val edit = callingActivity.findViewById<EditText>(R.id.password)
- edit.setText(passwordText.text)
- }
- builder.setNeutralButton(resources.getString(R.string.dialog_cancel)) { _, _ -> }
- builder.setNegativeButton(resources.getString(R.string.pwgen_generate), null)
- val dialog = builder.setTitle(this.resources.getString(R.string.pwgen_title)).create()
- dialog.setOnShowListener {
- setPreferences()
- try {
- passwordText.text = generate(requireActivity().applicationContext)[0]
- } catch (e: PasswordGeneratorExeption) {
- Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
- passwordText.text = ""
+ return MaterialAlertDialogBuilder(requireContext()).run {
+ setTitle(R.string.pwgen_title)
+ setView(view)
+ setPositiveButton(R.string.dialog_ok) { _, _ ->
+ val edit = callingActivity.findViewById<EditText>(R.id.password)
+ edit.setText(passwordText.text)
}
- dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
- setPreferences()
- try {
- passwordText.text = generate(callingActivity.applicationContext)[0]
- } catch (e: PasswordGeneratorExeption) {
- Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
- passwordText.text = ""
+ setNeutralButton(R.string.dialog_cancel) { _, _ -> }
+ setNegativeButton(R.string.pwgen_generate, null)
+ create()
+ }.apply {
+ setOnShowListener {
+ generate(passwordText)
+ getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener {
+ generate(passwordText)
}
}
}
- return dialog
}
- private fun setPreferences() {
- val preferences = ArrayList<String>()
- if (!(dialog!!.findViewById<CheckBox>(R.id.numerals)).isChecked) {
- preferences.add("0")
- }
- if ((dialog!!.findViewById<CheckBox>(R.id.symbols)).isChecked) {
- preferences.add("y")
- }
- if (!(dialog!!.findViewById<CheckBox>(R.id.uppercase)).isChecked) {
- preferences.add("A")
- }
- if (!(dialog!!.findViewById<CheckBox>(R.id.ambiguous)).isChecked) {
- preferences.add("B")
- }
- if (!(dialog!!.findViewById<CheckBox>(R.id.pronounceable)).isChecked) {
- preferences.add("s")
- }
- if (!(dialog!!.findViewById<CheckBox>(R.id.lowercase)).isChecked) {
- preferences.add("L")
- }
- val editText = dialog!!.findViewById<EditText>(R.id.lengthNumber)
+ private fun generate(passwordField: AppCompatTextView) {
+ setPreferences()
try {
- val length = Integer.valueOf(editText.text.toString())
- setPrefs(requireActivity().applicationContext, preferences, length)
- } catch (e: NumberFormatException) {
- setPrefs(requireActivity().applicationContext, preferences)
+ passwordField.text = generate(requireContext().applicationContext)
+ } catch (e: PasswordGeneratorException) {
+ Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
+ passwordField.text = ""
}
}
+
+ private fun isChecked(@IdRes id: Int): Boolean {
+ return requireDialog().findViewById<CheckBox>(id).isChecked
+ }
+
+ private fun setPreferences() {
+ val preferences = listOfNotNull(
+ PasswordOption.NoDigits.takeIf { !isChecked(R.id.numerals) },
+ PasswordOption.AtLeastOneSymbol.takeIf { isChecked(R.id.symbols) },
+ PasswordOption.NoUppercaseLetters.takeIf { !isChecked(R.id.uppercase) },
+ PasswordOption.NoAmbiguousCharacters.takeIf { !isChecked(R.id.ambiguous) },
+ PasswordOption.FullyRandom.takeIf { !isChecked(R.id.pronounceable) },
+ PasswordOption.NoLowercaseLetters.takeIf { !isChecked(R.id.lowercase) }
+ )
+ val lengthText = requireDialog().findViewById<EditText>(R.id.lengthNumber).text.toString()
+ val length = lengthText.toIntOrNull()?.takeIf { it >= 0 }
+ ?: PasswordGenerator.DEFAULT_LENGTH
+ setPrefs(requireActivity().applicationContext, preferences, length)
+ }
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt
index 52bc52c1..206b0533 100644
--- a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt
@@ -124,7 +124,7 @@ class XkPasswordGeneratorDialogFragment : DialogFragment() {
.appendNumbers(if (cbNumbers.isChecked) Integer.parseInt(spinnerNumbersCount.selectedItem as String) else 0)
.appendSymbols(if (cbSymbols.isChecked) Integer.parseInt(spinnerSymbolsCount.selectedItem as String) else 0)
.setCapitalization(CapsType.valueOf(spinnerCapsType.selectedItem.toString())).create()
- } catch (e: PasswordGenerator.PasswordGeneratorExeption) {
+ } catch (e: PasswordGenerator.PasswordGeneratorException) {
Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
tag("xkpw").e(e, "failure generating xkpasswd")
passwordText.text = FALLBACK_ERROR_PASS