diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 4910015614..77031187a8 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -83,6 +83,12 @@ class SessionService : Service() { private val steamDownloadMonitor = SteamDownloadMonitor() private var downloadMonitorTask: Runnable? = null private var downloadActive = false + /** + * The runtime is fetching the Steam client itself (a first run, a repair), before anything is on + * screen. That download keeps the session awake whatever the downloads setting says: paused, a + * first run never gets as far as showing Steam. + */ + private var clientInstallActive = false private var suspendDownloadNow = false private var suspendPolicy = SessionPrefs.SUSPEND_MANUAL private var steamDownloadsInBackground = false @@ -229,6 +235,7 @@ class SessionService : Service() { suspendDownloadNow = false steamDownloadMonitor.reset() downloadActive = false + clientInstallActive = false steamSleepToken = null completeNativeSleep() suspendOperationPending = false @@ -550,8 +557,9 @@ class SessionService : Service() { if (!pulse.setSinkSuspended(false)) Log.w(TAG, "could not resume audio sink") }, ) - mainHandler.post { updateSuspendPolicy() } + // Watching first: a first run's client download must be known before anything is paused. updateDownloadMonitoring() + mainHandler.post { updateSuspendPolicy() } } } @@ -1331,8 +1339,7 @@ class SessionService : Service() { } val controller = suspendController ?: return val hidden = !activityVisible || !screenOn - val downloadsBlockingSuspend = steamDownloadsInBackground && SessionState.mode == MODE_STEAM && - suspendPolicy != SessionPrefs.SUSPEND_NEVER && hidden && downloadActive && !suspendDownloadNow + val downloadsBlockingSuspend = downloadsKeepAwake() && hidden && !suspendDownloadNow if (downloadsBlockingSuspend) acquireLocks() if (suspendPolicy != SessionPrefs.SUSPEND_NATIVE || !hidden || downloadsBlockingSuspend) { completeNativeSleep() @@ -1709,8 +1716,7 @@ class SessionService : Service() { .setContentText(getString( when { SessionState.suspended -> R.string.session_notification_paused - downloadActive && steamDownloadsInBackground && - suspendPolicy != SessionPrefs.SUSPEND_NEVER -> R.string.session_notification_downloads + downloadsKeepAwake() -> R.string.session_notification_downloads else -> R.string.session_notification } )) @@ -1718,8 +1724,7 @@ class SessionService : Service() { .apply { if (SessionState.suspended) { addAction(Notification.Action.Builder(null, getString(R.string.resume_session), resume).build()) - } else if (downloadActive && steamDownloadsInBackground && - suspendPolicy != SessionPrefs.SUSPEND_NEVER) { + } else if (downloadsKeepAwake()) { val suspend = PendingIntent.getService( this@SessionService, 3, Intent(this@SessionService, SessionService::class.java).setAction(ACTION_SUSPEND_NOW), @@ -1739,31 +1744,51 @@ class SessionService : Service() { getSystemService(NotificationManager::class.java)?.notify(NOTIFICATION_ID, buildNotification()) } + /** A download is under way that holds the session awake while it is out of sight. */ + private fun downloadsKeepAwake(): Boolean = + downloadActive && (steamDownloadsInBackground || clientInstallActive) && + SessionState.mode == MODE_STEAM && suspendPolicy != SessionPrefs.SUSPEND_NEVER + + /** + * Steam's own downloads with the downloads setting on, and the Steam client's download until the + * session shows its first frame whatever the setting: once Steam is on screen it was fetched. + */ + private fun downloadWatchWanted(): Boolean = + SessionState.running && SessionState.mode == MODE_STEAM && suspendPolicy != SessionPrefs.SUSPEND_NEVER && + (steamDownloadsInBackground || !SessionState.firstFrameSeen) + private fun updateDownloadMonitoring() { - val enabled = SessionState.running && SessionState.mode == MODE_STEAM && - steamDownloadsInBackground && suspendPolicy != SessionPrefs.SUSPEND_NEVER - if (!enabled) { + if (!downloadWatchWanted()) { val wasActive = downloadActive downloadMonitorTask?.let(mainHandler::removeCallbacks) downloadMonitorTask = null steamDownloadMonitor.reset() downloadActive = false + clientInstallActive = false if (wasActive) refreshNotification() return } if (downloadMonitorTask != null) return val poll = object : Runnable { override fun run() { - if (downloadMonitorTask !== this || !SessionState.running || - SessionState.mode != MODE_STEAM || !steamDownloadsInBackground || - suspendPolicy == SessionPrefs.SUSPEND_NEVER) { + if (downloadMonitorTask !== this) return + if (!downloadWatchWanted()) { + downloadMonitorTask = null + updateDownloadMonitoring() + updateSuspendPolicy() return } + val root = LinuxRuntime.rootDir(this@SessionService) val wasActive = downloadActive - downloadActive = steamDownloadMonitor.poll( - LinuxRuntime.rootDir(this@SessionService), + val wasInstalling = clientInstallActive + clientInstallActive = !SessionState.firstFrameSeen && SteamDownloadMonitor.clientInstallPending(root) + downloadActive = clientInstallActive || steamDownloadsInBackground && steamDownloadMonitor.poll( + root, receivedBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid()), ) + if (wasInstalling != clientInstallActive) { + SessionEvents.record("steam.client_download", mapOf("active" to clientInstallActive)) + } if (wasActive != downloadActive) refreshNotification() updateSuspendPolicy() mainHandler.postDelayed(this, DOWNLOAD_POLL_MS) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SteamDownloadMonitor.kt b/app/src/main/java/com/droiddeck/launcher/session/SteamDownloadMonitor.kt index af8515fbb6..d43aa3ab27 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SteamDownloadMonitor.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SteamDownloadMonitor.kt @@ -126,13 +126,26 @@ internal class SteamDownloadMonitor { private fun value(text: String, key: String): String? = Regex("\"$key\"\\s+\"([^\"]*)\"").find(text)?.groupValues?.get(1) - private companion object { - const val MANIFEST_PROGRESS_GRACE_MS = 8_000L + companion object { + /** + * The runtime has yet to fetch the Steam client: a first run, or a repair asked for or begun + * (droiddeck-steam-install drops its stamp as it starts one). That download runs before + * anything is on screen, and a paused session pauses it with the rest. + */ + fun clientInstallPending(runtimeRoot: File): Boolean { + val home = File(runtimeRoot, "root") + val steam = File(home, ".local/share/Steam") + val stamped = File(steam, "package/droiddeck-installed").isFile || + File(steam, "package/bannerlator-installed").isFile + return !(stamped && File(steam, "steamrtarm64/steam").isFile) || File(home, ".bl-steam-repair").exists() + } + + private const val MANIFEST_PROGRESS_GRACE_MS = 8_000L // Longer than Steam's ~60 s between rate lines, for when no traffic counter is available. - const val CONTENT_ACTIVITY_GRACE_MS = 90_000L - const val MIN_RECEIVE_BYTES_PER_SEC = 64L * 1024L - val PATH = Regex("\"path\"\\s+\"([^\"]+)\"") - val CONTENT_UPDATE = Regex("""AppID (\d+) (App|Workshop|Shader) update changed : (.*)""") - val CURRENT_RATE = Regex("""Current download rate: ([\d.]+) Mbps""") + private const val CONTENT_ACTIVITY_GRACE_MS = 90_000L + private const val MIN_RECEIVE_BYTES_PER_SEC = 64L * 1024L + private val PATH = Regex("\"path\"\\s+\"([^\"]+)\"") + private val CONTENT_UPDATE = Regex("""AppID (\d+) (App|Workshop|Shader) update changed : (.*)""") + private val CURRENT_RATE = Regex("""Current download rate: ([\d.]+) Mbps""") } } diff --git a/app/src/test/java/com/droiddeck/launcher/session/SteamDownloadMonitorTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SteamDownloadMonitorTest.kt index 86cf320093..7ac818cfba 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SteamDownloadMonitorTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SteamDownloadMonitorTest.kt @@ -126,6 +126,39 @@ class SteamDownloadMonitorTest { } } + @Test fun theClientIsPendingUntilItIsInstalledAndStamped() { + val root = Files.createTempDirectory("droiddeck-steam-client-").toFile() + try { + val steam = File(root, "root/.local/share/Steam") + assertTrue(SteamDownloadMonitor.clientInstallPending(root)) + File(steam, "steamrtarm64").mkdirs() + File(steam, "steamrtarm64/steam").writeText("client") + // Unpacked but not finished: droiddeck-steam-install stamps the client last. + assertTrue(SteamDownloadMonitor.clientInstallPending(root)) + File(steam, "package").mkdirs() + File(steam, "package/droiddeck-installed").writeText("1791686101\n") + assertFalse(SteamDownloadMonitor.clientInstallPending(root)) + } finally { + root.deleteRecursively() + } + } + + @Test fun anOlderStampCountsAndARepairRequestIsPending() { + val root = Files.createTempDirectory("droiddeck-steam-client-").toFile() + try { + val steam = File(root, "root/.local/share/Steam") + File(steam, "steamrtarm64").mkdirs() + File(steam, "steamrtarm64/steam").writeText("client") + File(steam, "package").mkdirs() + File(steam, "package/bannerlator-installed").writeText("1\n") + assertFalse(SteamDownloadMonitor.clientInstallPending(root)) + File(root, "root/.bl-steam-repair").writeText("1\n") + assertTrue(SteamDownloadMonitor.clientInstallPending(root)) + } finally { + root.deleteRecursively() + } + } + private fun manifest(root: File, appId: String, downloaded: Long, total: Long, flags: Int): File { val file = File(root, "steamapps/appmanifest_$appId.acf") check(file.parentFile?.mkdirs() != false)