From c62064002fe417dda1e9dd115631ac58ea2cdd37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20D=C3=BCrholz?= Date: Tue, 27 Jul 2021 17:36:56 +0200 Subject: [PATCH 1/5] Fix small issue with credit card autofill --- .../com/kunzisoft/keepass/autofill/AutofillHelper.kt | 12 +++++++----- .../kunzisoft/keepass/autofill/StructureParser.kt | 12 ++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) 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 665e81686..41561b933 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt @@ -126,12 +126,14 @@ object AutofillHelper { if (entryInfo.expires) { val year = entryInfo.expiryTime.getYearInt() val month = entryInfo.expiryTime.getMonthInt() + val monthString = month.toString().padStart(2, '0') val day = entryInfo.expiryTime.getDay() + val dayString = day.toString().padStart(2, '0') struct.creditCardExpirationDateId?.let { if (struct.isWebView) { // set date string as defined in https://html.spec.whatwg.org - builder.setValue(it, AutofillValue.forText("$year\u002D$month")) + builder.setValue(it, AutofillValue.forText("$year\u002D$monthString")) } else { builder.setValue(it, AutofillValue.forDate(entryInfo.expiryTime.date.time)) } @@ -157,24 +159,24 @@ object AutofillHelper { } struct.creditCardExpirationMonthId?.let { if (struct.isWebView) { - builder.setValue(it, AutofillValue.forText(month.toString())) + builder.setValue(it, AutofillValue.forText(monthString)) } else { if (struct.creditCardExpirationMonthOptions != null) { // index starts at 0 builder.setValue(it, AutofillValue.forList(month - 1)) } else { - builder.setValue(it, AutofillValue.forText(month.toString())) + builder.setValue(it, AutofillValue.forText(monthString)) } } } struct.creditCardExpirationDayId?.let { if (struct.isWebView) { - builder.setValue(it, AutofillValue.forText(day.toString())) + builder.setValue(it, AutofillValue.forText(dayString)) } else { if (struct.creditCardExpirationDayOptions != null) { builder.setValue(it, AutofillValue.forList(day - 1)) } else { - builder.setValue(it, AutofillValue.forText(day.toString())) + builder.setValue(it, AutofillValue.forText(dayString)) } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt b/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt index 2110f2e8f..f10d916e0 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt @@ -143,19 +143,19 @@ class StructureParser(private val structure: AssistStructure) { Log.d(TAG, "Autofill password hint") return true } - it.contains("cc-name", true) -> { + it.equals("cc-name", true) -> { Log.d(TAG, "Autofill credit card name hint") result?.creditCardHolderId = autofillId result?.creditCardHolder = node.autofillValue?.textValue?.toString() } it.contains(View.AUTOFILL_HINT_CREDIT_CARD_NUMBER, true) - || it.contains("cc-number", true) -> { + || it.equals("cc-number", true) -> { Log.d(TAG, "Autofill credit card number hint") result?.creditCardNumberId = autofillId result?.creditCardNumber = node.autofillValue?.textValue?.toString() } // expect date string as defined in https://html.spec.whatwg.org, e.g. 2014-12 - it.contains("cc-exp", true) -> { + it.equals("cc-exp", true) -> { Log.d(TAG, "Autofill credit card expiration date hint") result?.creditCardExpirationDateId = autofillId node.autofillValue?.let { value -> @@ -182,7 +182,7 @@ class StructureParser(private val structure: AssistStructure) { } } it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR, true) - || it.contains("cc-exp-year", true) -> { + || it.equals("cc-exp-year", true) -> { Log.d(TAG, "Autofill credit card expiration year hint") result?.creditCardExpirationYearId = autofillId if (node.autofillOptions != null) { @@ -204,7 +204,7 @@ class StructureParser(private val structure: AssistStructure) { } } it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH, true) - || it.contains("cc-exp-month", true) -> { + || it.equals("cc-exp-month", true) -> { Log.d(TAG, "Autofill credit card expiration month hint") result?.creditCardExpirationMonthId = autofillId if (node.autofillOptions != null) { @@ -227,7 +227,7 @@ class StructureParser(private val structure: AssistStructure) { } } it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY, true) - || it.contains("cc-exp-day", true) -> { + || it.equals("cc-exp-day", true) -> { Log.d(TAG, "Autofill credit card expiration day hint") result?.creditCardExpirationDayId = autofillId if (node.autofillOptions != null) { From 39b817bc69c98d12b59059a972892e38cdf9210d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20D=C3=BCrholz?= Date: Fri, 27 Aug 2021 18:23:32 +0200 Subject: [PATCH 2/5] Let user select entry for autofill --- .../activities/AutofillLauncherActivity.kt | 3 ++ .../keepass/autofill/AutofillHelper.kt | 21 ++++++++++++ .../keepass/autofill/StructureParser.kt | 34 ++++++++++++++++++- .../keepass/database/search/SearchHelper.kt | 2 +- .../com/kunzisoft/keepass/model/SearchInfo.kt | 2 +- .../keepass/settings/PreferencesUtil.kt | 7 ++++ app/src/main/res/values-de/strings.xml | 3 ++ app/src/main/res/values/donottranslate.xml | 2 ++ app/src/main/res/values/strings.xml | 3 ++ app/src/main/res/xml/preferences_autofill.xml | 5 +++ 10 files changed, 79 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt index b0bbe0982..e3b56384e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt @@ -65,6 +65,7 @@ class AutofillLauncherActivity : DatabaseModeActivity() { applicationId = intent.getStringExtra(KEY_SEARCH_APPLICATION_ID) webDomain = intent.getStringExtra(KEY_SEARCH_DOMAIN) webScheme = intent.getStringExtra(KEY_SEARCH_SCHEME) + manualSelection = intent.getBooleanExtra(KEY_MANUAL_SELECTION, false) } SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain -> searchInfo.webDomain = concreteWebDomain @@ -198,6 +199,7 @@ class AutofillLauncherActivity : DatabaseModeActivity() { companion object { + private const val KEY_MANUAL_SELECTION = "KEY_MANUAL_SELECTION" private const val KEY_SEARCH_APPLICATION_ID = "KEY_SEARCH_APPLICATION_ID" private const val KEY_SEARCH_DOMAIN = "KEY_SEARCH_DOMAIN" private const val KEY_SEARCH_SCHEME = "KEY_SEARCH_SCHEME" @@ -214,6 +216,7 @@ class AutofillLauncherActivity : DatabaseModeActivity() { putExtra(KEY_SEARCH_APPLICATION_ID, it.applicationId) putExtra(KEY_SEARCH_DOMAIN, it.webDomain) putExtra(KEY_SEARCH_SCHEME, it.webScheme) + putExtra(KEY_MANUAL_SELECTION, it.manualSelection) } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { inlineSuggestionsRequest?.let { 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 41561b933..bf5a0ffac 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt @@ -42,6 +42,7 @@ import androidx.autofill.inline.UiVersions import androidx.autofill.inline.v1.InlineSuggestionUi import androidx.core.content.ContextCompat import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.activities.AutofillLauncherActivity import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper import com.kunzisoft.keepass.activities.helpers.SpecialMode import com.kunzisoft.keepass.database.element.Database @@ -309,6 +310,26 @@ object AutofillHelper { } } + if (PreferencesUtil.isAutofillManualSelectionEnable(context)) { + val searchInfo = SearchInfo().apply { + applicationId = parseResult.applicationId + webDomain = parseResult.webDomain + webScheme = parseResult.webScheme + manualSelection = true + } + + val manualSelectionView = newRemoteViews(context, database, context.getString(R.string.autofill_manual_selection_prompt), null) + val intentSender = AutofillLauncherActivity.getAuthIntentSenderForSelection(context, + searchInfo, null) + val builder = Dataset.Builder(manualSelectionView) + // enable manual selection only for the form field that has focus + parseResult.focusedId?.let { autofillId -> + builder.setValue(autofillId, AutofillValue.forText("dummy")) + builder.setAuthentication(intentSender) + responseBuilder.addDataset(builder.build()) + } + } + return try { responseBuilder.build() } catch (e: Exception) { diff --git a/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt b/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt index f10d916e0..fd52a2e4c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt @@ -44,6 +44,7 @@ class StructureParser(private val structure: AssistStructure) { fun parse(saveValue: Boolean = false): Result? { try { result = Result() + result?.focusedId = getFocusedID() result?.apply { allowSaveValues = saveValue usernameIdCandidate = null @@ -74,6 +75,35 @@ class StructureParser(private val structure: AssistStructure) { } } + /* + * Note: SDK >= Q provides this method... + * fillContext.getFocusedId() + */ + private fun getFocusedID(): AutofillId? { + for (i in 0 until structure.windowNodeCount) { + val windowNode = structure.getWindowNodeAt(i) + val autofillId = traverse(windowNode.rootViewNode) + if (autofillId != null) { + return autofillId + } + } + return null + } + + private fun traverse(node: AssistStructure.ViewNode): AutofillId? { + if (node.visibility == View.VISIBLE && node.autofillHints != null) { + return node.autofillId + } else { + for (i in 0 until node.childCount) { + val autoFillId = traverse(node.getChildAt(i)) + if (autoFillId != null) { + return autoFillId + } + } + } + return null + } + private fun parseViewNode(node: AssistStructure.ViewNode): Boolean { // remember this if (node.className == "android.webkit.WebView") { @@ -399,7 +429,6 @@ class StructureParser(private val structure: AssistStructure) { class Result { var isWebView: Boolean = false var applicationId: String? = null - var webDomain: String? = null set(value) { if (field == null) @@ -418,6 +447,9 @@ class StructureParser(private val structure: AssistStructure) { var creditCardExpirationMonthOptions: Array? = null var creditCardExpirationDayOptions: Array? = null + // the AutofillId of the view that triggered autofill. + var focusedId: AutofillId? = null + var usernameId: AutofillId? = null set(value) { if (field == null) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/search/SearchHelper.kt b/app/src/main/java/com/kunzisoft/keepass/database/search/SearchHelper.kt index 2a004e26c..6553523bc 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/search/SearchHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/search/SearchHelper.kt @@ -106,7 +106,7 @@ class SearchHelper { } else if (TimeoutHelper.checkTime(context)) { var searchWithoutUI = false if (PreferencesUtil.isAutofillAutoSearchEnable(context) - && searchInfo != null + && searchInfo != null && !searchInfo.manualSelection && !searchInfo.containsOnlyNullValues()) { // If search provide results database.createVirtualGroupFromSearchInfo( diff --git a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt index ac1f37b3a..7aca06bc9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt @@ -14,7 +14,7 @@ import kotlinx.coroutines.launch import mozilla.components.lib.publicsuffixlist.PublicSuffixList class SearchInfo : ObjectNameResource, Parcelable { - + var manualSelection: Boolean = false var applicationId: String? = null set(value) { field = when { 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 ede9728fd..5661d1dbd 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt @@ -487,6 +487,12 @@ object PreferencesUtil { context.resources.getBoolean(R.bool.autofill_inline_suggestions_default)) } + fun isAutofillManualSelectionEnable(context: Context): Boolean { + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + return prefs.getBoolean(context.getString(R.string.autofill_manual_selection_key), + context.resources.getBoolean(R.bool.autofill_manual_selection_default)) + } + fun isAutofillSaveSearchInfoEnable(context: Context): Boolean { val prefs = PreferenceManager.getDefaultSharedPreferences(context) return prefs.getBoolean(context.getString(R.string.autofill_save_search_info_key), @@ -624,6 +630,7 @@ object PreferencesUtil { context.getString(R.string.autofill_close_database_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.autofill_auto_search_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.autofill_inline_suggestions_key) -> editor.putBoolean(name, value.toBoolean()) + context.getString(R.string.autofill_manual_selection_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.autofill_save_search_info_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.autofill_ask_to_save_data_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.autofill_application_id_blocklist_key) -> editor.putStringSet(name, getStringSetFromProperties(value)) diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index acfa6f220..837d55c9e 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -448,6 +448,9 @@ Validieren Suchergebnisse automatisch nach Web-Domain oder Anwendungs-ID vorschlagen Automatische Suche + Eintrag auswählen... + Manuelle Auswahl + Manuelle Auswahl des Datenbank-Eintrags ermöglichen Zeigt die Sperrtaste in der Benutzeroberfläche an Sperrtaste anzeigen Einstellungen für automatisches Ausfüllen diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 059cb9b49..57d3c64f9 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -159,6 +159,8 @@ true autofill_inline_suggestions_key false + autofill_manual_selection_key + false autofill_save_search_info_key true autofill_ask_to_save_data_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b68ec92a..1365d4554 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -487,6 +487,9 @@ Automatically suggest search results from the web domain or application ID Inline suggestions Attempt to display autofill suggestions directly from a compatible keyboard + Choose entry... + Manual selection + Display option to let the user select database entry Save search info Try to save search information when making a manual entry selection Ask to save data diff --git a/app/src/main/res/xml/preferences_autofill.xml b/app/src/main/res/xml/preferences_autofill.xml index 5ab891b4c..27523bc6c 100644 --- a/app/src/main/res/xml/preferences_autofill.xml +++ b/app/src/main/res/xml/preferences_autofill.xml @@ -35,6 +35,11 @@ android:title="@string/autofill_inline_suggestions_title" android:summary="@string/autofill_inline_suggestions_summary" android:defaultValue="@bool/autofill_inline_suggestions_default"/> + From c71ef24052238e7e4c61870df24e7d79473b0c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20D=C3=BCrholz?= Date: Sat, 28 Aug 2021 12:48:03 +0200 Subject: [PATCH 3/5] Add icon for manual autofill selection --- .../com/kunzisoft/keepass/autofill/AutofillHelper.kt | 9 ++++++--- app/src/main/res/layout/item_autofill_entry.xml | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) 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 bf5a0ffac..6559fefd0 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt @@ -317,11 +317,14 @@ object AutofillHelper { webScheme = parseResult.webScheme manualSelection = true } - - val manualSelectionView = newRemoteViews(context, database, context.getString(R.string.autofill_manual_selection_prompt), null) + val manualSelectionView = RemoteViews(context.packageName, R.layout.item_autofill_entry) + manualSelectionView.setTextViewText(R.id.autofill_entry_text, context.getString(R.string.autofill_manual_selection_prompt)) + manualSelectionView.setImageViewResource(R.id.autofill_entry_icon, R.mipmap.ic_launcher_round) val intentSender = AutofillLauncherActivity.getAuthIntentSenderForSelection(context, - searchInfo, null) + searchInfo, inlineSuggestionsRequest) + val builder = Dataset.Builder(manualSelectionView) + // enable manual selection only for the form field that has focus parseResult.focusedId?.let { autofillId -> builder.setValue(autofillId, AutofillValue.forText("dummy")) diff --git a/app/src/main/res/layout/item_autofill_entry.xml b/app/src/main/res/layout/item_autofill_entry.xml index d5b99ab1b..6a9d72303 100644 --- a/app/src/main/res/layout/item_autofill_entry.xml +++ b/app/src/main/res/layout/item_autofill_entry.xml @@ -23,8 +23,8 @@ Date: Sun, 29 Aug 2021 10:16:15 +0200 Subject: [PATCH 4/5] Manual selection for inline suggestions --- .../activities/AutofillLauncherActivity.kt | 8 +-- .../keepass/autofill/AutofillHelper.kt | 69 +++++++++++++++---- .../keepass/autofill/KeeAutofillService.kt | 4 +- .../drawable/ic_arrow_right_green_24dp.xml | 9 +++ .../main/res/layout/item_autofill_entry.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 app/src/main/res/drawable/ic_arrow_right_green_24dp.xml diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt index e3b56384e..5f1687062 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/AutofillLauncherActivity.kt @@ -206,9 +206,9 @@ class AutofillLauncherActivity : DatabaseModeActivity() { private const val KEY_REGISTER_INFO = "KEY_REGISTER_INFO" - fun getAuthIntentSenderForSelection(context: Context, - searchInfo: SearchInfo? = null, - inlineSuggestionsRequest: InlineSuggestionsRequest? = null): IntentSender { + fun getPendingIntentForSelection(context: Context, + searchInfo: SearchInfo? = null, + inlineSuggestionsRequest: InlineSuggestionsRequest? = null): PendingIntent { return PendingIntent.getActivity(context, 0, // Doesn't work with Parcelable (don't know why?) Intent(context, AutofillLauncherActivity::class.java).apply { @@ -224,7 +224,7 @@ class AutofillLauncherActivity : DatabaseModeActivity() { } } }, - PendingIntent.FLAG_CANCEL_CURRENT).intentSender + PendingIntent.FLAG_CANCEL_CURRENT) } fun getAuthIntentSenderForRegistration(context: Context, 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 6559fefd0..997671a3f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt @@ -25,6 +25,7 @@ import android.app.PendingIntent import android.app.assist.AssistStructure import android.content.Context import android.content.Intent +import android.content.IntentSender import android.graphics.BlendMode import android.graphics.drawable.Icon import android.os.Build @@ -37,6 +38,7 @@ import android.view.autofill.AutofillValue import android.view.inputmethod.InlineSuggestionsRequest import android.widget.RemoteViews import android.widget.Toast +import android.widget.inline.InlinePresentationSpec import androidx.annotation.RequiresApi import androidx.autofill.inline.UiVersions import androidx.autofill.inline.v1.InlineSuggestionUi @@ -52,7 +54,6 @@ import com.kunzisoft.keepass.model.SearchInfo import com.kunzisoft.keepass.database.element.template.TemplateField import com.kunzisoft.keepass.settings.AutofillSettingsActivity import com.kunzisoft.keepass.settings.PreferencesUtil -import java.util.* import kotlin.collections.ArrayList @@ -273,6 +274,27 @@ object AutofillHelper { return null } + @RequiresApi(Build.VERSION_CODES.R) + @SuppressLint("RestrictedApi") + private fun buildInlinePresentationForManualSelection(context: Context, + inlinePresentationSpec: InlinePresentationSpec, + pendingIntent: PendingIntent): InlinePresentation? { + // Make sure that the IME spec claims support for v1 UI template. + val imeStyle = inlinePresentationSpec.style + if (!UiVersions.getVersions(imeStyle).contains(UiVersions.INLINE_UI_VERSION_1)) + return null + + // Build the content for IME UI + return InlinePresentation( + InlineSuggestionUi.newContentBuilder(pendingIntent).apply { + setContentDescription(context.getString(R.string.autofill_sign_in_prompt)) + setTitle(context.getString(R.string.autofill_manual_selection_prompt)) + setStartIcon(Icon.createWithResource(context, R.drawable.ic_arrow_right_green_24dp).apply { + setTintBlendMode(BlendMode.DST) + }) + }.build().slice, inlinePresentationSpec, false) + } + fun buildResponse(context: Context, database: Database, entriesInfo: List, @@ -296,18 +318,28 @@ object AutofillHelper { } // Add inline suggestion for new IME and dataset - entriesInfo.forEachIndexed { index, entryInfo -> - val inlinePresentation = inlineSuggestionsRequest?.let { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - buildInlinePresentationForEntry(context, database, inlineSuggestionsRequest, index, entryInfo) - } else { - null + var numberInlineSuggestions = 0 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + inlineSuggestionsRequest?.let { + numberInlineSuggestions = minOf(inlineSuggestionsRequest.maxSuggestionCount, entriesInfo.size) + if (PreferencesUtil.isAutofillManualSelectionEnable(context)) { + if (entriesInfo.size >= inlineSuggestionsRequest.maxSuggestionCount) { + --numberInlineSuggestions + } } } - val dataSet = buildDataset(context, database, entryInfo, parseResult, inlinePresentation) - dataSet?.let { - responseBuilder.addDataset(it) + + } + + entriesInfo.forEachIndexed { _, entry -> + val inlinePresentation = if (numberInlineSuggestions > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + inlineSuggestionsRequest?.let { + buildInlinePresentationForEntry(context, database, inlineSuggestionsRequest, numberInlineSuggestions--, entry) + } + } else { + null } + responseBuilder.addDataset(buildDataset(context, database, entry, parseResult, inlinePresentation)) } if (PreferencesUtil.isAutofillManualSelectionEnable(context)) { @@ -319,16 +351,26 @@ object AutofillHelper { } val manualSelectionView = RemoteViews(context.packageName, R.layout.item_autofill_entry) manualSelectionView.setTextViewText(R.id.autofill_entry_text, context.getString(R.string.autofill_manual_selection_prompt)) - manualSelectionView.setImageViewResource(R.id.autofill_entry_icon, R.mipmap.ic_launcher_round) - val intentSender = AutofillLauncherActivity.getAuthIntentSenderForSelection(context, + // manualSelectionView.setImageViewResource(R.id.autofill_entry_icon, R.mipmap.ic_launcher_round) + val pendingIntent = AutofillLauncherActivity.getPendingIntentForSelection(context, searchInfo, inlineSuggestionsRequest) val builder = Dataset.Builder(manualSelectionView) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + inlineSuggestionsRequest?.let { + val inlinePresentationSpec = inlineSuggestionsRequest.inlinePresentationSpecs[0] + val inlinePresentation = buildInlinePresentationForManualSelection(context, inlinePresentationSpec, pendingIntent) + inlinePresentation?.let { + builder.setInlinePresentation(it) + } + } + } + // enable manual selection only for the form field that has focus parseResult.focusedId?.let { autofillId -> builder.setValue(autofillId, AutofillValue.forText("dummy")) - builder.setAuthentication(intentSender) + builder.setAuthentication(pendingIntent.intentSender) responseBuilder.addDataset(builder.build()) } } @@ -371,6 +413,7 @@ object AutofillHelper { } else { buildResponse(activity, database, entriesInfo, result, null) } + response.apply { } val mReplyIntent = Intent() Log.d(activity.javaClass.name, "Successed Autofill auth.") mReplyIntent.putExtra( diff --git a/app/src/main/java/com/kunzisoft/keepass/autofill/KeeAutofillService.kt b/app/src/main/java/com/kunzisoft/keepass/autofill/KeeAutofillService.kt index 5fbcd68f7..fcb45cbc3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/KeeAutofillService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/KeeAutofillService.kt @@ -160,8 +160,8 @@ class KeeAutofillService : AutofillService() { if (autofillIds.isNotEmpty()) { // If the entire Autofill Response is authenticated, AuthActivity is used // to generate Response. - val intentSender = AutofillLauncherActivity.getAuthIntentSenderForSelection(this, - searchInfo, inlineSuggestionsRequest) + val intentSender = AutofillLauncherActivity.getPendingIntentForSelection(this, + searchInfo, inlineSuggestionsRequest).intentSender val responseBuilder = FillResponse.Builder() val remoteViewsUnlock: RemoteViews = if (!parseResult.webDomain.isNullOrEmpty()) { RemoteViews(packageName, R.layout.item_autofill_unlock_web_domain).apply { diff --git a/app/src/main/res/drawable/ic_arrow_right_green_24dp.xml b/app/src/main/res/drawable/ic_arrow_right_green_24dp.xml new file mode 100644 index 000000000..c353255cf --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_right_green_24dp.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_autofill_entry.xml b/app/src/main/res/layout/item_autofill_entry.xml index 6a9d72303..f19ec0c15 100644 --- a/app/src/main/res/layout/item_autofill_entry.xml +++ b/app/src/main/res/layout/item_autofill_entry.xml @@ -31,7 +31,7 @@ android:layout_marginLeft="12dp" android:layout_marginStart="12dp" android:contentDescription="@string/content_description_entry_icon" - android:src="@drawable/ic_key_white_24dp" /> + android:src="@drawable/ic_arrow_right_green_24dp" /> Automatically suggest search results from the web domain or application ID Inline suggestions Attempt to display autofill suggestions directly from a compatible keyboard - Choose entry... + Select entry... Manual selection Display option to let the user select database entry Save search info From b9777921689ae47bc3778441ff64946fee77a673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20D=C3=BCrholz?= Date: Sun, 29 Aug 2021 11:23:22 +0200 Subject: [PATCH 5/5] Autofill manual selection for all form fields --- .../keepass/autofill/AutofillHelper.kt | 30 ++++++++--------- .../keepass/autofill/StructureParser.kt | 33 ------------------- 2 files changed, 14 insertions(+), 49 deletions(-) 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 997671a3f..0556d4db5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/AutofillHelper.kt @@ -351,28 +351,27 @@ object AutofillHelper { } val manualSelectionView = RemoteViews(context.packageName, R.layout.item_autofill_entry) manualSelectionView.setTextViewText(R.id.autofill_entry_text, context.getString(R.string.autofill_manual_selection_prompt)) - // manualSelectionView.setImageViewResource(R.id.autofill_entry_icon, R.mipmap.ic_launcher_round) val pendingIntent = AutofillLauncherActivity.getPendingIntentForSelection(context, searchInfo, inlineSuggestionsRequest) - val builder = Dataset.Builder(manualSelectionView) + parseResult.allAutofillIds().let { autofillIds -> + autofillIds.forEach { id -> + val builder = Dataset.Builder(manualSelectionView) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - inlineSuggestionsRequest?.let { - val inlinePresentationSpec = inlineSuggestionsRequest.inlinePresentationSpecs[0] - val inlinePresentation = buildInlinePresentationForManualSelection(context, inlinePresentationSpec, pendingIntent) - inlinePresentation?.let { - builder.setInlinePresentation(it) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + inlineSuggestionsRequest?.let { + val inlinePresentationSpec = inlineSuggestionsRequest.inlinePresentationSpecs[0] + val inlinePresentation = buildInlinePresentationForManualSelection(context, inlinePresentationSpec, pendingIntent) + inlinePresentation?.let { + builder.setInlinePresentation(it) + } + } } + builder.setValue(id, AutofillValue.forText("dummy")) + builder.setAuthentication(pendingIntent.intentSender) + responseBuilder.addDataset(builder.build()) } } - - // enable manual selection only for the form field that has focus - parseResult.focusedId?.let { autofillId -> - builder.setValue(autofillId, AutofillValue.forText("dummy")) - builder.setAuthentication(pendingIntent.intentSender) - responseBuilder.addDataset(builder.build()) - } } return try { @@ -413,7 +412,6 @@ object AutofillHelper { } else { buildResponse(activity, database, entriesInfo, result, null) } - response.apply { } val mReplyIntent = Intent() Log.d(activity.javaClass.name, "Successed Autofill auth.") mReplyIntent.putExtra( diff --git a/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt b/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt index fd52a2e4c..e3422fe29 100644 --- a/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt +++ b/app/src/main/java/com/kunzisoft/keepass/autofill/StructureParser.kt @@ -44,7 +44,6 @@ class StructureParser(private val structure: AssistStructure) { fun parse(saveValue: Boolean = false): Result? { try { result = Result() - result?.focusedId = getFocusedID() result?.apply { allowSaveValues = saveValue usernameIdCandidate = null @@ -75,35 +74,6 @@ class StructureParser(private val structure: AssistStructure) { } } - /* - * Note: SDK >= Q provides this method... - * fillContext.getFocusedId() - */ - private fun getFocusedID(): AutofillId? { - for (i in 0 until structure.windowNodeCount) { - val windowNode = structure.getWindowNodeAt(i) - val autofillId = traverse(windowNode.rootViewNode) - if (autofillId != null) { - return autofillId - } - } - return null - } - - private fun traverse(node: AssistStructure.ViewNode): AutofillId? { - if (node.visibility == View.VISIBLE && node.autofillHints != null) { - return node.autofillId - } else { - for (i in 0 until node.childCount) { - val autoFillId = traverse(node.getChildAt(i)) - if (autoFillId != null) { - return autoFillId - } - } - } - return null - } - private fun parseViewNode(node: AssistStructure.ViewNode): Boolean { // remember this if (node.className == "android.webkit.WebView") { @@ -447,9 +417,6 @@ class StructureParser(private val structure: AssistStructure) { var creditCardExpirationMonthOptions: Array? = null var creditCardExpirationDayOptions: Array? = null - // the AutofillId of the view that triggered autofill. - var focusedId: AutofillId? = null - var usernameId: AutofillId? = null set(value) { if (field == null)