Modern Apps git

← Modern-Apps.git

setupwizard: survive provisioning writes without privapp grant

Commit
fdc1abd0a94cd2527395887f0dee2b719de5a598
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:39 UTC (28 days ago)
Parents
05b6fca
markSetupComplete() wrote Settings.Global.DEVICE_PROVISIONED and
Secure user_setup_complete with no guard, the only writes in
SystemSetup that could throw. Both need a privapp whitelist grant;
without it putInt throws SecurityException, which aborted
SetupViewModel.finishSetup() mid-sequence: task removal, finish(),
the status-bar restore and disableSelf() were all skipped, leaving
the wizard on screen and still winning HOME resolution. The
MainActivity relaunch path calls finishSetup() again when
isUserSetupComplete() is already true, so the same throw repeated
on every launch - a device that never finishes first boot.

The class contract is 'Nothing here throws', and the neighbouring
enableAutomaticTime() and seedDisplayMirroring() already degrade to
a Log.status on failure. Wrap each provisioning write in its own
runCatching with the same treatment, so one denied write cannot
block the other or the teardown that follows.

Adds SystemSetupTest: markSetupComplete returns normally and leaves
both provisioning flags set under Robolectric.

Verified with :setupwizard:testDevUnitTest (all green, including
the new test).

Diff

setupwizard/src/main/java/com/vayunmathur/setupwizard/platform/SystemSetup.kt
diff --git a/setupwizard/src/main/java/com/vayunmathur/setupwizard/platform/SystemSetup.kt b/setupwizard/src/main/java/com/vayunmathur/setupwizard/platform/SystemSetup.ktindex 7cfdd2d715..10e2fe66a5 100644--- a/setupwizard/src/main/java/com/vayunmathur/setupwizard/platform/SystemSetup.kt+++ b/setupwizard/src/main/java/com/vayunmathur/setupwizard/platform/SystemSetup.kt@@ -320,16 +320,25 @@ class SystemSetup(context: Context) {     /**      * The write that ends setup. `DEVICE_PROVISIONED` is global and belongs to the device, so      * only the owner sets it; `user_setup_complete` is per user and every user sets their own.+     *+     * Best effort like the clock and display seeds above: both writes need a privapp+     * whitelist grant, and a build that has not granted it should lose the provisioning+     * flags, not abort setup after the user already pressed finish - the exception would+     * skip task removal, `finish()`, the status-bar restore and `disableSelf()`.      */     fun markSetupComplete(activity: Activity) {         if (isPrimaryUser) {-            Settings.Global.putInt(-                activity.contentResolver,-                Settings.Global.DEVICE_PROVISIONED,-                1,-            )+            runCatching {+                Settings.Global.putInt(+                    activity.contentResolver,+                    Settings.Global.DEVICE_PROVISIONED,+                    1,+                )+            }.onFailure { Log.status(TAG, "could not mark device provisioned", it) }         }-        Settings.Secure.putInt(activity.contentResolver, SETTING_USER_SETUP_COMPLETE, 1)+        runCatching {+            Settings.Secure.putInt(activity.contentResolver, SETTING_USER_SETUP_COMPLETE, 1)+        }.onFailure { Log.status(TAG, "could not mark user setup complete", it) }     }      /**
setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SystemSetupTest.kt
diff --git a/setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SystemSetupTest.kt b/setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SystemSetupTest.ktnew file mode 100644index 0000000000..287447a17b--- /dev/null+++ b/setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SystemSetupTest.kt@@ -0,0 +1,36 @@+package com.vayunmathur.setupwizard.platform++import android.app.Activity+import android.app.Application+import android.provider.Settings+import androidx.test.core.app.ApplicationProvider+import org.junit.runner.RunWith+import org.robolectric.Robolectric+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class SystemSetupTest {+    private val app get() = ApplicationProvider.getApplicationContext<Application>()++    @Test fun `markSetupComplete writes provisioning flags without throwing`() {+        val system = SystemSetup(app)+        val activity = Robolectric.buildActivity(Activity::class.java).setup().get()+        // Must return normally: finishSetup() aborts mid-sequence (no task removal,+        // no finish(), no status-bar restore, no disableSelf()) if this throws.+        system.markSetupComplete(activity)+        assertTrue(system.isUserSetupComplete())+        if (system.isPrimaryUser) {+            assertEquals(+                1,+                Settings.Global.getInt(+                    activity.contentResolver,+                    Settings.Global.DEVICE_PROVISIONED,+                    0,+                ),+            )+        }+    }+}