From 6c75e178703c0c85151d0746a0b166a82e8a4d56 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Fri, 27 Nov 2026 17:22:48 +0000 Subject: [PATCH] fix(prefs): a setting stored with the wrong type no longer stops DroidDeck from opening --- .../com/droiddeck/launcher/agent/AgentEnv.kt | 13 ++++ .../launcher/session/BuildWatermarkSetting.kt | 2 +- .../launcher/session/EnhancedDiagnostics.kt | 11 ++- .../droiddeck/launcher/session/OfflineMode.kt | 2 +- .../launcher/session/QuestExperimental.kt | 2 +- .../launcher/session/SessionPrefs.kt | 2 +- .../launcher/session/TolerantPrefs.kt | 36 +++++++++ .../launcher/agent/AgentSecurityTest.kt | 22 ++++++ .../launcher/session/TolerantPrefsTest.kt | 77 +++++++++++++++++++ docs/agent-control.md | 8 +- 10 files changed, 162 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/session/TolerantPrefs.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/session/TolerantPrefsTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentEnv.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentEnv.kt index b1b4406520..cf290cf393 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentEnv.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentEnv.kt @@ -235,6 +235,10 @@ object AgentPrefs { if (e is AgentException) throw e throw AgentException("INVALID_PREF", "The value is not a $type") } + val stored = previous?.let(::typeOf) + if (type != "remove" && stored != null && stored != type) { + throw AgentException("INVALID_PREF", "$file/$key is stored as $stored; set it with type $stored, or remove it first") + } if (!editor.commit()) throw AgentException("PREF_WRITE_FAILED", "Could not write $file/$key") return JSONObject().put("file", file).put("key", key).put("type", type) .put("previous", previous ?: JSONObject.NULL).put("value", prefs.all[key] ?: JSONObject.NULL) @@ -247,6 +251,15 @@ object AgentPrefs { return JSONObject().put("file", file).put("values", JSONObject(all.mapValues { it.value ?: JSONObject.NULL })) } + private fun typeOf(value: Any): String = when (value) { + is Boolean -> "bool" + is Int -> "int" + is Long -> "long" + is Float -> "float" + is String -> "string" + else -> "string set" + } + private fun validateFile(file: String) { if (!Regex("[A-Za-z0-9_-][A-Za-z0-9._-]{0,127}").matches(file)) { throw AgentException("INVALID_PREF", "Preference files must be simple names") diff --git a/app/src/main/java/com/droiddeck/launcher/session/BuildWatermarkSetting.kt b/app/src/main/java/com/droiddeck/launcher/session/BuildWatermarkSetting.kt index cf31b02404..8f606e9345 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/BuildWatermarkSetting.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/BuildWatermarkSetting.kt @@ -24,7 +24,7 @@ internal object BuildWatermarkSetting { file(context).readFully().decodeToString() != "off" } } catch (_: java.io.IOException) { - context.getSharedPreferences("session", Context.MODE_PRIVATE).getBoolean("buildWatermark", true) + TolerantPrefs.session(context).getBoolean("buildWatermark", true) } fun setEnabled(context: Context, on: Boolean) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/EnhancedDiagnostics.kt b/app/src/main/java/com/droiddeck/launcher/session/EnhancedDiagnostics.kt index 2dfbacc496..67aaff0a50 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/EnhancedDiagnostics.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/EnhancedDiagnostics.kt @@ -11,16 +11,15 @@ object EnhancedDiagnostics { const val MANIFEST_FILE = "enhanced-diagnostics.json" private const val TAG = "EnhancedDiagnostics" - private const val PREFS = "session" private const val ARMED = "enhancedDiagnosticsNext" private const val ACTIVE_RECORDS = "enhancedDiagnosticsActiveSessions" private const val SCHEMA = 1 fun isArmed(context: Context): Boolean = - context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getBoolean(ARMED, false) + TolerantPrefs.session(context).getBoolean(ARMED, false) fun setArmed(context: Context, armed: Boolean): Boolean = - context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().putBoolean(ARMED, armed).commit() + TolerantPrefs.session(context).edit().putBoolean(ARMED, armed).commit() fun isActive(context: Context, folder: File?): Boolean = activeAt(context, folder) != null @@ -84,7 +83,7 @@ object EnhancedDiagnostics { if (!File(known).isDirectory) records.remove(known) } records.put(path, activatedAt) - return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit() + return TolerantPrefs.session(context).edit() .putString(ACTIVE_RECORDS, records.toString()) .putBoolean(ARMED, false) .commit() @@ -108,11 +107,11 @@ object EnhancedDiagnostics { } private fun records(context: Context): JSONObject { - val text = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(ACTIVE_RECORDS, null) + val text = TolerantPrefs.session(context).getString(ACTIVE_RECORDS, null) return runCatching { JSONObject(text ?: "{}") }.getOrDefault(JSONObject()) } private fun saveRecords(context: Context, records: JSONObject): Boolean = - context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit() + TolerantPrefs.session(context).edit() .putString(ACTIVE_RECORDS, records.toString()).commit() } diff --git a/app/src/main/java/com/droiddeck/launcher/session/OfflineMode.kt b/app/src/main/java/com/droiddeck/launcher/session/OfflineMode.kt index 095f81544e..606a12fb65 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/OfflineMode.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/OfflineMode.kt @@ -17,7 +17,7 @@ object OfflineMode { private val PERSONA = Regex(""""PersonaName"\s*"([^"]*)"""") private const val EVAL_TIMEOUT_MS = 10_000L - private fun prefs(context: Context) = context.getSharedPreferences("session", Context.MODE_PRIVATE) + private fun prefs(context: Context) = TolerantPrefs.session(context) private fun loginUsers(root: File) = File(root, "root/.local/share/Steam/config/loginusers.vdf") diff --git a/app/src/main/java/com/droiddeck/launcher/session/QuestExperimental.kt b/app/src/main/java/com/droiddeck/launcher/session/QuestExperimental.kt index 3fe95fc815..98fdade7c7 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/QuestExperimental.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/QuestExperimental.kt @@ -42,7 +42,7 @@ object QuestExperimental { fun initialize(context: Context) { if (!isSupported()) return - val prefs = context.getSharedPreferences("session", Context.MODE_PRIVATE) + val prefs = TolerantPrefs.session(context) val defaultsKey = if (isQuest2()) "quest2DefaultsV1" else "questDefaultsV1" if (!prefs.getBoolean(defaultsKey, false)) { prefs.edit() diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 1d2d9cc871..30763a61a3 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -48,7 +48,7 @@ object SessionPrefs { fun backActionsOrder(inverted: Boolean): Int = if (inverted) BACK_QAM_THEN_MENU else BACK_MENU_THEN_QAM - private fun prefs(context: Context) = context.getSharedPreferences("session", Context.MODE_PRIVATE) + private fun prefs(context: Context) = TolerantPrefs.session(context) val orientationOptions = listOf( ActivityInfo.SCREEN_ORIENTATION_USER to R.string.setup_launcher_orientation_auto, diff --git a/app/src/main/java/com/droiddeck/launcher/session/TolerantPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/TolerantPrefs.kt new file mode 100644 index 0000000000..4c243f0438 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/session/TolerantPrefs.kt @@ -0,0 +1,36 @@ +package com.droiddeck.launcher.session + +import android.content.Context +import android.content.SharedPreferences + +internal class TolerantPrefs(private val base: SharedPreferences) : SharedPreferences by base { + override fun getString(key: String?, defValue: String?): String? = + read(key, defValue, { base.getString(key, defValue) }) { it } + + override fun getStringSet(key: String?, defValues: MutableSet?): MutableSet? = + try { base.getStringSet(key, defValues) } catch (_: ClassCastException) { defValues } + + override fun getInt(key: String?, defValue: Int): Int = + read(key, defValue, { base.getInt(key, defValue) }) { it.toIntOrNull() } + + override fun getLong(key: String?, defValue: Long): Long = + read(key, defValue, { base.getLong(key, defValue) }) { it.toLongOrNull() } + + override fun getFloat(key: String?, defValue: Float): Float = + read(key, defValue, { base.getFloat(key, defValue) }) { it.toFloatOrNull() } + + override fun getBoolean(key: String?, defValue: Boolean): Boolean = + read(key, defValue, { base.getBoolean(key, defValue) }) { it.toBooleanStrictOrNull() } + + private inline fun read(key: String?, defValue: T, typed: () -> T, parse: (String) -> T?): T = + try { + typed() + } catch (_: ClassCastException) { + base.all[key]?.toString()?.let(parse) ?: defValue + } + + companion object { + fun session(context: Context): SharedPreferences = + TolerantPrefs(context.getSharedPreferences("session", Context.MODE_PRIVATE)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/agent/AgentSecurityTest.kt b/app/src/test/java/com/droiddeck/launcher/agent/AgentSecurityTest.kt index 98e2722386..ef439a7e69 100644 --- a/app/src/test/java/com/droiddeck/launcher/agent/AgentSecurityTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/agent/AgentSecurityTest.kt @@ -333,6 +333,28 @@ class AgentSecurityTest { assertFalse(error.message!!.contains("synthetic-private-value")) } + @Test fun preferencesKeepTheTypeAStoredKeyHas() { + AgentAccess.setCommandsEnabled(context, true) + val session = context.getSharedPreferences("session", Context.MODE_PRIVATE) + session.edit().clear().putBoolean("gameDebugLogsEnabled", false).putString("testText", "first").commit() + + val error = refused("INVALID_PREF") { AgentPrefs.set(context, "session", "gameDebugLogsEnabled", "string", "true") } + assertTrue(error.message!!.contains("bool")) + assertEquals(false, session.all["gameDebugLogsEnabled"]) + refused("INVALID_PREF") { AgentPrefs.set(context, "session", "testText", "bool", "true") } + assertEquals("first", session.all["testText"]) + + assertEquals(true, AgentPrefs.set(context, "session", "gameDebugLogsEnabled", "bool", "true").get("value")) + assertEquals("second", AgentPrefs.set(context, "session", "testText", "string", "second").get("value")) + assertEquals("fresh", AgentPrefs.set(context, "session", "unsetKey", "string", "fresh").get("value")) + + session.edit().putString("gameDebugLogsEnabled", "true").commit() + AgentPrefs.set(context, "session", "gameDebugLogsEnabled", "remove", null) + assertEquals(false, AgentPrefs.set(context, "session", "gameDebugLogsEnabled", "bool", "false").get("value")) + assertEquals(false, session.all["gameDebugLogsEnabled"]) + session.edit().clear().commit() + } + @Test fun overrideRefusesLinksAndTraversalNames() { AgentAccess.setCommandsEnabled(context, true) for (name in listOf(".", "..", "../gamescope")) refused("INVALID_NAME") { AgentEnv.override(context, name) } diff --git a/app/src/test/java/com/droiddeck/launcher/session/TolerantPrefsTest.kt b/app/src/test/java/com/droiddeck/launcher/session/TolerantPrefsTest.kt new file mode 100644 index 0000000000..3012e1aaaf --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/TolerantPrefsTest.kt @@ -0,0 +1,77 @@ +package com.droiddeck.launcher.session + +import android.app.Application +import android.content.Context +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [34], manifest = Config.NONE, application = Application::class) +class TolerantPrefsTest { + private val context get() = RuntimeEnvironment.getApplication() + private val raw get() = context.getSharedPreferences("session", Context.MODE_PRIVATE) + + @Before fun reset() { + raw.edit().clear().commit() + } + + @After fun cleanup() { + raw.edit().clear().commit() + } + + @Test fun aSwitchStoredAsTextReadsAsItsValue() { + raw.edit().putString("gameDebugLogsEnabled", "true").putString("logs", "false").commit() + + assertTrue(SessionPrefs.gameDebugLogsEnabled(context)) + assertFalse(SessionPrefs.logsEnabled(context)) + } + + @Test fun unreadableTextFallsBackToTheDefault() { + raw.edit().putString("gameDebugLogsEnabled", "yes").putString("logs", "").commit() + + assertFalse(SessionPrefs.gameDebugLogsEnabled(context)) + assertTrue(SessionPrefs.logsEnabled(context)) + } + + @Test fun everyTypedReadToleratesAnotherStoredType() { + raw.edit() + .putString("count", "7") + .putString("big", "8000000000") + .putString("ratio", "1.5") + .putBoolean("label", true) + .putInt("names", 3) + .commit() + val prefs = TolerantPrefs.session(context) + + assertEquals(7, prefs.getInt("count", 0)) + assertEquals(8_000_000_000L, prefs.getLong("big", 0L)) + assertEquals(1.5f, prefs.getFloat("ratio", 0f), 0f) + assertEquals("true", prefs.getString("label", null)) + assertEquals(setOf("x"), prefs.getStringSet("names", mutableSetOf("x"))) + assertEquals(0, prefs.getInt("ratio", 0)) + } + + @Test fun correctlyTypedSettingsAreUnchanged() { + raw.edit().putBoolean("gameDebugLogsEnabled", true).putBoolean("logs", false).putString("theme", "paper").commit() + + assertTrue(SessionPrefs.gameDebugLogsEnabled(context)) + assertFalse(SessionPrefs.logsEnabled(context)) + assertEquals("paper", SessionPrefs.theme(context)) + } + + @Test fun aSessionEndRepairsTheStoredType() { + raw.edit().putString("gameDebugLogsEnabled", "true").commit() + + SessionPrefs.expireGameDebugLogs(context) + + assertEquals(false, raw.all["gameDebugLogsEnabled"]) + } +} diff --git a/docs/agent-control.md b/docs/agent-control.md index ac097234cf..f911e781b4 100644 --- a/docs/agent-control.md +++ b/docs/agent-control.md @@ -43,9 +43,10 @@ Player controls and observations work over authorized ADB on any build: `state`, **Setup > Developer > Debugging tools** is off by default in every build, including debug builds. It enables `run`, `guest`, `cdp`, all `env` operations, `override`, `prefs`, and `glasses raw`. The toggle switches directly without a confirmation dialog. The bridge refuses to change it through `ui` -or preference writes. The existing preference key, `setting-agent-commands` test tag, JSON command -names, and `AGENT_COMMANDS_DISABLED` error code are unchanged. Refusals exit 7; `droiddeckctl access` -reports whether commands are enabled. +or preference writes. `prefs set` refuses to change the type a key is stored as; remove the key +first. The existing preference key, `setting-agent-commands` test tag, JSON command names, and +`AGENT_COMMANDS_DISABLED` error code are unchanged. Refusals exit 7; `droiddeckctl access` reports +whether commands are enabled. Disabling the toggle clears queued and persistent agent environment settings and installed binary overrides. Disabled settings cannot apply at the next session start. Already running commands and @@ -165,6 +166,7 @@ tools/droiddeckctl proot-profile disable tools/droiddeckctl proot-profile report ./session.log --sort wall tools/droiddeckctl prefs get session tools/droiddeckctl prefs set session some_key true --type bool +tools/droiddeckctl prefs set session some_key --type remove tools/droiddeckctl logs latest ./session-artifacts tools/droiddeckctl screenshot ./screen.png --display 4