diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt index 8e3717ed5..8aacc2334 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt @@ -276,7 +276,7 @@ class EntryActivity : LockingActivity() { } // Assign title text - val entryTitle = if (entryInfo.title.isNotEmpty()) entryInfo.title else entryInfo.id + val entryTitle = if (entryInfo.title.isNotEmpty()) entryInfo.title else entryInfo.id.toString() collapsingToolbarLayout?.title = entryTitle toolbar?.title = entryTitle diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/fragments/EntryFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/fragments/EntryFragment.kt index 633c262bd..88845000c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/fragments/EntryFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/fragments/EntryFragment.kt @@ -14,14 +14,12 @@ import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.SimpleItemAnimator import com.kunzisoft.keepass.R import com.kunzisoft.keepass.adapters.EntryAttachmentsItemsAdapter -import com.kunzisoft.keepass.adapters.EntryHistoryAdapter import com.kunzisoft.keepass.database.element.Attachment import com.kunzisoft.keepass.database.element.DateInstant -import com.kunzisoft.keepass.database.element.Entry import com.kunzisoft.keepass.database.element.security.ProtectedString -import com.kunzisoft.keepass.database.element.template.TemplateEngine import com.kunzisoft.keepass.database.element.template.TemplateField import com.kunzisoft.keepass.model.EntryAttachmentState +import com.kunzisoft.keepass.model.EntryInfo import com.kunzisoft.keepass.model.StreamDirection import com.kunzisoft.keepass.otp.OtpElement import com.kunzisoft.keepass.otp.OtpEntryFields @@ -29,7 +27,6 @@ import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.timeout.ClipboardHelper import com.kunzisoft.keepass.utils.UuidUtil import com.kunzisoft.keepass.view.EntryFieldView -import com.kunzisoft.keepass.view.showByFading import com.kunzisoft.keepass.viewmodels.EntryViewModel import java.util.* @@ -55,42 +52,40 @@ class EntryFragment: DatabaseFragment() { private lateinit var attachmentsListView: RecyclerView private var attachmentsAdapter: EntryAttachmentsItemsAdapter? = null - private lateinit var historyContainerView: View - private lateinit var historyListView: RecyclerView - private var historyAdapter: EntryHistoryAdapter? = null - private lateinit var uuidContainerView: View private lateinit var uuidView: TextView private lateinit var uuidReferenceView: TextView private var mFontInVisibility: Boolean = false private var mHideProtectedValue: Boolean = false + private var mIsFirstTimeAskAllowCopyPasswordAndProtectedFields: Boolean = false + private var mAllowCopyPasswordAndProtectedFields: Boolean = false private var mOtpRunnable: Runnable? = null private var mClipboardHelper: ClipboardHelper? = null private val mEntryViewModel: EntryViewModel by activityViewModels() - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { super.onCreateView(inflater, container, savedInstanceState) - val rootView = inflater.cloneInContext(contextThemed) - .inflate(R.layout.fragment_entry, container, false) + return inflater.cloneInContext(contextThemed) + .inflate(R.layout.fragment_entry, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) context?.let { context -> mClipboardHelper = ClipboardHelper(context) attachmentsAdapter = EntryAttachmentsItemsAdapter(context) attachmentsAdapter?.database = mDatabase - historyAdapter = EntryHistoryAdapter(context) } - rootView.showByFading() - return rootView - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - entryFieldsContainerView = view.findViewById(R.id.entry_fields_container) entryFieldsContainerView.visibility = View.GONE @@ -127,13 +122,6 @@ class EntryFragment: DatabaseFragment() { modificationDateView = view.findViewById(R.id.entry_modified) expiresImageView = view.findViewById(R.id.entry_expires_image) - historyContainerView = view.findViewById(R.id.entry_history_container) - historyListView = view.findViewById(R.id.entry_history_list) - historyListView.apply { - layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true) - adapter = historyAdapter - } - uuidContainerView = view.findViewById(R.id.entry_UUID_container) uuidContainerView.apply { visibility = if (PreferencesUtil.showUUID(context)) View.VISIBLE else View.GONE @@ -141,8 +129,8 @@ class EntryFragment: DatabaseFragment() { uuidView = view.findViewById(R.id.entry_UUID) uuidReferenceView = view.findViewById(R.id.entry_UUID_reference) - mEntryViewModel.entry.observe(viewLifecycleOwner) { entryHistory -> - assignEntry(entryHistory?.entry) + mEntryViewModel.entryInfo.observe(viewLifecycleOwner) { entryInfo -> + assignEntryInfo(entryInfo) } } @@ -152,6 +140,10 @@ class EntryFragment: DatabaseFragment() { context?.let { context -> mFontInVisibility = PreferencesUtil.fieldFontIsInVisibility(context) mHideProtectedValue = PreferencesUtil.hideProtectedValue(context) + mIsFirstTimeAskAllowCopyPasswordAndProtectedFields = + PreferencesUtil.isFirstTimeAskAllowCopyPasswordAndProtectedFields(context) + mAllowCopyPasswordAndProtectedFields = + PreferencesUtil.allowCopyPasswordAndProtectedFields(context) } } @@ -176,9 +168,8 @@ class EntryFragment: DatabaseFragment() { getString(R.string.copy_field, appNameString)) } - private fun assignEntry(entry: Entry?) { + private fun assignEntryInfo(entryInfo: EntryInfo?) { context?.let { context -> - val entryInfo = entry?.getEntryInfo(mDatabase) entryInfo?.username?.let { userName -> assignUserName(userName) { @@ -188,30 +179,10 @@ class EntryFragment: DatabaseFragment() { } } - val isFirstTimeAskAllowCopyPasswordAndProtectedFields = - PreferencesUtil.isFirstTimeAskAllowCopyPasswordAndProtectedFields(context) - val allowCopyPasswordAndProtectedFields = - PreferencesUtil.allowCopyPasswordAndProtectedFields(context) val showWarningClipboardDialogOnClickListener = View.OnClickListener { - AlertDialog.Builder(context) - .setMessage(getString(R.string.allow_copy_password_warning) + - "\n\n" + - getString(R.string.clipboard_warning)) - .create().apply { - setButton(AlertDialog.BUTTON_POSITIVE, getText(R.string.enable)) { dialog, _ -> - PreferencesUtil.setAllowCopyPasswordAndProtectedFields(context, true) - dialog.dismiss() - assignEntry(entry) - } - setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.disable)) { dialog, _ -> - PreferencesUtil.setAllowCopyPasswordAndProtectedFields(context, false) - dialog.dismiss() - assignEntry(entry) - } - show() - } + showClipboardDialog(entryInfo) } - val onPasswordCopyClickListener: View.OnClickListener? = if (allowCopyPasswordAndProtectedFields) { + val onPasswordCopyClickListener: View.OnClickListener? = if (mAllowCopyPasswordAndProtectedFields) { View.OnClickListener { entryInfo?.password?.let { password -> mClipboardHelper?.timeoutCopyToClipboard(password, @@ -221,18 +192,19 @@ class EntryFragment: DatabaseFragment() { } } else { // If dialog not already shown - if (isFirstTimeAskAllowCopyPasswordAndProtectedFields) { + if (mIsFirstTimeAskAllowCopyPasswordAndProtectedFields) { showWarningClipboardDialogOnClickListener } else { null } } assignPassword(entryInfo?.password, - allowCopyPasswordAndProtectedFields, + mAllowCopyPasswordAndProtectedFields, onPasswordCopyClickListener) //Assign OTP field - entry?.getOtpElement()?.let { otpElement -> + entryInfo?.otpModel?.let { otpModel -> + val otpElement = OtpElement(otpModel) assignOtp(otpElement) { mClipboardHelper?.timeoutCopyToClipboard( otpElement.token, @@ -252,7 +224,7 @@ class EntryFragment: DatabaseFragment() { // OTP field is already managed in dedicated view if (label != OtpEntryFields.OTP_TOKEN_FIELD) { val value = field.protectedValue - val allowCopyProtectedField = !value.isProtected || allowCopyPasswordAndProtectedFields + val allowCopyProtectedField = !value.isProtected || mAllowCopyPasswordAndProtectedFields if (allowCopyProtectedField) { addExtraField(label, value, allowCopyProtectedField) { mClipboardHelper?.timeoutCopyToClipboard( @@ -263,7 +235,7 @@ class EntryFragment: DatabaseFragment() { } } else { // If dialog not already shown - if (isFirstTimeAskAllowCopyPasswordAndProtectedFields) { + if (mIsFirstTimeAskAllowCopyPasswordAndProtectedFields) { addExtraField(label, value, allowCopyProtectedField, showWarningClipboardDialogOnClickListener) } else { addExtraField(label, value, allowCopyProtectedField, null) @@ -285,11 +257,32 @@ class EntryFragment: DatabaseFragment() { assignModificationDate(entryInfo?.lastModificationTime) setExpires(entryInfo?.expires ?: false, entryInfo?.expiryTime) - // Assign entry history - assignHistory(entry?.getHistory()) - // Assign special data - assignUUID(entry?.nodeId?.id) + assignUUID(entryInfo?.id) + } + } + + private fun showClipboardDialog(entryInfo: EntryInfo?) { + context?.let { + AlertDialog.Builder(it) + .setMessage( + getString(R.string.allow_copy_password_warning) + + "\n\n" + + getString(R.string.clipboard_warning) + ) + .create().apply { + setButton(AlertDialog.BUTTON_POSITIVE, getText(R.string.enable)) { dialog, _ -> + PreferencesUtil.setAllowCopyPasswordAndProtectedFields(context, true) + dialog.dismiss() + assignEntryInfo(entryInfo) + } + setButton(AlertDialog.BUTTON_NEGATIVE, getText(R.string.disable)) { dialog, _ -> + PreferencesUtil.setAllowCopyPasswordAndProtectedFields(context, false) + dialog.dismiss() + assignEntryInfo(entryInfo) + } + show() + } } } @@ -473,25 +466,6 @@ class EntryFragment: DatabaseFragment() { attachmentsAdapter?.putItem(attachmentToDownload) } - /* ------------- - * History - * ------------- - */ - private fun assignHistory(history: ArrayList?) { - historyAdapter?.clear() - history?.let { - historyAdapter?.entryHistoryList?.addAll(history) - } - historyAdapter?.onItemClickListener = { item, position -> - mEntryViewModel.onHistorySelected(item, position) - } - historyContainerView.visibility = if (historyAdapter?.entryHistoryList?.isEmpty() != false) - View.GONE - else - View.VISIBLE - historyAdapter?.notifyDataSetChanged() - } - companion object { fun getInstance(): EntryFragment { diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/fragments/EntryHistoryFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/fragments/EntryHistoryFragment.kt new file mode 100644 index 000000000..5213b85ab --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/activities/fragments/EntryHistoryFragment.kt @@ -0,0 +1,72 @@ +package com.kunzisoft.keepass.activities.fragments + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.activityViewModels +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.activities.stylish.StylishFragment +import com.kunzisoft.keepass.adapters.EntryHistoryAdapter +import com.kunzisoft.keepass.database.element.Entry +import com.kunzisoft.keepass.viewmodels.EntryViewModel + +class EntryHistoryFragment: StylishFragment() { + + private lateinit var historyContainerView: View + private lateinit var historyListView: RecyclerView + private var historyAdapter: EntryHistoryAdapter? = null + + private val mEntryViewModel: EntryViewModel by activityViewModels() + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + super.onCreateView(inflater, container, savedInstanceState) + + return inflater.cloneInContext(contextThemed) + .inflate(R.layout.fragment_entry_history, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + context?.let { context -> + historyAdapter = EntryHistoryAdapter(context) + } + + historyContainerView = view.findViewById(R.id.entry_history_container) + historyListView = view.findViewById(R.id.entry_history_list) + historyListView.apply { + layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true) + adapter = historyAdapter + } + + mEntryViewModel.entryHistory.observe(viewLifecycleOwner) { + assignHistory(it) + } + } + + /* ------------- + * History + * ------------- + */ + private fun assignHistory(history: List?) { + historyAdapter?.clear() + history?.let { + historyAdapter?.entryHistoryList?.addAll(history) + } + historyAdapter?.onItemClickListener = { item, position -> + mEntryViewModel.onHistorySelected(item, position) + } + historyContainerView.visibility = if (historyAdapter?.entryHistoryList?.isEmpty() != false) + View.GONE + else + View.VISIBLE + historyAdapter?.notifyDataSetChanged() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt b/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt index 2cc06e78d..1cce90cf1 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt @@ -114,7 +114,7 @@ object AutofillHelper { val title = makeEntryTitle(entryInfo) val views = newRemoteViews(context, database, title, entryInfo.icon) val builder = Dataset.Builder(views) - builder.setId(entryInfo.id) + builder.setId(entryInfo.id.toString()) struct.usernameId?.let { usernameId -> builder.setValue(usernameId, AutofillValue.forText(entryInfo.username)) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Entry.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Entry.kt index a69540d7d..a4d9bc0f3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Entry.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Entry.kt @@ -404,7 +404,7 @@ class Entry : Node, EntryVersionedInterface { else database?.startManageEntry(this) - entryInfo.id = nodeId.toString() + entryInfo.id = nodeId.id entryInfo.title = title entryInfo.icon = icon entryInfo.username = username diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt index 3ad95f254..0afc1d634 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt @@ -20,6 +20,7 @@ package com.kunzisoft.keepass.model import android.os.Parcel +import android.os.ParcelUuid import android.os.Parcelable import com.kunzisoft.keepass.database.element.Attachment import com.kunzisoft.keepass.database.element.Database @@ -29,10 +30,11 @@ import com.kunzisoft.keepass.database.element.template.TemplateField import com.kunzisoft.keepass.otp.OtpElement import com.kunzisoft.keepass.otp.OtpEntryFields import com.kunzisoft.keepass.otp.OtpEntryFields.OTP_TOKEN_FIELD +import java.util.* class EntryInfo : NodeInfo { - var id: String = "" + var id: UUID = UUID.randomUUID() var username: String = "" var password: String = "" var url: String = "" @@ -45,7 +47,7 @@ class EntryInfo : NodeInfo { constructor() : super() constructor(parcel: Parcel) : super(parcel) { - id = parcel.readString() ?: id + id = parcel.readParcelable(ParcelUuid::class.java.classLoader)?.uuid ?: id username = parcel.readString() ?: username password = parcel.readString() ?: password url = parcel.readString() ?: url @@ -62,7 +64,7 @@ class EntryInfo : NodeInfo { override fun writeToParcel(parcel: Parcel, flags: Int) { super.writeToParcel(parcel, flags) - parcel.writeString(id) + parcel.writeParcelable(ParcelUuid(id), flags) parcel.writeString(username) parcel.writeString(password) parcel.writeString(url) diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt index 30b4ceb90..ce3eb1f21 100644 --- a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt +++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt @@ -9,6 +9,7 @@ import com.kunzisoft.keepass.database.element.Attachment import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Entry import com.kunzisoft.keepass.database.element.node.NodeId +import com.kunzisoft.keepass.model.EntryInfo import com.kunzisoft.keepass.otp.OtpElement import java.util.* @@ -20,6 +21,12 @@ class EntryViewModel: ViewModel() { val entry : LiveData get() = _entry private val _entry = MutableLiveData() + val entryInfo : LiveData get() = _entryInfo + private val _entryInfo = MutableLiveData() + + val entryHistory : LiveData> get() = _entryHistory + private val _entryHistory = MutableLiveData>() + val otpElement : LiveData get() = _otpElement private val _otpElement = SingleLiveEvent() @@ -44,8 +51,8 @@ class EntryViewModel: ViewModel() { entry = mDatabase.decodeEntryWithTemplateConfiguration(it) } _entry.value = EntryHistory(nodeIdUUID, entry, entryLastVersion, historyPosition) - } else { - _entry.value = EntryHistory(null, null, null) + _entryInfo.value = entry?.getEntryInfo(mDatabase) + _entryHistory.value = entry?.getHistory() } } diff --git a/app/src/main/res/layout/fragment_entry.xml b/app/src/main/res/layout/fragment_entry.xml index 0e44154a0..0e36f056a 100644 --- a/app/src/main/res/layout/fragment_entry.xml +++ b/app/src/main/res/layout/fragment_entry.xml @@ -190,89 +190,11 @@ - - - - - - - - - - - - - - - - - - - - - + android:layout_height="wrap_content" /> + + + + + + + + + + + + + + + + + + + + + +