From d8961d2acb60962e1fee691477467014e3bb8672 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Tue, 13 Oct 2020 14:20:12 +0200 Subject: [PATCH] Save search info settings --- .../keepass/activities/EntryEditActivity.kt | 24 +---- .../keepass/activities/GroupActivity.kt | 88 +++++++++++++++---- .../com/kunzisoft/keepass/model/EntryInfo.kt | 32 ++++++- .../keepass/settings/PreferencesUtil.kt | 14 +++ app/src/main/res/values/donottranslate.xml | 6 +- app/src/main/res/values/strings.xml | 4 + app/src/main/res/xml/preferences_autofill.xml | 5 ++ app/src/main/res/xml/preferences_keyboard.xml | 6 ++ 8 files changed, 139 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index b063e0dde..674604829 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -183,29 +183,7 @@ class EntryEditActivity : LockingActivity(), tempEntryInfo?.password = it } registerInfo?.searchInfo?.let { searchInfo -> - searchInfo.webDomain?.let { webDomain -> - // If unable to save web domain in custom field or URL not populate, save in URL - if (mDatabase?.allowEntryCustomFields() != true) { - //|| tempEntryInfo?.url?.isEmpty() == true) { - val scheme = "http" - // TODO Retrieve scheme - tempEntryInfo?.url = "$scheme://$webDomain" - } else { - // Save web domain in custom field - tempEntryInfo?.addUniqueField(Field(EntryInfo.WEB_DOMAIN_FIELD_NAME, - ProtectedString(false, webDomain)) - ) - } - } ?: run { - // Save application id in custom field - if (mDatabase?.allowEntryCustomFields() == true) { - searchInfo.applicationId?.let { applicationId -> - tempEntryInfo?.addUniqueField(Field(EntryInfo.APPLICATION_ID_FIELD_NAME, - ProtectedString(false, applicationId)) - ) - } - } - } + tempEntryInfo?.saveSearchInfo(mDatabase, searchInfo) } // Build fragment to manage entry modification diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt index 8311f146a..17721f8c6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt @@ -72,6 +72,7 @@ import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Compa import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_GROUP_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_DELETE_NODES_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_MOVE_NODES_TASK +import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_ENTRY_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_GROUP_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.NEW_NODES_KEY import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.OLD_NODES_KEY @@ -252,6 +253,35 @@ class GroupActivity : LockingActivity(), refreshSearchGroup() when (actionTask) { + ACTION_DATABASE_UPDATE_ENTRY_TASK -> { + if (result.isSuccess) { + mListNodesFragment?.updateNodes(oldNodes, newNodes) + EntrySelectionHelper.doSpecialAction(intent, + { + // Not use + }, + { + try { + val entry = newNodes[0] as Entry + entrySelectedForKeyboardSelection(entry) + } catch (e: Exception) { + Log.e(TAG, "Unable to perform action for keyboard selection after entry update", e) + } + }, + { _, _ -> + try { + val entry = newNodes[0] as Entry + entrySelectedForAutofillSelection(entry) + } catch (e: Exception) { + Log.e(TAG, "Unable to perform action for autofill selection after entry update", e) + } + }, + { + // Not use + } + ) + } + } ACTION_DATABASE_UPDATE_GROUP_TASK -> { if (result.isSuccess) { mListNodesFragment?.updateNodes(oldNodes, newNodes) @@ -533,24 +563,19 @@ class GroupActivity : LockingActivity(), { EntryActivity.launch(this@GroupActivity, entryVersioned, mReadOnly) }, - { - rebuildListNodes() - // Populate Magikeyboard with entry - mDatabase?.let { database -> - populateKeyboardAndMoveAppToBackground(this@GroupActivity, - entryVersioned.getEntryInfo(database), - intent) + { searchInfo -> + if (PreferencesUtil.isKeyboardSaveSearchInfoEnable(this@GroupActivity)) { + updateEntryWithSearchInfo(entryVersioned, searchInfo) + } else { + entrySelectedForKeyboardSelection(entryVersioned) } }, - { _, _ -> - // Build response with the entry selected - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mDatabase != null) { - mDatabase?.let { database -> - AutofillHelper.buildResponse(this@GroupActivity, - entryVersioned.getEntryInfo(database)) - } + { searchInfo, _ -> + if (PreferencesUtil.isAutofillSaveSearchInfoEnable(this@GroupActivity)) { + updateEntryWithSearchInfo(entryVersioned, searchInfo) + } else { + entrySelectedForAutofillSelection(entryVersioned) } - finish() }, { searchInfo -> rebuildListNodes() @@ -566,6 +591,39 @@ class GroupActivity : LockingActivity(), } } + private fun entrySelectedForKeyboardSelection(entry: Entry) { + rebuildListNodes() + // Populate Magikeyboard with entry + mDatabase?.let { database -> + populateKeyboardAndMoveAppToBackground(this, + entry.getEntryInfo(database), + intent) + } + } + + private fun entrySelectedForAutofillSelection(entry: Entry) { + // Build response with the entry selected + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mDatabase != null) { + mDatabase?.let { database -> + AutofillHelper.buildResponse(this, + entry.getEntryInfo(database)) + } + } + finish() + } + + private fun updateEntryWithSearchInfo(entry: Entry, searchInfo: SearchInfo?) { + val newEntry = Entry(entry) + newEntry.setEntryInfo(mDatabase, newEntry.getEntryInfo(mDatabase).apply { + saveSearchInfo(mDatabase, searchInfo) + }) + mProgressDatabaseTaskProvider?.startDatabaseUpdateEntry( + entry, + newEntry, + !mReadOnly && mAutoSaveEnable + ) + } + private fun finishNodeAction() { actionNodeMode?.finish() } 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 c4092b88d..7eef8f0c5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt @@ -22,9 +22,11 @@ package com.kunzisoft.keepass.model import android.os.Parcel import android.os.Parcelable import com.kunzisoft.keepass.database.element.Attachment +import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.DateInstant import com.kunzisoft.keepass.database.element.icon.IconImage import com.kunzisoft.keepass.database.element.icon.IconImageStandard +import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.otp.OtpElement import com.kunzisoft.keepass.otp.OtpEntryFields.OTP_TOKEN_FIELD import kotlin.collections.ArrayList @@ -101,7 +103,7 @@ class EntryInfo : Parcelable { return customFields.lastOrNull { it.name == label }?.protectedValue?.toString() ?: "" } - fun addUniqueField(field: Field, number: Int = 0) { + private fun addUniqueField(field: Field, number: Int = 0) { var exists = false var sameData = false val suffix = if (number > 0) number.toString() else "" @@ -121,6 +123,34 @@ class EntryInfo : Parcelable { (customFields as ArrayList).add(Field(field.name + suffix, field.protectedValue)) } + fun saveSearchInfo(database: Database?, searchInfo: SearchInfo?) { + searchInfo?.let { mSearchInfo -> + mSearchInfo.webDomain?.let { webDomain -> + // If unable to save web domain in custom field or URL not populate, save in URL + if (database?.allowEntryCustomFields() != true) { + //|| tempEntryInfo?.url?.isEmpty() == true) { + val scheme = "http" + // TODO Retrieve scheme + url = "$scheme://$webDomain" + } else { + // Save web domain in custom field + addUniqueField(Field(WEB_DOMAIN_FIELD_NAME, + ProtectedString(false, webDomain)) + ) + } + } ?: run { + // Save application id in custom field + if (database?.allowEntryCustomFields() == true) { + mSearchInfo.applicationId?.let { applicationId -> + addUniqueField(Field(APPLICATION_ID_FIELD_NAME, + ProtectedString(false, applicationId)) + ) + } + } + } + } + } + companion object { const val WEB_DOMAIN_FIELD_NAME = "WebDomain" diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt index 472828799..fa9560571 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt @@ -345,6 +345,14 @@ object PreferencesUtil { context.resources.getBoolean(R.bool.keyboard_search_share_default)) } + fun isKeyboardSaveSearchInfoEnable(context: Context): Boolean { + if (!isKeyboardSearchShareEnable(context)) + return false + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + return prefs.getBoolean(context.getString(R.string.keyboard_save_search_info_key), + context.resources.getBoolean(R.bool.keyboard_save_search_info_default)) + } + fun isAutoGoActionEnable(context: Context): Boolean { val prefs = PreferenceManager.getDefaultSharedPreferences(context) return prefs.getBoolean(context.getString(R.string.keyboard_auto_go_action_key), @@ -381,6 +389,12 @@ object PreferencesUtil { context.resources.getBoolean(R.bool.autofill_auto_search_default)) } + fun isAutofillSaveSearchInfoEnable(context: Context): Boolean { + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + return prefs.getBoolean(context.getString(R.string.autofill_save_search_info_key), + context.resources.getBoolean(R.bool.autofill_save_search_info_default)) + } + fun askToSaveAutofillData(context: Context): Boolean { val prefs = PreferenceManager.getDefaultSharedPreferences(context) return prefs.getBoolean(context.getString(R.string.autofill_ask_to_save_data_key), diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index de4bf48d1..7824c08ec 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -120,9 +120,11 @@ keyboard_selection_entry_key false keyboard_notification_entry_key - keyboard_search_share_key true + keyboard_search_share_key false + keyboard_save_search_info_key + true keyboard_notification_entry_clear_close_key true keyboard_entry_timeout_key @@ -139,6 +141,8 @@ false autofill_auto_search_key true + autofill_save_search_info_key + true autofill_ask_to_save_data_key true autofill_application_id_blocklist_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6ce9ea286..fa6dc97e6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -381,6 +381,8 @@ Show a notification when an entry is available Search shared info Automatically search for shared information to populate the keyboard + Save shared info + Try to save shared information when making a manual entry selection Clear at closing Close the database when closing the notification Timeout @@ -402,6 +404,8 @@ Automatically switch back to the previous keyboard after executing "Auto key action" Auto search Automatically suggest search results from the web domain or application ID + Save search info + Try to save search information when making a manual entry selection Ask to save data Ask to save data when a form is validated Application blocklist diff --git a/app/src/main/res/xml/preferences_autofill.xml b/app/src/main/res/xml/preferences_autofill.xml index 1e4f032ff..5addda462 100644 --- a/app/src/main/res/xml/preferences_autofill.xml +++ b/app/src/main/res/xml/preferences_autofill.xml @@ -25,6 +25,11 @@ android:title="@string/autofill_auto_search_title" android:summary="@string/autofill_auto_search_summary" android:defaultValue="@bool/autofill_auto_search_default"/> + diff --git a/app/src/main/res/xml/preferences_keyboard.xml b/app/src/main/res/xml/preferences_keyboard.xml index 0150ba583..d18913d4a 100644 --- a/app/src/main/res/xml/preferences_keyboard.xml +++ b/app/src/main/res/xml/preferences_keyboard.xml @@ -35,6 +35,12 @@ android:title="@string/keyboard_search_share_title" android:summary="@string/keyboard_search_share_summary" android:defaultValue="@bool/keyboard_search_share_default"/> +