package com.peachpasswords.autofill import android.content.Context import android.os.Build import android.service.autofill.Dataset import android.service.autofill.InlinePresentation import android.view.autofill.AutofillId import android.view.autofill.AutofillValue import android.widget.RemoteViews import androidx.annotation.RequiresApi import androidx.autofill.inline.v1.InlineSuggestionUi import androidx.autofill.inline.UiVersions import com.peachpasswords.R import com.peachpasswords.shared.extractDomain import com.peachpasswords.vault.TOTPGenerator import com.peachpasswords.vault.VaultEntry /** * Turns parsed form fields + vault entries into autofill [Dataset]s — the menu * RemoteViews row, the inline keyboard chip, and the actual field values. * TOTP is filled as a freshly-generated code; the seed never leaves the vault. */ object AutofillResponseBuilder { /** Entries whose domain/title plausibly match the form origin, best first. */ fun matchEntries(entries: List, origin: String?): List { if (origin.isNullOrBlank()) return entries val core = coreLabel(origin) val (hits, rest) = entries.partition { entry -> val hay = listOfNotNull(extractDomain(entry.url ?: ""), entry.url, entry.title) .joinToString(" ").lowercase() core.isNotBlank() && (hay.contains(core) || hay.contains(origin.lowercase())) } return hits + rest } /** The registrable-ish core token of an origin: "login.github.com" → "github". */ private fun coreLabel(origin: String): String { val host = origin.substringAfter("://").substringBefore('/').lowercase() val parts = host.removePrefix("www.").split('.') return when { parts.size >= 2 -> parts[parts.size - 2] parts.isNotEmpty() -> parts[0] else -> host } } fun menuRow(context: Context, entry: VaultEntry): RemoteViews { val subtitle = entry.username ?: entry.email ?: entry.url?.let(::extractDomain) ?: "" return RemoteViews(context.packageName, R.layout.autofill_row).apply { setTextViewText(R.id.autofill_row_title, entry.title) setTextViewText(R.id.autofill_row_subtitle, subtitle) } } fun simpleRow(context: Context, title: String, subtitle: String): RemoteViews = RemoteViews(context.packageName, R.layout.autofill_row).apply { setTextViewText(R.id.autofill_row_title, title) setTextViewText(R.id.autofill_row_subtitle, subtitle) } /** * Build a dataset filling the parsed fields from [entry]. Attaches the inline * chip when the IME offered a spec (API 30+); otherwise menu-only. */ fun datasetFor( context: Context, parsed: ParsedStructure, entry: VaultEntry, inlineSpec: android.widget.inline.InlinePresentationSpec?, ): Dataset? { val menu = menuRow(context, entry) val inline = if (Build.VERSION.SDK_INT >= 30 && inlineSpec != null) { buildInline(context, entry, inlineSpec) } else { null } val builder = Dataset.Builder() var filledAny = false fun put(id: AutofillId?, value: String?) { if (id == null || value.isNullOrEmpty()) return val av = AutofillValue.forText(value) if (inline != null && Build.VERSION.SDK_INT >= 30) { builder.setValue(id, av, menu, inline) } else { builder.setValue(id, av, menu) } filledAny = true } put(parsed.identityId, entry.username ?: entry.email) put(parsed.passwordId, entry.password) put(parsed.otpId, entry.totp?.let { TOTPGenerator.generateCode(it) }) return if (filledAny) builder.build() else null } @RequiresApi(30) private fun buildInline( context: Context, entry: VaultEntry, spec: android.widget.inline.InlinePresentationSpec, ): InlinePresentation? = try { val style = spec.style if (!UiVersions.getVersions(style).contains(UiVersions.INLINE_UI_VERSION_1)) { null } else { val subtitle = entry.username ?: entry.email ?: entry.url?.let(::extractDomain) ?: "" val pending = AutofillIntents.noopChipIntent(context) val slice = InlineSuggestionUi.newContentBuilder(pending) .setTitle(entry.title) .apply { if (subtitle.isNotBlank()) setSubtitle(subtitle) } .setStartIcon(android.graphics.drawable.Icon.createWithResource(context, R.drawable.ic_peach_mark)) .build() .slice InlinePresentation(slice, spec, false) } } catch (_: Throwable) { // A non-conforming IME spec must never crash the fill — degrade to menu. null } }