AutofillIntents.kt

4.1 KB · raw

1package com.peachpasswords.autofill23import android.app.PendingIntent4import android.content.Context5import android.content.Intent6import android.content.IntentSender7import android.os.Build89/**10 * PendingIntents the autofill surfaces hand to the framework: a no-op11 * attribution target the inline chip requires, the authentication12 * IntentSender that opens the branded [AutofillEntryPickerActivity], and the13 * "open new entry" launcher that the picker fires when the user taps the14 * "+ Create new entry" affordance.15 */16object AutofillIntents {17    const val EXTRA_IDENTITY_ID = "peach.autofill.identityId"18    const val EXTRA_PASSWORD_ID = "peach.autofill.passwordId"19    const val EXTRA_OTP_ID = "peach.autofill.otpId"20    const val EXTRA_ORIGIN = "peach.autofill.origin"21    const val EXTRA_IS_WEB_ORIGIN = "peach.autofill.isWebOrigin"22    const val EXTRA_ORIGIN_TRUST = "peach.autofill.originTrust"23    const val EXTRA_VERIFIED_DOMAINS = "peach.autofill.verifiedDomains"2425    /** Host-typed username captured by the autofill framework — used to prefill the new-entry editor. */26    const val EXTRA_TYPED_USERNAME = "peach.autofill.typedUsername"27    /** Host-typed email captured by the autofill framework — used to prefill the new-entry editor. */28    const val EXTRA_TYPED_EMAIL = "peach.autofill.typedEmail"2930    /**31     * Sent by [AutofillEntryPickerActivity] to launch MainActivity on the "+ Create32     * new entry" path. The picker cancels itself and starts MainActivity with the33     * origin + typed identity so the user can save a brand-new entry pre-filled with34     * what they had typed on the host page. Carries the same key/value shape as the35     * update notification's [com.peachpasswords.updates.EXTRA_OPEN_UPDATE].36     */37    const val EXTRA_OPEN_NEW_ENTRY = "peach.autofill.openNewEntry"38    const val EXTRA_NEW_ENTRY_URL = "peach.autofill.newEntryUrl"39    const val EXTRA_NEW_ENTRY_USERNAME = "peach.autofill.newEntryUsername"40    const val EXTRA_NEW_ENTRY_EMAIL = "peach.autofill.newEntryEmail"4142    private const val REQ_CHIP = 0xC0DE43    private const val REQ_PICKER = 0xBEEF4445    /** Required attribution for an inline suggestion; tapping it is a no-op. */46    fun noopChipIntent(context: Context): PendingIntent {47        val intent = Intent(context, AutofillEntryPickerActivity::class.java)48        return PendingIntent.getActivity(context, REQ_CHIP, intent, immutableFlags())49    }5051    /** Authentication target for the "Search Peach…" dataset. */52    fun pickerIntentSender(context: Context, parsed: ParsedStructure): IntentSender {53        val intent = Intent(context, AutofillEntryPickerActivity::class.java).apply {54            putExtra(EXTRA_IDENTITY_ID, parsed.identityId)55            putExtra(EXTRA_PASSWORD_ID, parsed.passwordId)56            putExtra(EXTRA_OTP_ID, parsed.otpId)57            putExtra(EXTRA_ORIGIN, parsed.origin)58            putExtra(EXTRA_IS_WEB_ORIGIN, parsed.webDomain != null)59            putExtra(EXTRA_ORIGIN_TRUST, parsed.originTrust.name)60            putStringArrayListExtra(EXTRA_VERIFIED_DOMAINS, ArrayList(parsed.verifiedDomains))61            // Mirror the browser's preUsername/preEmail capture so the "+ Create62            // new entry" path can hand the typed identity to the new-entry editor.63            // Empty/blank values are dropped — the picker reads the typed username64            // from the identity field the host anchored on.65            parsed.usernameValue?.trim()?.takeIf { it.isNotBlank() }?.let {66                putExtra(EXTRA_TYPED_USERNAME, it)67            }68            parsed.emailValue?.trim()?.takeIf { it.isNotBlank() }?.let {69                putExtra(EXTRA_TYPED_EMAIL, it)70            }71        }72        return PendingIntent.getActivity(context, REQ_PICKER, intent, mutableFlags()).intentSender73    }7475    private fun immutableFlags(): Int =76        PendingIntent.FLAG_UPDATE_CURRENT or77            if (Build.VERSION.SDK_INT >= 23) PendingIntent.FLAG_IMMUTABLE else 07879    private fun mutableFlags(): Int =80        PendingIntent.FLAG_UPDATE_CURRENT or81            if (Build.VERSION.SDK_INT >= 31) PendingIntent.FLAG_MUTABLE else 082}