PeachApplication.kt

4.8 KB · raw

1package com.peachpasswords23import android.app.Application4import androidx.hilt.work.HiltWorkerFactory5import androidx.lifecycle.DefaultLifecycleObserver6import androidx.lifecycle.LifecycleOwner7import androidx.lifecycle.ProcessLifecycleOwner8import androidx.work.Configuration9import com.peachpasswords.sync.BackgroundSyncPreference10import com.peachpasswords.sync.ForegroundSyncCoordinator11import com.peachpasswords.sync.LockedSyncSpoolHooks12import com.peachpasswords.sync.LockedSyncSpoolStore13import com.peachpasswords.shared.PeachClipboard14import com.peachpasswords.ui.lock.BiometricKeyStore15import com.peachpasswords.updates.UpdateCheckWorker16import com.peachpasswords.vault.PinUnlockStore17import com.peachpasswords.vault.VaultKeyRotationHooks18import com.peachpasswords.vault.VaultLockTimer19import com.peachpasswords.vault.VaultRepository20import dagger.hilt.android.HiltAndroidApp21import javax.inject.Inject2223@HiltAndroidApp24class PeachApplication : Application(), Configuration.Provider {25    @Inject lateinit var vault: VaultRepository26    @Inject lateinit var lockTimer: VaultLockTimer27    @Inject lateinit var foregroundSync: ForegroundSyncCoordinator28    @Inject lateinit var backgroundSync: BackgroundSyncPreference29    @Inject lateinit var lockedSyncSpool: LockedSyncSpoolStore30    @Inject lateinit var pinUnlock: PinUnlockStore31    @Inject lateinit var workerFactory: HiltWorkerFactory3233    /**34     * Set by MainActivity when its stop is a configuration change — a fold, an35     * unfold, or a rotation — rather than a real backgrounding. ProcessLifecycleOwner36     * is debounced, but switching a foldable between its displays can outlast that37     * debounce, so without this the process-level stop locks a vault the user is38     * still looking at and they meet a second lock screen drawn with the new39     * metrics.40     */41    var stopWasConfigurationChange = false4243    override val workManagerConfiguration: Configuration44        get() = Configuration.Builder().setWorkerFactory(workerFactory).build()4546    override fun onCreate() {47        super.onCreate()48        LockedSyncSpoolHooks.refresh = lockedSyncSpool::refresh49        // Both convenience escrows wrap a copy of the slot-A key, so every50        // rotation site has to invalidate them together — restoring a Codex or51        // resetting through recovery words rotated the key while leaving a52        // biometric wrapper the hardware would still accept (PB-26-126).53        VaultKeyRotationHooks.onRotated = {54            pinUnlock.clear()55            BiometricKeyStore.clear(this)56        }5758        // Update checks: once a day in the background, plus one now — process59        // start is the honest "app open". Neither ever downloads or installs;60        // the most they do is post a notification.61        UpdateCheckWorker.schedulePeriodic(this)62        UpdateCheckWorker.checkNow(this)63        // Lock the vault the moment the whole app goes to background (CF-1).64        // ProcessLifecycleOwner is process-wide and debounced, so a rotation or a65        // brief Activity restart does not trip it — only a genuine backgrounding.66        ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver {67            override fun onStart(owner: LifecycleOwner) {68                foregroundSync.triggerForegroundSync()69            }7071            override fun onStop(owner: LifecycleOwner) {72                // A SAF file picker/save dialog or share sheet also stops the73                // process's activities while it's in the foreground — that's74                // an expected, momentary handoff as part of an in-app export/75                // import flow, not a real backgrounding, so it's exempted via76                // VaultRepository.expectTransientBackground().77                if (vault.consumeTransientBackgroundSuppression()) return78                if (stopWasConfigurationChange) {79                    stopWasConfigurationChange = false80                    return81                }82                if (backgroundSync.enabled.value && vault.isUnlocked.value) {83                    // Cache only opaque encrypted sync material, then perform a84                    // real lock. The service keeps transport authority, not the85                    // vault root or plaintext entries.86                    runCatching { lockedSyncSpool.refresh(vault) }87                    vault.lock()88                } else {89                    vault.lock()90                    lockTimer.cancel()91                }92                // The 30s timer can outlive the app being visible, and a process93                // without window focus cannot read the clipboard to confirm its94                // own value — so lock is the last point at which the wipe can be95                // guaranteed (PB-26-119).96                PeachClipboard.clearNow(this@PeachApplication)97            }98        })99    }100}