AutofillEnablement.kt

2.1 KB · raw

1package com.peachpasswords.autofill23import android.content.Context4import android.content.Intent5import android.net.Uri6import android.provider.Settings7import android.view.autofill.AutofillManager89/**10 * Whether Peach is actually the system's autofill service, and the one-time11 * offer to make it so.12 *13 * Nothing ever asked. Android does not prompt on install, so a user who never14 * goes looking in system settings has a password manager that appears to do15 * nothing on other apps' login screens — which reads as "autofill is broken",16 * not "autofill is off". June had to find the setting manually.17 *18 * Asked once and then never again: the system dialog is a full-screen list of19 * every autofill provider, and re-showing it to someone who deliberately uses a20 * different manager would be nagging.21 */22object AutofillEnablement {2324    private const val PREFS = "peach_autofill"25    private const val ASKED_KEY = "asked_enable"2627    fun isSupported(context: Context): Boolean =28        context.getSystemService(AutofillManager::class.java)?.isAutofillSupported == true2930    fun isEnabled(context: Context): Boolean =31        context.getSystemService(AutofillManager::class.java)?.hasEnabledAutofillServices() == true3233    fun shouldOffer(context: Context): Boolean =34        isSupported(context) && !isEnabled(context) && !asked(context)3536    private fun asked(context: Context) =37        context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getBoolean(ASKED_KEY, false)3839    fun markAsked(context: Context) {40        context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)41            .edit().putBoolean(ASKED_KEY, true).apply()42    }4344    /**45     * The system picker, pre-selecting Peach. Callers must handle this failing46     * to resolve — some OEM builds ship no handler for it, in which case the47     * only route is the general settings screen ([fallbackIntent]).48     */49    fun requestIntent(context: Context): Intent =50        Intent(Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE)51            .setData(Uri.parse("package:${context.packageName}"))5253    fun fallbackIntent(): Intent = Intent(Settings.ACTION_SETTINGS)54}