Skip to content

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

Make basic autofill dropdown work in chrome (#846)

Leendert de Borste8942c983396
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/AutofillService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/AutofillService.kt
index 88fc0584f..1789bd089 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/AutofillService.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/AutofillService.kt
@@ -23,7 +23,6 @@
* - Implementing field detection heuristics for apps without autofill hints
*/
package net.aliasvault.app
-import android.R
import android.app.assist.AssistStructure
import android.content.Intent
import android.os.CancellationSignal
@@ -39,18 +38,9 @@ import android.view.View
import android.view.autofill.AutofillId
import android.view.autofill.AutofillValue
import android.widget.RemoteViews
-import android.service.autofill.InlinePresentation
-import android.widget.inline.InlinePresentationSpec
-import android.app.slice.Slice
-import android.app.slice.SliceSpec
-import android.net.Uri
-import android.graphics.drawable.Icon
-import android.app.PendingIntent
-import android.content.Context
import net.aliasvault.app.vaultstore.VaultStore
import net.aliasvault.app.vaultstore.VaultStore.CredentialOperationCallback
import net.aliasvault.app.vaultstore.models.Credential
-import androidx.core.net.toUri
class AutofillService : AutofillService() {
private val TAG = "AliasVaultAutofill"
@@ -77,11 +67,11 @@ class AutofillService : AutofillService() {
parseStructure(structure, fieldFinder)
// If no password field was found, return an empty response
- if (!fieldFinder.foundPasswordField) {
+ /*if (!fieldFinder.foundPasswordField) {
Log.d(TAG, "No password field found, skipping autofill")
callback.onSuccess(null)
return
- }
+ }*/
// If we found a password field but no username field, and we have a last field,
// assume it's the username field
@@ -90,15 +80,6 @@ class AutofillService : AutofillService() {
Log.d(TAG, "Using last field as username field: ${fieldFinder.lastField}")
}
- // Handle inline suggestions request if present
- val inlineRequest = request.inlineSuggestionsRequest
- if (inlineRequest != null) {
- Log.d(TAG, "Inline suggestions request received!")
- handleInlineSuggestionsRequest(inlineRequest, callback, fieldFinder)
- return
- }
-
- // If no inline request, proceed with regular autofill
launchActivityForAutofill(fieldFinder, callback)
}
@@ -121,39 +102,39 @@ class AutofillService : AutofillService() {
if (store != null) {
// We have an existing instance, try to get credentials
if (store.tryGetAllCredentials(object : CredentialOperationCallback {
- override fun onSuccess(result: List<Credential>) {
- try {
- Log.d(TAG, "Retrieved ${result.size} credentials")
- if (result.size == 0) {
- // No credentials available
- Log.d(TAG, "No credentials available")
- callback.onSuccess(null)
- return
- }
+ override fun onSuccess(result: List<Credential>) {
+ try {
+ Log.d(TAG, "Retrieved ${result.size} credentials")
+ if (result.size == 0) {
+ // No credentials available
+ Log.d(TAG, "No credentials available")
+ callback.onSuccess(null)
+ return
+ }
- // Create a response with all credentials
- val responseBuilder = FillResponse.Builder()
+ // Create a response with all credentials
+ val responseBuilder = FillResponse.Builder()
- // Add each credential as a dataset
- for (credential in result) {
- // Create a dataset for this credential
- addDatasetForCredential(responseBuilder, fieldFinder, credential)
- }
+ // Add each credential as a dataset
+ for (credential in result) {
+ // Create a dataset for this credential
+ addDatasetForCredential(responseBuilder, fieldFinder, credential)
+ }
- // Send the response back
- callback.onSuccess(responseBuilder.build())
+ // Send the response back
+ callback.onSuccess(responseBuilder.build())
- } catch (e: Exception) {
- Log.e(TAG, "Error parsing credentials", e)
- callback.onSuccess(null)
+ } catch (e: Exception) {
+ Log.e(TAG, "Error parsing credentials", e)
+ callback.onSuccess(null)
+ }
}
- }
- override fun onError(e: Exception) {
- Log.e(TAG, "Error getting credentials", e)
- callback.onSuccess(null)
- }
- })) {
+ override fun onError(e: Exception) {
+ Log.e(TAG, "Error getting credentials", e)
+ callback.onSuccess(null)
+ }
+ })) {
// Successfully used cached key - method returns true
Log.d(TAG, "Successfully retrieved credentials with unlocked vault")
return
@@ -183,31 +164,12 @@ class AutofillService : AutofillService() {
credential: Credential
) {
// Create presentation for this credential
- val presentation = RemoteViews(packageName, R.layout.simple_list_item_1)
+ val presentation = RemoteViews(packageName, android.R.layout.simple_list_item_1)
presentation.setTextViewText(
- R.id.text1,
+ android.R.id.text1,
"${credential.username} (${credential.service.name})"
)
- // Create inline presentation spec
- val inlinePresentationSpec = InlinePresentationSpec.Builder(
- android.util.Size(0, 0), // minSize
- android.util.Size(500, 100) // maxSize
- ).build()
-
- // Create a Slice for the inline presentation
- val sliceUri = "content://${packageName}/autofill/${credential.id}".toUri()
- val slice = Slice.Builder(sliceUri, SliceSpec("autofill", 1))
- .addText("${credential.username} (${credential.service.name})", null, listOf("title"))
- .build()
-
- // Create inline presentation with the Slice
- val inlinePresentation = InlinePresentation(
- slice,
- inlinePresentationSpec,
- false // isPinned
- )
-
val dataSetBuilder = Dataset.Builder(presentation)
// Add autofill values for all fields
@@ -216,14 +178,10 @@ class AutofillService : AutofillService() {
if (isPassword) {
if (credential.password != null) {
dataSetBuilder.setValue(field.first, AutofillValue.forText(credential.password.value as CharSequence))
- // Add inline presentation for password field
- dataSetBuilder.setInlinePresentation(inlinePresentation)
}
} else {
if (credential.username != null) {
dataSetBuilder.setValue(field.first, AutofillValue.forText(credential.username))
- // Add inline presentation for username field
- dataSetBuilder.setInlinePresentation(inlinePresentation)
}
}
}
@@ -276,9 +234,9 @@ class AutofillService : AutofillService() {
private fun isEditableField(node: AssistStructure.ViewNode): Boolean {
// Check if the node is editable in any way
return node.inputType > 0 ||
- node.className?.contains("EditText") == true ||
- node.className?.contains("Input") == true ||
- node.htmlInfo?.tag?.equals("input", ignoreCase = true) == true
+ node.className?.contains("EditText") == true ||
+ node.className?.contains("Input") == true ||
+ node.htmlInfo?.tag?.equals("input", ignoreCase = true) == true
}
private fun isLikelyPasswordField(node: AssistStructure.ViewNode): Boolean {
@@ -359,11 +317,11 @@ class AutofillService : AutofillService() {
// Check if there's a label or placeholder that suggests username
val label = node.hint
if (label != null && (
- label.contains("username", ignoreCase = true) ||
- label.contains("email", ignoreCase = true) ||
- label.contains("login", ignoreCase = true) ||
- label.contains("user", ignoreCase = true)
- )) {
+ label.contains("username", ignoreCase = true) ||
+ label.contains("email", ignoreCase = true) ||
+ label.contains("login", ignoreCase = true) ||
+ label.contains("user", ignoreCase = true)
+ )) {
return true
}
}
@@ -374,115 +332,6 @@ class AutofillService : AutofillService() {
return false
}
- private fun handleInlineSuggestionsRequest(
- inlineRequest: android.view.inputmethod.InlineSuggestionsRequest,
- callback: FillCallback,
- fieldFinder: FieldFinder
- ) {
- // Get the maximum number of suggestions requested
- val maxSuggestions = inlineRequest.maxSuggestionCount
-
- // Get the inline presentation specs
- val specs = inlineRequest.inlinePresentationSpecs
-
- if (specs.isEmpty()) {
- Log.d(TAG, "No inline presentation specs provided")
- callback.onSuccess(null)
- return
- }
-
- // First try to get an existing instance
- val store = VaultStore.getExistingInstance()
-
- if (store != null) {
- // We have an existing instance, try to get credentials
- if (store.tryGetAllCredentials(object : CredentialOperationCallback {
- override fun onSuccess(result: List<Credential>) {
- try {
- Log.d(TAG, "Retrieved ${result.size} credentials")
- if (result.size == 0) {
- // No credentials available
- Log.d(TAG, "No credentials available")
- callback.onSuccess(null)
- return
- }
-
- // Create a response with credentials
- val responseBuilder = FillResponse.Builder()
-
- // Add each credential as a dataset, up to max suggestions
- for (i in 0 until minOf(result.size, maxSuggestions)) {
- val credential = result[i]
- val spec = specs[i % specs.size] // Cycle through available specs
-
- // Create inline presentation for this credential
- val inlinePresentation = createInlinePresentation(credential, spec)
-
- // Create dataset with inline presentation
- val datasetBuilder = Dataset.Builder()
- .setInlinePresentation(inlinePresentation)
-
- // Add the credential data to all detected fields
- for (field in fieldFinder.autofillableFields) {
- val isPassword = field.second
- val value = if (isPassword) {
- credential.password?.value as? CharSequence ?: ""
- } else {
- credential.username ?: ""
- }
- datasetBuilder.setValue(
- field.first,
- AutofillValue.forText(value),
- )
- }
-
- // Add this dataset to the response
- responseBuilder.addDataset(datasetBuilder.build())
- }
-
- // Send the response back
- callback.onSuccess(responseBuilder.build())
-
- } catch (e: Exception) {
- Log.e(TAG, "Error creating inline suggestions", e)
- callback.onSuccess(null)
- }
- }
-
- override fun onError(e: Exception) {
- Log.e(TAG, "Error getting credentials", e)
- callback.onSuccess(null)
- }
- })) {
- // Successfully used cached key - method returns true
- Log.d(TAG, "Successfully retrieved credentials with unlocked vault")
- return
- }
- }
-
- // If we get here, either there was no instance or the vault wasn't unlocked
- Log.d(TAG, "No unlocked vault available for inline suggestions")
- callback.onSuccess(null)
- }
-
- private fun createInlinePresentation(
- credential: Credential,
- spec: android.widget.inline.InlinePresentationSpec
- ): InlinePresentation {
- // Create a Slice for the inline presentation
- val sliceUri = "content://${packageName}/autofill/${credential.id}".toUri()
- val slice = Slice.Builder(sliceUri, SliceSpec("autofill", 1))
- .addText("${credential.username} (${credential.service.name})", null, listOf("title"))
- .build()
-
- // Create inline presentation with the Slice
- return InlinePresentation(
- slice,
- spec,
- false // isPinned
- )
- }
-
private class FieldFinder {
// Store pairs of (AutofillId, isPasswordField)
val autofillableFields = mutableListOf<Pair<AutofillId, Boolean>>()