PeachSyncService.kt

10.9 KB · raw

1package com.peachpasswords.sync23import android.app.Notification4import android.app.NotificationChannel5import android.app.NotificationManager6import android.app.PendingIntent7import android.app.Service8import android.content.BroadcastReceiver9import android.content.Context10import android.content.Intent11import android.os.Build12import android.os.IBinder13import androidx.core.app.NotificationCompat14import com.peachpasswords.MainActivity15import com.peachpasswords.R16import dagger.hilt.EntryPoint17import dagger.hilt.InstallIn18import dagger.hilt.android.AndroidEntryPoint19import dagger.hilt.android.EntryPointAccessors20import dagger.hilt.components.SingletonComponent21import com.peachpasswords.vault.VaultCrypto22import com.peachpasswords.vault.VaultRepository23import kotlinx.coroutines.CoroutineScope24import kotlinx.coroutines.SupervisorJob25import kotlinx.coroutines.cancel26import javax.inject.Inject2728/**29 * Keeps this device reachable on the relay so an already-paired peer can30 * reconnect. A browser extension can never listen, so if the phone is not31 * registered the two devices simply never sync and neither shows an error —32 * "no connection" and "nothing to sync" look identical.33 *34 * A browser extension can never listen for a connection, so the phone has to be35 * the side that is dialable — and a phone that is only dialable while its app36 * is foregrounded is not dialable in any useful sense. Doze and App Standby37 * will take an ordinary background socket away. This is the same standard38 * pattern Syncthing and Tailscale use for exactly the same reason: a foreground39 * service with a visible, honest notification.40 *41 * The notification is deliberately plain about what it is. A persistent42 * notification the user does not understand gets the app force-stopped, which43 * is the one outcome that breaks sync entirely.44 */45@AndroidEntryPoint46class PeachSyncService : Service() {4748    @Inject lateinit var cloudRelayListener: CloudRelayReconnectListener49    @Inject lateinit var vault: VaultRepository50    @Inject lateinit var deviceStore: PairedDeviceStore51    @Inject lateinit var pendingVouchedStore: PendingVouchedDeviceStore52    @Inject lateinit var syncAlertStore: SyncAlertStore53    @Inject lateinit var syncOutcomes: SyncOutcomeStore5455    private val scope = CoroutineScope(SupervisorJob())5657    override fun onBind(intent: Intent?): IBinder? = null5859    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {60        // The same restriction that can refuse [start] applies again here, and61        // a START_STICKY restart is exactly when it bites: the system brings us62        // back while the app is in the background. A service that stays up63        // without its notification is killed a second later by a different64        // exception, so stop deliberately and leave the reported reason65        // standing rather than come back to be refused every time.66        if (!startOrReportBlocked(syncOutcomes::report) {67                startForeground(SYNC_NOTIFICATION_ID, buildSyncNotification(this))68            }69        ) {70            stopSelf()71            return START_NOT_STICKY72        }73        startReconnectListener()74        // A start command means something changed — a new pairing, a manual75        // "sync now", the app coming forward. Re-read the device list now76        // rather than on the next interval; the listener is already running77        // on every start but the first.78        cloudRelayListener.requestReconcile()79        // START_STICKY: if the system reclaims us under pressure, come back —80        // a listener that silently stays down looks identical to a peer that81        // has simply gone offline, and the user gets no signal either way.82        return START_STICKY83    }8485    override fun onDestroy() {86        cloudRelayListener.stop()87        scope.cancel()88        super.onDestroy()89    }9091    /**92     * Only possible while unlocked — the sync identity is derived from the93     * vault's root key, so a locked phone genuinely has nothing to listen with.94     * That is also why this is re-attempted on every start command rather than95     * once: the service may well come up before the user has unlocked.96     *97     * The early return is deliberately the one terminating path that reports no98     * outcome. Coming up locked is the normal boot order, not a failed sync;99     * reporting it would leave a red light waiting on the other side of the100     * unlock screen. The locked case a person can actually see is reported from101     * `SyncViewModel.syncNow`, and from a session that reaches a locked vault102     * mid-handshake.103     */104    private fun startReconnectListener() {105        val master = vault.syncMasterKey() ?: return106        try {107            val keys = deriveLocalSyncKeypair(master, getOrCreateDeviceId(this))108            val identity = buildLocalIdentity(this, master)109            cloudRelayListener.start(scope, keys.secretKey, keys.publicKey, identity) {110                if (!vault.isUnlocked.value) null else VaultSyncAdapter(111                    repo = vault,112                    ourDeviceId = getOrCreateDeviceId(this),113                    pairedDevices = deviceStore,114                    pendingVouched = pendingVouchedStore,115                    syncAlerts = syncAlertStore,116                )117            }118        } finally {119            VaultCrypto.zero(master)120        }121    }122123    companion object {124        fun start(context: Context) {125            val intent = Intent(context, PeachSyncService::class.java)126            startOrReportBlocked({ context.reportSyncOutcome(it) }) {127                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {128                    context.startForegroundService(intent)129                } else {130                    context.startService(intent)131                }132            }133        }134135        fun stop(context: Context) {136            context.stopService(Intent(context, PeachSyncService::class.java))137        }138    }139}140141internal const val SYNC_NOTIFICATION_CHANNEL_ID = "peach_sync"142internal const val SYNC_NOTIFICATION_ID = 4201143internal const val ACTION_RESTORE_SYNC_NOTIFICATION =144    "com.peachpasswords.action.RESTORE_SYNC_NOTIFICATION"145146internal fun buildSyncNotification(context: Context): Notification {147    val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager148    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {149        manager.createNotificationChannel(150            NotificationChannel(151                SYNC_NOTIFICATION_CHANNEL_ID,152                "Peach sync",153                NotificationManager.IMPORTANCE_LOW,154            ).apply {155                description = "Shown while Peach is reachable for encrypted device sync."156                setShowBadge(false)157            },158        )159    }160    val open = PendingIntent.getActivity(161        context,162        0,163        Intent(context, MainActivity::class.java),164        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,165    )166    val restore = PendingIntent.getBroadcast(167        context,168        0,169        Intent(context, SyncNotificationDismissedReceiver::class.java)170            .setAction(ACTION_RESTORE_SYNC_NOTIFICATION),171        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,172    )173    return NotificationCompat.Builder(context, SYNC_NOTIFICATION_CHANNEL_ID)174        .setContentTitle("Peach sync is active")175        .setContentText("Your other devices can reach this phone for encrypted sync.")176        .setSmallIcon(R.drawable.ic_launcher_monochrome)177        .setOngoing(true)178        .setPriority(NotificationCompat.PRIORITY_LOW)179        .setContentIntent(open)180        .setDeleteIntent(restore)181        .build()182        .also {183            // FLAG_NO_CLEAR protects Clear all. Android 14 deliberately allows184            // users to swipe ongoing notifications individually, so the185            // deleteIntent above restores this status surface while the user186            // preference still says background sync is active.187            it.flags = it.flags or Notification.FLAG_ONGOING_EVENT or Notification.FLAG_NO_CLEAR188        }189}190191internal fun shouldRestoreSyncNotification(action: String?, backgroundSyncEnabled: Boolean): Boolean =192    action == ACTION_RESTORE_SYNC_NOTIFICATION && backgroundSyncEnabled193194/**195 * Android 14 permits an ongoing foreground-service notification to be swiped196 * away while its service keeps running. Peach's notification is the only197 * visible indication that the phone remains reachable, so restore it after a198 * user dismissal whenever background sync is still enabled.199 */200class SyncNotificationDismissedReceiver : BroadcastReceiver() {201    override fun onReceive(context: Context, intent: Intent?) {202        if (!shouldRestoreSyncNotification(203                intent?.action,204                BackgroundSyncPreference.isEnabled(context),205            )206        ) {207            return208        }209        val manager =210            context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager211        manager.notify(SYNC_NOTIFICATION_ID, buildSyncNotification(context))212    }213}214215/**216 * Every sync path asks for this service first, so a throw here is a sync that217 * never happened — and it used to take the caller with it: a crash from the218 * pairing and unlock screens, nothing at all from the fire-and-forget219 * coroutines. Returns false when the start was refused, having said why.220 *221 * [IllegalStateException] rather than API 31's222 * `ForegroundServiceStartNotAllowedException` is deliberate: that class is a223 * subclass of this one and does not exist on this app's minSdk, and 26..30224 * throws the plain one for the same background start. Catching the supertype225 * covers both without an SDK_INT guard around a class that may not be there.226 */227internal fun startOrReportBlocked(228    report: (SyncOutcomeSummary) -> Unit,229    start: () -> Unit,230): Boolean = try {231    start()232    true233} catch (blocked: IllegalStateException) {234    report(235        SyncOutcomeSummary(236            reason = SyncOutcomeReason.FOREGROUND_START_BLOCKED,237            detail = blocked.message,238        ),239    )240    false241} catch (error: Exception) {242    // Nothing about asking for a service is worth crashing the app over; the243    // reason is the only part of this a person can act on.244    report(SyncOutcomeSummary(reason = SyncOutcomeReason.RUN_FAILED, detail = error.message))245    false246}247248/**249 * [PeachSyncService.start] is called from five screens and coordinators that250 * hold no injector of their own, so the store is fetched here rather than251 * threaded through every one of them. Wrapped because a store that cannot even252 * be opened must not become the crash the guard above exists to prevent.253 */254private fun Context.reportSyncOutcome(summary: SyncOutcomeSummary) {255    runCatching {256        EntryPointAccessors257            .fromApplication(applicationContext, SyncOutcomeEntryPoint::class.java)258            .syncOutcomes()259            .report(summary)260    }261}262263@EntryPoint264@InstallIn(SingletonComponent::class)265internal interface SyncOutcomeEntryPoint {266    fun syncOutcomes(): SyncOutcomeStore267}