Modern Apps git

← Modern-Apps.git

education: survive platform backup via SqlCipherDbCodec agent

Commit
09fe8e01a2fb727b2d8d3c7c9eebccf5ebfccd6c
Author
Vayun Mathur <[email protected]>
When
2026-12-04 21:44 UTC (1 day ago)
Parents
30be4a9
Education restored clean-slate on platform backup: its only store,
education-db (Learner, SkillProgress, Deadline), was excluded from
data_extraction_rules because a raw SQLCipher copy is undecryptable
on a new device (passphrase wrapped by a non-exportable
AndroidKeyStore key), and no android:backupAgent existed to re-key
it. Badge unlocks in the shared datastore_default file were likewise
outside any backup path.

Fix following the weather pattern (AppBackupAgent + SqlCipherDbCodec
+ BaseBackupAgent):
- Add platform/AppBackupAgent with dbCodec = SqlCipherDbCodec,
  dbConfigs = educationDbConfigs (education-db + DatabaseHelper
  passphrase), and datastoreNames = [datastore_default] so badges
  migrate too; on restore the codec re-encrypts the plaintext export
  under the new device's key.
- Add educationDbConfigs() beside DB_NAME in EducationDatabase.kt.
- Declare android:backupAgent in the manifest (allowBackup stays
  true, no fullBackupOnly, so key/value backup carries the agent
  payload to cloud).
- Rewrite data_extraction_rules.xml header/scope to match: raw
  education-db (+sidecars) and secure_prefs.xml remain excluded in
  both cloud-backup and device-transfer, so D2D never ships an
  undecryptable file and the rules stay the correct fallback if the
  agent is ever removed.
- Add Robolectric AppBackupAgentTest (codec wired, datastore list,
  dbConfigs name/passphrase via FakeAndroidKeyStore) and update
  education/AGENTS.md.

Verification: baseline on 30be4a93a and final tree both pass
./gradlew :education:lint :education:lintVitalRelease
:education:testDevUnitTest (new test: 3 tests, 0 failures), so no
failure is pre-existing or introduced. Host tests prove wiring and
rules only; the SQLCipher export/import codec itself is
device-provable (library androidTest) and an on-device
backup/restore cycle remains to be proven on hardware.

Diff

education/AGENTS.md
diff --git a/education/AGENTS.md b/education/AGENTS.mdindex a0547dd4d1..c138c9bc82 100644--- a/education/AGENTS.md+++ b/education/AGENTS.md@@ -29,8 +29,10 @@ _Install: ./install education (dev by default)._ - com/vayunmathur/education/content/ContentRepository.kt - com/vayunmathur/education/content/ContentValidator.kt - com/vayunmathur/education/content/Question.kt+### platform/ — backup+- com/vayunmathur/education/platform/AppBackupAgent.kt — key/value BackupAgent (`android:backupAgent`): exports `education-db` to plaintext via `SqlCipherDbCodec` (`educationDbConfigs`) and ships `datastore_default` (badge unlocks); raw DB + `secure_prefs.xml` stay excluded in `data_extraction_rules.xml` (device-transfer / fallback control) ### data/ — learner progress (Room)-- com/vayunmathur/education/data/EducationDatabase.kt+- com/vayunmathur/education/data/EducationDatabase.kt — `DB_NAME` + `educationDbConfigs()` backup config - com/vayunmathur/education/data/EducationRepository.kt - com/vayunmathur/education/data/Learner.kt - com/vayunmathur/education/data/SkillProgress.kt
education/src/main/AndroidManifest.xml
diff --git a/education/src/main/AndroidManifest.xml b/education/src/main/AndroidManifest.xmlindex 962a695ad3..89f21430f8 100644--- a/education/src/main/AndroidManifest.xml+++ b/education/src/main/AndroidManifest.xml@@ -15,6 +15,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"
education/src/main/java/com/vayunmathur/education/data/EducationDatabase.kt
diff --git a/education/src/main/java/com/vayunmathur/education/data/EducationDatabase.kt b/education/src/main/java/com/vayunmathur/education/data/EducationDatabase.ktindex dd60fbc8f4..af57069bb7 100644--- a/education/src/main/java/com/vayunmathur/education/data/EducationDatabase.kt+++ b/education/src/main/java/com/vayunmathur/education/data/EducationDatabase.kt@@ -1,16 +1,22 @@ package com.vayunmathur.education.data +import android.content.Context import androidx.room3.Dao import androidx.room3.Database import androidx.room3.Delete import androidx.room3.Query import androidx.room3.RoomDatabase import androidx.room3.Upsert+import com.vayunmathur.library.util.DatabaseHelper import com.vayunmathur.library.util.DatabaseMigrations import kotlinx.coroutines.flow.Flow  const val DB_NAME = "education-db" +/** Backup config shared by [com.vayunmathur.education.platform.AppBackupAgent]. */+fun educationDbConfigs(context: Context): List<Pair<String, String>> =+    listOf(DB_NAME to DatabaseHelper(context).getPassphrase())+ @Dao /** Data-access object for the single [Learner] row. */ interface LearnerDao {
education/src/main/java/com/vayunmathur/education/platform/AppBackupAgent.kt
diff --git a/education/src/main/java/com/vayunmathur/education/platform/AppBackupAgent.kt b/education/src/main/java/com/vayunmathur/education/platform/AppBackupAgent.ktnew file mode 100644index 0000000000..894563227e--- /dev/null+++ b/education/src/main/java/com/vayunmathur/education/platform/AppBackupAgent.kt@@ -0,0 +1,19 @@+package com.vayunmathur.education.platform++import com.vayunmathur.library.room.SqlCipherDbCodec+import com.vayunmathur.library.util.BaseBackupAgent+import com.vayunmathur.education.data.educationDbConfigs++/** App Backup Agent: a base backup agent implementation for app backup agent. */+class AppBackupAgent : BaseBackupAgent() {+    override val dbCodec = SqlCipherDbCodec++    override val dbConfigs: List<Pair<String, String>>+        get() = educationDbConfigs(this)++    // datastore_default is the shared DataStoreUtils file, which is where+    // AchievementsManager keeps badge unlocks/progress. Omitting it silently+    // reset earned badges on every restore.+    override val datastoreNames: List<String>+        get() = listOf("datastore_default")+}
education/src/main/res/xml/data_extraction_rules.xml
diff --git a/education/src/main/res/xml/data_extraction_rules.xml b/education/src/main/res/xml/data_extraction_rules.xmlindex 18f0b3d4be..2fdf3cebb8 100644--- a/education/src/main/res/xml/data_extraction_rules.xml+++ b/education/src/main/res/xml/data_extraction_rules.xml@@ -2,28 +2,39 @@ <!--   Education. Audited state:     * databases/education-db — Room DB with Learner, SkillProgress and Deadline-      entities.-    * No SharedPreferences or DataStore usage found; the DB is the app's only-      persistent store.+      entities (learner progress, stars, streaks, deadlines).+    * files/datastore/datastore_default.preferences_pb — shared DataStoreUtils+      file where AchievementsManager keeps badge unlocks/progress.+    * secure_prefs.xml — SQLCipher passphrase for that DB.    education-db is excluded from both sections because it is SQLCipher-encrypted-  via library:room buildDatabase(), and the passphrase is wrapped by an-  AndroidKeystore key (DatabaseHelper alias db_no_auth_key) that is hardware--  bound and never leaves the device. A restored copy cannot be decrypted:-  isKeyGenerated() returns false on the new device, a fresh key and passphrase-  are minted, encryptExistingDatabase() skips the file because its header is-  not "SQLite format 3", and Room then fails to open it. Including the file+  via library:room, and the passphrase is wrapped by an AndroidKeystore key+  (DatabaseHelper alias db_no_auth_key) that is hardware-bound and never leaves+  the device. A restored raw copy cannot be decrypted: isKeyGenerated() returns+  false on the new device, a fresh key and passphrase are minted,+  encryptExistingDatabase() skips the file because its header is not+  "SQLite format 3", and Room then fails to open it. Including the raw file   would turn a clean install into a crash on launch.    secure_prefs.xml is excluded with it — it stores only the Keystore-wrapped   ciphertext of the passphrase, which is meaningless without the key. -  Heads-up: education declares no android:backupAgent, so there is no-  SqlCipherDbCodec re-keying path here. Learner progress therefore does not-  migrate to a new device today. That is a pre-existing gap in the app, not-  something these rules introduce, but it needs a BackupAgent to fix.+  Learner progress still migrates: education declares its own BackupAgent,+  which uses SqlCipherDbCodec to export the DB to plaintext inside the backup+  payload and re-encrypt it under the new device's key on restore. That path+  is the one that works; the raw file copy would only get in its way.++  SCOPE: education declares android:backupAgent=".platform.AppBackupAgent" and+  does NOT set android:fullBackupOnly, so Android uses key/value backup here+  and the <cloud-backup> section below is NOT consulted. Cloud content is+  exactly AppBackupAgent's educationDbConfigs(this) — education-db, exported+  to plaintext by SqlCipherDbCodec and re-encrypted on restore — plus its+  datastoreNames = ["datastore_default"]. The rules below govern+  <device-transfer>, and are the correct cloud control if the backupAgent is+  ever removed. --> <data-extraction-rules>+    <!-- Not consulted while android:backupAgent is declared; see SCOPE above. -->     <cloud-backup>         <include domain="root" path="." />         <include domain="file" path="." />
education/src/test/java/com/vayunmathur/education/platform/AppBackupAgentTest.kt
diff --git a/education/src/test/java/com/vayunmathur/education/platform/AppBackupAgentTest.kt b/education/src/test/java/com/vayunmathur/education/platform/AppBackupAgentTest.ktnew file mode 100644index 0000000000..1dac59b023--- /dev/null+++ b/education/src/test/java/com/vayunmathur/education/platform/AppBackupAgentTest.kt@@ -0,0 +1,39 @@+package com.vayunmathur.education.platform++import android.content.Context+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.education.data.DB_NAME+import com.vayunmathur.education.data.educationDbConfigs+import com.vayunmathur.library.util.DatabaseHelper+import com.vayunmathur.testsupport.FakeAndroidKeyStore+import org.junit.BeforeClass+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull++@RunWith(RobolectricTestRunner::class)+class AppBackupAgentTest {++    companion object {+        @BeforeClass @JvmStatic fun installKeyStore() = FakeAndroidKeyStore.install()+    }++    @Test fun backupAgentDbCodec() {+        val agent = AppBackupAgent()+        assertNotNull(agent.dbCodec)+    }++    @Test fun backupAgentDatastores() {+        assertEquals(listOf("datastore_default"), AppBackupAgent().datastoreNames)+    }++    @Test fun dbConfigsNamePassphrase() {+        val context = ApplicationProvider.getApplicationContext<Context>()+        DatabaseHelper(context).storePassphrase("education-backup-test-passphrase")+        val configs = educationDbConfigs(context)+        assertEquals(listOf(DB_NAME to "education-backup-test-passphrase"), configs)+        assertEquals("education-db", DB_NAME)+    }+}