package com.peachpasswords.autofill import android.content.Context import android.content.Intent import android.net.Uri import android.provider.Settings import android.view.autofill.AutofillManager /** * Whether Peach is actually the system's autofill service, and the one-time * offer to make it so. * * Nothing ever asked. Android does not prompt on install, so a user who never * goes looking in system settings has a password manager that appears to do * nothing on other apps' login screens — which reads as "autofill is broken", * not "autofill is off". June had to find the setting manually. * * Asked once and then never again: the system dialog is a full-screen list of * every autofill provider, and re-showing it to someone who deliberately uses a * different manager would be nagging. */ object AutofillEnablement { private const val PREFS = "peach_autofill" private const val ASKED_KEY = "asked_enable" fun isSupported(context: Context): Boolean = context.getSystemService(AutofillManager::class.java)?.isAutofillSupported == true fun isEnabled(context: Context): Boolean = context.getSystemService(AutofillManager::class.java)?.hasEnabledAutofillServices() == true fun shouldOffer(context: Context): Boolean = isSupported(context) && !isEnabled(context) && !asked(context) private fun asked(context: Context) = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getBoolean(ASKED_KEY, false) fun markAsked(context: Context) { context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) .edit().putBoolean(ASKED_KEY, true).apply() } /** * The system picker, pre-selecting Peach. Callers must handle this failing * to resolve — some OEM builds ship no handler for it, in which case the * only route is the general settings screen ([fallbackIntent]). */ fun requestIntent(context: Context): Intent = Intent(Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE) .setData(Uri.parse("package:${context.packageName}")) fun fallbackIntent(): Intent = Intent(Settings.ACTION_SETTINGS) }