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 98239f8eb0..cf31b02404 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/BuildWatermarkSetting.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/BuildWatermarkSetting.kt @@ -39,6 +39,10 @@ internal object BuildWatermarkSetting { throw error } } + notifyChanged(context) + } + + fun notifyChanged(context: Context) { context.sendBroadcast(Intent(CHANGED).setPackage(context.packageName)) } } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt index 69cb7d4fae..d8117ae714 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt @@ -26,6 +26,9 @@ object SessionPaths { @Volatile private var startedAt = 0L + @Volatile + private var debugLogsContext: Context? = null + @Synchronized fun beginOrCurrent(context: Context, label: String? = null): File { dir?.let { if (it.isDirectory) return it } @@ -44,6 +47,7 @@ object SessionPaths { startedAt = System.currentTimeMillis() if (!made.isDirectory && !made.mkdirs()) Log.e(TAG, "could not create $made") dir = made + debugLogsContext = if (SessionPrefs.gameDebugLogsEnabled(context)) context.applicationContext else null if (enhanced && !EnhancedDiagnostics.consumeInto(context, made)) { Log.e(TAG, "could not activate enhanced diagnostics for $made") } @@ -65,6 +69,9 @@ object SessionPaths { val taken = dir dir = null if (taken != null) ended = taken + val expiring = debugLogsContext + debugLogsContext = null + if (taken != null && expiring != null) SessionPrefs.expireGameDebugLogs(expiring) return taken } 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 b5794eff94..5d545c0b66 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -1228,7 +1228,7 @@ object SessionPrefs { oldValue = logsEnabled(context), newValue = on, newEffective = gameDebugLogsEffective(context, on, gameDebugLogsEnabled(context)), - ) + ).also { if (it) BuildWatermarkSetting.notifyChanged(context) } } fun gameDebugLogsEnabled(context: Context): Boolean = @@ -1241,7 +1241,13 @@ object SessionPrefs { oldValue = gameDebugLogsEnabled(context), newValue = on, newEffective = gameDebugLogsEffective(context, logsEnabled(context), on), - ) + ).also { if (it) BuildWatermarkSetting.notifyChanged(context) } + } + + /** The setting lasts for the session that started with it on; the next session's start republishes the environment. */ + internal fun expireGameDebugLogs(context: Context) { + prefs(context).edit().putBoolean("gameDebugLogsEnabled", false).apply() + runCatching { BuildWatermarkSetting.notifyChanged(context) } } private fun setLogPreference( diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt b/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt index 446420806c..c916b3f7ad 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BuildWatermark.kt @@ -81,10 +81,13 @@ object BuildWatermark { if (activity is QuestXrActivity) return val decor = activity.window.decorView as? ViewGroup ?: return decor.findViewWithTag(TAG)?.let(decor::removeView) - if (!com.droiddeck.launcher.session.SessionPrefs.buildWatermarkEnabled(activity)) return - val lab = com.droiddeck.launcher.lab.LabOverlay.active?.let { "LAB | ${truncate(it.label)}" } - val build = lineProvider().orEmpty() - val content = (listOfNotNull(lab) + build).ifEmpty { return } + val prefs = com.droiddeck.launcher.session.SessionPrefs + val debugLogs = if (prefs.logsEnabled(activity) && prefs.gameDebugLogsEnabled(activity)) DEBUG_LOGS_LINE else null + val build = if (prefs.buildWatermarkEnabled(activity)) { + listOfNotNull(com.droiddeck.launcher.lab.LabOverlay.active?.let { "LAB | ${truncate(it.label)}" }) + + lineProvider().orEmpty() + } else emptyList() + val content = (build + listOfNotNull(debugLogs)).ifEmpty { return } val margin = (8 * activity.resources.displayMetrics.density).toInt() decor.addView( MarkView(activity, content).apply { tag = TAG }, @@ -96,6 +99,8 @@ object BuildWatermark { ) } + internal const val DEBUG_LOGS_LINE = "GAME DEBUG LOGS ON | slows games" + private const val TAG = "build-watermark" private class MarkView(context: Context, private val lines: List) : View(context) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cc7f0aca34..e5d43de074 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -966,7 +966,7 @@ Games and compatibility All bundles Game debug logs - Include Proton, Wine and graphics diagnostics on the next game launch. Can reduce performance and use more storage. + Include Proton, Wine and graphics diagnostics on the next game launch. Turns itself off when that session ends. Can reduce performance and use more storage. Restart this session to save game debug logs Couldn\'t update logging. Check the switch and try again. Debugging tools diff --git a/app/src/test/java/com/droiddeck/launcher/session/GameDebugLogsExpiryTest.kt b/app/src/test/java/com/droiddeck/launcher/session/GameDebugLogsExpiryTest.kt new file mode 100644 index 0000000000..9642b27462 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/GameDebugLogsExpiryTest.kt @@ -0,0 +1,88 @@ +package com.droiddeck.launcher.session + +import android.app.Activity +import android.app.Application +import android.content.Context +import android.os.Looper +import android.view.View +import com.droiddeck.launcher.ui.BuildWatermark +import org.junit.After +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config +import org.robolectric.annotation.ConscryptMode +import org.robolectric.annotation.GraphicsMode +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@ConscryptMode(ConscryptMode.Mode.OFF) +@GraphicsMode(GraphicsMode.Mode.LEGACY) +@Config(sdk = [34], manifest = Config.NONE, application = Application::class) +class GameDebugLogsExpiryTest { + private val context get() = RuntimeEnvironment.getApplication() + private val prefs get() = context.getSharedPreferences("session", Context.MODE_PRIVATE) + + @Before fun reset() { + SessionPaths.take() + prefs.edit().clear().commit() + listOf("build-watermark", "build-watermark.lock").forEach { File(context.filesDir, it).delete() } + } + + @After fun end() { + SessionPaths.take() + } + + private fun setDebugLogs(on: Boolean) = prefs.edit().putBoolean("gameDebugLogsEnabled", on).commit() + + @Test fun settingTurnsOffWhenTheSessionItStartedWithEnds() { + setDebugLogs(true) + SessionPaths.beginOrCurrent(context, "steam") + assertTrue(SessionPrefs.gameDebugLogsEnabled(context)) + SessionPaths.take() + assertFalse(SessionPrefs.gameDebugLogsEnabled(context)) + } + + @Test fun settingStaysOnForTheSessionRunningWhenItWasEnabled() { + SessionPaths.beginOrCurrent(context, "steam") + setDebugLogs(true) + SessionPaths.take() + assertTrue(SessionPrefs.gameDebugLogsEnabled(context)) + SessionPaths.beginOrCurrent(context, "steam") + SessionPaths.take() + assertFalse(SessionPrefs.gameDebugLogsEnabled(context)) + } + + @Test fun settingThatWasOffStaysOff() { + SessionPaths.beginOrCurrent(context, "steam") + SessionPaths.take() + assertFalse(SessionPrefs.gameDebugLogsEnabled(context)) + } + + @Test fun badgeShowsWhileOnEvenWithTheBuildWatermarkOff() { + BuildWatermark.install(context) + SessionPrefs.setBuildWatermarkEnabled(context, false) + val activity = Robolectric.buildActivity(Activity::class.java).setup() + try { + assertNull(activity.get().window.decorView.findViewWithTag("build-watermark")) + setDebugLogs(true) + BuildWatermarkSetting.notifyChanged(context) + shadowOf(Looper.getMainLooper()).idle() + assertNotNull(activity.get().window.decorView.findViewWithTag("build-watermark")) + SessionPaths.beginOrCurrent(context, "steam") + SessionPaths.take() + shadowOf(Looper.getMainLooper()).idle() + assertNull(activity.get().window.decorView.findViewWithTag("build-watermark")) + } finally { + activity.pause().stop().destroy() + } + } +}