Move AutofillView data into a wrapper class (#590)

This commit is contained in:
Lucas Kivi
2024-01-12 12:56:00 -06:00
committed by Álison Fernandes
parent c06be2b8de
commit 33b6f994e6
9 changed files with 155 additions and 236 deletions

View File

@@ -50,7 +50,7 @@ class FilledDataBuilderImpl : FilledDataBuilder {
val filledItems = autofillViews
.map { autofillView ->
FilledItem(
autofillId = autofillView.autofillId,
autofillId = autofillView.data.autofillId,
)
}
@@ -68,7 +68,7 @@ class FilledDataBuilderImpl : FilledDataBuilder {
val filledItems = autofillViews
.map { autofillView ->
FilledItem(
autofillId = autofillView.autofillId,
autofillId = autofillView.data.autofillId,
)
}

View File

@@ -6,30 +6,28 @@ import android.view.autofill.AutofillId
* The processed, relevant data from an autofill view node.
*/
sealed class AutofillView {
/**
* The [AutofillId] associated with this view.
*/
abstract val autofillId: AutofillId
/**
* The package id for this view, if there is one. (ex: "com.x8bit.bitwarden")
* The data important to a given [AutofillView].
*
* @param autofillId The [AutofillId] associated with this view.
* @param idPackage The package id for this view, if there is one.
* @param isFocused Whether the view is currently focused.
* @param webDomain The web domain for this view, if there is one. (example: m.facebook.com)
* @param webScheme The web scheme for this view, if there is one. (example: https)
*/
abstract val idPackage: String?
data class Data(
val autofillId: AutofillId,
val idPackage: String?,
val isFocused: Boolean,
val webDomain: String?,
val webScheme: String?,
)
/**
* Whether the view is currently focused.
* The core data that describes this [AutofillView].
*/
abstract val isFocused: Boolean
/**
* The web domain for this view, if there is one. (ex: "m.facebook.com")
*/
abstract val webDomain: String?
/**
* The web scheme for this view, if there is one. (ex: "https")
*/
abstract val webScheme: String?
abstract val data: Data
/**
* A view that corresponds to the card data partition for autofill fields.
@@ -40,44 +38,28 @@ sealed class AutofillView {
* The expiration month [AutofillView] for the [Card] data partition.
*/
data class ExpirationMonth(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
/**
* The expiration year [AutofillView] for the [Card] data partition.
*/
data class ExpirationYear(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
/**
* The number [AutofillView] for the [Card] data partition.
*/
data class Number(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
/**
* The security code [AutofillView] for the [Card] data partition.
*/
data class SecurityCode(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Card()
}
@@ -90,33 +72,21 @@ sealed class AutofillView {
* The email address [AutofillView] for the [Login] data partition.
*/
data class EmailAddress(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Login()
/**
* The password [AutofillView] for the [Login] data partition.
*/
data class Password(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Login()
/**
* The username [AutofillView] for the [Login] data partition.
*/
data class Username(
override val autofillId: AutofillId,
override val idPackage: String?,
override val isFocused: Boolean,
override val webDomain: String?,
override val webScheme: String?,
override val data: Data,
) : Login()
}
}

View File

@@ -24,7 +24,7 @@ class AutofillParserImpl : AutofillParser {
// Find the focused view (or indicate there is no fulfillment to be performed.)
val focusedView = autofillViews
.firstOrNull { it.isFocused }
.firstOrNull { it.data.isFocused }
?: return AutofillRequest.Unfillable
val uri = traversalDataList.buildUriOrNull(

View File

@@ -2,7 +2,6 @@ package com.x8bit.bitwarden.data.autofill.util
import android.app.assist.AssistStructure
import android.view.View
import android.view.autofill.AutofillId
import com.x8bit.bitwarden.data.autofill.model.AutofillView
/**
@@ -16,96 +15,66 @@ fun AssistStructure.ViewNode.toAutofillView(): AutofillView? = autofillId
autofillHints
?.firstOrNull { SUPPORTED_HINTS.contains(it) }
?.let { supportedHint ->
buildAutofillView(
val autofillViewData = AutofillView.Data(
autofillId = nonNullAutofillId,
idPackage = idPackage,
isFocused = isFocused,
hint = supportedHint,
webDomain = webDomain,
webScheme = webScheme,
)
buildAutofillView(
autofillViewData = autofillViewData,
hint = supportedHint,
)
}
}
/**
* Convert the data into an [AutofillView] if the [hint] is supported.
* Convert [autofillViewData] into an [AutofillView] if the [hint] is supported.
*/
@Suppress("LongMethod", "LongParameterList")
private fun buildAutofillView(
autofillId: AutofillId,
idPackage: String?,
isFocused: Boolean,
autofillViewData: AutofillView.Data,
hint: String,
webDomain: String?,
webScheme: String?,
): AutofillView? = when (hint) {
View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH -> {
AutofillView.Card.ExpirationMonth(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR -> {
AutofillView.Card.ExpirationYear(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_CREDIT_CARD_NUMBER -> {
AutofillView.Card.Number(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE -> {
AutofillView.Card.SecurityCode(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_EMAIL_ADDRESS -> {
AutofillView.Login.EmailAddress(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_PASSWORD -> {
AutofillView.Login.Password(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}
View.AUTOFILL_HINT_USERNAME -> {
AutofillView.Login.Username(
autofillId = autofillId,
idPackage = idPackage,
isFocused = isFocused,
webDomain = webDomain,
webScheme = webScheme,
data = autofillViewData,
)
}

View File

@@ -81,7 +81,8 @@ private fun AssistStructure.buildPackageNameOrNull(): String? = if (windowNodeCo
*/
private fun List<ViewNodeTraversalData>.buildPackageNameOrNull(): String? =
flatMap { it.autofillViews }
.firstOrNull { !it.idPackage.isNullOrEmpty() }
.firstOrNull { !it.data.idPackage.isNullOrEmpty() }
?.data
?.idPackage
/**
@@ -90,10 +91,10 @@ private fun List<ViewNodeTraversalData>.buildPackageNameOrNull(): String? =
*/
private fun List<ViewNodeTraversalData>.buildWebsiteUriOrNull(): String? =
flatMap { it.autofillViews }
.firstOrNull { !it.webDomain.isNullOrEmpty() }
.firstOrNull { !it.data.webDomain.isNullOrEmpty() }
?.let { autofillView ->
val webDomain = requireNotNull(autofillView.webDomain)
val webScheme = autofillView.webScheme.orNullIfBlank() ?: DEFAULT_SCHEME
val webDomain = requireNotNull(autofillView.data.webDomain)
val webScheme = autofillView.data.webScheme.orNullIfBlank() ?: DEFAULT_SCHEME
buildUri(
domain = webDomain,
scheme = webScheme,