Skip to content

Commit

Permalink
Improve autofill field parse on compatibility mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Hector Godoy committed Nov 13, 2023
1 parent 9d36d37 commit 2ae49e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import androidx.annotation.RequiresApi
class AssistStructureParser(assistStructure: AssistStructure) {
val usernameAutofillIds = mutableListOf<AutofillId>()
val passwordAutofillIds = mutableListOf<AutofillId>()
private var lastTextAutofillId: AutofillId? = null
private var candidateTextAutofillId: AutofillId? = null

private val webDomains = HashMap<String, Int>()

Expand All @@ -31,6 +33,10 @@ class AssistStructureParser(assistStructure: AssistStructure) {
val windowNode = assistStructure.getWindowNodeAt(i)
windowNode.rootViewNode?.let { parseNode(it) }
}
if (usernameAutofillIds.isEmpty())
candidateTextAutofillId?.let {
usernameAutofillIds.add(it)
}
}

/**
Expand All @@ -42,10 +48,20 @@ class AssistStructureParser(assistStructure: AssistStructure) {
node.autofillId?.let { autofillId ->
val fieldType = getFieldType(node)
if (fieldType != null) {
if (fieldType == FIELD_TYPE_USERNAME)
usernameAutofillIds.add(autofillId)
else if (fieldType == FIELD_TYPE_PASSWORD)
passwordAutofillIds.add(autofillId)
when (fieldType) {
FIELD_TYPE_USERNAME -> {
usernameAutofillIds.add(autofillId)
}
FIELD_TYPE_PASSWORD -> {
passwordAutofillIds.add(autofillId)
// We save the autofillId of the field above the password field,
// in case we don't find any explicit username field
candidateTextAutofillId = lastTextAutofillId
}
FIELD_TYPE_TEXT -> {
lastTextAutofillId = autofillId
}
}
}
}

Expand Down Expand Up @@ -107,6 +123,10 @@ class AssistStructureParser(assistStructure: AssistStructure) {
) {
return FIELD_TYPE_PASSWORD
}

if (node.inputType.isTextType(InputType.TYPE_CLASS_TEXT)) {
return FIELD_TYPE_TEXT
}
}
return null
}
Expand Down Expand Up @@ -136,5 +156,6 @@ class AssistStructureParser(assistStructure: AssistStructure) {
companion object {
private const val FIELD_TYPE_USERNAME = 0
private const val FIELD_TYPE_PASSWORD = 1
private const val FIELD_TYPE_TEXT = 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ object AutofillHelper {
) {
val autofillLabel = label ?: context.getString(R.string.app_name)

val authIntent = Intent().apply {
setPackage(context.packageName)
identifier = AUTOFILL_INTENT_ID
}

val intentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_MUTABLE
} else {
Expand All @@ -164,7 +169,7 @@ object AutofillHelper {
val pendingIntent = PendingIntent.getActivity(
context,
1001,
Intent(),
authIntent,
intentFlags
)

Expand Down Expand Up @@ -208,4 +213,6 @@ object AutofillHelper {
)
}
}

const val AUTOFILL_INTENT_ID = "com.hegocre.nextcloudpasswords.intents.autofill"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import android.service.autofill.SaveRequest
import androidx.annotation.RequiresApi
import com.hegocre.nextcloudpasswords.data.user.UserController
import com.hegocre.nextcloudpasswords.data.user.UserException
import com.hegocre.nextcloudpasswords.ui.activities.MainActivity

@RequiresApi(Build.VERSION_CODES.O)
class NCPAutofillService : AutofillService() {
Expand Down Expand Up @@ -76,7 +75,8 @@ class NCPAutofillService : AutofillService() {
}

// Intent to open MainActivity and provide a response to the request
val authIntent = Intent(this, MainActivity::class.java).apply {
val authIntent = Intent("com.hegocre.nextcloudpasswords.action.main").apply {
setPackage(packageName)
putExtra(AUTOFILL_REQUEST, true)
searchHint?.let {
putExtra(AUTOFILL_SEARCH_HINT, it)
Expand Down

0 comments on commit 2ae49e1

Please sign in to comment.