1package com.peachpasswords.autofill23import android.content.Context4import android.os.Build5import android.service.autofill.Dataset6import android.service.autofill.InlinePresentation7import android.view.autofill.AutofillId8import android.view.autofill.AutofillValue9import android.widget.RemoteViews10import androidx.annotation.RequiresApi11import androidx.autofill.inline.v1.InlineSuggestionUi12import androidx.autofill.inline.UiVersions13import com.peachpasswords.R14import com.peachpasswords.shared.extractDomain15import com.peachpasswords.vault.TOTPGenerator16import com.peachpasswords.vault.VaultEntry17181920212223object AutofillResponseBuilder {2425 26 fun matchEntries(entries: List<VaultEntry>, origin: String?): List<VaultEntry> {27 if (origin.isNullOrBlank()) return entries28 val core = coreLabel(origin)29 val (hits, rest) = entries.partition { entry ->30 val hay = listOfNotNull(extractDomain(entry.url ?: ""), entry.url, entry.title)31 .joinToString(" ").lowercase()32 core.isNotBlank() && (hay.contains(core) || hay.contains(origin.lowercase()))33 }34 return hits + rest35 }3637 38 private fun coreLabel(origin: String): String {39 val host = origin.substringAfter("://").substringBefore('/').lowercase()40 val parts = host.removePrefix("www.").split('.')41 return when {42 parts.size >= 2 -> parts[parts.size - 2]43 parts.isNotEmpty() -> parts[0]44 else -> host45 }46 }4748 fun menuRow(context: Context, entry: VaultEntry): RemoteViews {49 val subtitle = entry.username ?: entry.email ?: entry.url?.let(::extractDomain) ?: ""50 return RemoteViews(context.packageName, R.layout.autofill_row).apply {51 setTextViewText(R.id.autofill_row_title, entry.title)52 setTextViewText(R.id.autofill_row_subtitle, subtitle)53 }54 }5556 fun simpleRow(context: Context, title: String, subtitle: String): RemoteViews =57 RemoteViews(context.packageName, R.layout.autofill_row).apply {58 setTextViewText(R.id.autofill_row_title, title)59 setTextViewText(R.id.autofill_row_subtitle, subtitle)60 }6162 63646566 fun datasetFor(67 context: Context,68 parsed: ParsedStructure,69 entry: VaultEntry,70 inlineSpec: android.widget.inline.InlinePresentationSpec?,71 ): Dataset? {72 val menu = menuRow(context, entry)73 val inline = if (Build.VERSION.SDK_INT >= 30 && inlineSpec != null) {74 buildInline(context, entry, inlineSpec)75 } else {76 null77 }7879 val builder = Dataset.Builder()80 var filledAny = false81 fun put(id: AutofillId?, value: String?) {82 if (id == null || value.isNullOrEmpty()) return83 val av = AutofillValue.forText(value)84 if (inline != null && Build.VERSION.SDK_INT >= 30) {85 builder.setValue(id, av, menu, inline)86 } else {87 builder.setValue(id, av, menu)88 }89 filledAny = true90 }91 put(parsed.identityId, entry.username ?: entry.email)92 put(parsed.passwordId, entry.password)93 put(parsed.otpId, entry.totp?.let { TOTPGenerator.generateCode(it) })9495 return if (filledAny) builder.build() else null96 }9798 @RequiresApi(30)99 private fun buildInline(100 context: Context,101 entry: VaultEntry,102 spec: android.widget.inline.InlinePresentationSpec,103 ): InlinePresentation? = try {104 val style = spec.style105 if (!UiVersions.getVersions(style).contains(UiVersions.INLINE_UI_VERSION_1)) {106 null107 } else {108 val subtitle = entry.username ?: entry.email ?: entry.url?.let(::extractDomain) ?: ""109 val pending = AutofillIntents.noopChipIntent(context)110 val slice = InlineSuggestionUi.newContentBuilder(pending)111 .setTitle(entry.title)112 .apply { if (subtitle.isNotBlank()) setSubtitle(subtitle) }113 .setStartIcon(android.graphics.drawable.Icon.createWithResource(context, R.drawable.ic_peach_mark))114 .build()115 .slice116 InlinePresentation(slice, spec, false)117 }118 } catch (_: Throwable) {119 120 null121 }122}