MainActivity.kt

7.9 KB · raw

1package com.peachpasswords23import android.content.Intent4import android.graphics.Color as AndroidColor5import android.graphics.drawable.ColorDrawable6import android.os.Bundle7import androidx.activity.compose.setContent8import androidx.activity.enableEdgeToEdge9import androidx.compose.runtime.SideEffect10import androidx.compose.ui.unit.dp11import androidx.compose.ui.unit.Density12import androidx.compose.ui.platform.LocalDensity13import androidx.compose.ui.Modifier14import androidx.compose.runtime.CompositionLocalProvider15import androidx.compose.runtime.Composable16import androidx.compose.foundation.layout.fillMaxSize17import androidx.compose.foundation.layout.BoxWithConstraints18import androidx.compose.runtime.collectAsState19import androidx.compose.runtime.getValue20import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen21import androidx.core.view.WindowCompat22import androidx.fragment.app.FragmentActivity23import dagger.hilt.android.AndroidEntryPoint24import javax.inject.Inject25import com.peachpasswords.autofill.AutofillIntents26import com.peachpasswords.autofill.AutofillNavRepository27import com.peachpasswords.ui.PeachAppNavHost28import com.peachpasswords.ui.components.QaFlags29import com.peachpasswords.ui.components.SecureWindowPolicy30import com.peachpasswords.ui.onboarding.ONBOARD_KEY31import com.peachpasswords.ui.onboarding.ONBOARD_PREFS32import com.peachpasswords.ui.onboarding.onboardingStartDestination33import com.peachpasswords.ui.theme.ACCENT_COLORS34import com.peachpasswords.ui.theme.DEFAULT_ACCENT35import com.peachpasswords.ui.theme.PeachTheme36import com.peachpasswords.updates.EXTRA_OPEN_UPDATE37import com.peachpasswords.updates.UpdateRepository38import com.peachpasswords.vault.VaultLockTimer39import com.peachpasswords.vault.VaultRepository4041// FragmentActivity (not ComponentActivity) so androidx BiometricPrompt can attach.42@AndroidEntryPoint43class MainActivity : FragmentActivity() {44    @Inject lateinit var vault: VaultRepository45    @Inject lateinit var lockTimer: VaultLockTimer46    @Inject lateinit var updates: UpdateRepository47    @Inject lateinit var autofillNav: AutofillNavRepository4849    // Every touch/key event resets the idle auto-lock countdown (CF-1).50    override fun onUserInteraction() {51        super.onUserInteraction()52        lockTimer.poke()53    }5455    // launchMode="singleTask" reuses this instance across `adb shell am start`56    // relaunches, so the QA capture-bypass extra (see onCreate) needs to be57    // re-read here too, not just once at process start.58    override fun onNewIntent(intent: Intent) {59        super.onNewIntent(intent)60        setIntent(intent)61        routeIntent(intent)62        if (BuildConfig.DEBUG) {63            QaFlags.disableSecureCaptureForQa = intent.getBooleanExtra(64                QaFlags.EXTRA_DISABLE_SECURE_CAPTURE,65                false,66            )67        }68    }6970    override fun onCreate(savedInstanceState: Bundle?) {71        installSplashScreen()72        super.onCreate(savedInstanceState)7374        // Cold launch from the update notification (the warm case is onNewIntent).75        routeIntent(intent)7677        // Match the window fill to --bg so launch never flashes a lighter colour78        // before Compose draws, whatever the device's own day/night mode is.79        window.setBackgroundDrawable(ColorDrawable(AndroidColor.parseColor("#0C0A06")))8081        enableEdgeToEdge()8283        // Default to FLAG_SECURE immediately (before Compose draws) so there is84        // never an unprotected flash. The user's "allow screenshots" preference85        // is applied reactively inside setContent once settings are available.86        if (!BuildConfig.DEBUG) {87            SecureWindowPolicy.setUserSecure(window, true)88        } else {89            QaFlags.disableSecureCaptureForQa = intent.getBooleanExtra(90                QaFlags.EXTRA_DISABLE_SECURE_CAPTURE,91                false,92            )93        }9495        // Onboarding runs once, only on a fresh install with no vault yet.96        val onboarded = getSharedPreferences(ONBOARD_PREFS, MODE_PRIVATE)97            .getBoolean(ONBOARD_KEY, false)98        val start = onboardingStartDestination(hasVault = vault.hasVault(), onboarded = onboarded)99100        setContent {101            val settings by vault.settings.collectAsState()102            // enableEdgeToEdge() picks bar icon appearance from the device's own103            // day/night mode. Peach is always dark, so on a device in light mode104            // the icons would render dark-on-dark and vanish against our105            // background — reading as if the app ran under the status bar.106            SideEffect {107                WindowCompat.getInsetsController(window, window.decorView).apply {108                    isAppearanceLightStatusBars = false109                    isAppearanceLightNavigationBars = false110                }111            }112            val accent = ACCENT_COLORS[settings.accentColor] ?: DEFAULT_ACCENT113            SideEffect {114                if (!BuildConfig.DEBUG) {115                    SecureWindowPolicy.setUserSecure(window, !settings.allowScreenshots)116                }117            }118            PeachTheme(119                accent = accent,120                reduceMotion = settings.reduceMotion,121            ) {122                WideScreenScale {123                    PeachAppNavHost(startDestination = start, isUnlocked = vault.isUnlocked, uiGateLocked = vault.uiGateLocked)124                }125            }126        }127    }128}129130/** Layout width a phone gives us; anything past this is "extra" to spend. */131private val BASE_WIDTH = 411.dp132133/**134 * The single place a launch intent is interpreted, shared by cold launch135 * (`onCreate`) and warm relaunch (`onNewIntent`) — launchMode="singleTask" means136 * either can be the one that carries a shortcut tap, and the two paths silently137 * drifting apart is how the shortcut actions went unhandled in the first place.138 *139 * Everything here only records a request; nothing navigates. The nav host acts on140 * it once the vault is unlocked.141 */142private fun MainActivity.routeIntent(intent: Intent) {143    if (intent.getBooleanExtra(EXTRA_OPEN_UPDATE, false)) updates.requestOpen()144    forwardNewEntryRequest(intent)145    routeAction(intent.action)?.let(PendingActionQueue::request)146}147148/**149 * Pick up the "+ Create new entry" launcher from the autofill picker and stash150 * the prefill payload in [autofillNav]. The NavHost observes that flow and151 * navigates to the new-entry editor, mirroring the UpdateRepository pattern.152 */153private fun MainActivity.forwardNewEntryRequest(intent: Intent) {154    if (!intent.getBooleanExtra(AutofillIntents.EXTRA_OPEN_NEW_ENTRY, false)) return155    autofillNav.requestNewEntry(156        url = intent.getStringExtra(AutofillIntents.EXTRA_NEW_ENTRY_URL),157        username = intent.getStringExtra(AutofillIntents.EXTRA_NEW_ENTRY_USERNAME),158        email = intent.getStringExtra(AutofillIntents.EXTRA_NEW_ENTRY_EMAIL),159    )160}161162/** Cap on the up-scale — past this, big text stops reading as deliberate. */163private const val MAX_SCALE = 1.35f164165/**166 * On a foldable's inner screen the app was a stretched phone: phone-sized type167 * and rows with the surplus width absorbed as empty space. Scaling the density168 * grows every dp and sp together, so the same single-column layout simply reads169 * as a bigger screen — no per-screen breakpoints to keep in sync.170 *171 * Ceiling: a uniform scale, not real adaptive layout. If a screen ever wants a172 * genuinely different wide-screen arrangement it needs its own breakpoint.173 * No-op at or below [BASE_WIDTH], so phones are untouched.174 */175@Composable176private fun WideScreenScale(content: @Composable () -> Unit) {177    BoxWithConstraints(Modifier.fillMaxSize()) {178        val base = LocalDensity.current179        val scale = (maxWidth / BASE_WIDTH).coerceIn(1f, MAX_SCALE)180        if (scale == 1f) {181            content()182        } else {183            CompositionLocalProvider(184                LocalDensity provides Density(base.density * scale, base.fontScale),185                content = content,186            )187        }188    }189}