Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / everysync / src / main / java / com / vayunmathur / everysync / data / AccountConfig.kt
AccountConfig.kt68 lines · 3.0 KiBRaw
| 1 | package com.vayunmathur.everysync.data |
| 2 | |
| 3 | import com.vayunmathur.everysync.provider.DataType |
| 4 | import kotlinx.serialization.KSerializer |
| 5 | import kotlinx.serialization.Serializable |
| 6 | import kotlinx.serialization.builtins.SetSerializer |
| 7 | import kotlinx.serialization.builtins.serializer |
| 8 | import kotlinx.serialization.descriptors.SerialDescriptor |
| 9 | import kotlinx.serialization.encoding.Decoder |
| 10 | import kotlinx.serialization.encoding.Encoder |
| 11 | import kotlinx.serialization.json.JsonDecoder |
| 12 | import kotlinx.serialization.json.JsonPrimitive |
| 13 | import kotlinx.serialization.json.jsonPrimitive |
| 14 | |
| 15 | /** |
| 16 | * Tolerant reader for [DataType]: unknown enum names (written by a newer app |
| 17 | * version, e.g. PASSWORDS/NOTES before this build knew them) decode as absent |
| 18 | * instead of failing the whole [AccountConfig] list — the version gate for |
| 19 | * forward-compatible account storage. |
| 20 | */ |
| 21 | object TolerantDataTypeSetSerializer : KSerializer<Set<DataType>> { |
| 22 | private val delegate = SetSerializer(DataType.serializer()) |
| 23 | |
| 24 | override val descriptor: SerialDescriptor = delegate.descriptor |
| 25 | |
| 26 | override fun serialize(encoder: Encoder, value: Set<DataType>) = |
| 27 | delegate.serialize(encoder, value) |
| 28 | |
| 29 | override fun deserialize(decoder: Decoder): Set<DataType> { |
| 30 | // JSON path: filter unknown names element-by-element so one future |
| 31 | // type never drops the stored account. Non-JSON decoders (protobuf, |
| 32 | // cbor) fall back to strict decode — they version by schema instead. |
| 33 | val jsonDecoder = decoder as? JsonDecoder ?: return delegate.deserialize(decoder) |
| 34 | val element = jsonDecoder.decodeJsonElement() |
| 35 | if (element !is kotlinx.serialization.json.JsonArray) return emptySet() |
| 36 | return element.mapNotNull { item -> |
| 37 | val name = (item as? JsonPrimitive)?.takeIf { it.isString }?.content ?: return@mapNotNull null |
| 38 | runCatching { DataType.valueOf(name) }.getOrNull() |
| 39 | }.toSet() |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * A configured EverySync account. Persisted as JSON in [AccountStore]; the |
| 45 | * matching [android.accounts.Account] (type `com.vayunmathur.everysync`) is |
| 46 | * registered with AccountManager so synced contacts/calendar rows are owned by |
| 47 | * a real system account — the system providers key rows by (name, type) and |
| 48 | * some OEMs reject rows whose type has no registered authenticator. |
| 49 | * |
| 50 | * Secrets (vault keys) live in the encrypted [TokenStore], |
| 51 | * keyed by [accountName] — never in this object. |
| 52 | */ |
| 53 | @Serializable |
| 54 | data class AccountConfig( |
| 55 | /** Unique account name, also the system account name owning the local rows. */ |
| 56 | val accountName: String, |
| 57 | val providerId: String, |
| 58 | /** |
| 59 | * Data types the user enabled for this account. Decodes tolerantly: names |
| 60 | * from a newer version are dropped rather than failing the account. |
| 61 | */ |
| 62 | @Serializable(with = TolerantDataTypeSetSerializer::class) |
| 63 | val enabledTypes: Set<DataType> = emptySet(), |
| 64 | /** Per-account sync interval override in minutes (null = use global). */ |
| 65 | val intervalMinutes: Int? = null, |
| 66 | val lastSyncEpochMs: Long = 0L, |
| 67 | val lastSyncError: String? = null, |
| 68 | ) |