AutofillIntents.kt

4.9 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    /** Authenticated caller package so the picker can re-resolve web-origin domains in-process. */23    const val EXTRA_CALLER_PACKAGE = "peach.autofill.callerPackage"2425    const val EXTRA_CARD_NUMBER_ID = "peach.autofill.cardNumberId"26    const val EXTRA_CARD_EXP_MONTH_ID = "peach.autofill.cardExpMonthId"27    const val EXTRA_CARD_EXP_YEAR_ID = "peach.autofill.cardExpYearId"28    const val EXTRA_CARD_EXP_DATE_ID = "peach.autofill.cardExpDateId"29    const val EXTRA_CARD_CVC_ID = "peach.autofill.cardCvcId"30    const val EXTRA_CARD_NAME_ID = "peach.autofill.cardNameId"3132    /** Host-typed username captured by the autofill framework — used to prefill the new-entry editor. */33    const val EXTRA_TYPED_USERNAME = "peach.autofill.typedUsername"34    /** Host-typed email captured by the autofill framework — used to prefill the new-entry editor. */35    const val EXTRA_TYPED_EMAIL = "peach.autofill.typedEmail"3637    /**38     * Sent by [AutofillEntryPickerActivity] to launch MainActivity on the "+ Create39     * new entry" path. The picker cancels itself and starts MainActivity with the40     * origin + typed identity so the user can save a brand-new entry pre-filled with41     * what they had typed on the host page. Carries the same key/value shape as the42     * update notification's [com.peachpasswords.updates.EXTRA_OPEN_UPDATE].43     */44    const val EXTRA_OPEN_NEW_ENTRY = "peach.autofill.openNewEntry"45    const val EXTRA_NEW_ENTRY_URL = "peach.autofill.newEntryUrl"46    const val EXTRA_NEW_ENTRY_USERNAME = "peach.autofill.newEntryUsername"47    const val EXTRA_NEW_ENTRY_EMAIL = "peach.autofill.newEntryEmail"4849    private const val REQ_CHIP = 0xC0DE50    private const val REQ_PICKER = 0xBEEF5152    /** Required attribution for an inline suggestion; tapping it is a no-op. */53    fun noopChipIntent(context: Context): PendingIntent {54        val intent = Intent(context, AutofillEntryPickerActivity::class.java)55        return PendingIntent.getActivity(context, REQ_CHIP, intent, immutableFlags())56    }5758    /** Authentication target for the "Search Peach…" dataset. */59    fun pickerIntentSender(context: Context, parsed: ParsedStructure): IntentSender {60        val intent = Intent(context, AutofillEntryPickerActivity::class.java).apply {61            putExtra(EXTRA_IDENTITY_ID, parsed.identityId)62            putExtra(EXTRA_PASSWORD_ID, parsed.passwordId)63            putExtra(EXTRA_OTP_ID, parsed.otpId)64            putExtra(EXTRA_ORIGIN, parsed.origin)65            putExtra(EXTRA_IS_WEB_ORIGIN, parsed.webDomain != null)66            // The picker re-resolves origin domains/app title in-process from67            // the authenticated caller package.68            putExtra(EXTRA_CALLER_PACKAGE, parsed.packageName)69            putExtra(EXTRA_CARD_NUMBER_ID, parsed.cardNumberId)70            putExtra(EXTRA_CARD_EXP_MONTH_ID, parsed.cardExpMonthId)71            putExtra(EXTRA_CARD_EXP_YEAR_ID, parsed.cardExpYearId)72            putExtra(EXTRA_CARD_EXP_DATE_ID, parsed.cardExpDateId)73            putExtra(EXTRA_CARD_CVC_ID, parsed.cardCvcId)74            putExtra(EXTRA_CARD_NAME_ID, parsed.cardNameId)75            // Mirror the browser's preUsername/preEmail capture so the "+ Create76            // new entry" path can hand the typed identity to the new-entry editor.77            // Empty/blank values are dropped — the picker reads the typed username78            // from the identity field the host anchored on.79            parsed.usernameValue?.trim()?.takeIf { it.isNotBlank() }?.let {80                putExtra(EXTRA_TYPED_USERNAME, it)81            }82            parsed.emailValue?.trim()?.takeIf { it.isNotBlank() }?.let {83                putExtra(EXTRA_TYPED_EMAIL, it)84            }85        }86        // Freezing the sender prevents the host app from rewriting origin87        // extras before the private picker receives them. The picker re-resolves88        // the origin from raw authenticated inputs in-process.89        return PendingIntent.getActivity(context, REQ_PICKER, intent, immutableFlags()).intentSender90    }9192    private fun immutableFlags(): Int =93        PendingIntent.FLAG_UPDATE_CURRENT or94            if (Build.VERSION.SDK_INT >= 23) PendingIntent.FLAG_IMMUTABLE else 09596}