RelaySettingsStore.kt

2.2 KB · raw

1package com.peachpasswords.sync23import android.content.Context4import dagger.hilt.android.qualifiers.ApplicationContext5import javax.inject.Inject6import javax.inject.Singleton78/**9 * Whether this device relays sync traffic for other paired devices.10 *11 * Decided 2026-07-24 (`PEACH_SYNC_RELAY_DESIGN.md` §5): on by default. Two12 * laptops cannot sync with each other at all unless some native device relays,13 * so a phone that had to be configured first would leave that path silently14 * broken for everyone who never opened Settings.15 *16 * Plain SharedPreferences rather than the encrypted store: this is a boolean17 * about this handset's role, not a secret, and it must not travel to other18 * devices the way a vault setting would.19 */20@Singleton21class RelaySettingsStore @Inject constructor(22    @ApplicationContext context: Context,23) : RelayPolicy {24    private val prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)2526    override fun isRelayEnabled(): Boolean = prefs.getBoolean(KEY_ENABLED, true)2728    fun setRelayEnabled(enabled: Boolean) {29        prefs.edit().putBoolean(KEY_ENABLED, enabled).apply()30    }3132    fun relayUrl(): String = prefs.getString(KEY_URL, null)33        ?.let { runCatching { Rendezvous.normalizeRelayUrl(it) }.getOrNull() }34        ?: Rendezvous.DEFAULT_RELAY_URL3536    fun setRelayUrl(input: String): String {37        val normalized = Rendezvous.normalizeRelayUrl(input)38        prefs.edit().putString(KEY_URL, normalized).apply()39        return normalized40    }4142    fun isLanOnly(): Boolean = prefs.getBoolean(KEY_LAN_ONLY, false)4344    fun setLanOnly(lanOnly: Boolean) {45        prefs.edit().putBoolean(KEY_LAN_ONLY, lanOnly).apply()46    }4748    companion object {49        const val PREFS_FILE = "peach_relay_settings"50        const val KEY_ENABLED = "relay_enabled"51        const val KEY_URL = "relay_url"52        const val KEY_LAN_ONLY = "lan_only"5354        fun readRelayUrl(context: Context): String =55            context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE)56                .getString(KEY_URL, null)57                ?.let { runCatching { Rendezvous.normalizeRelayUrl(it) }.getOrNull() }58                ?: Rendezvous.DEFAULT_RELAY_URL59    }60}