mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-30 22:02:57 -08:00
Manage Templates with TemplateEngine
This commit is contained in:
1 parent
9c30183068
commit
fac727cd3d
8 files changed
+236
-48
No files matched your search
@@ -202,27 +202,15 @@ class EntryEditActivity : LockingActivity(),
|
||||
}
|
||||
|
||||
// Build template selector
|
||||
val templates = ArrayList<Template>()
|
||||
|
||||
// TODO Dynamic templates
|
||||
templates.add(Template.STANDARD)
|
||||
templates.add(Template(UUID.randomUUID(), "Credit Card",
|
||||
IconImageStandard(37).getIconImageToDraw(), TemplateSection(ArrayList<TemplateAttribute>().apply {
|
||||
add(TemplateAttribute("Number", TemplateAttributeType.INLINE))
|
||||
add(TemplateAttribute("CVV", TemplateAttributeType.INLINE, true))
|
||||
add(TemplateAttribute("PIN", TemplateAttributeType.INLINE, true))
|
||||
add(TemplateAttribute("Card holder", TemplateAttributeType.INLINE))
|
||||
add(TemplateAttribute("Expires", TemplateAttributeType.DATETIME))
|
||||
add(TemplateAttribute("Test", TemplateAttributeType.DATE))
|
||||
add(TemplateAttribute("Test2", TemplateAttributeType.TIME))
|
||||
add(TemplateAttribute("Test3", TemplateAttributeType.DATE))
|
||||
add(TemplateAttribute("Another password", TemplateAttributeType.INLINE, true, TemplateAttributeAction.PASSWORD_GENERATION))
|
||||
})))
|
||||
val templates = mDatabase?.getTemplates()
|
||||
val entryTemplate: Template? = mEntry?.let {
|
||||
mDatabase?.getTemplate(it)
|
||||
} ?: if (templates?.isNotEmpty() == true) Template.STANDARD else null
|
||||
|
||||
// Build fragment to manage entry modification
|
||||
entryEditFragment = supportFragmentManager.findFragmentByTag(ENTRY_EDIT_FRAGMENT_TAG) as? EntryEditFragment?
|
||||
if (entryEditFragment == null) {
|
||||
entryEditFragment = EntryEditFragment.getInstance(tempEntryInfo)
|
||||
entryEditFragment = EntryEditFragment.getInstance(tempEntryInfo, entryTemplate)
|
||||
}
|
||||
entryEditFragment?.apply {
|
||||
drawFactory = mDatabase?.iconDrawableFactory
|
||||
@@ -263,17 +251,22 @@ class EntryEditActivity : LockingActivity(),
|
||||
// Change template dynamically
|
||||
templateSelectorSpinner = findViewById(R.id.entry_edit_template_selector)
|
||||
templateSelectorSpinner?.apply {
|
||||
adapter = TemplatesSelectorAdapter(this@EntryEditActivity, mDatabase, templates)
|
||||
onItemSelectedListener = object: AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
val newTemplate = templates[position]
|
||||
entryEditFragment?.apply {
|
||||
if (getTemplate() != newTemplate) {
|
||||
assignTemplate(newTemplate)
|
||||
if (templates != null && templates.isNotEmpty()) {
|
||||
adapter = TemplatesSelectorAdapter(this@EntryEditActivity, mDatabase, templates)
|
||||
setSelection(templates.indexOf(entryTemplate))
|
||||
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
val newTemplate = templates[position]
|
||||
entryEditFragment?.apply {
|
||||
if (getTemplate() != newTemplate) {
|
||||
assignTemplate(newTemplate)
|
||||
}
|
||||
}
|
||||
}
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
||||
}
|
||||
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
||||
} else {
|
||||
visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -154,7 +154,9 @@ class EntryEditFragment : StylishFragment() {
|
||||
mEntryInfo = savedInstanceState.getParcelable(KEY_TEMP_ENTRY_INFO) ?: mEntryInfo
|
||||
}
|
||||
|
||||
if (savedInstanceState?.containsKey(KEY_TEMPLATE) == true) {
|
||||
if (arguments?.containsKey(KEY_TEMPLATE) == true)
|
||||
mTemplate = arguments?.getParcelable(KEY_TEMPLATE) ?: mTemplate
|
||||
else if (savedInstanceState?.containsKey(KEY_TEMPLATE) == true) {
|
||||
mTemplate = savedInstanceState.getParcelable(KEY_TEMPLATE) ?: mTemplate
|
||||
}
|
||||
if (savedInstanceState?.containsKey(KEY_SELECTION_DATE_TIME_ID) == true) {
|
||||
@@ -218,8 +220,8 @@ class EntryEditFragment : StylishFragment() {
|
||||
}
|
||||
|
||||
fun assignTemplate(template: Template) {
|
||||
populateEntryWithViews()
|
||||
this.mTemplate = template
|
||||
// TODO Add custom template UUID
|
||||
populateViewsWithEntry()
|
||||
rootView.showByFading()
|
||||
}
|
||||
@@ -325,7 +327,6 @@ class EntryEditFragment : StylishFragment() {
|
||||
// Build main view depending on type
|
||||
val itemView: View? = when (templateAttribute.type) {
|
||||
TemplateAttributeType.INLINE,
|
||||
TemplateAttributeType.URL,
|
||||
TemplateAttributeType.MULTILINE -> {
|
||||
buildLinearTextView(templateAttribute, field)
|
||||
}
|
||||
@@ -334,9 +335,6 @@ class EntryEditFragment : StylishFragment() {
|
||||
TemplateAttributeType.DATETIME -> {
|
||||
buildDataTimeView(templateAttribute, field)
|
||||
}
|
||||
TemplateAttributeType.LISTBOX -> TODO()
|
||||
TemplateAttributeType.POPOUT -> TODO()
|
||||
TemplateAttributeType.RICH_TEXTBOX -> TODO()
|
||||
}
|
||||
// Custom id defined by field name, use getViewByField(field: Field) to retrieve it
|
||||
itemView?.id = field.name.hashCode()
|
||||
@@ -689,11 +687,13 @@ class EntryEditFragment : StylishFragment() {
|
||||
private const val FIELD_NOTES_TAG = "FIELD_NOTES_TAG"
|
||||
private const val FIELD_CUSTOM_TAG = "FIELD_CUSTOM_TAG"
|
||||
|
||||
fun getInstance(entryInfo: EntryInfo?): EntryEditFragment {
|
||||
fun getInstance(entryInfo: EntryInfo?,
|
||||
template: Template?): EntryEditFragment {
|
||||
//database: Database?): EntryEditFragment {
|
||||
return EntryEditFragment().apply {
|
||||
arguments = Bundle().apply {
|
||||
putParcelable(KEY_TEMP_ENTRY_INFO, entryInfo)
|
||||
putParcelable(KEY_TEMPLATE, template)
|
||||
// TODO Unique database key database.key
|
||||
putInt(KEY_DATABASE, 0)
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.kunzisoft.keepass.database.element.icon.IconsManager
|
||||
import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdInt
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.template.Template
|
||||
import com.kunzisoft.keepass.database.exception.*
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDB
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX
|
||||
@@ -142,6 +143,17 @@ class Database {
|
||||
iconsManager.removeCustomIcon(binaryCache, customIcon.uuid)
|
||||
}
|
||||
|
||||
fun getTemplates(): List<Template> {
|
||||
return mDatabaseKDBX?.getTemplates() ?: listOf()
|
||||
}
|
||||
|
||||
fun getTemplate(entry: Entry): Template? {
|
||||
entry.entryKDBX?.let { entryKDBX ->
|
||||
return mDatabaseKDBX?.getTemplate(entryKDBX)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
val allowName: Boolean
|
||||
get() = mDatabaseKDBX != null
|
||||
|
||||
|
||||
@@ -45,6 +45,8 @@ import com.kunzisoft.keepass.database.element.icon.IconImageStandard
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.node.NodeVersioned
|
||||
import com.kunzisoft.keepass.database.element.security.MemoryProtectionConfig
|
||||
import com.kunzisoft.keepass.database.element.template.Template
|
||||
import com.kunzisoft.keepass.database.element.template.TemplateEngine
|
||||
import com.kunzisoft.keepass.database.exception.UnknownKDF
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_31
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_40
|
||||
@@ -79,6 +81,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
private var numKeyEncRounds: Long = 0
|
||||
var publicCustomData = VariantDictionary()
|
||||
private val mFieldReferenceEngine = FieldReferencesEngine(this)
|
||||
private val mTemplateEngine = TemplateEngine(this)
|
||||
|
||||
var kdbxVersion = UnsignedInt(0)
|
||||
var name = ""
|
||||
@@ -332,10 +335,26 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
return this.iconsManager.getIcon(iconUuid)
|
||||
}
|
||||
|
||||
fun getTemplates(): List<Template> {
|
||||
return mTemplateEngine.getTemplates()
|
||||
}
|
||||
|
||||
fun getTemplate(entry: EntryKDBX): Template? {
|
||||
return mTemplateEngine.getTemplate(entry)
|
||||
}
|
||||
|
||||
/*
|
||||
* Search methods
|
||||
*/
|
||||
|
||||
fun getGroupById(id: UUID): GroupKDBX? {
|
||||
return this.getGroupById(NodeIdUUID(id))
|
||||
}
|
||||
|
||||
fun getEntryById(id: UUID): EntryKDBX? {
|
||||
return this.getEntryById(NodeIdUUID(id))
|
||||
}
|
||||
|
||||
fun getEntryByTitle(title: String, recursionLevel: Int): EntryKDBX? {
|
||||
return this.entryIndexes.values.find { entry ->
|
||||
entry.decodeTitleKey(recursionLevel).equals(title, true)
|
||||
|
||||
@@ -37,6 +37,7 @@ import com.kunzisoft.keepass.database.element.node.Type
|
||||
import com.kunzisoft.keepass.database.element.security.ProtectedString
|
||||
import com.kunzisoft.keepass.utils.ParcelableUtil
|
||||
import com.kunzisoft.keepass.utils.UnsignedLong
|
||||
import com.kunzisoft.keepass.utils.UuidUtil
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import kotlin.collections.LinkedHashMap
|
||||
@@ -227,6 +228,10 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
|
||||
fields[STR_NOTES] = ProtectedString(protect, value)
|
||||
}
|
||||
|
||||
fun getCustomFieldValue(label: String): String {
|
||||
return decodeRefKey(mDecodeRef, label, 0)
|
||||
}
|
||||
|
||||
fun getSize(attachmentPool: AttachmentPool): Long {
|
||||
var size = FIXED_LENGTH_SIZE
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ class Template : Parcelable {
|
||||
true,
|
||||
TemplateAttributeAction.PASSWORD_GENERATION)
|
||||
)
|
||||
add(TemplateAttribute(STANDARD_URL, TemplateAttributeType.URL))
|
||||
add(TemplateAttribute(STANDARD_URL, TemplateAttributeType.INLINE))
|
||||
add(TemplateAttribute(STANDARD_EXPIRATION, TemplateAttributeType.DATETIME))
|
||||
add(TemplateAttribute(STANDARD_NOTES, TemplateAttributeType.MULTILINE))
|
||||
})
|
||||
|
||||
+6
-15
@@ -18,19 +18,10 @@
|
||||
*/
|
||||
package com.kunzisoft.keepass.database.element.template
|
||||
|
||||
enum class TemplateAttributeType(var value: String) {
|
||||
|
||||
INLINE("Inline"),
|
||||
URL("Inline URL"),
|
||||
MULTILINE("Multiline"),
|
||||
DATE("Date"),
|
||||
TIME("Time"),
|
||||
DATETIME("Date Time"),
|
||||
LISTBOX("Listbox"),
|
||||
POPOUT("Popout"),
|
||||
RICH_TEXTBOX("Rich Textbox");
|
||||
|
||||
companion object {
|
||||
val PROTECTED = "Protected"
|
||||
}
|
||||
enum class TemplateAttributeType {
|
||||
INLINE,
|
||||
MULTILINE,
|
||||
DATE,
|
||||
TIME,
|
||||
DATETIME
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.kunzisoft.keepass.database.element.template
|
||||
|
||||
import android.util.Log
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
|
||||
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
|
||||
import com.kunzisoft.keepass.utils.UuidUtil
|
||||
import java.util.*
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
class TemplateEngine(private val databaseKDBX: DatabaseKDBX) {
|
||||
|
||||
private val mCacheTemplates = HashMap<UUID, Template>()
|
||||
|
||||
fun getTemplates(): List<Template> {
|
||||
// TODO retrieve template group
|
||||
val templates = mutableListOf<Template>()
|
||||
try {
|
||||
val templateGroup = databaseKDBX.getGroupById(UUID.fromString("48071a4e-9577-af41-adbc-723e17a1f2ac"))
|
||||
mCacheTemplates.clear()
|
||||
templates.add(Template.STANDARD)
|
||||
templateGroup?.getChildEntries()?.forEach { templateEntry ->
|
||||
getTemplateFromTemplateEntry(templateEntry)?.let {
|
||||
mCacheTemplates[templateEntry.id] = it
|
||||
}
|
||||
}
|
||||
templates.addAll(mCacheTemplates.values)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to get templates from group", e)
|
||||
}
|
||||
return templates
|
||||
}
|
||||
|
||||
private fun getTemplateByCache(uuid: UUID): Template? {
|
||||
try {
|
||||
if (mCacheTemplates.containsKey(uuid))
|
||||
return mCacheTemplates[uuid]
|
||||
else {
|
||||
databaseKDBX.getEntryById(uuid)?.let { templateEntry ->
|
||||
getTemplateFromTemplateEntry(templateEntry)?.let { newTemplate ->
|
||||
mCacheTemplates[uuid] = newTemplate
|
||||
return newTemplate
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to get Entry by $uuid", e)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getTemplateFromTemplateEntry(templateEntry: EntryKDBX): Template? {
|
||||
var templateVersion: Int? = null
|
||||
val attributes = HashMap<String, TemplateAttributePosition>()
|
||||
for ((key, value) in templateEntry.customFields) {
|
||||
when {
|
||||
key.equals(TEMPLATE_LABEL_VERSION, true) -> {
|
||||
try {
|
||||
templateVersion = value.stringValue.toInt()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to retrieve template version", e)
|
||||
}
|
||||
}
|
||||
key.startsWith(TEMPLATE_ATTRIBUTE_POSITION_PREFIX) -> {
|
||||
try {
|
||||
val attributeName = key.substring(TEMPLATE_ATTRIBUTE_POSITION_PREFIX.length)
|
||||
val attribute = getOrRetrieveAttributeFromName(attributes, attributeName)
|
||||
attribute.position = value.stringValue.toInt()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to retrieve template position", e)
|
||||
}
|
||||
}
|
||||
key.startsWith(TEMPLATE_ATTRIBUTE_TITLE_PREFIX) -> {
|
||||
try {
|
||||
val attributeName = key.substring(TEMPLATE_ATTRIBUTE_TITLE_PREFIX.length)
|
||||
val attribute = getOrRetrieveAttributeFromName(attributes, attributeName)
|
||||
attribute.attribute.label = value.stringValue
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to retrieve template position", e)
|
||||
}
|
||||
}
|
||||
key.startsWith(TEMPLATE_ATTRIBUTE_TYPE_PREFIX) -> {
|
||||
try {
|
||||
val attributeName = key.substring(TEMPLATE_ATTRIBUTE_TYPE_PREFIX.length)
|
||||
val attribute = getOrRetrieveAttributeFromName(attributes, attributeName)
|
||||
val attributeValue = value.stringValue
|
||||
if (attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_PROTECTED, true)) {
|
||||
attribute.attribute.protected = true
|
||||
}
|
||||
when {
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_INLINE, true) ||
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_POPOUT, true) -> {
|
||||
attribute.attribute.type = TemplateAttributeType.INLINE
|
||||
}
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_MULTILINE, true) ||
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX, true) -> {
|
||||
attribute.attribute.type = TemplateAttributeType.MULTILINE
|
||||
}
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_DATE_TIME, true) -> {
|
||||
attribute.attribute.type = TemplateAttributeType.DATETIME
|
||||
}
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_DATE, true) -> {
|
||||
attribute.attribute.type = TemplateAttributeType.DATE
|
||||
}
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_TIME, true) -> {
|
||||
attribute.attribute.type = TemplateAttributeType.TIME
|
||||
}
|
||||
attributeValue.contains(TEMPLATE_ATTRIBUTE_TYPE_LISTBOX, true) -> {
|
||||
// TODO List box
|
||||
attribute.attribute.type = TemplateAttributeType.INLINE
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to retrieve template position", e)
|
||||
}
|
||||
}
|
||||
// TODO section
|
||||
}
|
||||
}
|
||||
|
||||
return templateVersion?.let { version ->
|
||||
val templateAttributes = arrayOfNulls<TemplateAttribute>(attributes.size)
|
||||
attributes.values.forEach {
|
||||
templateAttributes[it.position] = it.attribute
|
||||
}
|
||||
val templateSections = mutableListOf<TemplateSection>()
|
||||
val templateSection = TemplateSection(templateAttributes.filterNotNull())
|
||||
templateSections.add(templateSection)
|
||||
Template(templateEntry.id, templateEntry.title, templateEntry.icon, templateSections, version)
|
||||
}
|
||||
}
|
||||
|
||||
fun getTemplate(entryKDBX: EntryKDBX): Template? {
|
||||
UuidUtil.fromHexString(entryKDBX.getCustomFieldValue(TEMPLATE_ENTRY_UUID))?.let { templateUUID ->
|
||||
return getTemplateByCache(templateUUID)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getOrRetrieveAttributeFromName(attributes: HashMap<String, TemplateAttributePosition>, name: String): TemplateAttributePosition {
|
||||
return if (attributes.containsKey(name)) {
|
||||
attributes[name]!!
|
||||
} else {
|
||||
val newAttribute = TemplateAttributePosition(-1, TemplateAttribute("", TemplateAttributeType.INLINE))
|
||||
attributes[name] = newAttribute
|
||||
newAttribute
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private data class TemplateAttributePosition(var position: Int, var attribute: TemplateAttribute)
|
||||
|
||||
private val TAG = TemplateEngine::class.java.name
|
||||
private const val TEMPLATE_LABEL_VERSION = "_etm_template"
|
||||
private const val TEMPLATE_ENTRY_UUID = "_etm_template_uuid"
|
||||
private const val TEMPLATE_ATTRIBUTE_POSITION_PREFIX = "_etm_position"
|
||||
private const val TEMPLATE_ATTRIBUTE_TITLE_PREFIX = "_etm_title"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_PREFIX = "_etm_type"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_PROTECTED = "Protected"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_INLINE = "Inline"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_MULTILINE = "Multiline"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_DATE_TIME = "Date Time"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_DATE = "Date"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_TIME = "Time"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_LISTBOX = "Listbox"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_POPOUT = "Popout"
|
||||
private const val TEMPLATE_ATTRIBUTE_TYPE_RICH_TEXTBOX = "Rich Textbox"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user