fix: a first run's Steam client download is not paused out of sight

With the default background behavior (Manual), a session pauses as soon
as DroidDeck is hidden or the screen turns off - one second after start in
the report this came from, before the download had begun. "Keep downloads
running" did not help: it is off by default, and it only recognizes the
client's own game downloads, not the runtime fetching the client itself.

Until the session shows its first frame, the download watch now also
asks whether the client is still to be fetched (no install stamp or no
client binary, or a repair asked for). While it is, the session is kept
awake with its wake and Wi-Fi locks whatever the downloads setting says,
the notification reads "Steam download active" with Suspend now, and a
steam.client_download event marks start and end. The watch is set up
before the first suspend decision, so a session started with the screen
off is not paused first. Once the client is there, the chosen behavior
applies again.
This commit is contained in:
maxjivi05 committed 2026-10-11 14:45:57 -04:00
1 parent 342d2e7f20
commit 0cd7f07725
3 files changed
+93 -22

No files matched your search

@@ -83,6 +83,12 @@ class SessionService : Service() {
private val steamDownloadMonitor = SteamDownloadMonitor() private val steamDownloadMonitor = SteamDownloadMonitor()
private var downloadMonitorTask: Runnable? = null private var downloadMonitorTask: Runnable? = null
private var downloadActive = false 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 suspendDownloadNow = false
private var suspendPolicy = SessionPrefs.SUSPEND_MANUAL private var suspendPolicy = SessionPrefs.SUSPEND_MANUAL
private var steamDownloadsInBackground = false private var steamDownloadsInBackground = false
@@ -229,6 +235,7 @@ class SessionService : Service() {
suspendDownloadNow = false suspendDownloadNow = false
steamDownloadMonitor.reset() steamDownloadMonitor.reset()
downloadActive = false downloadActive = false
clientInstallActive = false
steamSleepToken = null steamSleepToken = null
completeNativeSleep() completeNativeSleep()
suspendOperationPending = false suspendOperationPending = false
@@ -550,8 +557,9 @@ class SessionService : Service() {
if (!pulse.setSinkSuspended(false)) Log.w(TAG, "could not resume audio sink") 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() updateDownloadMonitoring()
mainHandler.post { updateSuspendPolicy() }
} }
} }
@@ -1331,8 +1339,7 @@ class SessionService : Service() {
} }
val controller = suspendController ?: return val controller = suspendController ?: return
val hidden = !activityVisible || !screenOn val hidden = !activityVisible || !screenOn
val downloadsBlockingSuspend = steamDownloadsInBackground && SessionState.mode == MODE_STEAM && val downloadsBlockingSuspend = downloadsKeepAwake() && hidden && !suspendDownloadNow
suspendPolicy != SessionPrefs.SUSPEND_NEVER && hidden && downloadActive && !suspendDownloadNow
if (downloadsBlockingSuspend) acquireLocks() if (downloadsBlockingSuspend) acquireLocks()
if (suspendPolicy != SessionPrefs.SUSPEND_NATIVE || !hidden || downloadsBlockingSuspend) { if (suspendPolicy != SessionPrefs.SUSPEND_NATIVE || !hidden || downloadsBlockingSuspend) {
completeNativeSleep() completeNativeSleep()
@@ -1709,8 +1716,7 @@ class SessionService : Service() {
.setContentText(getString( .setContentText(getString(
when { when {
SessionState.suspended -> R.string.session_notification_paused SessionState.suspended -> R.string.session_notification_paused
downloadActive && steamDownloadsInBackground && downloadsKeepAwake() -> R.string.session_notification_downloads
suspendPolicy != SessionPrefs.SUSPEND_NEVER -> R.string.session_notification_downloads
else -> R.string.session_notification else -> R.string.session_notification
} }
)) ))
@@ -1718,8 +1724,7 @@ class SessionService : Service() {
.apply { .apply {
if (SessionState.suspended) { if (SessionState.suspended) {
addAction(Notification.Action.Builder(null, getString(R.string.resume_session), resume).build()) addAction(Notification.Action.Builder(null, getString(R.string.resume_session), resume).build())
} else if (downloadActive && steamDownloadsInBackground && } else if (downloadsKeepAwake()) {
suspendPolicy != SessionPrefs.SUSPEND_NEVER) {
val suspend = PendingIntent.getService( val suspend = PendingIntent.getService(
this@SessionService, 3, this@SessionService, 3,
Intent(this@SessionService, SessionService::class.java).setAction(ACTION_SUSPEND_NOW), 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()) 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() { private fun updateDownloadMonitoring() {
val enabled = SessionState.running && SessionState.mode == MODE_STEAM && if (!downloadWatchWanted()) {
steamDownloadsInBackground && suspendPolicy != SessionPrefs.SUSPEND_NEVER
if (!enabled) {
val wasActive = downloadActive val wasActive = downloadActive
downloadMonitorTask?.let(mainHandler::removeCallbacks) downloadMonitorTask?.let(mainHandler::removeCallbacks)
downloadMonitorTask = null downloadMonitorTask = null
steamDownloadMonitor.reset() steamDownloadMonitor.reset()
downloadActive = false downloadActive = false
clientInstallActive = false
if (wasActive) refreshNotification() if (wasActive) refreshNotification()
return return
} }
if (downloadMonitorTask != null) return if (downloadMonitorTask != null) return
val poll = object : Runnable { val poll = object : Runnable {
override fun run() { override fun run() {
if (downloadMonitorTask !== this || !SessionState.running || if (downloadMonitorTask !== this) return
SessionState.mode != MODE_STEAM || !steamDownloadsInBackground || if (!downloadWatchWanted()) {
suspendPolicy == SessionPrefs.SUSPEND_NEVER) { downloadMonitorTask = null
updateDownloadMonitoring()
updateSuspendPolicy()
return return
} }
val root = LinuxRuntime.rootDir(this@SessionService)
val wasActive = downloadActive val wasActive = downloadActive
downloadActive = steamDownloadMonitor.poll( val wasInstalling = clientInstallActive
LinuxRuntime.rootDir(this@SessionService), clientInstallActive = !SessionState.firstFrameSeen && SteamDownloadMonitor.clientInstallPending(root)
downloadActive = clientInstallActive || steamDownloadsInBackground && steamDownloadMonitor.poll(
root,
receivedBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid()), receivedBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid()),
) )
if (wasInstalling != clientInstallActive) {
SessionEvents.record("steam.client_download", mapOf("active" to clientInstallActive))
}
if (wasActive != downloadActive) refreshNotification() if (wasActive != downloadActive) refreshNotification()
updateSuspendPolicy() updateSuspendPolicy()
mainHandler.postDelayed(this, DOWNLOAD_POLL_MS) mainHandler.postDelayed(this, DOWNLOAD_POLL_MS)
@@ -126,13 +126,26 @@ internal class SteamDownloadMonitor {
private fun value(text: String, key: String): String? = private fun value(text: String, key: String): String? =
Regex("\"$key\"\\s+\"([^\"]*)\"").find(text)?.groupValues?.get(1) Regex("\"$key\"\\s+\"([^\"]*)\"").find(text)?.groupValues?.get(1)
private companion object { companion object {
const val MANIFEST_PROGRESS_GRACE_MS = 8_000L /**
* 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. // Longer than Steam's ~60 s between rate lines, for when no traffic counter is available.
const val CONTENT_ACTIVITY_GRACE_MS = 90_000L private const val CONTENT_ACTIVITY_GRACE_MS = 90_000L
const val MIN_RECEIVE_BYTES_PER_SEC = 64L * 1024L private const val MIN_RECEIVE_BYTES_PER_SEC = 64L * 1024L
val PATH = Regex("\"path\"\\s+\"([^\"]+)\"") private val PATH = Regex("\"path\"\\s+\"([^\"]+)\"")
val CONTENT_UPDATE = Regex("""AppID (\d+) (App|Workshop|Shader) update changed : (.*)""") private val CONTENT_UPDATE = Regex("""AppID (\d+) (App|Workshop|Shader) update changed : (.*)""")
val CURRENT_RATE = Regex("""Current download rate: ([\d.]+) Mbps""") private val CURRENT_RATE = Regex("""Current download rate: ([\d.]+) Mbps""")
} }
} }
@@ -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 { private fun manifest(root: File, appId: String, downloaded: Long, total: Long, flags: Int): File {
val file = File(root, "steamapps/appmanifest_$appId.acf") val file = File(root, "steamapps/appmanifest_$appId.acf")
check(file.parentFile?.mkdirs() != false) check(file.parentFile?.mkdirs() != false)