aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-12-21 01:13:17 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2021-12-21 13:01:41 +0530
commitfd46f1f0baba65fee2c967ef8409c37307ca60bc (patch)
treea7e043e086a07499471dec2d6467b4fcf0a6a925 /app/src/main/java
parent0e8f8994344c38fcf09e5910d8a599530fb58027 (diff)
app: add diceware generator dialog
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/injection/prefs/PasswordGeneratorPreferences.kt10
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt5
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/ui/dialogs/DicewarePasswordGeneratorDialogFragment.kt88
-rw-r--r--app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt3
4 files changed, 106 insertions, 0 deletions
diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PasswordGeneratorPreferences.kt b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PasswordGeneratorPreferences.kt
new file mode 100644
index 00000000..14b3a6f2
--- /dev/null
+++ b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PasswordGeneratorPreferences.kt
@@ -0,0 +1,10 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package dev.msfjarvis.aps.injection.prefs
+
+import javax.inject.Qualifier
+
+@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class PasswordGeneratorPreferences
diff --git a/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt
index 537ee1ad..e68a998f 100644
--- a/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/injection/prefs/PreferenceModule.kt
@@ -32,6 +32,11 @@ class PreferenceModule {
)
}
+ @[Provides PasswordGeneratorPreferences Reusable]
+ fun providePwgenPreferences(@ApplicationContext context: Context): SharedPreferences {
+ return provideBaseEncryptedPreferences(context, "pwgen_preferences")
+ }
+
@Provides
@SettingsPreferences
@Reusable
diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/DicewarePasswordGeneratorDialogFragment.kt b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/DicewarePasswordGeneratorDialogFragment.kt
new file mode 100644
index 00000000..cac60de3
--- /dev/null
+++ b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/DicewarePasswordGeneratorDialogFragment.kt
@@ -0,0 +1,88 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package dev.msfjarvis.aps.ui.dialogs
+
+import android.app.AlertDialog
+import android.app.Dialog
+import android.content.SharedPreferences
+import android.graphics.Typeface
+import android.os.Bundle
+import androidx.core.content.edit
+import androidx.core.os.bundleOf
+import androidx.fragment.app.DialogFragment
+import androidx.fragment.app.setFragmentResult
+import androidx.lifecycle.lifecycleScope
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import dagger.hilt.android.AndroidEntryPoint
+import dev.msfjarvis.aps.R
+import dev.msfjarvis.aps.databinding.FragmentPwgenDicewareBinding
+import dev.msfjarvis.aps.injection.prefs.PasswordGeneratorPreferences
+import dev.msfjarvis.aps.passgen.diceware.DicewarePassphraseGenerator
+import dev.msfjarvis.aps.ui.crypto.PasswordCreationActivity
+import dev.msfjarvis.aps.util.extensions.getString
+import dev.msfjarvis.aps.util.settings.PreferenceKeys.DICEWARE_LENGTH
+import dev.msfjarvis.aps.util.settings.PreferenceKeys.DICEWARE_SEPARATOR
+import javax.inject.Inject
+import kotlinx.coroutines.flow.launchIn
+import kotlinx.coroutines.flow.merge
+import kotlinx.coroutines.flow.onEach
+import reactivecircus.flowbinding.android.widget.afterTextChanges
+
+@AndroidEntryPoint
+class DicewarePasswordGeneratorDialogFragment : DialogFragment() {
+
+ @Inject lateinit var dicewareGenerator: DicewarePassphraseGenerator
+ @Inject @PasswordGeneratorPreferences lateinit var prefs: SharedPreferences
+
+ override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
+ val builder = MaterialAlertDialogBuilder(requireContext())
+ val binding = FragmentPwgenDicewareBinding.inflate(layoutInflater)
+ val monoTypeface = Typeface.createFromAsset(requireContext().assets, "fonts/sourcecodepro.ttf")
+ binding.passwordSeparatorText.setText(prefs.getString(DICEWARE_SEPARATOR) ?: "-")
+ binding.passwordLengthText.setText(prefs.getInt(DICEWARE_LENGTH, 5).toString())
+ binding.passwordText.typeface = monoTypeface
+ builder.setView(binding.root)
+ merge(
+ binding.passwordLengthText.afterTextChanges(),
+ binding.passwordSeparatorText.afterTextChanges(),
+ )
+ .onEach { generatePassword(binding) }
+ .launchIn(lifecycleScope)
+ return builder
+ .run {
+ setTitle(R.string.pwgen_title)
+ setPositiveButton(R.string.dialog_ok) { _, _ ->
+ setFragmentResult(
+ PasswordCreationActivity.PASSWORD_RESULT_REQUEST_KEY,
+ bundleOf(PasswordCreationActivity.RESULT to "${binding.passwordText.text}")
+ )
+ }
+ setNeutralButton(R.string.dialog_cancel) { _, _ -> }
+ setNegativeButton(R.string.pwgen_generate, null)
+ create()
+ }
+ .apply {
+ setOnShowListener {
+ generatePassword(binding)
+ getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener { generatePassword(binding) }
+ }
+ }
+ }
+
+ private fun generatePassword(binding: FragmentPwgenDicewareBinding) {
+ val length = binding.passwordLengthText.text?.toString()?.toIntOrNull() ?: 5
+ val separator = binding.passwordSeparatorText.text?.toString()?.getOrNull(0) ?: '-'
+ setPreferences(length, separator)
+ binding.passwordText.text = dicewareGenerator.generatePassphrase(length, separator)
+ }
+
+ private fun setPreferences(length: Int, separator: Char) {
+ prefs.edit {
+ putInt(DICEWARE_LENGTH, length)
+ putString(DICEWARE_SEPARATOR, separator.toString())
+ }
+ }
+}
diff --git a/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt b/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt
index 7e3166d8..6168cfee 100644
--- a/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt
+++ b/app/src/main/java/dev/msfjarvis/aps/util/settings/PreferenceKeys.kt
@@ -82,4 +82,7 @@ object PreferenceKeys {
const val PROXY_PASSWORD = "proxy_password"
const val REBASE_ON_PULL = "rebase_on_pull"
+
+ const val DICEWARE_SEPARATOR = "diceware_separator"
+ const val DICEWARE_LENGTH = "diceware_length"
}