package com.peachpasswords.sync import android.content.Context import dagger.hilt.android.qualifiers.ApplicationContext import javax.inject.Inject import javax.inject.Singleton /** * Whether this device relays sync traffic for other paired devices. * * Decided 2026-07-24 (`PEACH_SYNC_RELAY_DESIGN.md` ยง5): on by default. Two * laptops cannot sync with each other at all unless some native device relays, * so a phone that had to be configured first would leave that path silently * broken for everyone who never opened Settings. * * Plain SharedPreferences rather than the encrypted store: this is a boolean * about this handset's role, not a secret, and it must not travel to other * devices the way a vault setting would. */ @Singleton class RelaySettingsStore @Inject constructor( @ApplicationContext context: Context, ) : RelayPolicy { private val prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE) override fun isRelayEnabled(): Boolean = prefs.getBoolean(KEY_ENABLED, true) fun setRelayEnabled(enabled: Boolean) { prefs.edit().putBoolean(KEY_ENABLED, enabled).apply() } fun relayUrl(): String = prefs.getString(KEY_URL, null) ?.let { runCatching { Rendezvous.normalizeRelayUrl(it) }.getOrNull() } ?: Rendezvous.DEFAULT_RELAY_URL fun setRelayUrl(input: String): String { val normalized = Rendezvous.normalizeRelayUrl(input) prefs.edit().putString(KEY_URL, normalized).apply() return normalized } fun isLanOnly(): Boolean = prefs.getBoolean(KEY_LAN_ONLY, false) fun setLanOnly(lanOnly: Boolean) { prefs.edit().putBoolean(KEY_LAN_ONLY, lanOnly).apply() } companion object { const val PREFS_FILE = "peach_relay_settings" const val KEY_ENABLED = "relay_enabled" const val KEY_URL = "relay_url" const val KEY_LAN_ONLY = "lan_only" fun readRelayUrl(context: Context): String = context.getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE) .getString(KEY_URL, null) ?.let { runCatching { Rendezvous.normalizeRelayUrl(it) }.getOrNull() } ?: Rendezvous.DEFAULT_RELAY_URL } }