feat: Add passphraseConditionsView #2102

This commit is contained in:
J-Jamet committed 2026-09-15 09:54:34 +02:00
1 parent ae3d60f88f
commit dfba53d6eb
4 files changed
+245 -177

No files matched your search

@@ -19,24 +19,16 @@
*/
package com.kunzisoft.keepass.activities.fragments
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ImageView
import android.widget.Spinner
import android.widget.TextView
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.slider.Slider
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.ContextualDatabase
import com.kunzisoft.keepass.password.PassphraseGenerator
@@ -44,6 +36,7 @@ import com.kunzisoft.keepass.password.PasswordGenerator
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.timeout.timeoutCopyToClipboard
import com.kunzisoft.keepass.utils.clear
import com.kunzisoft.keepass.view.PassphraseConditionsView
import com.kunzisoft.keepass.view.PasswordConditionsView
import com.kunzisoft.keepass.view.PasswordEditView
import com.kunzisoft.keepass.viewmodels.KeyGeneratorViewModel
@@ -52,17 +45,9 @@ import kotlinx.coroutines.launch
class PassphraseGeneratorFragment : DatabaseFragment() {
private lateinit var passwordEditView: PasswordEditView
private lateinit var passphraseConditionsView: PassphraseConditionsView
private lateinit var passwordConditionsView: PasswordConditionsView
private lateinit var sliderWordCount: Slider
private lateinit var wordCountText: EditText
private lateinit var charactersCountText: TextView
private lateinit var wordCaseSpinner: Spinner
private var minSliderWordCount: Int = 0
private var maxSliderWordCount: Int = 0
private var wordCaseAdapter: ArrayAdapter<String>? = null
private val mKeyGeneratorViewModel: KeyGeneratorViewModel by activityViewModels()
override fun onCreateView(
@@ -81,14 +66,8 @@ class PassphraseGeneratorFragment : DatabaseFragment() {
passwordEditView = view.findViewById(R.id.passphrase_view)
val passphraseCopyView: ImageView? = view.findViewById(R.id.passphrase_copy_button)
sliderWordCount = view.findViewById(R.id.slider_word_count)
wordCountText = view.findViewById(R.id.word_count)
charactersCountText = view.findViewById(R.id.character_count)
passphraseConditionsView = view.findViewById(R.id.passphrase_words_conditions_view)
passwordConditionsView = view.findViewById(R.id.passphrase_separator_conditions_view)
wordCaseSpinner = view.findViewById(R.id.word_case)
minSliderWordCount = resources.getInteger(R.integer.passphrase_generator_word_count_min)
maxSliderWordCount = resources.getInteger(R.integer.passphrase_generator_word_count_max)
context?.let { context ->
passphraseCopyView?.visibility = if (PreferencesUtil.allowCopyProtectedFields(context))
@@ -100,72 +79,15 @@ class PassphraseGeneratorFragment : DatabaseFragment() {
sensitive = true
)
}
wordCaseAdapter = ArrayAdapter(
context,
android.R.layout.simple_spinner_item,
resources.getStringArray(R.array.word_case_array)
).apply {
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
}
wordCaseSpinner.adapter = wordCaseAdapter
}
loadSettings()
passwordConditionsView.onConditionsChanged = {
passphraseConditionsView.onConditionsChanged = {
generatePassphrase()
}
var listenSlider = true
var listenEditText = true
sliderWordCount.addOnChangeListener { _, value, _ ->
try {
listenEditText = false
if (listenSlider) {
wordCountText.setText(value.toInt().toString())
}
} catch (e: Exception) {
Log.e(TAG, "Unable to set the word count value", e)
} finally {
listenEditText = true
}
}
sliderWordCount.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
// TODO upgrade material-components lib
// https://stackoverflow.com/questions/70873160/material-slider-onslidertouchlisteners-methods-can-only-be-called-from-within-t
@SuppressLint("RestrictedApi")
override fun onStartTrackingTouch(slider: Slider) {}
@SuppressLint("RestrictedApi")
override fun onStopTrackingTouch(slider: Slider) {
generatePassphrase()
}
})
wordCountText.doOnTextChanged { _, _, _, _ ->
if (listenEditText) {
try {
listenSlider = false
setSliderValue(getWordCount())
} catch (e: Exception) {
Log.e(TAG, "Unable to get the word count value", e)
} finally {
listenSlider = true
generatePassphrase()
}
}
}
wordCaseSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
generatePassphrase()
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
passwordConditionsView.onConditionsChanged = {
generatePassphrase()
}
generatePassphrase()
@@ -188,33 +110,6 @@ class PassphraseGeneratorFragment : DatabaseFragment() {
resetAppTimeoutWhenViewFocusedOrChanged(view)
}
private fun getWordCount(): Int {
return try {
Integer.valueOf(wordCountText.text.toString())
} catch (_: NumberFormatException) {
minSliderWordCount
}
}
private fun setWordCount(wordCount: Int) {
setSliderValue(wordCount)
wordCountText.setText(wordCount.toString())
}
private fun setSliderValue(value: Int) {
when {
value < minSliderWordCount -> {
sliderWordCount.value = minSliderWordCount.toFloat()
}
value > maxSliderWordCount -> {
sliderWordCount.value = maxSliderWordCount.toFloat()
}
else -> {
sliderWordCount.value = value.toFloat()
}
}
}
private fun getWordSeparator(): String {
return try {
val generatedSeparator = PasswordGenerator(resources).generatePassword(
@@ -242,29 +137,15 @@ class PassphraseGeneratorFragment : DatabaseFragment() {
}
}
private fun getWordCase(): PassphraseGenerator.WordCase {
var wordCase = PassphraseGenerator.WordCase.LOWER_CASE
try {
wordCase = PassphraseGenerator.WordCase.getByOrdinal(wordCaseSpinner.selectedItemPosition)
} catch (caseException: Exception) {
Log.e(TAG, "Unable to retrieve the word case", caseException)
}
return wordCase
}
private fun setWordCase(wordCase: PassphraseGenerator.WordCase) {
wordCaseSpinner.setSelection(wordCase.ordinal)
}
private fun generatePassphrase() {
try {
val passphrase = PassphraseGenerator().generatePassphrase(
getWordCount(),
passphraseConditionsView.getWordCount(),
getWordSeparator(),
getWordCase()
passphraseConditionsView.getWordCase()
)
passwordEditView.passwordCharArray = passphrase
charactersCountText.text = getString(R.string.character_count, passphrase.size)
passphraseConditionsView.setCharacterCountText(getString(R.string.character_count, passphrase.size))
passphrase.clear()
} catch (e: Exception) {
Log.e(TAG, "Unable to generate a passphrase", e)
@@ -278,8 +159,8 @@ class PassphraseGeneratorFragment : DatabaseFragment() {
private fun saveSettings() {
context?.let { context ->
PreferencesUtil.setDefaultPassphraseWordCount(context, getWordCount())
PreferencesUtil.setDefaultPassphraseWordCase(context, getWordCase())
PreferencesUtil.setDefaultPassphraseWordCount(context, passphraseConditionsView.getWordCount())
PreferencesUtil.setDefaultPassphraseWordCase(context, passphraseConditionsView.getWordCase())
PreferencesUtil.setDefaultPassphraseSeparatorOptions(context, passwordConditionsView.getOptions())
PreferencesUtil.setDefaultPassphraseSeparatorLength(context, passwordConditionsView.getPasswordLength())
PreferencesUtil.setDefaultPassphraseSeparatorConsiderChars(context, passwordConditionsView.getConsiderChars())
@@ -289,8 +170,8 @@ class PassphraseGeneratorFragment : DatabaseFragment() {
private fun loadSettings() {
context?.let { context ->
setWordCount(PreferencesUtil.getDefaultPassphraseWordCount(context))
setWordCase(PreferencesUtil.getDefaultPassphraseWordCase(context))
passphraseConditionsView.setWordCount(PreferencesUtil.getDefaultPassphraseWordCount(context))
passphraseConditionsView.setWordCase(PreferencesUtil.getDefaultPassphraseWordCase(context))
passwordConditionsView.setOptions(PreferencesUtil.getDefaultPassphraseSeparatorOptions(context))
passwordConditionsView.setPasswordLength(PreferencesUtil.getDefaultPassphraseSeparatorLength(context))
passwordConditionsView.setConsiderChars(PreferencesUtil.getDefaultPassphraseSeparatorConsiderChars(context))
@@ -0,0 +1,177 @@
/*
* Copyright 2026 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePassDX.
*
* KeePassDX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.view
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.widget.doOnTextChanged
import com.google.android.material.slider.Slider
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.password.PassphraseGenerator
class PassphraseConditionsView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : ConstraintLayout(context, attrs, defStyle) {
private val wordCountSlider: Slider
private val wordCountEditText: EditText
private val wordCaseSpinner: Spinner
private val charactersCountText: TextView
private val minSliderWordCount: Int
private val maxSliderWordCount: Int
private var wordCaseAdapter: ArrayAdapter<String>? = null
var onConditionsChanged: (() -> Unit)? = null
init {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.view_passphrase_conditions, this)
wordCountSlider = findViewById(R.id.passphrase_slider_word_count)
wordCountEditText = findViewById(R.id.passphrase_word_count_edit_text)
wordCaseSpinner = findViewById(R.id.passphrase_word_case_selection)
charactersCountText = findViewById(R.id.passphrase_character_count)
minSliderWordCount = resources.getInteger(R.integer.passphrase_generator_word_count_min)
maxSliderWordCount = resources.getInteger(R.integer.passphrase_generator_word_count_max)
wordCaseAdapter = ArrayAdapter(
context,
android.R.layout.simple_spinner_item,
resources.getStringArray(R.array.word_case_array)
).apply {
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
}
wordCaseSpinner.adapter = wordCaseAdapter
var listenSlider = true
var listenEditText = true
wordCountSlider.addOnChangeListener { _, value, _ ->
try {
listenEditText = false
if (listenSlider) {
wordCountEditText.setText(value.toInt().toString())
}
} catch (e: Exception) {
Log.e(TAG, "Unable to set the word count value", e)
} finally {
listenEditText = true
}
}
wordCountSlider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
// TODO upgrade material-components lib
// https://stackoverflow.com/questions/70873160/material-slider-onslidertouchlisteners-methods-can-only-be-called-from-within-t
@SuppressLint("RestrictedApi")
override fun onStartTrackingTouch(slider: Slider) {}
@SuppressLint("RestrictedApi")
override fun onStopTrackingTouch(slider: Slider) {
onConditionsChanged?.invoke()
}
})
wordCountEditText.doOnTextChanged { _, _, _, _ ->
if (listenEditText) {
try {
listenSlider = false
setSliderValue(getWordCount())
} catch (e: Exception) {
Log.e(TAG, "Unable to get the word count value", e)
} finally {
listenSlider = true
onConditionsChanged?.invoke()
}
}
}
wordCaseSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
onConditionsChanged?.invoke()
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
}
fun getWordCount(): Int {
return try {
Integer.valueOf(wordCountEditText.text.toString())
} catch (_: NumberFormatException) {
minSliderWordCount
}
}
fun setWordCount(wordCount: Int) {
setSliderValue(wordCount)
wordCountEditText.setText(wordCount.toString())
}
fun getWordCase(): PassphraseGenerator.WordCase {
var wordCase = PassphraseGenerator.WordCase.LOWER_CASE
try {
wordCase = PassphraseGenerator.WordCase.getByOrdinal(wordCaseSpinner.selectedItemPosition)
} catch (caseException: Exception) {
Log.e(TAG, "Unable to retrieve the word case", caseException)
}
return wordCase
}
fun setWordCase(wordCase: PassphraseGenerator.WordCase) {
wordCaseSpinner.setSelection(wordCase.ordinal)
}
fun setCharacterCountText(text: String) {
charactersCountText.text = text
}
private fun setSliderValue(value: Int) {
when {
value < minSliderWordCount -> {
wordCountSlider.value = minSliderWordCount.toFloat()
}
value > maxSliderWordCount -> {
wordCountSlider.value = maxSliderWordCount.toFloat()
}
else -> {
wordCountSlider.value = value.toFloat()
}
}
}
companion object {
private val TAG = PassphraseConditionsView::class.simpleName
}
}
@@ -92,53 +92,10 @@
android:layout_height="wrap_content"
style="@style/KeepassDXStyle.Title"
android:text="@string/words"/>
<androidx.constraintlayout.widget.ConstraintLayout
<com.kunzisoft.keepass.view.PassphraseConditionsView
android:id="@+id/passphrase_words_conditions_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/card_view_padding"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/passphrase_slider_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.slider.Slider
android:id="@+id/slider_word_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/word_count"
android:layout_alignBottom="@+id/word_count"
android:contentDescription="@string/content_description_password_length"
android:stepSize="1"
android:value="@integer/passphrase_generator_word_count_default"
android:valueFrom="@integer/passphrase_generator_word_count_min"
android:valueTo="@integer/passphrase_generator_word_count_max" />
<EditText
android:id="@+id/word_count"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:inputType="number"
android:maxLength="2"
android:maxLines="1" />
</RelativeLayout>
<Spinner
android:id="@+id/word_case"
android:layout_width="wrap_content"
android:layout_height="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passphrase_slider_container"/>
<TextView
android:id="@+id/character_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Character count: 51"
app:layout_constraintStart_toEndOf="@+id/word_case"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/word_case"
app:layout_constraintBottom_toBottomOf="@+id/word_case"/>
</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/card_view_padding">
<RelativeLayout
android:id="@+id/passphrase_word_count_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.slider.Slider
android:id="@+id/passphrase_slider_word_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/passphrase_word_count_edit_text"
android:layout_alignBottom="@+id/passphrase_word_count_edit_text"
android:contentDescription="@string/content_description_password_length"
android:stepSize="1"
android:value="@integer/passphrase_generator_word_count_default"
android:valueFrom="@integer/passphrase_generator_word_count_min"
android:valueTo="@integer/passphrase_generator_word_count_max" />
<EditText
android:id="@+id/passphrase_word_count_edit_text"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="2"
android:maxLines="1"
tools:ignore="LabelFor" />
</RelativeLayout>
<Spinner
android:id="@+id/passphrase_word_case_selection"
android:layout_width="wrap_content"
android:layout_height="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passphrase_word_count_container"/>
<TextView
android:id="@+id/passphrase_character_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Character count: 51"
app:layout_constraintStart_toEndOf="@+id/passphrase_word_case_selection"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/passphrase_word_case_selection"
app:layout_constraintBottom_toBottomOf="@+id/passphrase_word_case_selection"/>
</androidx.constraintlayout.widget.ConstraintLayout>