diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt index d0546ff8d..0cbd61c65 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/Template.kt @@ -121,8 +121,8 @@ class Template : Parcelable { true, TemplateAttributeOption().apply { setNumberLines(3) - }, - TemplateAttributeAction.PASSWORD_GENERATION + associatePasswordGenerator() + } ) val URL_ATTRIBUTE = TemplateAttribute( TemplateField.LABEL_URL, diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeAction.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeAction.kt index 27cf345a1..be02a73d0 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeAction.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeAction.kt @@ -1,5 +1,5 @@ package com.kunzisoft.keepass.database.element.template enum class TemplateAttributeAction { - NONE, CUSTOM_EDITION, PASSWORD_GENERATION + NONE, CUSTOM_EDITION } \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt index 4d5be7e69..12afb7f24 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateAttributeOption.kt @@ -96,6 +96,18 @@ class TemplateAttributeOption() : Parcelable { mOptions[TEXT_LINK_ATTR] = isLink.toString() } + fun isAssociatedWithPasswordGenerator(): Boolean { + return try { + mOptions[PASSWORD_GENERATOR_ATTR]?.toBoolean() ?: PASSWORD_GENERATOR_VALUE_DEFAULT + } catch (e: Exception) { + PASSWORD_GENERATOR_VALUE_DEFAULT + } + } + + fun associatePasswordGenerator() { + mOptions[PASSWORD_GENERATOR_ATTR] = true.toString() + } + fun getListItems(): List { return mOptions[LIST_ITEMS]?.split(LIST_ITEMS_SEPARATOR) ?: listOf() } @@ -158,6 +170,7 @@ class TemplateAttributeOption() : Parcelable { /** * Applicable to type TEXT + * Define a number of chars * Integer, can be "many" or "-1" to infinite value * "1" if not defined */ @@ -168,6 +181,7 @@ class TemplateAttributeOption() : Parcelable { /** * Applicable to type TEXT + * Define a number of lines * Integer, can be "-1" to infinite value * "1" if not defined */ @@ -178,14 +192,25 @@ class TemplateAttributeOption() : Parcelable { /** * Applicable to type TEXT + * Define if a text is a link * Boolean ("true" or "false") * "true" if not defined */ private const val TEXT_LINK_ATTR = "link" private const val TEXT_LINK_VALUE_DEFAULT = false + /** + * Applicable to type TEXT + * Define if a password generator is associated with the text + * Boolean ("true" or "false") + * "false" if not defined + */ + private const val PASSWORD_GENERATOR_ATTR = "generator" + private const val PASSWORD_GENERATOR_VALUE_DEFAULT = false + /** * Applicable to type LIST + * Define items of a list * List of items, separator is '|' */ private const val LIST_ITEMS = "items" @@ -193,6 +218,7 @@ class TemplateAttributeOption() : Parcelable { /** * Applicable to type DATETIME + * Define the type of date * String ("date" or "time" or "datetime" or based on https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) * "datetime" if not defined */ @@ -200,7 +226,6 @@ class TemplateAttributeOption() : Parcelable { private const val DATETIME_FORMAT_VALUE_DATE = "date" private const val DATETIME_FORMAT_VALUE_TIME = "time" - fun getOptionsFromString(label: String): TemplateAttributeOption { val options = TemplateAttributeOption() val optionsMap = if (label.contains("{") || label.contains("}")) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt index 06e04fae5..1d7b9b15b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/template/TemplateEngineCompatible.kt @@ -195,6 +195,11 @@ class TemplateEngineCompatible(database: DatabaseKDBX): TemplateEngine(database) val attribute = it.attribute + // Add password generator + if (attribute.label.equals(TEMPLATE_ATTRIBUTE_PASSWORD, true)) { + attribute.options.associatePasswordGenerator() + } + // Recognize each temp option attribute.options.get(TEMPLATE_ATTRIBUTE_OPTIONS_TEMP)?.let { defaultOption -> when (attribute.type) { diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt index 661f3c33b..4bde304c2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt @@ -88,6 +88,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context, ?: TemplateField.getLocalizedName(context, field.name) val fieldValue = field.protectedValue.stringValue value = if (fieldValue.isEmpty()) templateAttribute.default else fieldValue + // TODO edition and password generator at same time when (templateAttribute.action) { TemplateAttributeAction.NONE -> { setOnActionClickListener(null) @@ -97,11 +98,11 @@ class TemplateEditView @JvmOverloads constructor(context: Context, mOnCustomEditionActionClickListener?.invoke(field) }, R.drawable.ic_more_white_24dp) } - TemplateAttributeAction.PASSWORD_GENERATION -> { - setOnActionClickListener({ - mOnPasswordGenerationActionClickListener?.invoke(field) - }, R.drawable.ic_generate_password_white_24dp) - } + } + if (templateAttribute.options.isAssociatedWithPasswordGenerator()) { + setOnActionClickListener({ + mOnPasswordGenerationActionClickListener?.invoke(field) + }, R.drawable.ic_generate_password_white_24dp) } } }