From a4732194ef0daf3f2bae58275596abfa2bd5075f Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Tue, 25 Jan 2022 10:59:39 +0100 Subject: [PATCH] Better quick search implementation and add path --- .../adapters/SearchEntryCursorAdapter.kt | 93 ++++++++++--------- .../keepass/database/cursor/EntryCursor.kt | 89 ------------------ .../keepass/database/cursor/EntryCursorKDB.kt | 45 --------- .../database/cursor/EntryCursorKDBX.kt | 73 --------------- .../database/cursor/EntryCursorUUID.kt | 34 ------- .../database/cursor/ExtraFieldCursor.kt | 62 ------------- app/src/main/res/layout/item_search_entry.xml | 22 ++++- 7 files changed, 70 insertions(+), 348 deletions(-) delete mode 100644 app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.kt delete mode 100644 app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDB.kt delete mode 100644 app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDBX.kt delete mode 100644 app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorUUID.kt delete mode 100644 app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.kt diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.kt b/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.kt index 9ae3beef7..88cdb8862 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.kt +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/SearchEntryCursorAdapter.kt @@ -21,28 +21,29 @@ package com.kunzisoft.keepass.adapters import android.content.Context import android.database.Cursor +import android.database.MatrixCursor import android.graphics.Color +import android.provider.BaseColumns import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView +import androidx.cursoradapter.widget.CursorAdapter import com.kunzisoft.keepass.R -import com.kunzisoft.keepass.database.cursor.EntryCursorKDB -import com.kunzisoft.keepass.database.cursor.EntryCursorKDBX import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Entry import com.kunzisoft.keepass.database.element.Group -import com.kunzisoft.keepass.database.element.database.DatabaseKDB -import com.kunzisoft.keepass.database.element.database.DatabaseKDBX -import com.kunzisoft.keepass.database.element.icon.IconImageCustom +import com.kunzisoft.keepass.database.element.node.NodeId +import com.kunzisoft.keepass.database.element.node.NodeIdUUID import com.kunzisoft.keepass.database.search.SearchHelper import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.view.strikeOut +import java.util.* class SearchEntryCursorAdapter(private val context: Context, private val database: Database) - : androidx.cursoradapter.widget.CursorAdapter(context, null, FLAG_REGISTER_CONTENT_OBSERVER) { + : CursorAdapter(context, null, FLAG_REGISTER_CONTENT_OBSERVER) { private val cursorInflater: LayoutInflater? = context.getSystemService( Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater? @@ -71,6 +72,7 @@ class SearchEntryCursorAdapter(private val context: Context, viewHolder.imageViewIcon = view.findViewById(R.id.entry_icon) viewHolder.textViewTitle = view.findViewById(R.id.entry_text) viewHolder.textViewSubTitle = view.findViewById(R.id.entry_subtext) + viewHolder.textViewPath = view.findViewById(R.id.entry_path) view.tag = viewHolder return view @@ -102,32 +104,16 @@ class SearchEntryCursorAdapter(private val context: Context, visibility = if (text.isEmpty()) View.GONE else View.VISIBLE strikeOut(currentEntry.isCurrentlyExpires) } + + viewHolder.textViewPath?.apply { + text = currentEntry.getPathString() + } } } private fun getEntryFrom(cursor: Cursor): Entry? { - return database.createEntry()?.apply { - entryKDB?.let { entryKDB -> - (cursor as EntryCursorKDB).populateEntry(entryKDB, - { standardIconId -> - database.getStandardIcon(standardIconId) - }, - { customIconId -> - database.getCustomIcon(customIconId) ?: IconImageCustom(customIconId) - } - ) - } - entryKDBX?.let { entryKDBX -> - (cursor as EntryCursorKDBX).populateEntry(entryKDBX, - { standardIconId -> - database.getStandardIcon(standardIconId) - }, - { customIconId -> - database.getCustomIcon(customIconId) ?: IconImageCustom(customIconId) - } - ) - } - } + val entryCursor = cursor as EntryCursor + return database.getEntryById(entryCursor.getNodeId()) } override fun runQueryOnBackgroundThread(constraint: CharSequence): Cursor? { @@ -135,14 +121,7 @@ class SearchEntryCursorAdapter(private val context: Context, } private fun searchEntries(context: Context, query: String): Cursor? { - var cursorKDB: EntryCursorKDB? = null - var cursorKDBX: EntryCursorKDBX? = null - - if (database.type == DatabaseKDB.TYPE) - cursorKDB = EntryCursorKDB() - if (database.type == DatabaseKDBX.TYPE) - cursorKDBX = EntryCursorKDBX() - + val cursor = EntryCursor() val searchGroup = database.createVirtualGroupFromSearch(query, mOmitBackup, SearchHelper.MAX_SEARCH_ENTRY) @@ -150,17 +129,11 @@ class SearchEntryCursorAdapter(private val context: Context, // Search in hide entries but not meta-stream for (entry in searchGroup.getFilteredChildEntries(Group.ChildFilter.getDefaults(context))) { database.startManageEntry(entry) - entry.entryKDB?.let { - cursorKDB?.addEntry(it) - } - entry.entryKDBX?.let { - cursorKDBX?.addEntry(it) - } + cursor.addEntry(entry) database.stopManageEntry(entry) } } - - return cursorKDB ?: cursorKDBX + return cursor } fun getEntryFromPosition(position: Int): Entry? { @@ -177,5 +150,37 @@ class SearchEntryCursorAdapter(private val context: Context, var imageViewIcon: ImageView? = null var textViewTitle: TextView? = null var textViewSubTitle: TextView? = null + var textViewPath: TextView? = null + } + + private class EntryCursor : MatrixCursor(arrayOf( + ID, + COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS, + COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS + )) { + + private var entryId: Long = 0 + + fun addEntry(entry: Entry) { + addRow(arrayOf( + entryId, + entry.nodeId.id.mostSignificantBits, + entry.nodeId.id.leastSignificantBits + )) + entryId++ + } + + fun getNodeId(): NodeId { + return NodeIdUUID( + UUID(getLong(getColumnIndex(COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS)), + getLong(getColumnIndex(COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS))) + ) + } + + companion object { + const val ID = BaseColumns._ID + const val COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS = "UUID_most_significant_bits" + const val COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS = "UUID_least_significant_bits" + } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.kt b/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.kt deleted file mode 100644 index 986802a02..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursor.kt +++ /dev/null @@ -1,89 +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.database.cursor - -import android.database.MatrixCursor -import android.provider.BaseColumns -import com.kunzisoft.keepass.database.element.DateInstant -import com.kunzisoft.keepass.database.element.entry.EntryVersioned -import com.kunzisoft.keepass.database.element.icon.IconImage -import com.kunzisoft.keepass.database.element.icon.IconImageCustom -import com.kunzisoft.keepass.database.element.icon.IconImageStandard -import com.kunzisoft.keepass.database.element.node.NodeId -import java.util.* - -abstract class EntryCursor> : MatrixCursor(arrayOf( - _ID, - COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS, - COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS, - COLUMN_INDEX_TITLE, - COLUMN_INDEX_ICON_STANDARD, - COLUMN_INDEX_ICON_CUSTOM_UUID_MOST_SIGNIFICANT_BITS, - COLUMN_INDEX_ICON_CUSTOM_UUID_LEAST_SIGNIFICANT_BITS, - COLUMN_INDEX_USERNAME, - COLUMN_INDEX_PASSWORD, - COLUMN_INDEX_URL, - COLUMN_INDEX_NOTES, - COLUMN_INDEX_EXPIRY_TIME, - COLUMN_INDEX_EXPIRES - )) { - - protected var entryId: Long = 0 - - abstract fun addEntry(entry: PwEntryV) - - abstract fun getPwNodeId(): NodeId - - open fun populateEntry(pwEntry: PwEntryV, - retrieveStandardIcon: (Int) -> IconImageStandard, - retrieveCustomIcon: (UUID) -> IconImageCustom) { - pwEntry.nodeId = getPwNodeId() - pwEntry.title = getString(getColumnIndex(COLUMN_INDEX_TITLE)) - - val iconStandard = retrieveStandardIcon.invoke(getInt(getColumnIndex(COLUMN_INDEX_ICON_STANDARD))) - val iconCustom = retrieveCustomIcon.invoke(UUID(getLong(getColumnIndex(COLUMN_INDEX_ICON_CUSTOM_UUID_MOST_SIGNIFICANT_BITS)), - getLong(getColumnIndex(COLUMN_INDEX_ICON_CUSTOM_UUID_LEAST_SIGNIFICANT_BITS)))) - pwEntry.icon = IconImage(iconStandard, iconCustom) - - pwEntry.username = getString(getColumnIndex(COLUMN_INDEX_USERNAME)) - pwEntry.password = getString(getColumnIndex(COLUMN_INDEX_PASSWORD)) - pwEntry.url = getString(getColumnIndex(COLUMN_INDEX_URL)) - pwEntry.notes = getString(getColumnIndex(COLUMN_INDEX_NOTES)) - pwEntry.expiryTime = DateInstant(getString(getColumnIndex(COLUMN_INDEX_EXPIRY_TIME))) - pwEntry.expires = getString(getColumnIndex(COLUMN_INDEX_EXPIRES)) - .lowercase(Locale.ENGLISH) != "false" - } - - companion object { - const val _ID = BaseColumns._ID - const val COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS = "UUID_most_significant_bits" - const val COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS = "UUID_least_significant_bits" - const val COLUMN_INDEX_TITLE = "title" - const val COLUMN_INDEX_ICON_STANDARD = "icon_standard" - const val COLUMN_INDEX_ICON_CUSTOM_UUID_MOST_SIGNIFICANT_BITS = "icon_custom_UUID_most_significant_bits" - const val COLUMN_INDEX_ICON_CUSTOM_UUID_LEAST_SIGNIFICANT_BITS = "icon_custom_UUID_least_significant_bits" - const val COLUMN_INDEX_USERNAME = "username" - const val COLUMN_INDEX_PASSWORD = "password" - const val COLUMN_INDEX_URL = "URL" - const val COLUMN_INDEX_NOTES = "notes" - const val COLUMN_INDEX_EXPIRY_TIME = "expiry_time" - const val COLUMN_INDEX_EXPIRES = "expires" - } -} diff --git a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDB.kt deleted file mode 100644 index 6debdd427..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDB.kt +++ /dev/null @@ -1,45 +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.database.cursor - -import com.kunzisoft.keepass.database.element.entry.EntryKDB - -class EntryCursorKDB : EntryCursorUUID() { - - override fun addEntry(entry: EntryKDB) { - addRow(arrayOf( - entryId, - entry.id.mostSignificantBits, - entry.id.leastSignificantBits, - entry.title, - entry.icon.standard.id, - entry.icon.custom.uuid.mostSignificantBits, - entry.icon.custom.uuid.leastSignificantBits, - entry.username, - entry.password, - entry.url, - entry.notes, - entry.expiryTime, - entry.expires - )) - entryId++ - } - -} diff --git a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDBX.kt deleted file mode 100644 index 21159e5c5..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorKDBX.kt +++ /dev/null @@ -1,73 +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.database.cursor - -import com.kunzisoft.keepass.database.element.entry.EntryKDBX -import com.kunzisoft.keepass.database.element.icon.IconImageCustom -import com.kunzisoft.keepass.database.element.icon.IconImageStandard -import java.util.* - -class EntryCursorKDBX : EntryCursorUUID() { - - private val extraFieldCursor: ExtraFieldCursor = ExtraFieldCursor() - - override fun addEntry(entry: EntryKDBX) { - addRow(arrayOf( - entryId, - entry.id.mostSignificantBits, - entry.id.leastSignificantBits, - entry.title, - entry.icon.standard.id, - entry.icon.custom.uuid.mostSignificantBits, - entry.icon.custom.uuid.leastSignificantBits, - entry.username, - entry.password, - entry.url, - entry.notes, - entry.expiryTime, - entry.expires - )) - - entry.doForEachDecodedCustomField { field -> - extraFieldCursor.addExtraField(entryId, field) - } - - entryId++ - } - - override fun populateEntry(pwEntry: EntryKDBX, - retrieveStandardIcon: (Int) -> IconImageStandard, - retrieveCustomIcon: (UUID) -> IconImageCustom) { - super.populateEntry(pwEntry, retrieveStandardIcon, retrieveCustomIcon) - - // Retrieve extra fields - if (extraFieldCursor.moveToFirst()) { - while (!extraFieldCursor.isAfterLast) { - // Add a new extra field only if entryId is the one we want - if (extraFieldCursor.getLong(extraFieldCursor - .getColumnIndex(ExtraFieldCursor.FOREIGN_KEY_ENTRY_ID)) - == getLong(getColumnIndex(_ID))) { - extraFieldCursor.populateExtraFieldInEntry(pwEntry) - } - extraFieldCursor.moveToNext() - } - } - } -} diff --git a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorUUID.kt b/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorUUID.kt deleted file mode 100644 index 778bc506d..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/database/cursor/EntryCursorUUID.kt +++ /dev/null @@ -1,34 +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.database.cursor - -import com.kunzisoft.keepass.database.element.entry.EntryVersioned -import com.kunzisoft.keepass.database.element.node.NodeId -import com.kunzisoft.keepass.database.element.node.NodeIdUUID -import java.util.* - -abstract class EntryCursorUUID>: EntryCursor() { - - override fun getPwNodeId(): NodeId { - return NodeIdUUID( - UUID(getLong(getColumnIndex(COLUMN_INDEX_UUID_MOST_SIGNIFICANT_BITS)), - getLong(getColumnIndex(COLUMN_INDEX_UUID_LEAST_SIGNIFICANT_BITS)))) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.kt b/app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.kt deleted file mode 100644 index 60241e473..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/database/cursor/ExtraFieldCursor.kt +++ /dev/null @@ -1,62 +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.database.cursor - -import android.database.MatrixCursor -import android.provider.BaseColumns -import com.kunzisoft.keepass.database.element.Field - -import com.kunzisoft.keepass.database.element.entry.EntryKDBX -import com.kunzisoft.keepass.database.element.security.ProtectedString - -class ExtraFieldCursor : MatrixCursor(arrayOf( - _ID, - FOREIGN_KEY_ENTRY_ID, - COLUMN_LABEL, - COLUMN_PROTECTION, - COLUMN_VALUE - )) { - - private var fieldId: Long = 0 - - @Synchronized - fun addExtraField(entryId: Long, field: Field) { - addRow(arrayOf(fieldId, - entryId, - field.name, - if (field.protectedValue.isProtected) 1 else 0, - field.protectedValue.toString())) - fieldId++ - } - - fun populateExtraFieldInEntry(pwEntry: EntryKDBX) { - pwEntry.putField(getString(getColumnIndex(COLUMN_LABEL)), - ProtectedString(getInt(getColumnIndex(COLUMN_PROTECTION)) > 0, - getString(getColumnIndex(COLUMN_VALUE)))) - } - - companion object { - const val _ID = BaseColumns._ID - const val FOREIGN_KEY_ENTRY_ID = "entry_id" - const val COLUMN_LABEL = "label" - const val COLUMN_PROTECTION = "protection" - const val COLUMN_VALUE = "value" - } -} diff --git a/app/src/main/res/layout/item_search_entry.xml b/app/src/main/res/layout/item_search_entry.xml index 9ce768591..6918589d2 100644 --- a/app/src/main/res/layout/item_search_entry.xml +++ b/app/src/main/res/layout/item_search_entry.xml @@ -18,6 +18,7 @@ along with KeePassDX. If not, see . --> + android:background="?attr/colorPrimary"> + \ No newline at end of file