New UI for custom field

This commit is contained in:
J-Jamet committed 2020-08-03 18:45:50 +02:00
1 parent 9679d24414
commit 1087dcd714
8 files changed
+281 -94

No files matched your search

@@ -45,6 +45,7 @@ import com.kunzisoft.keepass.database.element.Group
import com.kunzisoft.keepass.database.element.icon.IconImage
import com.kunzisoft.keepass.database.element.icon.IconImageStandard
import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.database.element.security.ProtectedString
import com.kunzisoft.keepass.education.EntryEditActivityEducation
import com.kunzisoft.keepass.notifications.ClipboardEntryNotificationService
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_ENTRY_TASK
@@ -62,6 +63,7 @@ import java.util.*
class EntryEditActivity : LockingActivity(),
IconPickerDialogFragment.IconPickerListener,
EntryCustomFieldDialogFragment.EntryCustomFieldListener,
GeneratePasswordDialogFragment.GeneratePasswordListener,
SetOTPDialogFragment.CreateOtpListener,
DatePickerDialog.OnDateSetListener,
@@ -201,7 +203,7 @@ class EntryEditActivity : LockingActivity(),
entryEditContentsView?.setOnIconViewClickListener { IconPickerDialogFragment.launch(this@EntryEditActivity) }
// Bottom Bar
entryEditAddToolBar = findViewById(R.id.entry_edit_bottom_bar)
entryEditAddToolBar = findViewById(R.id.entry_edit_bottom_menu_bar)
entryEditAddToolBar?.apply {
menuInflater.inflate(R.menu.entry_edit, menu)
@@ -335,12 +337,18 @@ class EntryEditActivity : LockingActivity(),
}
/**
* Add a new customized field view and scroll to bottom
* Add a new customized field
*/
private fun addNewCustomField() {
entryEditContentsView?.addEmptyCustomField()
EntryCustomFieldDialogFragment.getInstance().show(supportFragmentManager, "customFieldDialog")
}
override fun onNewCustomFieldApproved(label: String, name: ProtectedString) {
entryEditContentsView?.putCustomField(label, name, true)
}
override fun onNewCustomFieldCanceled(label: String, name: ProtectedString) {}
private fun setupOTP() {
// Retrieve the current otpElement if exists
// and open the dialog to set up the OTP
@@ -0,0 +1,126 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.activities.dialogs
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.widget.Button
import android.widget.CompoundButton
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.security.ProtectedString
class EntryCustomFieldDialogFragment: DialogFragment() {
private var entryCustomFieldListener: EntryCustomFieldListener? = null
private var newFieldLabel: TextView? = null
private var newFieldValue: TextView? = null
private var newFieldProtection: CompoundButton? = null
override fun onAttach(context: Context) {
super.onAttach(context)
try {
entryCustomFieldListener = context as EntryCustomFieldListener
} catch (e: ClassCastException) {
// The activity doesn't implement the interface, throw exception
throw ClassCastException(context.toString()
+ " must implement " + EntryCustomFieldListener::class.java.name)
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
activity?.let { activity ->
val root = activity.layoutInflater.inflate(R.layout.fragment_entry_new_field, null)
newFieldLabel = root?.findViewById(R.id.entry_new_field_label)
newFieldValue = root?.findViewById(R.id.entry_new_field_value)
newFieldProtection = root?.findViewById(R.id.entry_new_field_protection)
val builder = AlertDialog.Builder(activity)
builder.setView(root)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel) { _, _ ->
entryCustomFieldListener?.onNewCustomFieldCanceled(
newFieldLabel?.text.toString(),
ProtectedString(
newFieldProtection?.isChecked == true,
newFieldValue?.text.toString()
)
)
}
return builder.create()
}
return super.onCreateDialog(savedInstanceState)
}
override fun onResume() {
super.onResume()
// To prevent auto dismiss
val d = dialog as AlertDialog?
if (d != null) {
val positiveButton = d.getButton(Dialog.BUTTON_POSITIVE) as Button
positiveButton.setOnClickListener {
if (isValid()) {
entryCustomFieldListener?.onNewCustomFieldApproved(
newFieldLabel?.text.toString(),
ProtectedString(
newFieldProtection?.isChecked == true,
newFieldValue?.text.toString()
)
)
d.dismiss()
}
}
}
}
private fun isValid(): Boolean {
return if (newFieldLabel?.text?.toString()?.isNotEmpty() != true) {
setError(R.string.error_string_key)
false
} else {
setError(null)
true
}
}
fun setError(@StringRes errorId: Int?) {
newFieldLabel?.error = if (errorId == null) null else {
requireContext().getString(errorId)
}
}
interface EntryCustomFieldListener {
fun onNewCustomFieldApproved(label: String, name: ProtectedString)
fun onNewCustomFieldCanceled(label: String, name: ProtectedString)
}
companion object {
fun getInstance(): EntryCustomFieldDialogFragment {
return EntryCustomFieldDialogFragment()
}
}
}
@@ -215,25 +215,12 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
return customFieldsArray
}
/**
* Add a new view to fill in the information of the customized field and focus it
*/
fun addEmptyCustomField() {
// Fix current custom field before adding a new one
if (isValid()) {
val entryEditCustomField = EntryEditCustomField(context).apply {
setFontVisibility(fontInVisibility)
requestFocus()
}
entryExtraFieldsContainer.addView(entryEditCustomField)
}
}
/**
* Update a custom field or create a new one if doesn't exists
*/
fun putCustomField(name: String,
value: ProtectedString = ProtectedString()) {
value: ProtectedString = ProtectedString(),
requestFocus: Boolean = false) {
var updateField = false
for (i in 0..entryExtraFieldsContainer.childCount) {
try {
@@ -252,6 +239,8 @@ class EntryEditContentsView @JvmOverloads constructor(context: Context,
setData(name, value, fontInVisibility)
}
entryExtraFieldsContainer.addView(entryEditCustomField)
if (requestFocus)
entryEditCustomField.requestFocus()
}
}
@@ -20,7 +20,6 @@
package com.kunzisoft.keepass.view
import android.content.Context
import com.google.android.material.textfield.TextInputLayout
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
@@ -29,8 +28,8 @@ import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.StringRes
import com.google.android.material.textfield.TextInputLayout
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.security.ProtectedString
@@ -39,13 +38,12 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
defStyle: Int = 0)
: RelativeLayout(context, attrs, defStyle) {
private val labelLayoutView: TextInputLayout
private val labelView: TextView
private val valueLayoutView: TextInputLayout
private val valueView: EditText
private val protectionCheckView: CompoundButton
val label: String
get() = labelView.text.toString()
get() = valueLayoutView.hint.toString()
val value: String
get() = valueView.text.toString()
@@ -56,20 +54,19 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
init {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
inflater?.inflate(R.layout.view_entry_new_field, this)
inflater?.inflate(R.layout.view_entry_custom_field, this)
val deleteView = findViewById<View>(R.id.entry_new_field_delete)
deleteView.setOnClickListener { deleteViewFromParent() }
labelLayoutView = findViewById(R.id.title_container)
labelView = findViewById(R.id.entry_new_field_label)
valueLayoutView = findViewById(R.id.value_container)
valueView = findViewById(R.id.entry_new_field_value)
protectionCheckView = findViewById(R.id.protection)
}
fun setData(label: String?, value: ProtectedString?, fontInVisibility: Boolean) {
if (label != null)
labelView.text = label
valueLayoutView.hint = label
if (value != null) {
valueView.setText(value.toString())
protectionCheckView.isChecked = value.isProtected
@@ -94,7 +91,7 @@ class EntryEditCustomField @JvmOverloads constructor(context: Context,
}
fun setError(@StringRes errorId: Int?) {
labelLayoutView.error = if (errorId == null) null else {
valueLayoutView.error = if (errorId == null) null else {
context.getString(errorId)
}
}
+20 -26
View File
@@ -43,28 +43,6 @@
app:toolbarId="@+id/toolbar"
app:layout_scrollFlags="enterAlways|enterAlwaysCollapsed|scroll|exitUntilCollapsed|snap">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:theme="?attr/toolbarAppearance"
android:popupTheme="?attr/toolbarPopupAppearance">
<androidx.appcompat.widget.ActionMenuView
android:id="@+id/entry_edit_bottom_bar"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"/>
</androidx.appcompat.widget.Toolbar>
<View
android:id="@+id/biometric_delimiter"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/colorAccent"/>
</FrameLayout>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
@@ -88,7 +66,8 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:paddingBottom="@dimen/default_margin">
<com.kunzisoft.keepass.view.EntryEditContentsView
android:id="@+id/entry_edit_contents"
@@ -104,16 +83,31 @@
<include
layout="@layout/view_button_lock"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="start|bottom" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/entry_edit_bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="?attr/colorAccent"
app:fabAlignmentMode="end"
app:hideOnScroll="true"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_gravity="bottom" >
<androidx.appcompat.widget.ActionMenuView
android:id="@+id/entry_edit_bottom_menu_bar"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/entry_edit_validate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom|end"
app:layout_anchor="@+id/entry_edit_scroll"
app:layout_anchor="@+id/entry_edit_bottom_bar"
android:src="@drawable/ic_check_white_24dp"
android:contentDescription="@string/validate"
app:useCompatPadding="true"
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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 <http://www.gnu.org/licenses/>.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="noExcludeDescendants"
tools:targetApi="o">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/title_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingLeft="@dimen/default_margin"
android:paddingStart="@dimen/default_margin"
android:paddingRight="@dimen/default_margin"
android:paddingEnd="@dimen/default_margin">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/entry_new_field_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/field_name"
android:importantForAccessibility="no"
android:importantForAutofill="no"
android:inputType="text"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/value_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/default_margin"
android:paddingStart="@dimen/default_margin"
android:paddingRight="@dimen/default_margin"
android:paddingEnd="@dimen/default_margin"
android:layout_below="@+id/title_container">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/entry_new_field_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:importantForAccessibility="no"
android:importantForAutofill="no"
android:hint="@string/field_value" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/entry_new_field_protection"
style="@style/KeepassDXStyle.TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:layout_below="@+id/value_container"
android:layout_centerHorizontal="true"
android:text="@string/protection" />
</RelativeLayout>
@@ -18,11 +18,11 @@
along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
-->
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="0dp"
android:layout_marginBottom="@dimen/card_view_margin_bottom"
android:layout_marginStart="0dp"
@@ -30,63 +30,35 @@
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp">
<RelativeLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="noExcludeDescendants"
tools:targetApi="o">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/title_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingLeft="@dimen/default_margin"
android:paddingStart="@dimen/default_margin"
android:paddingRight="@dimen/default_margin"
android:paddingEnd="@dimen/default_margin">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/entry_new_field_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/field_name"
android:importantForAccessibility="no"
android:importantForAutofill="no"
android:inputType="text"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/entry_new_field_delete"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="12dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_close_white_24dp"
android:contentDescription="@string/content_description_remove_field"
android:tint="?attr/colorAccent" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/protection"
style="@style/KeepassDXStyle.TextAppearance.Default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:layout_below="@+id/value_container"
android:layout_centerHorizontal="true"
android:text="@string/protection" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/value_container"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:paddingLeft="@dimen/default_margin"
android:paddingStart="@dimen/default_margin"
android:paddingRight="@dimen/default_margin"
android:paddingEnd="@dimen/default_margin"
android:layout_below="@+id/title_container">
android:paddingEnd="@dimen/default_margin">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/entry_new_field_value"
@@ -98,6 +70,22 @@
android:hint="@string/field_value" />
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/protection"
style="@style/KeepassDXStyle.TextAppearance.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginStart="@dimen/default_margin"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginEnd="@dimen/default_margin"
android:layout_marginRight="@dimen/default_margin"
app:layout_constraintTop_toBottomOf="@+id/entry_new_field_delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="@string/protection" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
+5
View File
@@ -21,6 +21,11 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="AlwaysShowAction">
<item android:id="@+id/menu_lock"
android:icon="@drawable/ic_lock_white_24dp"
android:title="@string/lock"
android:orderInCategory="91"
app:showAsAction="always" />
<item android:id="@+id/menu_add_field"
android:icon="@drawable/ic_new_field_white_24dp"
android:title="@string/entry_add_field"