MainActivity.kt

7.0 KB · raw

1package com.peachpasswords23import android.content.Intent4import android.content.res.Configuration5import android.graphics.Color as AndroidColor6import android.graphics.drawable.ColorDrawable7import android.os.Bundle8import android.view.WindowManager9import androidx.activity.compose.setContent10import androidx.activity.enableEdgeToEdge11import androidx.compose.foundation.isSystemInDarkTheme12import androidx.compose.runtime.SideEffect13import androidx.compose.ui.unit.dp14import androidx.compose.ui.unit.Density15import androidx.compose.ui.platform.LocalDensity16import androidx.compose.ui.Modifier17import androidx.compose.runtime.CompositionLocalProvider18import androidx.compose.runtime.Composable19import androidx.compose.foundation.layout.fillMaxSize20import androidx.compose.foundation.layout.BoxWithConstraints21import androidx.compose.runtime.collectAsState22import androidx.compose.runtime.getValue23import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen24import androidx.core.view.WindowCompat25import androidx.fragment.app.FragmentActivity26import dagger.hilt.android.AndroidEntryPoint27import javax.inject.Inject28import com.peachpasswords.ui.PeachAppNavHost29import com.peachpasswords.ui.components.QaFlags30import 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.vault.VaultLockTimer37import com.peachpasswords.vault.VaultRepository3839// FragmentActivity (not ComponentActivity) so androidx BiometricPrompt can attach.40@AndroidEntryPoint41class MainActivity : FragmentActivity() {42    @Inject lateinit var vault: VaultRepository43    @Inject lateinit var lockTimer: VaultLockTimer4445    // Every touch/key event resets the idle auto-lock countdown (CF-1).46    override fun onUserInteraction() {47        super.onUserInteraction()48        lockTimer.poke()49    }5051    // launchMode="singleTask" reuses this instance across `adb shell am start`52    // relaunches, so the QA capture-bypass extra (see onCreate) needs to be53    // re-read here too, not just once at process start.54    override fun onNewIntent(intent: Intent) {55        super.onNewIntent(intent)56        setIntent(intent)57        if (BuildConfig.DEBUG) {58            QaFlags.disableSecureCaptureForQa = intent.getBooleanExtra("qa_disable_secure_flag", false)59        }60    }6162    override fun onCreate(savedInstanceState: Bundle?) {63        installSplashScreen()64        super.onCreate(savedInstanceState)6566        // Match the splash/window fill to the resolved theme's --bg so launch67        // never flashes the wrong colour before Compose draws. Resolved from the68        // persisted setting synchronously (system mode reads the device night mode).69        val initialTheme = vault.settings.value.theme70        val initialDark = when (initialTheme) {71            "light" -> false72            "system" -> (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==73                Configuration.UI_MODE_NIGHT_YES74            else -> true75        }76        window.setBackgroundDrawable(77            ColorDrawable(AndroidColor.parseColor(if (initialDark) "#0C0A06" else "#EFEEE9")),78        )7980        enableEdgeToEdge()8182        // Block screenshots / recents thumbnails of the vault in release builds.83        if (!BuildConfig.DEBUG) {84            window.setFlags(85                WindowManager.LayoutParams.FLAG_SECURE,86                WindowManager.LayoutParams.FLAG_SECURE,87            )88        } else {89            // QA-only, debug-build-only: `adb shell am start -n <pkg>/.MainActivity90            // --ez qa_disable_secure_flag true` lets automated visual QA screenshot91            // otherwise-FLAG_SECURE screens (SecureFlagEffect). Off unless asked.92            QaFlags.disableSecureCaptureForQa = intent.getBooleanExtra("qa_disable_secure_flag", false)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            val darkTheme = when (settings.theme) {103                "light" -> false104                "system" -> isSystemInDarkTheme()105                else -> true106            }107            // enableEdgeToEdge() above only knows the system's own day/night108            // mode at launch. This app's theme can be forced independent of109            // that (settings.theme == "dark"/"light"), so on a mismatch —110            // e.g. system in light mode, app forced dark — the status bar111            // icons render in the system's assumed color (dark-on-dark) and112            // become invisible against the app's actual background, reading113            // as if the app extends behind/under the status bar. Keep the114            // system bar icon appearance in sync with the resolved theme.115            SideEffect {116                WindowCompat.getInsetsController(window, window.decorView).apply {117                    isAppearanceLightStatusBars = !darkTheme118                    isAppearanceLightNavigationBars = !darkTheme119                }120            }121            val accent = ACCENT_COLORS[settings.accentColor] ?: DEFAULT_ACCENT122            PeachTheme(123                darkTheme = darkTheme,124                accent = accent,125                reduceMotion = settings.reduceMotion,126            ) {127                WideScreenScale {128                    PeachAppNavHost(startDestination = start, isUnlocked = vault.isUnlocked)129                }130            }131        }132    }133}134135/** Layout width a phone gives us; anything past this is "extra" to spend. */136private val BASE_WIDTH = 411.dp137138/** Cap on the up-scale — past this, big text stops reading as deliberate. */139private const val MAX_SCALE = 1.35f140141/**142 * On a foldable's inner screen the app was a stretched phone: phone-sized type143 * and rows with the surplus width absorbed as empty space. Scaling the density144 * grows every dp and sp together, so the same single-column layout simply reads145 * as a bigger screen — no per-screen breakpoints to keep in sync.146 *147 * Ceiling: a uniform scale, not real adaptive layout. If a screen ever wants a148 * genuinely different wide-screen arrangement it needs its own breakpoint.149 * No-op at or below [BASE_WIDTH], so phones are untouched.150 */151@Composable152private fun WideScreenScale(content: @Composable () -> Unit) {153    BoxWithConstraints(Modifier.fillMaxSize()) {154        val base = LocalDensity.current155        val scale = (maxWidth / BASE_WIDTH).coerceIn(1f, MAX_SCALE)156        if (scale == 1f) {157            content()158        } else {159            CompositionLocalProvider(160                LocalDensity provides Density(base.density * scale, base.fontScale),161                content = content,162            )163        }164    }165}