diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentStorage.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentStorage.kt new file mode 100644 index 000000000..855fd1370 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentStorage.kt @@ -0,0 +1,95 @@ +package com.droiddeck.launcher.agent + +import android.content.Context +import android.os.Handler +import android.os.Looper +import androidx.annotation.VisibleForTesting +import androidx.compose.runtime.mutableStateMapOf +import com.droiddeck.launcher.runtime.LinuxRuntime +import java.io.File +import java.io.IOException +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes + +/** + * What the agent adds to the Linux userland, measured and removable piece by piece. Sign-ins + * (~/.claude, ~/.codex, gh's config) are left alone, so removing a CLI and installing it again + * does not sign out. + */ +object AgentStorage { + enum class Item(val paths: List) { + CLAUDE_CODE(listOf("/root/.local/share/claude", "/root/.local/bin/claude", "/root/cc")), + CODEX(listOf("/root/.local/bin/codex")), + BUILD_TOOLS(listOf("/root/android-sdk", "/root/aapt2", "/root/x86root", "/root/.gradle")), + SOURCE(listOf("/root/DroidDeck")), + CACHES(listOf("/root/.cache", "/root/.npm")), + } + + /** Bytes per item; missing while it is being measured. */ + val sizes = mutableStateMapOf() + val removing = mutableStateMapOf() + + private val main = Handler(Looper.getMainLooper()) + + fun measure(context: Context) { + val root = LinuxRuntime.rootDir(context.applicationContext) + Thread({ + for (item in Item.entries) { + val bytes = item.paths.sumOf { size(File(root, it.removePrefix("/")).toPath()) } + main.post { sizes[item] = bytes } + } + }, "agent-storage").start() + } + + fun remove(context: Context, item: Item) { + val app = context.applicationContext + val root = LinuxRuntime.rootDir(app) + removing[item] = true + Thread({ + for (path in item.paths) runCatching { delete(File(root, path.removePrefix("/")).toPath()) } + main.post { + removing.remove(item) + AgentHost.refresh(app) + AgentSetup.check(app) + measure(app) + } + }, "agent-storage-remove").start() + } + + /** Walks without following links, so a link out of the rootfs (into /sdcard, say) counts and removes only itself. */ + @VisibleForTesting + internal fun size(path: Path): Long { + if (!Files.exists(path, java.nio.file.LinkOption.NOFOLLOW_LINKS)) return 0 + var total = 0L + Files.walkFileTree(path, object : SimpleFileVisitor() { + override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { + total += attrs.size() + return FileVisitResult.CONTINUE + } + override fun visitFileFailed(file: Path, exc: IOException) = FileVisitResult.CONTINUE + }) + return total + } + + @VisibleForTesting + internal fun delete(path: Path) { + if (!Files.exists(path, java.nio.file.LinkOption.NOFOLLOW_LINKS)) return + Files.walkFileTree(path, object : SimpleFileVisitor() { + override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { + Files.deleteIfExists(file) + return FileVisitResult.CONTINUE + } + override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult { + Files.deleteIfExists(dir) + return FileVisitResult.CONTINUE + } + override fun visitFileFailed(file: Path, exc: IOException): FileVisitResult { + runCatching { Files.deleteIfExists(file) } + return FileVisitResult.CONTINUE + } + }) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AgentSetupView.kt b/app/src/main/java/com/droiddeck/launcher/ui/AgentSetupView.kt index 99f503f27..924ed59aa 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AgentSetupView.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AgentSetupView.kt @@ -25,6 +25,16 @@ import com.droiddeck.launcher.R import com.droiddeck.launcher.agent.AgentHarness import com.droiddeck.launcher.agent.AgentHost import com.droiddeck.launcher.agent.AgentSetup +import com.droiddeck.launcher.agent.AgentStorage +import android.text.format.Formatter +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment import com.droiddeck.launcher.agent.AgentSetup.Status import com.droiddeck.launcher.agent.AgentSetup.Step import com.droiddeck.launcher.core.WirelessAdbPairingService @@ -34,7 +44,10 @@ internal fun AgentSetupView(modifier: Modifier, onDone: () -> Unit = {}) { val context = LocalContext.current val colors = MaterialTheme.colorScheme val pal = LocalPalette.current - LaunchedEffect(AgentHost.harness) { AgentSetup.check(context) } + LaunchedEffect(AgentHost.harness) { + AgentSetup.check(context) + AgentStorage.measure(context) + } Column(modifier.verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(14.dp)) { Text(stringResource(R.string.agent_setup_title), fontSize = 22.sp, fontWeight = FontWeight.Bold, color = colors.onBackground) if (!AgentSetup.runtimeInstalled) { @@ -71,9 +84,66 @@ internal fun AgentSetupView(modifier: Modifier, onDone: () -> Unit = {}) { } } if (AgentSetup.ready) PrimaryButton(stringResource(R.string.agent_setup_done), main = true, onClick = onDone) + StorageCard() } } +@Composable +private fun StorageCard() { + val context = LocalContext.current + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val items = AgentStorage.Item.entries.filter { (AgentStorage.sizes[it] ?: 0L) > 0L } + if (items.isEmpty()) return + val total = items.sumOf { AgentStorage.sizes[it] ?: 0L } + Row(verticalAlignment = Alignment.Bottom, modifier = Modifier.fillMaxWidth().padding(top = 10.dp)) { + Text(stringResource(R.string.agent_storage_title), fontSize = 17.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, modifier = Modifier.weight(1f)) + Text(Formatter.formatShortFileSize(context, total), fontSize = 14.sp, color = pal.onSurfaceVariant) + } + Column(Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, pal.line, Shape14)) { + items.forEachIndexed { index, item -> + var armed by remember(item) { mutableStateOf(false) } + val busy = AgentStorage.removing[item] == true + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().heightIn(min = 60.dp).padding(horizontal = 14.dp, vertical = 10.dp), + ) { + Column(Modifier.weight(1f)) { + Text(stringResource(storageTitle(item)), fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground) + Text( + if (armed) stringResource(storageWarning(item)) else Formatter.formatShortFileSize(context, AgentStorage.sizes[item] ?: 0L), + fontSize = 13.sp, color = if (armed) AttentionAmber else pal.onSurfaceVariant, + ) + } + SecondaryButton( + stringResource(if (armed) R.string.agent_storage_confirm else R.string.agent_storage_remove), + enabled = !busy && AgentHost.state != AgentHost.State.WORKING, compact = true, + ) { + if (armed) { + armed = false + AgentStorage.remove(context, item) + } else armed = true + } + } + if (index < items.lastIndex) Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line)) + } + } +} + +private fun storageTitle(item: AgentStorage.Item) = when (item) { + AgentStorage.Item.CLAUDE_CODE -> R.string.agent_storage_claude + AgentStorage.Item.CODEX -> R.string.agent_storage_codex + AgentStorage.Item.BUILD_TOOLS -> R.string.agent_step_build + AgentStorage.Item.SOURCE -> R.string.agent_storage_source + AgentStorage.Item.CACHES -> R.string.agent_storage_caches +} + +private fun storageWarning(item: AgentStorage.Item) = when (item) { + AgentStorage.Item.SOURCE -> R.string.agent_storage_source_warning + AgentStorage.Item.CACHES -> R.string.agent_storage_caches_warning + else -> R.string.agent_storage_reinstall_warning +} + @Composable private fun labels(step: Step, harness: String): Pair = when (step) { Step.CONNECTION -> stringResource(R.string.agent_step_connection) to stringResource(R.string.agent_step_connection_hint) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 47854fc09..da8861f1a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1149,6 +1149,16 @@ Send New conversation Stop + Storage + Remove + Remove now + Claude Code + Codex + DroidDeck source + Download caches + Changes that are not pushed are lost + Builds download these again + Setup can install it again; sign-ins are kept DroidDeck restarted on your Lab build. Continue where you left off. Ask it why a game won\'t start, to explain a log, or to change DroidDeck and try the change on this device. Stopped: %1$s diff --git a/app/src/test/java/com/droiddeck/launcher/agent/AgentStorageTest.kt b/app/src/test/java/com/droiddeck/launcher/agent/AgentStorageTest.kt new file mode 100644 index 000000000..dc1220654 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/agent/AgentStorageTest.kt @@ -0,0 +1,29 @@ +package com.droiddeck.launcher.agent + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.nio.file.Files + +class AgentStorageTest { + @Test fun removingAFolderKeepsWhatItsLinksPointAt() { + val base = Files.createTempDirectory("agent-storage") + val outside = Files.createDirectories(base.resolve("sdcard")) + Files.write(outside.resolve("save.dat"), ByteArray(100)) + val item = Files.createDirectories(base.resolve("rootfs/android-sdk")) + Files.write(item.resolve("a.jar"), ByteArray(10)) + val linked = runCatching { Files.createSymbolicLink(item.resolve("saves"), outside) }.isSuccess + assumeTrue("symbolic links need privileges on this OS", linked) + + assertTrue(AgentStorage.size(item) < 100) + AgentStorage.delete(item) + assertFalse(Files.exists(item)) + assertTrue(Files.exists(outside.resolve("save.dat"))) + } + + @Test fun aMissingPathHasNoSize() { + assertEquals(0L, AgentStorage.size(Files.createTempDirectory("agent-storage").resolve("gone"))) + } +}