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 679dce6e2e..7e748b7f92 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -70,6 +70,7 @@ class SessionService : Service() { private val mainHandler = Handler(Looper.getMainLooper()) private var suspendController: SessionSuspendController? = null private val steamDownloadMonitor = SteamDownloadMonitor() + private val downloadMonitorExecutor = java.util.concurrent.Executors.newSingleThreadExecutor { Thread(it, "steam-download-monitor").apply { isDaemon = true } } private var downloadMonitorTask: Runnable? = null private var downloadActive = false private var clientInstallActive = false @@ -346,7 +347,7 @@ class SessionService : Service() { screenOn = readScreenOn() manualPauseRequested = false suspendDownloadNow = false - steamDownloadMonitor.reset() + runOnDownloadMonitor { steamDownloadMonitor.reset() } downloadActive = false clientInstallActive = false steamSleepToken = null @@ -1979,6 +1980,9 @@ class SessionService : Service() { (getSystemService(Context.DISPLAY_SERVICE) as? DisplayManager)?.unregisterDisplayListener(displayListener) displayListenerRegistered = false } + downloadMonitorTask?.let(mainHandler::removeCallbacks) + downloadMonitorTask = null + downloadMonitorExecutor.shutdown() super.onDestroy() } @@ -2102,12 +2106,20 @@ class SessionService : Service() { SessionState.running && SessionState.mode == MODE_STEAM && suspendPolicy != SessionPrefs.SUSPEND_NEVER && (steamDownloadsInBackground || !SessionState.firstFrameSeen) + private fun runOnDownloadMonitor(task: () -> Unit) { + try { + downloadMonitorExecutor.execute(task) + } catch (e: java.util.concurrent.RejectedExecutionException) { + Log.d(TAG, "download monitor already shut down") + } + } + private fun updateDownloadMonitoring() { if (!downloadWatchWanted()) { val wasActive = downloadActive downloadMonitorTask?.let(mainHandler::removeCallbacks) downloadMonitorTask = null - steamDownloadMonitor.reset() + runOnDownloadMonitor { steamDownloadMonitor.reset() } downloadActive = false if (clientInstallActive) SessionEvents.record("steam.client_download", mapOf("active" to false)) clientInstallActive = false @@ -2129,18 +2141,30 @@ class SessionService : Service() { val wasInstalling = clientInstallActive clientInstallActive = !SessionState.firstFrameSeen && (clientInstallActive || SteamDownloadMonitor.clientInstallPending(root)) - downloadActive = clientInstallActive || steamDownloadsInBackground && steamDownloadMonitor.poll( - root, - receivedBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid()), - client = SessionState.steamClientArch, - mounts = steamLibraryMounts, - ) - if (wasInstalling != clientInstallActive) { - SessionEvents.record("steam.client_download", mapOf("active" to clientInstallActive)) + val receivedBytes = TrafficStats.getUidRxBytes(android.os.Process.myUid()) + val client = SessionState.steamClientArch + val mounts = steamLibraryMounts + val self = this + val finish = { monitored: Boolean -> + if (downloadMonitorTask === self) { + downloadActive = clientInstallActive || monitored + if (wasInstalling != clientInstallActive) { + SessionEvents.record("steam.client_download", mapOf("active" to clientInstallActive)) + } + if (wasActive != downloadActive) refreshNotification() + updateSuspendPolicy() + mainHandler.postDelayed(self, DOWNLOAD_POLL_MS) + } + } + if (clientInstallActive || !steamDownloadsInBackground) { + finish(false) + return + } + runOnDownloadMonitor { + val monitored = runCatching { steamDownloadMonitor.poll(root, receivedBytes = receivedBytes, client = client, mounts = mounts) } + .getOrDefault(false) + mainHandler.post { finish(monitored) } } - if (wasActive != downloadActive) refreshNotification() - updateSuspendPolicy() - mainHandler.postDelayed(this, DOWNLOAD_POLL_MS) } } downloadMonitorTask = poll