Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add service name that is being searched for to Android autofill list (#1264)

Leendert de Borst375b2e3c122b
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/AutofillService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/AutofillService.kt
index 8cf9da826..f5ffce93e 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/AutofillService.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/autofill/AutofillService.kt
@@ -10,6 +10,7 @@ package net.aliasvault.app.autofill
import android.app.PendingIntent
import android.content.Intent
+import android.graphics.Typeface
import android.os.CancellationSignal
import android.service.autofill.AutofillService
import android.service.autofill.Dataset
@@ -18,6 +19,8 @@ import android.service.autofill.FillRequest
import android.service.autofill.FillResponse
import android.service.autofill.SaveCallback
import android.service.autofill.SaveRequest
+import android.text.SpannableString
+import android.text.style.StyleSpan
import android.util.Log
import android.view.autofill.AutofillValue
import android.widget.RemoteViews
@@ -85,26 +88,22 @@ class AutofillService : AutofillService() {
Log.e(TAG, "Unexpected error in onFillRequest", e)
// Provide a simple fallback response to prevent white flash
try {
+ // Get the app/website information to include in debug dataset
+ val contexts = request.fillContexts
+ val context = contexts.last()
+ val structure = context.structure
+ val fieldFinder = FieldFinder(structure)
+ fieldFinder.parseStructure()
+ val appInfo = fieldFinder.getAppInfo()
+
val responseBuilder = FillResponse.Builder()
- val presentation = RemoteViews(packageName, R.layout.autofill_dataset_item_logo)
- presentation.setTextViewText(R.id.text, getString(R.string.autofill_failed_to_retrieve))
- val dataSetBuilder = Dataset.Builder(presentation)
+ // Add debug dataset showing what we're searching for
+ responseBuilder.addDataset(createSearchDebugDataset(fieldFinder, appInfo ?: "unknown"))
+
+ // Add failed to retrieve dataset
+ responseBuilder.addDataset(createFailedToRetrieveDataset(fieldFinder))
- // Add a click listener to open AliasVault app
- val intent = Intent(this@AutofillService, MainActivity::class.java).apply {
- addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- putExtra("OPEN_CREDENTIALS", true)
- }
- val pendingIntent = PendingIntent.getActivity(
- this@AutofillService,
- 0,
- intent,
- PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
- )
- dataSetBuilder.setAuthentication(pendingIntent.intentSender)
-
- responseBuilder.addDataset(dataSetBuilder.build())
safeCallback(responseBuilder.build())
} catch (fallbackError: Exception) {
Log.e(TAG, "Error creating fallback response", fallbackError)
@@ -167,6 +166,9 @@ class AutofillService : AutofillService() {
val responseBuilder = FillResponse.Builder()
+ // Always add debug dataset as first option showing what we're searching for
+ responseBuilder.addDataset(createSearchDebugDataset(fieldFinder, appInfo ?: "unknown"))
+
// If there are no results, return "no matches" placeholder option.
if (filteredCredentials.isEmpty()) {
Log.d(
@@ -181,9 +183,6 @@ class AutofillService : AutofillService() {
createCredentialDataset(fieldFinder, credential),
)
}
-
- // Add "Open AliasVault app" as the last option
- responseBuilder.addDataset(createOpenAppDataset(fieldFinder))
}
callback(responseBuilder.build())
@@ -191,6 +190,7 @@ class AutofillService : AutofillService() {
Log.e(TAG, "Error parsing credentials", e)
// Show "Failed to retrieve, open app" option instead of failing
val responseBuilder = FillResponse.Builder()
+ responseBuilder.addDataset(createSearchDebugDataset(fieldFinder, appInfo ?: "unknown"))
responseBuilder.addDataset(createFailedToRetrieveDataset(fieldFinder))
callback(responseBuilder.build())
}
@@ -200,6 +200,7 @@ class AutofillService : AutofillService() {
Log.e(TAG, "Error getting credentials", e)
// Show "Failed to retrieve, open app" option instead of failing
val responseBuilder = FillResponse.Builder()
+ responseBuilder.addDataset(createSearchDebugDataset(fieldFinder, appInfo ?: "unknown"))
responseBuilder.addDataset(createFailedToRetrieveDataset(fieldFinder))
callback(responseBuilder.build())
}
@@ -383,30 +384,24 @@ class AutofillService : AutofillService() {
}
/**
- * Create a dataset for the "open app" option.
+ * Create a dataset for the "vault locked" option.
* @param fieldFinder The field finder
* @return The dataset
*/
- private fun createOpenAppDataset(fieldFinder: FieldFinder): Dataset {
- val openAppPresentation = RemoteViews(packageName, R.layout.autofill_dataset_item_logo)
- openAppPresentation.setTextViewText(
+ private fun createVaultLockedDataset(fieldFinder: FieldFinder): Dataset {
+ // Create presentation for the "vault locked" option
+ val presentation = RemoteViews(packageName, R.layout.autofill_dataset_item_logo)
+ presentation.setTextViewText(
R.id.text,
- getString(R.string.autofill_open_app),
+ getString(R.string.autofill_vault_locked),
)
- val dataSetBuilder = Dataset.Builder(openAppPresentation)
-
- // Get the app/website information to use as service URL
- val appInfo = fieldFinder.getAppInfo()
- val encodedUrl = appInfo?.let { java.net.URLEncoder.encode(it, "UTF-8") } ?: ""
-
- // Create deep link URL to credentials page with service URL
- val deepLinkUrl = "net.aliasvault.app://credentials?serviceUrl=$encodedUrl"
+ val dataSetBuilder = Dataset.Builder(presentation)
- // Add a click listener to open AliasVault app with deep link
- val intent = Intent(Intent.ACTION_VIEW).apply {
- data = android.net.Uri.parse(deepLinkUrl)
+ // Add a click listener to open AliasVault app
+ val intent = Intent(this@AutofillService, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ putExtra("OPEN_CREDENTIALS", true)
}
val pendingIntent = PendingIntent.getActivity(
this@AutofillService,
@@ -416,7 +411,7 @@ class AutofillService : AutofillService() {
)
dataSetBuilder.setAuthentication(pendingIntent.intentSender)
- // Set an empty value for the field to satisfy the requirement
+ // Add a placeholder value to both username and password fields to satisfy the requirement that at least one value must be set
if (fieldFinder.autofillableFields.isNotEmpty()) {
for (field in fieldFinder.autofillableFields) {
dataSetBuilder.setValue(field.first, AutofillValue.forText(""))
@@ -427,24 +422,27 @@ class AutofillService : AutofillService() {
}
/**
- * Create a dataset for the "vault locked" option.
+ * Create a dataset for the "failed to retrieve" option.
* @param fieldFinder The field finder
* @return The dataset
*/
- private fun createVaultLockedDataset(fieldFinder: FieldFinder): Dataset {
- // Create presentation for the "vault locked" option
+ private fun createFailedToRetrieveDataset(fieldFinder: FieldFinder): Dataset {
+ // Create presentation for the "failed to retrieve" option
val presentation = RemoteViews(packageName, R.layout.autofill_dataset_item_logo)
presentation.setTextViewText(
R.id.text,
- getString(R.string.autofill_vault_locked),
+ getString(R.string.autofill_failed_to_retrieve),
)
val dataSetBuilder = Dataset.Builder(presentation)
- // Add a click listener to open AliasVault app
- val intent = Intent(this@AutofillService, MainActivity::class.java).apply {
+ // Create deep link URL
+ val deepLinkUrl = "net.aliasvault.app://reinitialize"
+
+ // Add a click listener to open AliasVault app with deep link
+ val intent = Intent(Intent.ACTION_VIEW).apply {
+ data = android.net.Uri.parse(deepLinkUrl)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- putExtra("OPEN_CREDENTIALS", true)
}
val pendingIntent = PendingIntent.getActivity(
this@AutofillService,
@@ -465,22 +463,30 @@ class AutofillService : AutofillService() {
}
/**
- * Create a dataset for the "failed to retrieve" option.
+ * Create a debug dataset showing what string we're searching for, clickable to open the app.
* @param fieldFinder The field finder
+ * @param searchText The text being searched for
* @return The dataset
*/
- private fun createFailedToRetrieveDataset(fieldFinder: FieldFinder): Dataset {
- // Create presentation for the "failed to retrieve" option
- val presentation = RemoteViews(packageName, R.layout.autofill_dataset_item_logo)
- presentation.setTextViewText(
- R.id.text,
- getString(R.string.autofill_failed_to_retrieve),
- )
+ private fun createSearchDebugDataset(fieldFinder: FieldFinder, searchText: String): Dataset {
+ // Create presentation for the debug option (with search icon)
+ val presentation = RemoteViews(packageName, R.layout.autofill_dataset_item_icon)
+
+ // Create bold text for the search string
+ val boldText = SpannableString(searchText)
+ boldText.setSpan(StyleSpan(Typeface.BOLD), 0, searchText.length, 0)
+
+ presentation.setTextViewText(R.id.text, boldText)
+ presentation.setImageViewResource(R.id.icon, R.drawable.ic_search)
val dataSetBuilder = Dataset.Builder(presentation)
- // Create deep link URL
- val deepLinkUrl = "net.aliasvault.app://reinitialize"
+ // Get the app/website information to use as service URL
+ val appInfo = fieldFinder.getAppInfo()
+ val encodedUrl = appInfo?.let { java.net.URLEncoder.encode(it, "UTF-8") } ?: ""
+
+ // Create deep link URL to credentials page with service URL
+ val deepLinkUrl = "net.aliasvault.app://credentials?serviceUrl=$encodedUrl"
// Add a click listener to open AliasVault app with deep link
val intent = Intent(Intent.ACTION_VIEW).apply {
@@ -495,7 +501,7 @@ class AutofillService : AutofillService() {
)
dataSetBuilder.setAuthentication(pendingIntent.intentSender)
- // Add a placeholder value to both username and password fields to satisfy the requirement that at least one value must be set
+ // Add placeholder values to satisfy Android's requirement that at least one value must be set
if (fieldFinder.autofillableFields.isNotEmpty()) {
for (field in fieldFinder.autofillableFields) {
dataSetBuilder.setValue(field.first, AutofillValue.forText(""))
diff --git a/apps/mobile-app/android/app/src/main/res/drawable/ic_search.xml b/apps/mobile-app/android/app/src/main/res/drawable/ic_search.xml
new file mode 100644
index 000000000..00284e8c1
--- /dev/null
+++ b/apps/mobile-app/android/app/src/main/res/drawable/ic_search.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp"
+ android:height="24dp"
+ android:viewportWidth="24"
+ android:viewportHeight="24"
+ android:tint="?android:attr/textColorSecondary">
+ <path
+ android:fillColor="@android:color/white"
+ android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
+</vector>