From 3b9abf63022e440906f4263a5c5c0901ab12de5a Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Thu, 29 Oct 2026 18:50:26 -0400 Subject: [PATCH] Update the Linux runtime by itself when a new one is out (#1787) --- .../droiddeck/launcher/FrontEndActivity.kt | 45 +++++++++++++---- .../launcher/agent/AgentStartActivity.kt | 10 ++++ .../launcher/runtime/RuntimeAutoUpdate.kt | 30 ++++++++++++ .../launcher/runtime/RuntimeAutoUpdateTest.kt | 48 +++++++++++++++++++ 4 files changed, 123 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdate.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdateTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index 3f03afc2c1..fe384b2511 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -109,6 +109,8 @@ class FrontEndActivity : ComponentActivity() { private var ready by mutableStateOf(false) private var removalPending by mutableStateOf(false) private var available by mutableStateOf(null) + private var catalogFresh = false + private var componentsQueueBusy by mutableStateOf(false) private var busy by mutableStateOf(false) private var stage by mutableStateOf("") private var percent by mutableIntStateOf(-1) @@ -1630,14 +1632,21 @@ class FrontEndActivity : ComponentActivity() { drivers.checkOnOpen() resumeInterruptedPackages() protons.checkUpdates() - if (!SessionState.running) Thread({ - val applied = runCatching { ComponentsManager.applyQueued(this) }.getOrDefault(emptyList()) - if (applied.isNotEmpty()) ui.post { - android.widget.Toast.makeText(this, getString(R.string.main_applied, applied.joinToString(", ")), android.widget.Toast.LENGTH_LONG).show() - } - ui.post { if (showComponents) components.refreshComponents() } - }, "components-queue").start() - else if (showComponents) components.refreshComponents() + catalogFresh = false + if (!SessionState.running && !componentsQueueBusy && !LinuxRuntimeInstaller.isBusy()) { + componentsQueueBusy = true + Thread({ + val applied = runCatching { ComponentsManager.applyQueued(this) }.getOrDefault(emptyList()) + if (applied.isNotEmpty()) ui.post { + android.widget.Toast.makeText(this, getString(R.string.main_applied, applied.joinToString(", ")), android.widget.Toast.LENGTH_LONG).show() + } + ui.post { + componentsQueueBusy = false + if (showComponents) components.refreshComponents() + autoUpdateRuntime() + } + }, "components-queue").start() + } else if (showComponents) components.refreshComponents() oscMode = SessionPrefs.oscMode(this) refreshController() refreshHomeAppState() @@ -2441,6 +2450,7 @@ class FrontEndActivity : ComponentActivity() { installed = LinuxRuntimeInstaller.installedVersion(this) ready = LinuxRuntime.isInstalled(this) removalPending = LinuxRuntimeInstaller.hasRemovalPending(this) + ui.post { autoUpdateRuntime() } frameGenLabel = FrameGen.label(this) romsDir = SessionPrefs.romsDir(this).takeIf { it.isNotEmpty() } logsEnabled = SessionPrefs.logsEnabled(this) @@ -2588,7 +2598,7 @@ class FrontEndActivity : ComponentActivity() { private fun runtimeChangesBlocked(): Boolean = busy || LinuxRuntimeInstaller.isBusy() || SessionState.running || SessionState.phase !in setOf(SessionPhase.IDLE, SessionPhase.FAILED) || - protons.protonBusyId != null || ProtonExtras.installInProgress || pkgStage != null || + protons.protonBusyId != null || ProtonExtras.installInProgress || pkgStage != null || componentsQueueBusy || components.compBusy != null || saveBusy != null || com.droiddeck.launcher.store.UserAppsState.working != null private fun removeDesktop() { @@ -2648,10 +2658,25 @@ class FrontEndActivity : ComponentActivity() { }, "runtime-operation").start() } + private fun autoUpdateRuntime() { + val release = available?.takeIf { catalogFresh } ?: return + if (!lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED) || runtimeChangesBlocked() || removalPending) return + if (release.version == installed || installed == null) return + if (com.droiddeck.launcher.runtime.RuntimeAutoUpdate.claim(this, installed, release.version)) { + Log.i(TAG, "runtime: updating $installed to ${release.version} by itself") + install(release) + } + } + private fun checkCatalog() { val release = LinuxRuntimeInstaller.fetchRelease() Log.i(TAG, "catalog: " + (release?.version ?: "unreachable")) - ui.post { if (release != null) available = release } + ui.post { + if (release == null) return@post + available = release + catalogFresh = true + autoUpdateRuntime() + } } companion object { diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt index f052df93cf..ce8cca924e 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentStartActivity.kt @@ -23,6 +23,9 @@ class AgentStartActivity : Activity() { Thread({ try { AgentBridgeProvider.awaitRecovery() + val until = android.os.SystemClock.elapsedRealtime() + RUNTIME_WAIT_MS + while (com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isBusy() && !isFinishing && + android.os.SystemClock.elapsedRealtime() < until) Thread.sleep(RUNTIME_POLL_MS) runOnUiThread { if (!isFinishing) startSession(request) } @@ -51,6 +54,11 @@ class AgentStartActivity : Activity() { } private fun startSession(request: JSONObject) { + if (com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.isBusy()) { + Log.w(TAG, "the Linux runtime is being installed or changed") + finish() + return + } try { AgentAccess.requireStart(this, request) } catch (e: AgentException) { @@ -107,5 +115,7 @@ class AgentStartActivity : Activity() { companion object { const val EXTRA_REQUEST = "request" private const val TAG = "AgentStartActivity" + private const val RUNTIME_WAIT_MS = 15 * 60_000L + private const val RUNTIME_POLL_MS = 500L } } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdate.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdate.kt new file mode 100644 index 0000000000..5bb53bbc0e --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdate.kt @@ -0,0 +1,30 @@ +package com.droiddeck.launcher.runtime + +import android.content.Context +import com.droiddeck.launcher.BuildConfig + +object RuntimeAutoUpdate { + private const val PREFS = "runtime-auto-update" + const val MAX_ATTEMPTS = 3 + const val RETRY_MS = 10 * 60_000L + + fun due(installed: String?, release: String?, attempts: Int, lastAt: Long = 0L, now: Long = lastAt + RETRY_MS): Boolean = + installed != null && !release.isNullOrEmpty() && release != installed && attempts < MAX_ATTEMPTS && + (attempts == 0 || now - lastAt !in 0 until RETRY_MS) + + fun key(appVersion: Int, release: String) = "$appVersion:$release" + + fun claim(context: Context, installed: String?, release: String?, now: Long = System.currentTimeMillis()): Boolean { + if (release.isNullOrEmpty()) return false + val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) + val key = key(BuildConfig.VERSION_CODE, release) + synchronized(this) { + val attempts = prefs.getInt(key, 0) + if (!due(installed, release, attempts, prefs.getLong("$key@at", 0L), now)) return false + val edit = prefs.edit() + prefs.all.keys.filterNot { it.startsWith("${BuildConfig.VERSION_CODE}:") }.forEach { edit.remove(it) } + edit.putInt(key, attempts + 1).putLong("$key@at", now).apply() + return true + } + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdateTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdateTest.kt new file mode 100644 index 0000000000..8357f9694c --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeAutoUpdateTest.kt @@ -0,0 +1,48 @@ +package com.droiddeck.launcher.runtime + +import android.app.Application +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33], manifest = Config.NONE, application = Application::class) +class RuntimeAutoUpdateTest { + private val context get() = RuntimeEnvironment.getApplication() + + @Test fun anInstalledRuntimeBehindTheCatalogUpdatesByItself() { + assertTrue(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2")) + } + + @Test fun noRuntimeOrTheSameOneIsLeftAlone() { + assertFalse(RuntimeAutoUpdate.claim(context, null, "ubuntu-r1-rc2")) + assertFalse(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc2", "ubuntu-r1-rc2")) + assertFalse(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", null)) + assertFalse(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "")) + } + + @Test fun aFailingUpdateStopsTryingAfterAFewLaunches() { + repeat(RuntimeAutoUpdate.MAX_ATTEMPTS) { assertTrue(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", it * RuntimeAutoUpdate.RETRY_MS)) } + assertFalse(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", 10 * RuntimeAutoUpdate.RETRY_MS)) + } + + @Test fun aRetryWaitsBeforeUsingTheNextTry() { + assertTrue(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", 1_000L)) + assertFalse(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", 2_000L)) + assertTrue(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", 1_000L + RuntimeAutoUpdate.RETRY_MS)) + } + + @Test fun aNewerCatalogReleaseStartsAgain() { + repeat(RuntimeAutoUpdate.MAX_ATTEMPTS) { RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", it * RuntimeAutoUpdate.RETRY_MS) } + assertTrue(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc3", 5 * RuntimeAutoUpdate.RETRY_MS)) + } + + @Test fun goingBackToAnEarlierReleaseKeepsItsCount() { + repeat(RuntimeAutoUpdate.MAX_ATTEMPTS) { RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", it * RuntimeAutoUpdate.RETRY_MS) } + assertTrue(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc3", 5 * RuntimeAutoUpdate.RETRY_MS)) + assertFalse(RuntimeAutoUpdate.claim(context, "ubuntu-r1-rc1", "ubuntu-r1-rc2", 10 * RuntimeAutoUpdate.RETRY_MS)) + } +}