Compare commits

...
10 changed files with 162 additions and 13 deletions

No files matched your search

@@ -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")
@@ -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) {
@@ -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()
}
@@ -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")
@@ -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()
@@ -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,
@@ -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<String>?): MutableSet<String>? =
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 <T> 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))
}
}
@@ -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) }
@@ -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"])
}
}
+5 -3
View File
@@ -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