Add setting to colorize password

This commit is contained in:
J-Jamet committed 2022-03-29 16:41:23 +02:00
1 parent 7fa141dd1b
commit d57e0cf601
9 files changed
+62 -48

No files matched your search

@@ -21,12 +21,10 @@ package com.kunzisoft.keepass.password
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import com.kunzisoft.keepass.R
import java.security.SecureRandom
import java.util.*
@@ -256,39 +254,39 @@ class PasswordGenerator(private val resources: Resources) {
}
fun getColorizedPassword(password: String): Spannable {
val coloredString = SpannableStringBuilder()
val spannableString = SpannableStringBuilder()
if (password.isNotEmpty()) {
password.forEach {
when {
UPPERCASE_CHARS.contains(it)||
LOWERCASE_CHARS.contains(it) -> {
coloredString.append(it)
spannableString.append(it)
}
DIGIT_CHARS.contains(it) -> {
// RED
coloredString.append(colorizeChar(it, Color.rgb(246, 79, 62)))
spannableString.append(colorizeChar(it, Color.rgb(246, 79, 62)))
}
SPECIAL_CHARS.contains(it) -> {
// Blue
coloredString.append(colorizeChar(it, Color.rgb(39, 166, 228)))
spannableString.append(colorizeChar(it, Color.rgb(39, 166, 228)))
}
MINUS_CHAR.contains(it)||
UNDERLINE_CHAR.contains(it)||
BRACKET_CHARS.contains(it) -> {
// Purple
coloredString.append(colorizeChar(it, Color.rgb(185, 38, 209)))
spannableString.append(colorizeChar(it, Color.rgb(185, 38, 209)))
}
extendedChars().contains(it) -> {
// Green
coloredString.append(colorizeChar(it, Color.rgb(44, 181, 50)))
spannableString.append(colorizeChar(it, Color.rgb(44, 181, 50)))
}
else -> {
coloredString.append(it)
spannableString.append(it)
}
}
}
}
return coloredString
return spannableString
}
private fun colorizeChar(char: Char, color: Int): Spannable {
@@ -135,6 +135,18 @@ object PreferencesUtil {
context.resources.getBoolean(R.bool.show_uuid_default))
}
fun hideProtectedValue(context: Context): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getBoolean(context.getString(R.string.hide_password_key),
context.resources.getBoolean(R.bool.hide_password_default))
}
fun colorizePassword(context: Context): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getBoolean(context.getString(R.string.colorize_password_key),
context.resources.getBoolean(R.bool.colorize_password_default))
}
fun showExpiredEntries(context: Context): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return ! prefs.getBoolean(context.getString(R.string.hide_expired_entries_key),
@@ -371,12 +383,6 @@ object PreferencesUtil {
context.resources.getBoolean(R.bool.sort_recycle_bin_bottom_default))
}
fun hideProtectedValue(context: Context): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getBoolean(context.getString(R.string.hide_password_key),
context.resources.getBoolean(R.bool.hide_password_default))
}
fun fieldFontIsInVisibility(context: Context): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getBoolean(context.getString(R.string.monospace_font_fields_enable_key),
@@ -631,7 +637,6 @@ object PreferencesUtil {
context.getString(R.string.lock_database_show_button_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.password_length_key) -> editor.putInt(name, value.toInt())
context.getString(R.string.list_password_generator_options_key) -> editor.putStringSet(name, getStringSetFromProperties(value))
context.getString(R.string.hide_password_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.allow_copy_password_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.remember_database_locations_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.show_recent_files_key) -> editor.putBoolean(name, value.toBoolean())
@@ -679,6 +684,8 @@ object PreferencesUtil {
context.getString(R.string.show_uuid_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.list_size_key) -> editor.putString(name, value)
context.getString(R.string.monospace_font_fields_enable_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.hide_password_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.colorize_password_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.hide_expired_entries_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.enable_education_screens_key) -> editor.putBoolean(name, value.toBoolean())
@@ -21,6 +21,7 @@ package com.kunzisoft.keepass.view
import android.content.Context
import android.text.Editable
import android.text.SpannableString
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
@@ -31,6 +32,7 @@ import com.google.android.material.textfield.TextInputLayout
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.password.PasswordGenerator
import com.kunzisoft.keepass.password.PasswordEntropy
import com.kunzisoft.keepass.settings.PreferencesUtil
class PasswordView @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
@@ -119,7 +121,11 @@ class PasswordView @JvmOverloads constructor(context: Context,
return passwordText.text.toString()
}
set(value) {
passwordText.text = PasswordGenerator.getColorizedPassword(value)
getEntropyStrength(value)
val spannableString =
if (PreferencesUtil.colorizePassword(context))
PasswordGenerator.getColorizedPassword(value)
else
SpannableString(value)
passwordText.text = spannableString
}
}
@@ -25,6 +25,7 @@ import com.google.android.material.textfield.TextInputLayout
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.template.TemplateField
import com.kunzisoft.keepass.password.PasswordGenerator
import com.kunzisoft.keepass.settings.PreferencesUtil
class TextEditFieldView @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
@@ -130,16 +131,11 @@ class TextEditFieldView @JvmOverloads constructor(context: Context,
}
set(value) {
val spannableString =
if (TemplateField.isStandardPasswordName(context, label))
if (PreferencesUtil.colorizePassword(context)
&& TemplateField.isStandardPasswordName(context, label))
PasswordGenerator.getColorizedPassword(value)
else
SpannableString(value)
spannableString.setSpan(
StyleSpan(Typeface.BOLD),
0,
spannableString.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
valueView.setText(spannableString)
}
@@ -44,6 +44,7 @@ import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.template.TemplateField
import com.kunzisoft.keepass.model.EntryInfo.Companion.APPLICATION_ID_FIELD_NAME
import com.kunzisoft.keepass.password.PasswordGenerator
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.utils.UriUtil
@@ -199,16 +200,11 @@ class TextFieldView @JvmOverloads constructor(context: Context,
}
set(value) {
val spannableString =
if (TemplateField.isStandardPasswordName(context, label))
if (PreferencesUtil.colorizePassword(context)
&& TemplateField.isStandardPasswordName(context, label))
PasswordGenerator.getColorizedPassword(value)
else
SpannableString(value)
spannableString.setSpan(
StyleSpan(Typeface.BOLD),
0,
spannableString.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
valueView.text = spannableString
changeProtectedValueParameters()
}