First commit for new search

This commit is contained in:
J-Jamet committed 2022-01-30 18:37:06 +01:00
1 parent ba0e87f32a
commit 5cb42264c5
49 files changed
+228 -427

No files matched your search

@@ -22,7 +22,6 @@ import android.app.Activity
import android.app.DatePickerDialog
import android.app.SearchManager
import android.app.TimePickerDialog
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.PorterDuff
@@ -41,7 +40,6 @@ import androidx.appcompat.view.ActionMode
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.*
@@ -50,7 +48,6 @@ import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.helpers.SpecialMode
import com.kunzisoft.keepass.activities.legacy.DatabaseLockActivity
import com.kunzisoft.keepass.adapters.BreadcrumbAdapter
import com.kunzisoft.keepass.adapters.SearchEntryCursorAdapter
import com.kunzisoft.keepass.autofill.AutofillComponent
import com.kunzisoft.keepass.autofill.AutofillHelper
import com.kunzisoft.keepass.database.element.*
@@ -94,9 +91,8 @@ class GroupActivity : DatabaseLockActivity(),
private var databaseNameView: TextView? = null
private var searchContainer: ViewGroup? = null
private var searchNumbers: TextView? = null
private var searchString: TextView? = null
private var searchView: SearchView? = null
private var toolbarBreadcrumb: Toolbar? = null
private var searchTitleView: View? = null
private var toolbarAction: ToolbarAction? = null
private var numberChildrenView: TextView? = null
private var addNodeButtonView: AddNodeButtonView? = null
@@ -119,11 +115,34 @@ class GroupActivity : DatabaseLockActivity(),
private var mCurrentGroupState: GroupState? = null
private var mRootGroup: Group? = null
private var mCurrentGroup: Group? = null
private var mSearchGroup: Group? = null
private var mPreviousGroupsIds = mutableListOf<GroupState>()
private var mOldGroupToUpdate: Group? = null
private var mSearchSuggestionAdapter: SearchEntryCursorAdapter? = null
private var mOnSuggestionListener: SearchView.OnSuggestionListener? = null
private val mOnSearchActionExpandListener = object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
searchContainer?.visibility = View.VISIBLE
return true
}
override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
searchContainer?.visibility = View.GONE
mSearchGroup = null
loadGroup(mDatabase)
return true
}
}
private val mOnSearchQueryTextListener = object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
loadSearchGroup(query)
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
loadSearchGroup(newText)
return true
}
}
private var mIconSelectionActivityResultLauncher = IconPickerActivity.registerIconSelectionForResult(this) { icon ->
// To create tree dialog for icon
@@ -152,9 +171,7 @@ class GroupActivity : DatabaseLockActivity(),
databaseNameView = findViewById(R.id.database_name)
searchContainer = findViewById(R.id.search_container)
searchNumbers = findViewById(R.id.search_numbers)
searchString = findViewById(R.id.search_string)
toolbarBreadcrumb = findViewById(R.id.toolbar_breadcrumb)
searchTitleView = findViewById(R.id.search_title)
breadcrumbListView = findViewById(R.id.breadcrumb_list)
toolbarAction = findViewById(R.id.toolbar_action)
lockView = findViewById(R.id.lock_button)
@@ -247,11 +264,11 @@ class GroupActivity : DatabaseLockActivity(),
// Observe group
mGroupViewModel.group.observe(this) {
val currentGroup = it.group
mCurrentGroup = currentGroup
mRecyclingBinIsCurrentGroup = it.isRecycleBin
if (!currentGroup.isVirtual) {
mCurrentGroup = currentGroup
mRecyclingBinIsCurrentGroup = it.isRecycleBin
// Save group id if real group
mCurrentGroupState = GroupState(currentGroup.nodeId, it.showFromPosition)
@@ -325,6 +342,8 @@ class GroupActivity : DatabaseLockActivity(),
)
}
}
} else {
mSearchGroup = currentGroup
}
loadingView?.hideByFading()
@@ -424,22 +443,7 @@ class GroupActivity : DatabaseLockActivity(),
} else {
databaseColorView?.visibility = View.GONE
}
mSearchSuggestionAdapter = SearchEntryCursorAdapter(this, it)
mBreadcrumbAdapter?.iconDrawableFactory = it.iconDrawableFactory
mOnSuggestionListener = object : SearchView.OnSuggestionListener {
override fun onSuggestionClick(position: Int): Boolean {
mSearchSuggestionAdapter?.let { searchAdapter ->
searchAdapter.getEntryFromPosition(position)?.let { entry ->
onNodeClick(database, entry)
}
}
return true
}
override fun onSuggestionSelect(position: Int): Boolean {
return true
}
}
}
invalidateOptionsMenu()
@@ -566,36 +570,27 @@ class GroupActivity : DatabaseLockActivity(),
}
override fun onGroupRefreshed() {
mCurrentGroup?.let { currentGroup ->
assignGroupViewElements(currentGroup)
if (mSearchGroup != null) {
assignGroupViewElements(mSearchGroup)
} else {
assignGroupViewElements(mCurrentGroup)
}
}
private fun assignGroupViewElements(group: Group?) {
// TODO in real time
// Assign title
if (group?.isVirtual == true) {
searchContainer?.visibility = View.VISIBLE
val title = group.title
searchString?.text = if (title.isNotEmpty()) title else ""
searchNumbers?.text = group.numberOfChildEntries.toString()
databaseNameContainer?.visibility = View.GONE
toolbarBreadcrumb?.navigationIcon = null
toolbarBreadcrumb?.collapse()
} else {
searchContainer?.visibility = View.GONE
databaseNameContainer?.visibility = View.VISIBLE
// Refresh breadcrumb
if (toolbarBreadcrumb?.isVisible != true) {
toolbarBreadcrumb?.expand {
setBreadcrumbNode(group)
}
} else {
// Add breadcrumb
setBreadcrumbNode(group)
}
// Add breadcrumb
setBreadcrumbNode(group)
invalidateOptionsMenu()
}
initAddButton(group)
invalidateOptionsMenu()
}
private fun setBreadcrumbNode(group: Group?) {
@@ -950,8 +945,6 @@ class GroupActivity : DatabaseLockActivity(),
} else {
View.GONE
}
// Refresh suggestions to change preferences
mSearchSuggestionAdapter?.reInit(this)
// Padding if lock button visible
toolbarAction?.updateLockPaddingLeft()
}
@@ -987,19 +980,21 @@ class GroupActivity : DatabaseLockActivity(),
}
// Get the SearchView and set the searchable configuration
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager?
menu.findItem(R.id.menu_search)?.let {
val searchView = it.actionView as SearchView?
it.setOnActionExpandListener(mOnSearchActionExpandListener)
searchView = it.actionView as SearchView?
searchView?.apply {
/*
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager?
(searchManager?.getSearchableInfo(
ComponentName(this@GroupActivity, GroupActivity::class.java)
))?.let { searchableInfo ->
setSearchableInfo(searchableInfo)
}
setIconifiedByDefault(false) // Do not iconify the widget; expand it by default
suggestionsAdapter = mSearchSuggestionAdapter
setOnSuggestionListener(mOnSuggestionListener)
*/
isIconified = false
setOnQueryTextListener(mOnSearchQueryTextListener)
}
// Expand the search view if defined in settings
if (mRequestStartupSearch
@@ -1024,6 +1019,16 @@ class GroupActivity : DatabaseLockActivity(),
return true
}
private fun loadSearchGroup(query: String?) {
if (query != null && query.isNotEmpty()) {
mGroupViewModel.loadGroupFromSearch(
mDatabase,
query,
PreferencesUtil.omitBackup(this)
)
}
}
private fun performedNextEducation(
groupActivityEducation: GroupActivityEducation,
menu: Menu
@@ -1092,7 +1097,6 @@ class GroupActivity : DatabaseLockActivity(),
return true
}
R.id.menu_search -> {
//onSearchRequested()
return true
}
R.id.menu_save_database -> {
@@ -1,189 +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 <http://www.gnu.org/licenses/>.
*
*/
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.element.Database
import com.kunzisoft.keepass.database.element.Entry
import com.kunzisoft.keepass.database.element.Group
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)
: CursorAdapter(context, null, FLAG_REGISTER_CONTENT_OBSERVER) {
private val cursorInflater: LayoutInflater? = context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
private var mDisplayUsername: Boolean = false
private var mOmitBackup: Boolean = true
private val iconColor: Int
init {
// Get the icon color
val taTextColor = context.theme.obtainStyledAttributes(intArrayOf(R.attr.textColorInverse))
this.iconColor = taTextColor.getColor(0, Color.WHITE)
taTextColor.recycle()
reInit(context)
}
fun reInit(context: Context) {
this.mDisplayUsername = PreferencesUtil.showUsernamesListEntries(context)
this.mOmitBackup = PreferencesUtil.omitBackup(context)
}
override fun newView(context: Context, cursor: Cursor, parent: ViewGroup): View {
val view = cursorInflater!!.inflate(R.layout.item_search_entry, parent, false)
val viewHolder = ViewHolder()
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
}
override fun bindView(view: View, context: Context, cursor: Cursor?) {
getEntryFrom(cursor)?.let { currentEntry ->
val viewHolder = view.tag as ViewHolder
// Assign image
viewHolder.imageViewIcon?.let { iconView ->
database.iconDrawableFactory.assignDatabaseIcon(iconView, currentEntry.icon, iconColor)
}
// Assign title
viewHolder.textViewTitle?.apply {
text = currentEntry.getVisualTitle()
strikeOut(currentEntry.isCurrentlyExpires)
}
// Assign subtitle
viewHolder.textViewSubTitle?.apply {
val entryUsername = currentEntry.username
text = if (mDisplayUsername && entryUsername.isNotEmpty()) {
String.format("(%s)", entryUsername)
} else {
""
}
visibility = if (text.isEmpty()) View.GONE else View.VISIBLE
strikeOut(currentEntry.isCurrentlyExpires)
}
viewHolder.textViewPath?.apply {
text = currentEntry.getPathString()
}
}
}
private fun getEntryFrom(cursor: Cursor?): Entry? {
val entryCursor = cursor as? EntryCursor?
entryCursor?.getNodeId()?.let {
return database.getEntryById(it)
}
return null
}
override fun runQueryOnBackgroundThread(constraint: CharSequence): Cursor? {
return searchEntries(context, constraint.toString())
}
private fun searchEntries(context: Context, query: String): Cursor {
val cursor = EntryCursor()
val searchGroup = database.createVirtualGroupFromSearch(query,
mOmitBackup,
SearchHelper.MAX_SEARCH_ENTRY)
if (searchGroup != null) {
// Search in hide entries but not meta-stream
for (entry in searchGroup.getFilteredChildEntries(Group.ChildFilter.getDefaults(context))) {
database.startManageEntry(entry)
cursor.addEntry(entry)
database.stopManageEntry(entry)
}
}
return cursor
}
fun getEntryFromPosition(position: Int): Entry? {
var pwEntry: Entry? = null
val cursor = this.cursor
if (cursor.moveToFirst() && cursor.move(position)) {
pwEntry = getEntryFrom(cursor)
}
return pwEntry
}
private class ViewHolder {
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<UUID> {
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"
}
}
}
@@ -89,7 +89,7 @@ class SearchHelper {
}
companion object {
const val MAX_SEARCH_ENTRY = 10
const val MAX_SEARCH_ENTRY = 100
/**
* Utility method to perform actions if item is found or not after an auto search in [database]
@@ -26,7 +26,7 @@ import com.kunzisoft.keepass.activities.GroupActivity
import com.kunzisoft.keepass.app.database.IOActionTask
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.Group
import com.kunzisoft.keepass.database.element.node.Node
import com.kunzisoft.keepass.database.search.SearchHelper
class GroupViewModel: ViewModel() {
@@ -75,7 +75,11 @@ class GroupViewModel: ViewModel() {
omitBackup: Boolean) {
IOActionTask(
{
database?.createVirtualGroupFromSearch(searchQuery, omitBackup)
database?.createVirtualGroupFromSearch(
searchQuery,
omitBackup,
SearchHelper.MAX_SEARCH_ENTRY
)
},
{ group ->
if (group != null) {