Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,12 @@ repairs phones already restored that way (entries present, Drinks never switched
decides whether data shows, a plan, a log kept as JSON) **goes on the `ArchivedSettings`
allowlist in the same change.** Device state stays off it: ring pairing, Health Connect grants
and watermarks, notification scheduling, algorithm and scoring versions.
- **The encrypted store `pulseloop_secure` rides by key, not by file** (`SECURE_KEYS`): it mixes
the user's choices with this phone's state. The coach's whole setup is carried at the owner's
request, **API keys included** (every provider's key and model, and the custom API's address,
key and model), plus calibration, physiology, units and check-in hours. The archive is plain
JSON, so the Backup screen warns that the file exposes those keys. Don't drop the keys to make
the file "safer" without asking: losing the coach setup on a phone change is what was reported.
- **The EPDS answers are deliberately not carried** (`postpartum`'s `epds` key; see postpartum
mode). The life stage is.
- `body_measurements` and `findings` are deliberately not in the archive either: Health Connect
Expand Down
1 change: 1 addition & 0 deletions app/src/main/assets/i18n/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
"Backend proxy": "Proxy de backend",
"Backing up…": "Fazendo backup…",
"Backup & data": "Backup e dados",
"Backups include your AI coach setup and its API keys, readable by anyone with the file. Keep backup files private.": "Os backups incluem a configuração do coach de IA e as chaves de API, legíveis por quem tiver o arquivo. Guarde os arquivos de backup em local privado.",
"Balance Macros (30/40/30)": "Equilibrar macros (30/40/30)",
"Banana": "Banana",
"Barcode": "Código de barras",
Expand Down
55 changes: 53 additions & 2 deletions app/src/main/java/com/pulseloop/data/ArchivedSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,51 @@ object ArchivedSettings {
"rest_mode" to emptySet(),
)

/**
* The encrypted store (`pulseloop_secure`, shared by `ApiKeyStore` and
* `CoachProviderSettingsStore`). It mixes the user's choices with this phone's own state, so
* only these keys are carried, and a restore writes only these keys.
*
* The coach's setup is here at the owner's request, **its API keys included**: the provider,
* every provider's key and model, and the custom (local / own server) API's address, key,
* model and options. The archive is plain JSON, so those keys are readable by anyone holding
* the backup file; the Backup screen says so.
*/
private const val SECURE_FILE = "pulseloop_secure"
private val SECURE_KEYS: Set<String> = setOf(
// AI coach: switches, provider, keys, models, the custom API.
"coach_enabled", "coach_model", "openai_api_key", "coach_provider_mode",
"gemini_api_key", "gemini_model", "openrouter_api_key", "openrouter_model",
"minimax_api_key", "minimax_model", "openrouter_privacy_routing", "openrouter_provider_sort",
"coach_reasoning_effort", "coach_image_input_enabled", "web_search_enabled",
"write_tools_enabled", "environment_context_enabled",
"local_llm_base_url", "local_llm_api_key", "local_llm_model", "local_llm_tool_calling",
"local_llm_structured_output", "local_llm_max_tokens", "local_llm_timeout_seconds",
// Coach check-ins and their hours.
"notifications_enabled", "morning_hour", "evening_hour",
// Calibration and physiology change how stored readings are read.
"bp_adjust_systolic", "bp_adjust_diastolic", "glucose_offset_mgdl", "glucose_ref_mgdl",
"athlete_mode", "altitude_meters", "uses_beta_blockers", "has_known_lung_condition",
// Units and the other switches the user set.
"unit_system", "preferred_glucose_unit", "battery_alerts_enabled", "live_measurements_enabled",
)

fun export(context: Context): Map<String, Map<String, PrefValue>> =
FILES.mapValues { (file, skipped) ->
(FILES.mapValues { (file, skipped) ->
prefs(context, file).all.filterKeys { it !in skipped }.mapNotNull { (k, v) -> encode(v)?.let { k to it } }.toMap()
}.filterValues { it.isNotEmpty() }
} + (SECURE_FILE to secureValues(context))).filterValues { it.isNotEmpty() }

private fun secureValues(context: Context): Map<String, PrefValue> = runCatching {
secure(context).all.filterKeys { it in SECURE_KEYS }.mapNotNull { (k, v) -> encode(v)?.let { k to it } }.toMap()
}.getOrDefault(emptyMap())

private fun secure(context: Context): SharedPreferences = androidx.security.crypto.EncryptedSharedPreferences.create(
SECURE_FILE,
androidx.security.crypto.MasterKeys.getOrCreate(androidx.security.crypto.MasterKeys.AES256_GCM_SPEC),
context.applicationContext,
androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)

/**
* Writes the archive's values back. Only files on the allowlist are touched, and each is
Expand All @@ -62,6 +103,16 @@ object ArchivedSettings {
values.filterKeys { it !in skipped }.forEach { (k, v) -> put(e, k, v) }
e.commit()
}
// The secure file keeps this phone's own keys: only the carried ones are replaced.
settings[SECURE_FILE]?.let { values ->
runCatching {
val p = secure(context)
val e = p.edit()
SECURE_KEYS.filter { it in p.all && it !in values }.forEach { e.remove(it) }
values.filterKeys { it in SECURE_KEYS }.forEach { (k, v) -> put(e, k, v) }
e.commit()
}
}
}

private fun prefs(context: Context, file: String): SharedPreferences =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2482,6 +2482,13 @@ fun PrivacyDataSettingsScreen(
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Spacer(Modifier.height(8.dp))
// The archive carries the coach's API keys (data/ArchivedSettings.kt) in plain JSON.
Text(
tr("Backups include your AI coach setup and its API keys, readable by anyone with the file. Keep backup files private."),
style = MaterialTheme.typography.bodySmall,
color = com.pulseloop.ui.theme.PulseColors.warning,
)
Spacer(Modifier.height(12.dp))
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
Button(
Expand Down
Loading