From a8ccb67a87eb737ae9db97ccc403c775569ca80a Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sun, 15 Sep 2019 17:56:25 +0200 Subject: [PATCH] Entry history first code --- .../keepass/activities/EntryActivity.kt | 2 + .../keepass/adapters/EntryHistoryAdapter.kt | 56 +++++++++++++++++++ .../keepass/database/element/Database.kt | 2 +- .../database/element/EntryVersioned.kt | 13 ++++- .../keepass/view/EntryContentsView.kt | 18 ++++++ .../res/layout/item_list_entry_history.xml | 53 ++++++++++++++++++ .../main/res/layout/view_entry_contents.xml | 33 ++++++++++- app/src/main/res/values/strings.xml | 1 + 8 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/kunzisoft/keepass/adapters/EntryHistoryAdapter.kt create mode 100644 app/src/main/res/layout/item_list_entry_history.xml 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 6a54c6b1d..27fd04025 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt @@ -257,6 +257,8 @@ class EntryActivity : LockingHideActivity() { // Assign special data entryContentsView?.assignUUID(entry.nodeId.id) + entryContentsView?.assignHistory(entry.getHistory()) + database.stopManageEntry(entry) } diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/EntryHistoryAdapter.kt b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryHistoryAdapter.kt new file mode 100644 index 000000000..d3bb0c424 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/EntryHistoryAdapter.kt @@ -0,0 +1,56 @@ +package com.kunzisoft.keepass.adapters + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.database.element.EntryVersioned + +class EntryHistoryAdapter(context: Context) : RecyclerView.Adapter() { + + private val inflater: LayoutInflater = LayoutInflater.from(context) + var entryHistoryList: MutableList = ArrayList() + var onItemClickListener: OnItemClickListener? = null + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EntryHistoryViewHolder { + return EntryHistoryViewHolder(inflater.inflate(R.layout.item_list_entry_history, parent, false)) + } + + override fun onBindViewHolder(holder: EntryHistoryViewHolder, position: Int) { + val entryHistory = entryHistoryList[position] + + holder.lastModifiedView.text = entryHistory.lastModificationTime.date.toString() + holder.titleView.text = entryHistory.title + holder.usernameView.text = entryHistory.username + holder.urlView.text = entryHistory.url + + holder.bind(entryHistory, onItemClickListener) + } + + override fun getItemCount(): Int { + return entryHistoryList.size + } + + fun clear() { + entryHistoryList.clear() + } + + interface OnItemClickListener { + fun onItemClick(item: EntryVersioned) + } + + inner class EntryHistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + + var lastModifiedView: TextView = itemView.findViewById(R.id.entry_history_last_modified) + var titleView: TextView = itemView.findViewById(R.id.entry_history_title) + var usernameView: TextView = itemView.findViewById(R.id.entry_history_username) + var urlView: TextView = itemView.findViewById(R.id.entry_history_url) + + fun bind(item: EntryVersioned, listener: OnItemClickListener?) { + itemView.setOnClickListener { listener?.onItemClick(item) } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt index 8954abf88..e4061579f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt @@ -723,7 +723,7 @@ class Database { while (true) { var histSize: Long = 0 for (backup in history) { - histSize += backup.size + histSize += backup.getSize() } if (histSize > maxSize) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt index 676f2ceb0..38d74cf85 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/EntryVersioned.kt @@ -270,8 +270,17 @@ class EntryVersioned : NodeVersioned, PwEntryInterface { pwEntryV4?.removeOldestEntryFromHistory() } - fun getHistory(): ArrayList { - return pwEntryV4?.history ?: ArrayList() + fun getHistory(): ArrayList { + val history = ArrayList() + val entryV4History = pwEntryV4?.history ?: ArrayList() + for (entryHistory in entryV4History) { + history.add(EntryVersioned(entryHistory)) + } + return history + } + + fun getSize(): Long { + return pwEntryV4?.size ?: 0L } fun containsCustomData(): Boolean { 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 d2d36a523..ecf6cf149 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/EntryContentsView.kt @@ -29,7 +29,11 @@ import android.view.ViewGroup import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.adapters.EntryHistoryAdapter +import com.kunzisoft.keepass.database.element.EntryVersioned import com.kunzisoft.keepass.database.element.security.ProtectedString import java.text.DateFormat import java.util.* @@ -69,6 +73,9 @@ class EntryContentsView @JvmOverloads constructor(context: Context, private val uuidView: TextView + private val historyView: RecyclerView + private val historyAdapter = EntryHistoryAdapter(context) + val isUserNamePresent: Boolean get() = userNameContainerView.visibility == View.VISIBLE @@ -103,6 +110,12 @@ class EntryContentsView @JvmOverloads constructor(context: Context, uuidView = findViewById(R.id.entry_UUID) + historyView = findViewById(R.id.entry_history_list) + historyView?.apply { + layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true) + adapter = historyAdapter + } + val attrColorAccent = intArrayOf(R.attr.colorAccent) val taColorAccent = context.theme.obtainStyledAttributes(attrColorAccent) colorAccent = taColorAccent.getColor(0, Color.BLACK) @@ -258,6 +271,11 @@ class EntryContentsView @JvmOverloads constructor(context: Context, uuidView.text = uuid.toString() } + fun assignHistory(history: ArrayList) { + historyAdapter.clear() + historyAdapter.entryHistoryList.addAll(history) + } + override fun generateDefaultLayoutParams(): LayoutParams { return LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) } diff --git a/app/src/main/res/layout/item_list_entry_history.xml b/app/src/main/res/layout/item_list_entry_history.xml new file mode 100644 index 000000000..95c477325 --- /dev/null +++ b/app/src/main/res/layout/item_list_entry_history.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/view_entry_contents.xml b/app/src/main/res/layout/view_entry_contents.xml index 506510098..85fda0f0b 100644 --- a/app/src/main/res/layout/view_entry_contents.xml +++ b/app/src/main/res/layout/view_entry_contents.xml @@ -254,7 +254,7 @@ android:layout_margin="@dimen/default_margin" android:orientation="vertical"> - + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4c33bec66..f5c053ce9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,6 +81,7 @@ Created Expires UUID + History Keyfile Modified Could not find entry data.