diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index 38b8e47f5..a4ad938a5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -323,7 +323,7 @@ class EntryEditActivity : LockingActivity(), expiryTime = entryView.expiresDate } notes = entryView. notes - entryView.customFields.forEach { customField -> + entryView.getExtraField().forEach { customField -> putExtraField(customField.name, customField.protectedValue) } } @@ -353,20 +353,8 @@ class EntryEditActivity : LockingActivity(), EntryCustomFieldDialogFragment.getInstance().show(supportFragmentManager, "customFieldDialog") } - private fun scrollToView(view: View?) { - view?.postDelayed({ - view.requestFocus() - scrollView?.post { - scrollView?.smoothScrollTo(0, view.bottom) - } - }, 500) - } - override fun onNewCustomFieldApproved(label: String, protection: Boolean) { - val customFieldView = entryEditContentsView?.putExtraField( - Field(label, ProtectedString(protection)) - ) - scrollToView(customFieldView) + entryEditContentsView?.putExtraField(Field(label, ProtectedString(protection))) } override fun onNewCustomFieldCanceled(label: String, protection: Boolean) {} @@ -382,7 +370,6 @@ class EntryEditActivity : LockingActivity(), * Saves the new entry or update an existing entry in the database */ private fun saveEntry() { - // Launch a validation and show the error if present if (entryEditContentsView?.isValid() == true) { // Clone the entry @@ -493,8 +480,7 @@ class EntryEditActivity : LockingActivity(), // Update the otp field with otpauth:// url val otpField = OtpEntryFields.buildOtpField(otpElement, mEntry?.title, mEntry?.username) - val otpCustomView = entryEditContentsView?.putExtraField(otpField) - scrollToView(otpCustomView) + entryEditContentsView?.putExtraField(otpField) mEntry?.putExtraField(otpField.name, otpField.protectedValue) } diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsAdapter.kt b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsAdapter.kt index ad7189049..a1873651b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsAdapter.kt +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryAttachmentsAdapter.kt @@ -41,7 +41,7 @@ class EntryAttachmentsAdapter(val context: Context, private val editable: Boolea var entryAttachmentsList: MutableList = ArrayList() var onItemClickListener: ((item: EntryAttachment)->Unit)? = null var onDeleteButtonClickListener: ((item: EntryAttachment)->Unit)? = null - var onItemDeletedListener: ((item: EntryAttachment, lastOne: Boolean)->Unit)? = null + var onListSizeChangedListener: ((previousSize: Int, newSize: Int)->Unit)? = null private var mAttachmentToRemove: EntryAttachment? = null @@ -54,6 +54,7 @@ class EntryAttachmentsAdapter(val context: Context, private val editable: Boolea override fun onBindViewHolder(holder: EntryBinariesViewHolder, position: Int) { val entryAttachment = entryAttachmentsList[position] + holder.itemView.visibility = View.VISIBLE holder.binaryFileTitle.text = entryAttachment.name holder.binaryFileSize.text = Formatter.formatFileSize(context, entryAttachment.binaryAttachment.length()) @@ -105,24 +106,27 @@ class EntryAttachmentsAdapter(val context: Context, private val editable: Boolea } fun assignAttachments(attachments: List) { + val previousSize = entryAttachmentsList.size entryAttachmentsList.apply { clear() addAll(attachments) } notifyDataSetChanged() + onListSizeChangedListener?.invoke(previousSize, entryAttachmentsList.size) } private fun deleteAttachment(attachment: EntryAttachment) { + val previousSize = entryAttachmentsList.size val position = entryAttachmentsList.indexOf(attachment) if (position >= 0) { entryAttachmentsList.removeAt(position) notifyItemRemoved(position) - onItemDeletedListener?.invoke(attachment, entryAttachmentsList.isEmpty()) mAttachmentToRemove = null for (i in 0 until entryAttachmentsList.size) { notifyItemChanged(i) } } + onListSizeChangedListener?.invoke(previousSize, entryAttachmentsList.size) } fun updateProgress(entryAttachment: EntryAttachment) { diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryExtraFieldsAdapter.kt b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryExtraFieldsAdapter.kt new file mode 100644 index 000000000..ca924e4d4 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryExtraFieldsAdapter.kt @@ -0,0 +1,186 @@ +/* + * Copyright 2019 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 . + * + */ +package com.kunzisoft.keepass.adapters + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.view.inputmethod.EditorInfo +import android.widget.EditText +import android.widget.TextView +import androidx.core.widget.doOnTextChanged +import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.textfield.TextInputLayout +import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.model.Field +import com.kunzisoft.keepass.view.applyFontVisibility +import com.kunzisoft.keepass.view.collapse + +class EntryExtraFieldsAdapter(val context: Context) + : RecyclerView.Adapter() { + + private val inflater: LayoutInflater = LayoutInflater.from(context) + var extraFieldsList: MutableList = ArrayList() + private set + var onDeleteButtonClickListener: ((item: Field)->Unit)? = null + var onListSizeChangedListener: ((previousSize: Int, newSize: Int)->Unit)? = null + + var applyFontVisibility = false + set(value) { + field = value + notifyDataSetChanged() + } + private var mValueViewInputType: Int = 0 + private var mItemToRemove: Field? = null + private var mLastFocused: Field? = null + private var mLastFocusedTimestamp: Long = 0 + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EntryExtraFieldViewHolder { + val view = EntryExtraFieldViewHolder( + inflater.inflate(R.layout.item_entry_edit_extra_field, parent, false) + ) + mValueViewInputType = view.extraFieldValue.inputType + return view + } + + override fun onBindViewHolder(holder: EntryExtraFieldViewHolder, position: Int) { + val extraField = extraFieldsList[position] + + holder.itemView.visibility = View.VISIBLE + if (extraField.protectedValue.isProtected) { + holder.extraFieldValueContainer.isPasswordVisibilityToggleEnabled = true + holder.extraFieldValue.inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD or mValueViewInputType + } else { + holder.extraFieldValueContainer.isPasswordVisibilityToggleEnabled = false + holder.extraFieldValue.inputType = mValueViewInputType + } + holder.extraFieldValueContainer.hint = extraField.name + holder.extraFieldValue.apply { + setText(extraField.protectedValue.toString()) + // To Fix focus in RecyclerView + setOnFocusChangeListener { _, hasFocus -> + if (hasFocus) { + focusField(extraField) + } else { + requestFocusOnLastTextFocused(this, extraField) + } + } + if (extraField == mLastFocused) { + post { requestFocus() } + mLastFocused = null + } + doOnTextChanged { text, _, _, _ -> + extraField.protectedValue.stringValue = text.toString() + } + if (applyFontVisibility) + applyFontVisibility() + } + holder.extraFieldDeleteButton.apply { + if (mItemToRemove == extraField) { + holder.itemView.collapse(true) { + deleteExtraField(extraField) + } + setOnClickListener(null) + } else { + setOnClickListener { + onDeleteButtonClickListener?.invoke(extraField) + mItemToRemove = extraField + notifyItemChanged(position) + } + } + } + } + + /* TODO Error + fun setError(@StringRes errorId: Int?) { + valueLayoutView.error = if (errorId == null) null else { + context.getString(errorId) + } + } + */ + + private fun focusField(field: Field) { + mLastFocused = field + mLastFocusedTimestamp = System.currentTimeMillis() + } + + private fun requestFocusOnLastTextFocused(textView: TextView, field: Field) { + if (field == mLastFocused) { + if ((mLastFocusedTimestamp + 350L) > System.currentTimeMillis()) + textView.post { textView.requestFocus() } + mLastFocused = null + } + } + + override fun getItemCount(): Int { + return extraFieldsList.size + } + + fun assignExtraFields(fields: List) { + val previousSize = extraFieldsList.size + extraFieldsList.apply { + clear() + addAll(fields) + } + notifyDataSetChanged() + onListSizeChangedListener?.invoke(previousSize, extraFieldsList.size) + } + + fun putExtraField(field: Field) { + val previousSize = extraFieldsList.size + if (extraFieldsList.contains(field)) { + val index = extraFieldsList.indexOf(field) + extraFieldsList.removeAt(index) + extraFieldsList.add(index, field) + focusField(field) + notifyItemChanged(index) + } else { + extraFieldsList.add(field) + focusField(field) + notifyItemInserted(extraFieldsList.indexOf(field)) + } + onListSizeChangedListener?.invoke(previousSize, extraFieldsList.size) + } + + private fun deleteExtraField(field: Field) { + val previousSize = extraFieldsList.size + val position = extraFieldsList.indexOf(field) + if (position >= 0) { + extraFieldsList.removeAt(position) + notifyItemRemoved(position) + mItemToRemove = null + for (i in 0 until extraFieldsList.size) { + notifyItemChanged(i) + } + } + onListSizeChangedListener?.invoke(previousSize, extraFieldsList.size) + } + + fun clear() { + extraFieldsList.clear() + } + + inner class EntryExtraFieldViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + var extraFieldValueContainer: TextInputLayout = itemView.findViewById(R.id.entry_extra_field_value_container) + var extraFieldValue: EditText = itemView.findViewById(R.id.entry_extra_field_value) + var extraFieldDeleteButton: View = itemView.findViewById(R.id.entry_extra_field_delete) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/security/ProtectedString.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/security/ProtectedString.kt index a29f09b01..6e5f468bd 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/security/ProtectedString.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/security/ProtectedString.kt @@ -26,7 +26,7 @@ class ProtectedString : Parcelable { var isProtected: Boolean = false private set - private var stringValue: String = "" + var stringValue: String = "" constructor(toCopy: ProtectedString) { this.isProtected = toCopy.isProtected diff --git a/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt b/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt index f479d6d42..475c96434 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt @@ -19,7 +19,6 @@ package com.kunzisoft.keepass.view import android.content.Context -import android.graphics.Color import android.text.util.Linkify import android.util.AttributeSet import android.view.LayoutInflater @@ -74,8 +73,8 @@ class EntryContentsView @JvmOverloads constructor(context: Context, private val notesContainerView: View private val notesView: TextView - private val entryExtraFieldsContainerParent: View - private val entryExtraFieldsContainer: ViewGroup + private val extraFieldsContainerView: View + private val extraFieldsListView: ViewGroup private val creationDateView: TextView private val modificationDateView: TextView @@ -123,8 +122,8 @@ class EntryContentsView @JvmOverloads constructor(context: Context, notesContainerView = findViewById(R.id.entry_notes_container) notesView = findViewById(R.id.entry_notes) - entryExtraFieldsContainerParent = findViewById(R.id.extra_fields_container_parent) - entryExtraFieldsContainer = findViewById(R.id.extra_fields_container) + extraFieldsContainerView = findViewById(R.id.extra_fields_container) + extraFieldsListView = findViewById(R.id.extra_fields_list) attachmentsContainerView = findViewById(R.id.entry_attachments_container) attachmentsListView = findViewById(R.id.entry_attachments_list) @@ -196,7 +195,7 @@ class EntryContentsView @JvmOverloads constructor(context: Context, } fun atLeastOneFieldProtectedPresent(): Boolean { - entryExtraFieldsContainer.let { + extraFieldsListView.let { for (i in 0 until it.childCount) { val childCustomView = it.getChildAt(i) if (childCustomView is EntryExtraField) @@ -210,7 +209,7 @@ class EntryContentsView @JvmOverloads constructor(context: Context, fun setHiddenPasswordStyle(hiddenStyle: Boolean) { passwordView.applyHiddenStyle(hiddenStyle) // Hidden style for custom fields - entryExtraFieldsContainer.let { + extraFieldsListView.let { for (i in 0 until it.childCount) { val childCustomView = it.getChildAt(i) if (childCustomView is EntryExtraField) @@ -329,7 +328,7 @@ class EntryContentsView @JvmOverloads constructor(context: Context, */ private fun showOrHideExtraFieldsContainer(hide: Boolean) { - entryExtraFieldsContainerParent.visibility = if (hide) View.GONE else View.VISIBLE + extraFieldsContainerView.visibility = if (hide) View.GONE else View.VISIBLE } fun addExtraField(title: String, @@ -346,13 +345,13 @@ class EntryContentsView @JvmOverloads constructor(context: Context, applyFontVisibility(fontInVisibility) } entryCustomField?.let { - entryExtraFieldsContainer.addView(it) + extraFieldsListView.addView(it) } showOrHideExtraFieldsContainer(false) } fun clearExtraFields() { - entryExtraFieldsContainer.removeAllViews() + extraFieldsListView.removeAllViews() showOrHideExtraFieldsContainer(true) } diff --git a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt b/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt index c4bc42ca1..74d773bac 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/EntryEditContentsView.kt @@ -32,6 +32,7 @@ import androidx.recyclerview.widget.SimpleItemAnimator import com.google.android.material.textfield.TextInputLayout import com.kunzisoft.keepass.R import com.kunzisoft.keepass.adapters.EntryAttachmentsAdapter +import com.kunzisoft.keepass.adapters.EntryExtraFieldsAdapter import com.kunzisoft.keepass.database.element.DateInstant import com.kunzisoft.keepass.database.element.icon.IconImage import com.kunzisoft.keepass.icons.IconDrawableFactory @@ -60,11 +61,12 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context, private val entryExpiresCheckBox: CompoundButton private val entryExpiresTextView: TextView private val entryNotesView: EditText - private val entryExtraFieldsContainerParent: ViewGroup - private val entryExtraFieldsContainer: ViewGroup + private val extraFieldsContainerView: ViewGroup + private val extraFieldsListView: RecyclerView private val attachmentsContainerView: View private val attachmentsListView: RecyclerView + private val extraFieldsAdapter = EntryExtraFieldsAdapter(context) private val attachmentsAdapter = EntryAttachmentsAdapter(context, true) private var iconColor: Int = 0 @@ -94,10 +96,32 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context, entryExpiresCheckBox = findViewById(R.id.entry_edit_expires_checkbox) entryExpiresTextView = findViewById(R.id.entry_edit_expires_text) entryNotesView = findViewById(R.id.entry_edit_notes) - entryExtraFieldsContainerParent = findViewById(R.id.extra_fields_container_parent) - entryExtraFieldsContainer = findViewById(R.id.extra_fields_container) + + extraFieldsContainerView = findViewById(R.id.extra_fields_container) + extraFieldsListView = findViewById(R.id.extra_fields_list) + // To hide or not the container + extraFieldsAdapter.onListSizeChangedListener = { previousSize, newSize -> + if (previousSize > 0 && newSize == 0) { + extraFieldsContainerView.collapse(true) + } else if (previousSize == 0 && newSize == 1) { + extraFieldsContainerView.expand(true) + } + } + extraFieldsListView?.apply { + layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) + adapter = extraFieldsAdapter + (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false + } + attachmentsContainerView = findViewById(R.id.entry_attachments_container) attachmentsListView = findViewById(R.id.entry_attachments_list) + attachmentsAdapter.onListSizeChangedListener = { previousSize, newSize -> + if (previousSize > 0 && newSize == 0) { + attachmentsContainerView.collapse(true) + } else if (previousSize == 0 && newSize == 1) { + attachmentsContainerView.expand(true) + } + } attachmentsListView?.apply { layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true) adapter = attachmentsAdapter @@ -108,17 +132,6 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context, assignExpiresDateText() } - entryExtraFieldsContainer.setOnHierarchyChangeListener(object: OnHierarchyChangeListener { - override fun onChildViewRemoved(parent: View?, child: View?) {} - - override fun onChildViewAdded(parent: View?, child: View?) { - parent?.let { - if ((parent as ViewGroup).childCount == 1) - parent.expand() - } - } - }) - // Retrieve the textColor to tint the icon val taIconColor = context.theme.obtainStyledAttributes(intArrayOf(android.R.attr.textColor)) iconColor = taIconColor.getColor(0, Color.WHITE) @@ -127,6 +140,7 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context, fun applyFontVisibilityToFields(fontInVisibility: Boolean) { this.fontInVisibility = fontInVisibility + this.extraFieldsAdapter.applyFontVisibility = fontInVisibility } var title: String @@ -227,90 +241,25 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context, * ------------- */ - val customFields: MutableList - get() { - val customFieldsArray = ArrayList() - // Add extra fields from views - entryExtraFieldsContainer.let { - try { - for (i in 0 until it.childCount) { - val view = it.getChildAt(i) as EntryEditExtraField - customFieldsArray.add(view.extraField) - } - } catch (exception: Exception) { - // Extra field container contains another type of view - } - } - return customFieldsArray - } - - private fun getExtraFieldByLabel(label: String): EntryEditExtraField? { - for (i in 0..entryExtraFieldsContainer.childCount) { - try { - val extraFieldView = entryExtraFieldsContainer.getChildAt(i) as EntryEditExtraField? - if (extraFieldView?.extraField?.name == label) { - return extraFieldView - } - } catch(e: Exception) { - // Simply ignore when child view is not a custom field - } - } - return null - } - - private fun showOrHideExtraFieldsContainer(hide: Boolean, animate: Boolean = false) { - entryExtraFieldsContainerParent.apply { - if (!animate) - visibility = if (hide) View.GONE else View.VISIBLE - else - if (hide) collapse(true) else expand(true) - } - } - - private fun buildNewEntryEditExtraField(): EntryEditExtraField { - return EntryEditExtraField(context).apply { - applyFontVisibility(fontInVisibility) - onViewDeletedListener = { - // remove callback - if (entryExtraFieldsContainer.childCount <= 1) { - showOrHideExtraFieldsContainer(hide = true, animate = true) - } - } - } + fun getExtraField(): MutableList { + return extraFieldsAdapter.extraFieldsList } /** * Remove all children and add new views for each field */ fun assignExtraFields(fields: List) { - showOrHideExtraFieldsContainer(fields.isEmpty()) - entryExtraFieldsContainer.removeAllViews() - fields.forEach { extraField -> - entryExtraFieldsContainer.post { - entryExtraFieldsContainer.addView(buildNewEntryEditExtraField().apply { - this.extraField = extraField - }) - } - } + extraFieldsContainerView.visibility = if (fields.isEmpty()) View.GONE else View.VISIBLE + extraFieldsAdapter.assignExtraFields(fields) } /** * Update an extra field or create a new one if doesn't exists */ - fun putExtraField(extraField: Field) - : EntryEditExtraField { - showOrHideExtraFieldsContainer(false) - var extraFieldView = getExtraFieldByLabel(extraField.name) - // Create new view if not exists - if (extraFieldView == null) { - extraFieldView = buildNewEntryEditExtraField() - // No need animation because of scroll - entryExtraFieldsContainer.post { - entryExtraFieldsContainer.addView(extraFieldView) - } - } - extraFieldView.extraField = extraField - return extraFieldView + fun putExtraField(extraField: Field) { + extraFieldsContainerView.visibility = View.VISIBLE + // TODO setError(R.string.error_label_exists) + extraFieldsAdapter.putExtraField(extraField) } /* ------------- @@ -318,26 +267,13 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context, * ------------- */ - private fun showOrHideAttachmentsContainer(hide: Boolean, animate: Boolean = false) { - attachmentsContainerView.apply { - if (!animate) - visibility = if (hide) View.GONE else View.VISIBLE - else - if (hide) collapse(true) else expand(true) - } - } - - fun assignAttachments(attachments: java.util.ArrayList, + fun assignAttachments(attachments: ArrayList, onDeleteItem: (attachment: EntryAttachment)->Unit) { - showOrHideAttachmentsContainer(attachments.isEmpty()) + attachmentsContainerView.visibility = if (attachments.isEmpty()) View.GONE else View.VISIBLE attachmentsAdapter.assignAttachments(attachments) attachmentsAdapter.onDeleteButtonClickListener = { item -> onDeleteItem.invoke(item) } - attachmentsAdapter.onItemDeletedListener = { _, lastOne -> - if (lastOne) - showOrHideAttachmentsContainer(hide = true, animate = true) - } } /** @@ -346,25 +282,7 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context, * @return ErrorValidation An error with a message or a validation without message */ fun isValid(): Boolean { - // Validate extra fields - entryExtraFieldsContainer.let { - try { - val customFieldLabelSet = HashSet() - for (i in 0 until it.childCount) { - val entryEditCustomField = it.getChildAt(i) as EntryEditExtraField - if (customFieldLabelSet.contains(entryEditCustomField.extraField.name)) { - entryEditCustomField.setError(R.string.error_label_exists) - return false - } - customFieldLabelSet.add(entryEditCustomField.extraField.name) - if (!entryEditCustomField.isValid()) { - return false - } - } - } catch (exception: Exception) { - return false - } - } + // TODO return true } diff --git a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditExtraField.kt b/app/src/main/java/com/kunzisoft/keepass/view/EntryEditExtraField.kt deleted file mode 100644 index baa9dd87d..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/view/EntryEditExtraField.kt +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2019 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 . - * - */ -package com.kunzisoft.keepass.view - -import android.content.Context -import android.graphics.Rect -import android.util.AttributeSet -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.view.inputmethod.EditorInfo -import android.widget.EditText -import android.widget.RelativeLayout -import androidx.annotation.StringRes -import com.google.android.material.textfield.TextInputLayout -import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.database.element.security.ProtectedString -import com.kunzisoft.keepass.model.Field - -class EntryEditExtraField @JvmOverloads constructor(context: Context, - attrs: AttributeSet? = null, - defStyle: Int = 0) - : RelativeLayout(context, attrs, defStyle) { - - private val valueLayoutView: TextInputLayout - private val valueView: EditText - private val deleteButton: View - - private var mApplyFontVisibility = false - private var isProtected = false - private var mValueViewInputType: Int = 0 - - var onViewDeletedListener: ((Field) -> Unit)? = null - - var extraField: Field - get() { - return Field(valueLayoutView.hint.toString(), ProtectedString(isProtected, valueView.text.toString())) - } - set(value) { - valueLayoutView.hint = value.name - isProtected = value.protectedValue.isProtected - if (isProtected) { - valueLayoutView.isPasswordVisibilityToggleEnabled = true - valueView.inputType = EditorInfo.TYPE_TEXT_VARIATION_PASSWORD or mValueViewInputType - } else { - valueLayoutView.isPasswordVisibilityToggleEnabled = false - valueView.inputType = mValueViewInputType - } - valueView.setText(value.protectedValue.toString()) - } - - init { - val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater? - inflater?.inflate(R.layout.item_entry_edit_extra_field, this) - - valueLayoutView = findViewById(R.id.entry_extra_field_value_container) - valueView = findViewById(R.id.entry_extra_field_value) - deleteButton = findViewById(R.id.entry_extra_field_delete) - - mValueViewInputType = valueView.inputType - deleteButton.setOnClickListener { - collapse(true) { - onViewDeletedListener?.invoke(extraField) - onViewDeletedListener = null - (parent as ViewGroup?)?.apply { - removeView(this@EntryEditExtraField) - } - } - } - } - - fun applyFontVisibility(applyFontVisibility: Boolean) { - mApplyFontVisibility = applyFontVisibility - if (applyFontVisibility) - valueView.applyFontVisibility() - } - - /** - * Validate or not the entry form - * - * @return ErrorValidation An error with a message or a validation without message - */ - fun isValid(): Boolean { - // Validate extra field - if (valueLayoutView.hint.toString().isEmpty()) { - setError(R.string.error_string_key) - return false - } else { - setError(null) - } - return true - } - - fun setError(@StringRes errorId: Int?) { - valueLayoutView.error = if (errorId == null) null else { - context.getString(errorId) - } - } - - override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean { - valueView.requestFocus(direction, previouslyFocusedRect) - return true - } -} diff --git a/app/src/main/res/layout/item_entry_edit_extra_field.xml b/app/src/main/res/layout/item_entry_edit_extra_field.xml index ad1cd90cf..4f9775558 100644 --- a/app/src/main/res/layout/item_entry_edit_extra_field.xml +++ b/app/src/main/res/layout/item_entry_edit_extra_field.xml @@ -23,7 +23,6 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" - android:importantForAutofill="noExcludeDescendants" tools:targetApi="o"> + android:importantForAutofill="no" /> + app:layout_constraintBottom_toTopOf="@+id/extra_fields_container"> - - +