games-hub: restore achievements on platform backup
- Commit
b0c99585d54c2dfea7999ebb5cea491c6331614b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-04 21:48 UTC (1 day ago)
- Parents
09fe8e0
The Games Hub declared no backupAgent, so stock-OS platform backup governed it only by data_extraction_rules, which exclude the SQLCipher games-hub-db. A restored install therefore started clean-slate: achievement progress, XP/level stats, streaks, sessions, profile and Yuu avatars were silently lost. The other games avoid this with a key/value BaseBackupAgent. Add platform/AppBackupAgent following that pattern, with the weather-style DB handling the hub needs because its stats live in Room, not prefs: SqlCipherDbCodec exports the DB to plaintext inside the backup payload and re-encrypts it under the new device's passphrase on restore. hubDbConfigs in GamesHubDatabase mints that passphrase when none exists, since a platform restore runs before first launch on the new device where no AndroidKeyStore key exists yet; on the source device the existing passphrase is reused. datastore_default travels alongside. The raw DB file is never copied: its passphrase is wrapped by a non-exportable AndroidKeyStore key, so a raw copy could never be opened on another device and would brick first launch. secure_prefs and the hub_controller Play-on-TV pairing keys are never backed up either. data_extraction_rules now documents this scope (key/value agent governs cloud; rules govern device-transfer and remain the fallback), keeps the raw DB and secure_prefs exclusions, and excludes the hub_controller pairing file from both channels. AGENTS.md moves AppBackupAgent to platform/ where it lives now. Verified: ./gradlew :games:hub:lint :games:hub:lintVitalRelease :games:hub:testDevUnitTest --max-workers=128 — BUILD SUCCESSFUL, including the new AppBackupAgentTest asserting the codec and datastore payload.
Diff
games/hub/AGENTS.md
diff --git a/games/hub/AGENTS.md b/games/hub/AGENTS.mdindex 1d9899e24f..e48f173936 100644--- a/games/hub/AGENTS.md+++ b/games/hub/AGENTS.md@@ -4,7 +4,7 @@ _Install: ./install games/hub (dev by default)._ - Gradle: :games:hub / dir: games/hub/-- Package roots present: ui, data, provider, intents+- Package roots present: ui, data, provider, intents, platform - Entry files: MainActivity.kt (no Route.kt — dashboard/list/detail/profile flow) - Deps: :library, :library:ui, :library:room, :sdk:games, :sdk:controller, :library:e2ee-p2p - Play on TV: the `:sdk:controller` pad screen lives here (`ui/screens/ControllerPad.kt`), not in the SDK@@ -49,7 +49,8 @@ _Install: ./install games/hub (dev by default)._ - com/vayunmathur/games/hub/util/XpLevelCalculator.kt - com/vayunmathur/games/hub/util/GameIconResolver.kt - com/vayunmathur/games/hub/util/FormatUtils.kt-- com/vayunmathur/games/hub/util/AppBackupAgent.kt+### platform/ — backup agent+- com/vayunmathur/games/hub/platform/AppBackupAgent.kt — key/value platform backup: `SqlCipherDbCodec` export/re-encrypt of `games-hub-db` (achievements/XP/streaks/profile survive restore; raw DB + `secure_prefs` + `hub_controller` pairings never ship) + `datastore_default`; DB passphrase config is `hubDbConfigs` in `data/GamesHubDatabase.kt` ### ui/ — dashboard, lists, profile (shared game chrome from library/ui/game/) - com/vayunmathur/games/hub/ui/screens/DashboardScreen.kt - com/vayunmathur/games/hub/ui/screens/DashboardPage.kt
games/hub/src/main/AndroidManifest.xml
diff --git a/games/hub/src/main/AndroidManifest.xml b/games/hub/src/main/AndroidManifest.xmlindex 43793bed40..2d1e796ba8 100644--- a/games/hub/src/main/AndroidManifest.xml+++ b/games/hub/src/main/AndroidManifest.xml@@ -48,6 +48,7 @@ <application android:allowBackup="true"+ android:backupAgent=".platform.AppBackupAgent" android:dataExtractionRules="@xml/data_extraction_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
games/hub/src/main/java/com/vayunmathur/games/hub/data/GamesHubDatabase.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/data/GamesHubDatabase.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/data/GamesHubDatabase.ktindex b3031cafad..3fea783fe3 100644--- a/games/hub/src/main/java/com/vayunmathur/games/hub/data/GamesHubDatabase.kt+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/data/GamesHubDatabase.kt@@ -1,5 +1,6 @@ package com.vayunmathur.games.hub.data +import android.content.Context import androidx.room3.Database import androidx.room3.RoomDatabase import androidx.room3.migration.Migration@@ -19,10 +20,31 @@ import com.vayunmathur.games.hub.data.entities.HubGameEntity import com.vayunmathur.games.hub.data.entities.PlaySessionEntity import com.vayunmathur.games.hub.data.entities.PlayerProfileEntity import com.vayunmathur.games.hub.data.entities.YuuEntity+import com.vayunmathur.library.util.DatabaseHelper import com.vayunmathur.library.util.DatabaseMigrations const val DB_NAME = "games-hub-db" +/**+ * Backup config shared by [com.vayunmathur.games.hub.platform.AppBackupAgent].+ *+ * The passphrase is minted when none exists: a platform restore runs before+ * first launch on the new device, where no AndroidKeyStore key or wrapped+ * passphrase exists yet. Minting here gives the backup codec the new device's+ * passphrase to re-encrypt the restored database under; on the source device+ * the existing passphrase is reused unchanged.+ */+fun hubDbConfigs(context: Context): List<Pair<String, String>> {+ val helper = DatabaseHelper(context)+ val passphrase = if (helper.isKeyGenerated()) {+ helper.getPassphrase()+ } else {+ helper.generateKey()+ helper.createAndStorePassphrase(helper.getCipherForEncryption())+ }+ return listOf(DB_NAME to passphrase)+}+ @Database( entities = [ HubGameEntity::class,
games/hub/src/main/java/com/vayunmathur/games/hub/platform/AppBackupAgent.kt
diff --git a/games/hub/src/main/java/com/vayunmathur/games/hub/platform/AppBackupAgent.kt b/games/hub/src/main/java/com/vayunmathur/games/hub/platform/AppBackupAgent.ktnew file mode 100644index 0000000000..2d6b752976--- /dev/null+++ b/games/hub/src/main/java/com/vayunmathur/games/hub/platform/AppBackupAgent.kt@@ -0,0 +1,32 @@+package com.vayunmathur.games.hub.platform++import com.vayunmathur.games.hub.data.hubDbConfigs+import com.vayunmathur.library.room.SqlCipherDbCodec+import com.vayunmathur.library.util.BaseBackupAgent++/**+ * Platform (key/value) backup agent for the Games Hub.+ *+ * Achievements, XP/level progress, streaks, sessions and the player profile+ * all live in the SQLCipher Room database, not in preferences, so a+ * datastore-only agent (the other games' shape) would restore a clean slate.+ * That database is encrypted under a passphrase wrapped by a non-exportable+ * AndroidKeyStore key, so a raw file copy of `games-hub-db` can never be+ * opened on another device. [SqlCipherDbCodec] therefore exports the database+ * to plaintext inside the backup payload and re-encrypts it under the new+ * device's passphrase on restore (see [hubDbConfigs], which mints that+ * passphrase when the restore runs before first launch).+ *+ * `secure_prefs` (the wrapped passphrase) and the `hub_controller` DataStore+ * (Play-on-TV pairing keys) are deliberately never declared here: both are+ * only meaningful on the source device.+ */+class AppBackupAgent : BaseBackupAgent() {+ override val dbCodec = SqlCipherDbCodec++ override val dbConfigs: List<Pair<String, String>>+ get() = hubDbConfigs(this)++ override val datastoreNames: List<String>+ get() = listOf("datastore_default")+}
games/hub/src/main/res/xml/data_extraction_rules.xml
diff --git a/games/hub/src/main/res/xml/data_extraction_rules.xml b/games/hub/src/main/res/xml/data_extraction_rules.xmlindex ca686b24eb..cacfff488c 100644--- a/games/hub/src/main/res/xml/data_extraction_rules.xml+++ b/games/hub/src/main/res/xml/data_extraction_rules.xml@@ -1,44 +1,49 @@ <?xml version="1.0" encoding="utf-8"?> <!-- Games Hub. Audited state:- * databases/games-hub-db — Room DB (GamesHubDatabase) with play sessions,- achievement progress, player profile and a cross-app activity feed- aggregated from the other eight games.- * secure_prefs.xml — the SQLCipher passphrase for that DB.- * filesDir/datastore_default.preferences_pb — UI settings.+ * databases/games-hub-db — Room DB (GamesHubDatabase) with achievement+ definitions/progress, XP/level stats, play sessions, daily streaks,+ player profile, Yuu avatars and a cross-app activity feed.+ * secure_prefs.xml — the SQLCipher passphrase for that DB, wrapped by a+ non-exportable AndroidKeyStore key.+ * filesDir/datastore_default.preferences_pb — shared DataStoreUtils file.+ * filesDir/datastore/hub_controller.preferences_pb — Play-on-TV pairing+ keys; device-bound, never backed up or transferred. - games-hub-db is excluded from BOTH sections, for a correctness reason before- a privacy one. It is built via library:room buildDatabase(), so it is- SQLCipher-encrypted with a random passphrase that DatabaseHelper wraps using- an AndroidKeystore key (alias db_no_auth_key). Keystore keys are hardware-bound- and are never backed up or transferred. On a restored device the alias is- gone, so DatabaseHelper.isKeyGenerated() returns false, buildDatabase()- generates a fresh key and a brand-new passphrase, and- encryptExistingDatabase() sees a non-plaintext header and leaves the file- alone — Room then opens the restored ciphertext with the wrong key and throws- SQLiteNotADatabaseException. Shipping the raw file doesn't restore the user's- data, it bricks first launch.+ games-hub-db is excluded from BOTH sections below, for a correctness reason+ before a privacy one. It is SQLCipher-encrypted with a random passphrase+ that DatabaseHelper wraps using an AndroidKeystore key (alias+ db_no_auth_key). Keystore keys are hardware-bound and are never backed up+ or transferred. On a restored device the alias is gone, so a raw copy of+ the file would be opened with a fresh passphrase and Room would throw+ SQLiteNotADatabaseException. Shipping the raw file doesn't restore the+ user's data, it bricks first launch. - Migration is handled properly by the app's own BackupAgent instead: it uses- SqlCipherDbCodec to sqlcipher_export() to plaintext at backup time and- re-encrypt under the new device's key at restore time.+ Achievements and level stats survive a platform restore through the app's+ own key/value BackupAgent instead (platform/AppBackupAgent, declared in+ the manifest): SqlCipherDbCodec sqlcipher_export()s the DB to plaintext+ inside the backup payload at backup time and re-encrypts it under the new+ device's key at restore time (hubDbConfigs mints that device's passphrase+ when the restore runs before first launch). The agent also carries+ datastore_default. It never carries secure_prefs or hub_controller. - secure_prefs.xml goes with it — it holds only the Keystore-wrapped ciphertext- of the passphrase, which is undecryptable off-device and is regenerated on- first launch anyway.+ secure_prefs.xml is excluded for the same reason — it holds only the+ Keystore-wrapped ciphertext of the passphrase, which is undecryptable+ off-device and is regenerated on first launch anyway. - These rules govern both cloud backup and device transfer. The legacy- android:backupAgent was removed: it shipped games-hub-db — a cross-app- behavioural profile (what the user plays and for how long, across eight apps) —- to cloud as PLAINTEXT via SqlCipherDbCodec.exportDatabase() (sqlcipher_export- with KEY ''), regardless of the exclusions below. With no agent declared, the- hub DB stays off both channels, as excluded below.+ SCOPE: hub declares android:backupAgent=".platform.AppBackupAgent" and+ does NOT set android:fullBackupOnly, so Android uses key/value backup and+ the <cloud-backup> section below is NOT consulted. Cloud content is+ exactly AppBackupAgent's payload. <device-transfer> IS governed here, and+ these rules become the cloud control too if the backupAgent is ever+ removed. --> <data-extraction-rules>- <!-- Effective for cloud backup now that no backupAgent is declared. -->+ <!-- Not consulted while android:backupAgent is declared; see SCOPE above. --> <cloud-backup> <include domain="root" path="." /> <include domain="file" path="." />+ <exclude domain="file" path="datastore/hub_controller.preferences_pb" /> <include domain="database" path="." /> <exclude domain="database" path="games-hub-db" /> <exclude domain="database" path="games-hub-db-wal" />@@ -51,6 +56,7 @@ <device-transfer> <include domain="root" path="." /> <include domain="file" path="." />+ <exclude domain="file" path="datastore/hub_controller.preferences_pb" /> <include domain="database" path="." /> <!-- Same Keystore problem applies to D2D: the wrapping key does not leave the source device either. -->
games/hub/src/test/java/com/vayunmathur/games/hub/platform/AppBackupAgentTest.kt
diff --git a/games/hub/src/test/java/com/vayunmathur/games/hub/platform/AppBackupAgentTest.kt b/games/hub/src/test/java/com/vayunmathur/games/hub/platform/AppBackupAgentTest.ktnew file mode 100644index 0000000000..c4948e4c97--- /dev/null+++ b/games/hub/src/test/java/com/vayunmathur/games/hub/platform/AppBackupAgentTest.kt@@ -0,0 +1,21 @@+package com.vayunmathur.games.hub.platform++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner++/** Platform backup must carry the hub database (via its codec), not just prefs. */+@RunWith(RobolectricTestRunner::class)+class AppBackupAgentTest {++ @Test+ fun backupAgentDeclaresDbCodecAndDatastore() {+ val agent = AppBackupAgent()+ // The codec is what turns the SQLCipher DB into a restorable payload;+ // without it BaseBackupAgent silently skips every dbConfig.+ assertNotNull(agent.dbCodec)+ assertEquals(listOf("datastore_default"), agent.datastoreNames)+ }+}