diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index f7ff85e2c..dc35ec198 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -937,6 +937,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { gestureGame = nextGame refreshGestureConfig() } + if (secondScreenDisplays.isNotEmpty()) kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) { + runCatching { com.droiddeck.launcher.session.TelemetryOutput.applyAutomatic(this@SessionActivity, nextGame) } + } } LaunchedEffect(drawerOpen, pcKeyboardOpen, achievementsOpen, gestureSettingsOpen, pipUi, SessionState.suspended) { if (drawerOpen || pcKeyboardOpen || achievementsOpen || gestureSettingsOpen || pipUi || SessionState.suspended) { 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 d3548c577..98f695e5c 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -106,6 +106,20 @@ object SessionPrefs { prefs(context).edit().putString("bottomPage", page).apply() } + /** In-game stats "Send dashboard data" Automatic: on unless the user switched it off. */ + fun telemetryAuto(context: Context): Boolean = prefs(context).getBoolean("telemetryAuto", true) + + fun setTelemetryAuto(context: Context, on: Boolean) { + prefs(context).edit().putBoolean("telemetryAuto", on).apply() + } + + /** Games the user switched off by hand, as TelemetryOutput keys; Automatic leaves them alone. */ + fun telemetryOff(context: Context): Set = prefs(context).getStringSet("telemetryOff", null)?.toSet().orEmpty() + + fun setTelemetryOff(context: Context, keys: Set) { + prefs(context).edit().putStringSet("telemetryOff", keys.toSet()).apply() + } + fun dualScreenLauncher(context: Context): Boolean = prefs(context).getBoolean("dualScreenLauncher", true) fun setDualScreenLauncher(context: Context, on: Boolean) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt index b78b1896c..55efc2a5a 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt @@ -26,7 +26,7 @@ object SettingsReset { "upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias", "steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound", "bootMovieVolume", "bootMovieSeconds", "bootMovieApplyNext", "bootMovieSteamSeen", - "questDefaultsV1", + "questDefaultsV1", "telemetryAuto", "telemetryOff", ) internal val SESSION_PREFIXES = listOf( diff --git a/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt b/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt index eba41fb9a..afd60b02d 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/TelemetryOutput.kt @@ -1,5 +1,7 @@ package com.droiddeck.launcher.session +import android.content.Context +import com.droiddeck.launcher.frontend.Library import java.io.File /** @@ -60,6 +62,21 @@ object TelemetryOutput { true }.getOrDefault(false) + /** Automatic: turns the output on for every found game the user has not switched off and that is not running. */ + fun applyAutomatic(context: Context, running: String?) { + if (!SessionPrefs.telemetryAuto(context)) return + automaticTargets(scan(Library.compatdataDirs(context)), SessionPrefs.telemetryOff(context), running).forEach { set(it, true) } + } + + internal fun key(game: Game) = "${game.prefixId}/${game.name}" + + internal fun automaticTargets(games: List, off: Set, running: String?): List = + games.filter { !it.on && key(it) !in off && !isRunning(it.prefixId, running) } + + /** Games DroidDeck changed, which go back to their own setting when Automatic is switched off. */ + internal fun restoreTargets(games: List, running: String?): List = + games.filter { File(it.file.path + ORIGINAL_SUFFIX).isFile && !isRunning(it.prefixId, running) } + /** Whether [running] (the focused Steam app id, or another key for non-Steam programs) may be the game in [prefixId]. */ internal fun isRunning(prefixId: String, running: String?): Boolean { val id = running?.toLongOrNull() ?: return running != null diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt index c599b6fd3..b8069a1c1 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt @@ -44,6 +44,7 @@ import com.droiddeck.launcher.hud.MangoReadings import com.droiddeck.launcher.runtime.LinuxRuntime import com.droiddeck.launcher.session.GameStatusFiles import com.droiddeck.launcher.session.GameTelemetry +import com.droiddeck.launcher.session.SessionPrefs import com.droiddeck.launcher.session.SessionState import com.droiddeck.launcher.session.Telemetry import com.droiddeck.launcher.session.TelemetryOutput @@ -89,6 +90,7 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } var running by remember { mutableStateOf(null) } var outputFailed by remember { mutableStateOf(null) } var rescan by remember { mutableIntStateOf(0) } + var auto by remember { mutableStateOf(SessionPrefs.telemetryAuto(context)) } val latest = remember { AtomicReference?>(null) } var race by remember { mutableStateOf(null) } var fpsNow by remember { mutableStateOf(null) } @@ -208,23 +210,30 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } fontSize = 14.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(horizontal = 4.dp)) } if (outputs.isNotEmpty() || menuGames.isNotEmpty()) TelemetryCard(stringResource(R.string.bottom_telemetry_send_title)) { + if (outputs.isNotEmpty()) SwitchRow(stringResource(R.string.bottom_telemetry_send_auto), stringResource(R.string.bottom_telemetry_send_auto_hint), auto, true) { want -> + scope.launch { + withContext(Dispatchers.IO) { + SessionPrefs.setTelemetryAuto(context, want) + if (want) TelemetryOutput.applyAutomatic(context, running) + else TelemetryOutput.restoreTargets(outputs, running).forEach { TelemetryOutput.set(it, false) } + } + auto = want + rescan++ + } + } for (game in outputs) { val locked = TelemetryOutput.isRunning(game.prefixId, running) - Row( - Modifier.fillMaxWidth().toggleable(game.on, enabled = !locked, role = Role.Switch) { want -> - scope.launch { - val ok = withContext(Dispatchers.IO) { TelemetryOutput.set(game, want) } - outputFailed = if (ok) null else game.name - rescan++ + SwitchRow(game.name, if (locked) stringResource(R.string.bottom_telemetry_send_close, game.name) else null, game.on, !locked) { want -> + scope.launch { + val ok = withContext(Dispatchers.IO) { + val key = TelemetryOutput.key(game) + val off = SessionPrefs.telemetryOff(context) + SessionPrefs.setTelemetryOff(context, if (want) off - key else off + key) + TelemetryOutput.set(game, want) } - }.padding(vertical = 4.dp), - verticalAlignment = Alignment.CenterVertically, - ) { - Column(Modifier.weight(1f).padding(end = 12.dp)) { - Text(game.name, fontSize = 15.sp, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) - if (locked) Text(stringResource(R.string.bottom_telemetry_send_close, game.name), fontSize = 12.sp, color = colors.onSurfaceVariant) + outputFailed = if (ok) null else game.name + rescan++ } - Switch(game.on, null, enabled = !locked) } } for (game in menuGames) { @@ -268,6 +277,21 @@ private fun DeviceRow(fps: Float?, s: HudMetrics.Snapshot?) { } } +@Composable +private fun SwitchRow(title: String, hint: String?, checked: Boolean, enabled: Boolean, onChange: (Boolean) -> Unit) { + val colors = MaterialTheme.colorScheme + Row( + Modifier.fillMaxWidth().toggleable(checked, enabled = enabled, role = Role.Switch, onValueChange = onChange).padding(vertical = 4.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Column(Modifier.weight(1f).padding(end = 12.dp)) { + Text(title, fontSize = 15.sp, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) + if (hint != null) Text(hint, fontSize = 12.sp, color = colors.onSurfaceVariant) + } + Switch(checked, null, enabled = enabled) + } +} + @Composable private fun TelemetryTile(label: String, value: String, modifier: Modifier) { val colors = MaterialTheme.colorScheme diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a2f1494df..c6bd42901 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1892,6 +1892,8 @@ Clear Screenshots Send dashboard data + Automatic + Turns it on for every game listed here, including games found later. Games you switch off stay off. This changes the game\'s own data output setting. It takes effect the next time the game starts; turning it off puts the old setting back. Close %1$s to change this. Could not change the settings of %1$s. diff --git a/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt b/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt index 1512ce4a1..597820af8 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/TelemetryOutputTest.kt @@ -77,6 +77,30 @@ class TelemetryOutputTest { assertTrue(TelemetryOutput.isRunning("2848447563", "program:/games/x.exe")) } + @Test fun automaticSkipsGamesThatAreOnSwitchedOffOrRunning() { + val f = File("x") + val showdownGame = TelemetryOutput.Game("DiRT Showdown", "2848447563", f, on = false) + val rally = TelemetryOutput.Game("DiRT Rally 2.0", "690790", f, on = false) + val grid = TelemetryOutput.Game("GRID", "703860", f, on = true) + val games = listOf(showdownGame, rally, grid) + assertEquals(listOf(showdownGame, rally), TelemetryOutput.automaticTargets(games, emptySet(), null)) + assertEquals(listOf(rally), TelemetryOutput.automaticTargets(games, setOf("2848447563/DiRT Showdown"), null)) + assertEquals(listOf(showdownGame), TelemetryOutput.automaticTargets(games, emptySet(), "690790")) + assertEquals("690790/DiRT Rally 2.0", TelemetryOutput.key(rally)) + } + + @Test fun switchingAutomaticOffRestoresOnlyGamesDroidDeckChanged() { + val compatdata = tmp.newFolder("steamapps", "compatdata") + val changed = configFile(compatdata, "2848447563", "DiRT Showdown").apply { writeText(showdown) } + val own = configFile(compatdata, "690790", "DiRT Rally 2.0") + .apply { writeText("") } + assertTrue(TelemetryOutput.set(TelemetryOutput.scan(listOf(compatdata)).first { it.name == "DiRT Showdown" }, true)) + val targets = TelemetryOutput.restoreTargets(TelemetryOutput.scan(listOf(compatdata)), null) + assertEquals(listOf(changed), targets.map { it.file }) + assertTrue(own.isFile) + assertTrue(TelemetryOutput.restoreTargets(TelemetryOutput.scan(listOf(compatdata)), "2848447563").isEmpty()) + } + private fun configFile(compatdata: File, id: String, game: String): File = File(compatdata, "$id/pfx/drive_c/users/steamuser/Documents/My Games/$game/hardwaresettings/hardware_settings_config.xml") .apply { parentFile!!.mkdirs() }