diff --git a/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt b/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt index db4c06939e..d54a5fff5d 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt @@ -19,7 +19,10 @@ import java.io.FileInputStream * yet to its pr-N release on DroidDeck-CI. The APK pins the pack's SHA-256, so only the bytes this build * was made with install, whichever host serves them. */ -internal class AddonPack(val name: String, private val sha256: String, val size: Long, private val required: List) { +internal class AddonPack( + val name: String, private val sha256: String, val size: Long, private val required: List, + private val reuseDeviceCopy: Boolean = sha256.isEmpty() && !BuildConfig.CI_BUILD, +) { sealed interface State { data object Missing : State data class Downloading(val progress: Float) : State @@ -40,10 +43,20 @@ internal class AddonPack(val name: String, private val sha256: String, val size: private fun root(context: Context) = File(context.filesDir, "addons/$name") /** App-private and written only after the archive matched the pinned digest. */ - fun dir(context: Context) = File(root(context), sha256) + fun dir(context: Context) = File(root(context), version(context)) fun file(context: Context, relative: String) = File(dir(context), relative) - fun installed(context: Context): Boolean = available && File(dir(context), COMPLETE).isFile + fun installed(context: Context): Boolean = version(context).isNotEmpty() && File(dir(context), COMPLETE).isFile + + // A build made outside CI pins no packs, so it runs the newest one a CI build already verified and installed. + private fun version(context: Context): String = if (!reuseDeviceCopy) sha256 else deviceCopy(context).orEmpty() + + private fun deviceCopy(context: Context): String? = root(context).listFiles().orEmpty() + .filter { dir -> + dir.isDirectory && runCatching { File(dir, COMPLETE).readText().trim() }.getOrNull() == dir.name && + required.all { File(dir, it).isFile } + } + .maxByOrNull { it.lastModified() }?.name fun refresh(context: Context): State { val now = mutableState.value @@ -86,7 +99,7 @@ internal class AddonPack(val name: String, private val sha256: String, val size: private fun install(context: Context): Boolean { if (!available) { - Log.w(TAG, "$name: this build has no pack") + Log.w(TAG, if (reuseDeviceCopy) "$name: this build has no pack and none is installed" else "$name: this build has no pack") return false } val root = root(context).apply { mkdirs() } diff --git a/app/src/test/java/com/droiddeck/launcher/core/AddonPackTest.kt b/app/src/test/java/com/droiddeck/launcher/core/AddonPackTest.kt new file mode 100644 index 0000000000..9172c5a5c3 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/core/AddonPackTest.kt @@ -0,0 +1,57 @@ +package com.droiddeck.launcher.core + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import java.io.File +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class AddonPackTest { + private val context: Context = ApplicationProvider.getApplicationContext() + private val root get() = File(context.filesDir, "addons/sample") + + @Before fun clean() { + root.deleteRecursively() + } + + private fun installed(version: String, complete: Boolean = true, modified: Long = 1_000L): File = + File(root, version).apply { + mkdirs() + File(this, "tool").writeText("x") + if (complete) File(this, AddonPack.COMPLETE).writeText(version) + setLastModified(modified) + } + + @Test fun unpinnedLocalBuildReusesTheNewestCompletePack() { + installed("a".repeat(64), modified = 1_000L) + installed("b".repeat(64), modified = 2_000L) + installed("c".repeat(64), complete = false, modified = 3_000L) + val pack = AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = true) + + assertTrue(pack.installed(context)) + assertEquals("b".repeat(64), pack.dir(context).name) + } + + @Test fun unpinnedLocalBuildWithNothingInstalledHasNoPack() { + assertFalse(AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = true).installed(context)) + } + + @Test fun pinnedBuildIgnoresOtherInstalledVersions() { + installed("b".repeat(64)) + val pack = AddonPack("sample", "a".repeat(64), 1L, listOf("tool")) + + assertFalse(pack.installed(context)) + assertEquals("a".repeat(64), pack.dir(context).name) + } + + @Test fun ciBuildWithoutAPinNeverReuses() { + installed("b".repeat(64)) + assertFalse(AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = false).installed(context)) + } +}