From 46f24b0236b22641c44a48451a22e49d238948d2 Mon Sep 17 00:00:00 2001 From: Ruchira Edirisinghe Date: Sat, 28 Nov 2026 20:56:45 +0530 Subject: [PATCH 1/7] Phone: tell apart offline, unpaired, a closed session, an older Zendr and the computer's own errors --- .../src/main/java/dev/zendr/companion/Api.kt | 33 +++++++-- .../java/dev/zendr/companion/ProblemTest.kt | 69 +++++++++++++++++++ 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 mobile/android/app/src/test/java/dev/zendr/companion/ProblemTest.kt diff --git a/mobile/android/app/src/main/java/dev/zendr/companion/Api.kt b/mobile/android/app/src/main/java/dev/zendr/companion/Api.kt index 23933da..46694f3 100644 --- a/mobile/android/app/src/main/java/dev/zendr/companion/Api.kt +++ b/mobile/android/app/src/main/java/dev/zendr/companion/Api.kt @@ -130,7 +130,28 @@ class Store(context: Context) { set(v) = prefs.edit().putString("skipVersion", v).apply() } -class ApiError(message: String, val unpaired: Boolean = false) : Exception(message) +/** What kind of trouble reaching a computer is: each has its own screen and way out. */ +enum class Problem { + /** No address answered: Tailscale off on one side, the computer asleep, Zendr closed. */ + Offline, + /** The computer has a new pairing code, so this phone's token stopped working. */ + Unpaired, + /** The session isn't there any more. */ + Gone, + /** Zendr on the computer doesn't know what the app asked: it's older than the app. */ + Outdated, + /** Zendr answered with an error of its own. */ + Server, + /** The computer isn't paired with this phone (forgotten since). */ + Missing, +} + +class ApiError(message: String, val kind: Problem = Problem.Server) : Exception(message) { + val unpaired get() = kind == Problem.Unpaired +} + +/** The kind of trouble behind a failed call (anything but the computer's own answer is offline). */ +fun Throwable.problem(): Problem = (this as? ApiError)?.kind ?: Problem.Offline object Api { /** Calls the computer, trying the address that worked last, then each of the others. */ @@ -154,7 +175,7 @@ object Api { // Not this address: try the next. } } - throw ApiError(refused ?: "Can't reach ${c.name}. Check that Zendr is open there and Tailscale is on here.") + throw ApiError(refused ?: "Can't reach ${c.name}. Check that Zendr is open there and Tailscale is on here.", Problem.Offline) } private class Refused(message: String) : IOException(message) @@ -177,10 +198,14 @@ object Api { val text = (if (code < 400) conn.inputStream else conn.errorStream)?.bufferedReader()?.use { it.readText() }.orEmpty() val json = runCatching { JSONObject(text) }.getOrDefault(JSONObject()) when { - code == 401 -> throw ApiError("This phone isn't paired with it any more. Scan its code again.", unpaired = true) + code == 401 -> throw ApiError("This phone isn't paired with it any more. Scan its code again.", Problem.Unpaired) // Answered only over Tailscale: another address may work. code == 403 -> throw Refused(errorText(json) ?: "That computer only answers over Tailscale.") - code >= 400 -> throw ApiError(errorText(json) ?: "The computer said: error $code") + code == 404 -> throw ApiError("Zendr on the computer is older than this app. Update it there.", Problem.Outdated) + code >= 400 -> { + val said = errorText(json) ?: "The computer said: error $code" + throw ApiError(said, if (said.contains("no terminal", ignoreCase = true)) Problem.Gone else Problem.Server) + } } return json } finally { diff --git a/mobile/android/app/src/test/java/dev/zendr/companion/ProblemTest.kt b/mobile/android/app/src/test/java/dev/zendr/companion/ProblemTest.kt new file mode 100644 index 0000000..fe56caf --- /dev/null +++ b/mobile/android/app/src/test/java/dev/zendr/companion/ProblemTest.kt @@ -0,0 +1,69 @@ +package dev.zendr.companion + +import kotlinx.coroutines.runBlocking +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.fail +import org.junit.Before +import org.junit.Test +import java.net.ServerSocket +import kotlin.concurrent.thread + +/** Each way a computer can turn the phone down gets its own kind of problem (and screen). */ +class ProblemTest { + // A tiny computer that turns every request down, as Zendr does for each path. + private val answers = mapOf( + "/api/hello" to (401 to """{"error":"not paired"}"""), + "/api/read" to (502 to """{"error":"no terminal t9"}"""), + "/api/state" to (502 to """{"error":"cannot run the core: not found"}"""), + ) + private lateinit var server: ServerSocket + + @Before fun start() { + server = ServerSocket(0) + thread(isDaemon = true) { + while (!server.isClosed) { + val s = runCatching { server.accept() }.getOrNull() ?: break + s.use { + val path = it.getInputStream().bufferedReader().readLine().orEmpty().split(' ').getOrNull(1).orEmpty().substringBefore('?') + val (code, body) = answers[path] ?: (404 to """{"error":"no such route"}""") + val b = body.toByteArray() + val head = "HTTP/1.1 $code X\r\nContent-Type: application/json\r\nContent-Length: ${b.size}\r\nConnection: close\r\n\r\n" + it.getOutputStream().write(head.toByteArray() + b) + it.getOutputStream().flush() + } + } + } + } + + @After fun stop() = server.close() + + private fun computer(port: Int = server.localPort) = + Computer("c1", "TEST-PC", listOf("127.0.0.1"), port, "0123456789abcdef0123456789abcdef") + + private fun kindOf(block: suspend () -> Unit): Problem = runBlocking { + try { + block() + fail("it was answered") + throw IllegalStateException() + } catch (e: ApiError) { + e.problem() + } + } + + @Test fun aNewPairingCodeIsUnpaired() = assertEquals(Problem.Unpaired, kindOf { Api.hello(null, computer()) }) + + @Test fun aClosedSessionIsGone() = assertEquals(Problem.Gone, kindOf { Api.read(null, computer(), "t9") }) + + @Test fun theCoresOwnTroubleIsTheServers() = assertEquals(Problem.Server, kindOf { Api.state(null, computer()) }) + + @Test fun anUnknownRouteMeansAnOlderZendr() = + assertEquals(Problem.Outdated, kindOf { Api.call(null, computer(), "GET", "/api/suggest?prefix=x") }) + + @Test fun nobodyAnsweringIsOffline() { + val free = ServerSocket(0).use { it.localPort } + assertEquals(Problem.Offline, kindOf { Api.hello(null, computer(free)) }) + } + + @Test fun otherFailuresCountAsOffline() = assertEquals(Problem.Offline, RuntimeException("x").problem()) +} From 78b23fe3ff15fb01554f63db483bcf364f240514 Mon Sep 17 00:00:00 2001 From: Ruchira Edirisinghe Date: Sat, 28 Nov 2026 20:56:46 +0530 Subject: [PATCH 2/7] Phone: see whether Tailscale is on, and open it --- mobile/android/app/src/main/AndroidManifest.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mobile/android/app/src/main/AndroidManifest.xml b/mobile/android/app/src/main/AndroidManifest.xml index 80f9fae..b75694b 100644 --- a/mobile/android/app/src/main/AndroidManifest.xml +++ b/mobile/android/app/src/main/AndroidManifest.xml @@ -2,10 +2,17 @@ + + + + + + + Date: Sat, 28 Nov 2026 20:56:46 +0530 Subject: [PATCH 3/7] Phone: icons for the connection pictures --- .../app/src/main/java/dev/zendr/companion/Glyphs.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mobile/android/app/src/main/java/dev/zendr/companion/Glyphs.kt b/mobile/android/app/src/main/java/dev/zendr/companion/Glyphs.kt index 2483d77..a2975ec 100644 --- a/mobile/android/app/src/main/java/dev/zendr/companion/Glyphs.kt +++ b/mobile/android/app/src/main/java/dev/zendr/companion/Glyphs.kt @@ -41,5 +41,14 @@ object Glyphs { val Heart by lazy { line("heart", "M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2c-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7z") } + val Unlink by lazy { + line( + "unlink", "M18.84 12.25l1.72-1.71a5 5 0 0 0-7.07-7.07l-1.72 1.71", "M5.17 11.75l-1.71 1.71a5 5 0 0 0 7.07 7.07l1.71-1.71", + "M8 2v3", "M2 8h3", "M16 22v-3", "M22 16h-3", + ) + } + val Up by lazy { line("up", "M12 19V5", "M5 12l7-7l7 7") } + val Bang by lazy { line("bang", "M12 6v8", "M12 18h.01") } + val Cross by lazy { line("cross", "M6 6l12 12", "M18 6L6 18") } val Open by lazy { line("open", "M15 3h6v6", "M10 14L21 3", "M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6") } } From 90dec4864d0c0ac206c5dc93ec7d098a5e71acaf Mon Sep 17 00:00:00 2001 From: Ruchira Edirisinghe Date: Sat, 28 Nov 2026 20:56:47 +0530 Subject: [PATCH 4/7] Phone: a 'what to check' popup and a designed screen for each problem --- .../main/java/dev/zendr/companion/Problems.kt | 283 ++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 mobile/android/app/src/main/java/dev/zendr/companion/Problems.kt diff --git a/mobile/android/app/src/main/java/dev/zendr/companion/Problems.kt b/mobile/android/app/src/main/java/dev/zendr/companion/Problems.kt new file mode 100644 index 0000000..c9fa651 --- /dev/null +++ b/mobile/android/app/src/main/java/dev/zendr/companion/Problems.kt @@ -0,0 +1,283 @@ +package dev.zendr.companion + +import android.content.Context +import android.content.Intent +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import android.net.Uri +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Check +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Button +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedButton +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.CornerRadius +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.PathEffect +import androidx.compose.ui.graphics.StrokeCap +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalUriHandler +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp + +/** Tailscale on this phone: is it on, and opening it. */ +object Tailscale { + const val PACKAGE = "com.tailscale.ipn" + + /** True when a VPN (Tailscale is one on Android) is up on this phone; null when it can't tell. */ + @Suppress("DEPRECATION") + fun on(context: Context): Boolean? = runCatching { + val cm = context.getSystemService(ConnectivityManager::class.java) + cm.allNetworks.any { cm.getNetworkCapabilities(it)?.hasTransport(NetworkCapabilities.TRANSPORT_VPN) == true } + }.getOrNull() + + /** Opens the Tailscale app, or its store page when it isn't installed. */ + fun open(context: Context) { + val app = context.packageManager.getLaunchIntentForPackage(PACKAGE) + val store = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$PACKAGE")) + val web = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$PACKAGE")) + for (i in listOfNotNull(app, store, web)) { + if (runCatching { context.startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) }.isSuccess) return + } + } +} + +/** How many failed looks in a row before the connection help pops up by itself. */ +const val HELP_AFTER = 2 + +/** + * "This isn't working": what to check when the phone can't reach a computer (Tailscale on both, + * the same account, Zendr open there), then try again. + */ +@Composable +fun ConnectionHelp(name: String, onRetry: () -> Unit, onClose: () -> Unit) { + val context = LocalContext.current + val here = remember { Tailscale.on(context) } + AlertDialog( + onDismissRequest = onClose, + icon = { ProblemArt(Problem.Offline, width = 168.dp) }, + title = { Text("Can't connect to $name", textAlign = TextAlign.Center) }, + text = { + Column(Modifier.heightIn(max = 360.dp).verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(12.dp)) { + Text("This phone and $name aren't talking right now. Check these, then try again:", fontSize = 14.sp) + CheckItem( + 1, here, "Tailscale is on, on this phone", + when (here) { + false -> "It looks off. Open Tailscale and connect." + true -> "It's on." + null -> "Open Tailscale and check it says Connected." + }, + ) + CheckItem(2, null, "Tailscale is on, on the computer", "Signed in to the same account as this phone.") + CheckItem(3, null, "Zendr is open on the computer", "With Settings → Phone turned on, and the computer awake.") + } + }, + confirmButton = { Button(onClick = onRetry) { Text("Try again") } }, + dismissButton = { + Row { + TextButton(onClick = { Tailscale.open(context) }) { Text("Open Tailscale") } + TextButton(onClick = onClose) { Text("Close") } + } + }, + ) +} + +@Composable +private fun CheckItem(n: Int, ok: Boolean?, title: String, detail: String) { + Row(verticalAlignment = Alignment.Top) { + val (tile, ink) = when (ok) { + true -> Green to Color.White + false -> Red to Color.White + null -> MaterialTheme.colorScheme.secondaryContainer to MaterialTheme.colorScheme.onSecondaryContainer + } + Box(Modifier.size(26.dp).background(tile, CircleShape), contentAlignment = Alignment.Center) { + when (ok) { + true -> Icon(Icons.Default.Check, null, tint = ink, modifier = Modifier.size(16.dp)) + false -> Icon(Glyphs.Cross, null, tint = ink, modifier = Modifier.size(14.dp)) + null -> Text("$n", color = ink, fontWeight = FontWeight.Bold, fontSize = 13.sp) + } + } + Spacer(Modifier.width(12.dp)) + Column { + Text(title, fontWeight = FontWeight.SemiBold, fontSize = 14.sp) + Text(detail, fontSize = 13.sp, color = if (ok == false) Red else MaterialTheme.colorScheme.onSurfaceVariant) + } + } +} + +/** + * A picture of what went wrong: the phone and the computer with what's between them, or a 404 + * with the iguana for the zero when what you opened isn't there. + */ +@Composable +fun ProblemArt(problem: Problem, width: Dp = 240.dp) { + val k = width / 240.dp + if (problem == Problem.Gone || problem == Problem.Missing) { + val ink = MaterialTheme.colorScheme.primary + Row(verticalAlignment = Alignment.CenterVertically) { + Text("4", fontSize = (96 * k).sp, fontWeight = FontWeight.Black, color = ink, fontFamily = FontFamily.Monospace) + ZendrLogo((84 * k).dp) + Text("4", fontSize = (96 * k).sp, fontWeight = FontWeight.Black, color = ink, fontFamily = FontFamily.Monospace) + } + return + } + val accent = when (problem) { + Problem.Server -> Amber + Problem.Outdated -> MaterialTheme.colorScheme.primary + else -> Red + } + val glyph = when (problem) { + Problem.Unpaired -> Glyphs.Unlink + Problem.Outdated -> Glyphs.Up + Problem.Server -> Glyphs.Bang + else -> Glyphs.Cross + } + val ink = MaterialTheme.colorScheme.onSurfaceVariant + Box(Modifier.size(width, width * 0.54f), contentAlignment = Alignment.Center) { + Canvas(Modifier.fillMaxSize()) { + val mid = size.height / 2 + drawCircle( + Brush.radialGradient(listOf(accent.copy(alpha = 0.20f), accent.copy(alpha = 0f)), center, size.height * 0.75f), + radius = size.height * 0.75f, + ) + // One unit of the drawing (it's laid out at 240 wide). + val d = size.width / 240f + val stroke = Stroke(3.2f * d, cap = StrokeCap.Round) + // The phone, on the left. + val pw = 34 * d + val ph = 60 * d + val px = 16 * d + val py = mid - ph / 2 + drawRoundRect(ink, Offset(px, py), Size(pw, ph), CornerRadius(9 * d), style = stroke) + drawLine(ink, Offset(px + pw / 2 - 5 * d, py + ph - 8 * d), Offset(px + pw / 2 + 5 * d, py + ph - 8 * d), 3.2f * d, StrokeCap.Round) + // The computer, on the right. + val lw = 62 * d + val lh = 40 * d + val lx = size.width - 22 * d - lw + val ly = mid - lh / 2 - 6 * d + drawRoundRect(ink, Offset(lx, ly), Size(lw, lh), CornerRadius(6 * d), style = stroke) + drawLine(ink, Offset(lx - 8 * d, ly + lh + 9 * d), Offset(lx + lw + 8 * d, ly + lh + 9 * d), 3.2f * d, StrokeCap.Round) + drawLine(ink.copy(alpha = 0.5f), Offset(lx + 10 * d, ly + 12 * d), Offset(lx + 28 * d, ly + 12 * d), 3.2f * d, StrokeCap.Round) + drawLine(ink.copy(alpha = 0.5f), Offset(lx + 10 * d, ly + 21 * d), Offset(lx + 38 * d, ly + 21 * d), 3.2f * d, StrokeCap.Round) + // What's between them. + drawLine( + ink.copy(alpha = 0.55f), Offset(px + pw + 12 * d, mid), Offset(lx - 12 * d, mid), 3.2f * d, StrokeCap.Round, + pathEffect = PathEffect.dashPathEffect(floatArrayOf(1f, 10 * d)), + ) + } + Box(Modifier.size(width * 0.16f).background(accent, CircleShape), contentAlignment = Alignment.Center) { + Icon(glyph, null, tint = Color.White, modifier = Modifier.size(width * 0.09f)) + } + } +} + +/** + * A whole screen for when something can't be shown: what happened, in plain words, and the way out. + * [onRetry] tries again; [onHelp] shows what to check; [onPair] scans the code again; [onBack] + * goes back to where you were. + */ +@Composable +fun ProblemView( + problem: Problem, + name: String, + detail: String? = null, + modifier: Modifier = Modifier, + onRetry: (() -> Unit)? = null, + onHelp: (() -> Unit)? = null, + onPair: (() -> Unit)? = null, + onBack: (() -> Unit)? = null, +) { + val uri = LocalUriHandler.current + val (title, body) = when (problem) { + Problem.Offline -> "Can't reach $name" to + "This phone and $name aren't connected right now. Check that Tailscale is on, on both, and that Zendr is open on the computer." + Problem.Unpaired -> "This phone was unpaired" to + "$name has a new pairing code, so this phone can't get in any more. Show the code in Settings → Phone on the computer and scan it again." + Problem.Gone -> "This session has ended" to + "It was closed on $name, or Zendr there started over. Your other sessions are still there." + Problem.Outdated -> "Update Zendr on $name" to + "This phone app needs a newer Zendr on the computer. Install the newest one there: your terminals keep running." + Problem.Server -> "$name ran into a problem" to + "Zendr there didn't answer the way it should. Try again in a moment." + Problem.Missing -> "Computer not found" to + "That computer isn't paired with this phone any more." + } + Column( + modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 28.dp, vertical = 24.dp), + horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, + ) { + ProblemArt(problem) + Spacer(Modifier.height(20.dp)) + Text(title, fontSize = 22.sp, fontWeight = FontWeight.SemiBold, textAlign = TextAlign.Center) + Spacer(Modifier.height(8.dp)) + Text(body, fontSize = 15.sp, lineHeight = 21.sp, textAlign = TextAlign.Center, color = MaterialTheme.colorScheme.onSurfaceVariant) + if (problem == Problem.Server && !detail.isNullOrBlank()) { + Spacer(Modifier.height(14.dp)) + Surface(color = MaterialTheme.colorScheme.surfaceContainer, shape = RoundedCornerShape(10.dp)) { + Text( + detail, fontSize = 12.sp, fontFamily = FontFamily.Monospace, color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp), + ) + } + } + Spacer(Modifier.height(24.dp)) + val wide = Modifier.fillMaxWidth(0.8f).height(48.dp) + // The main way out first, then the others. + when (problem) { + Problem.Offline -> { + onRetry?.let { Button(onClick = it, modifier = wide) { Text("Try again") } } + onHelp?.let { Spacer(Modifier.height(10.dp)); OutlinedButton(onClick = it, modifier = wide) { Text("What to check") } } + } + Problem.Unpaired -> { + onPair?.let { Button(onClick = it, modifier = wide) { Text("Scan the code again") } } + onBack?.let { Spacer(Modifier.height(10.dp)); OutlinedButton(onClick = it, modifier = wide) { Text("Back") } } + } + Problem.Gone, Problem.Missing -> { + onBack?.let { Button(onClick = it, modifier = wide) { Text(if (problem == Problem.Gone) "Back to the sessions" else "Back to your computers") } } + } + Problem.Outdated -> { + Button(onClick = { runCatching { uri.openUri("${Updates.REPO}/releases/latest") } }, modifier = wide) { Text("Get the newest Zendr") } + onRetry?.let { Spacer(Modifier.height(10.dp)); OutlinedButton(onClick = it, modifier = wide) { Text("Try again") } } + } + Problem.Server -> { + onRetry?.let { Button(onClick = it, modifier = wide) { Text("Try again") } } + Spacer(Modifier.height(10.dp)) + OutlinedButton(onClick = { runCatching { uri.openUri("${Updates.REPO}/issues/new") } }, modifier = wide) { Text("Report a problem") } + } + } + } +} From 9d6f7b2d20a20b2f7af735c70a18689445632fb9 Mon Sep 17 00:00:00 2001 From: Ruchira Edirisinghe Date: Sat, 28 Nov 2026 20:56:48 +0530 Subject: [PATCH 5/7] Phone: problem screens and the connection help on sessions, the computers list and pairing --- .../java/dev/zendr/companion/MainActivity.kt | 64 +++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/mobile/android/app/src/main/java/dev/zendr/companion/MainActivity.kt b/mobile/android/app/src/main/java/dev/zendr/companion/MainActivity.kt index 59db031..027d5b1 100644 --- a/mobile/android/app/src/main/java/dev/zendr/companion/MainActivity.kt +++ b/mobile/android/app/src/main/java/dev/zendr/companion/MainActivity.kt @@ -151,13 +151,13 @@ class MainActivity : ComponentActivity() { } is Screen.Sessions -> { BackHandler { screen = Screen.Home } - SessionsScreen(store, s.computer, onBack = { screen = Screen.Home }, onAbout = about, onBell = bell, onOpen = { + SessionsScreen(store, s.computer, onBack = { screen = Screen.Home }, onAbout = about, onBell = bell, onPair = { screen = Screen.Pair }, onOpen = { screen = Screen.Terminal(s.computer, it.terminal, it.title, it.app, it.status) }) } is Screen.Terminal -> { BackHandler { screen = Screen.Sessions(s.computer) } - TerminalScreen(store, s, onBack = { screen = Screen.Sessions(s.computer) }) + TerminalScreen(store, s, onBack = { screen = Screen.Sessions(s.computer) }, onPair = { screen = Screen.Pair }) } is Screen.About -> { BackHandler { screen = s.from } @@ -248,7 +248,14 @@ fun HomeScreen(store: Store, onOpen: (Computer) -> Unit, onAdd: () -> Unit, onAb while (true) { summary = computers.associate { c -> c.id to runCatching { Api.state(store, c).also { Feed.observe(store, c, it) }.let(::summarize) to true } - .getOrElse { "Offline: ${it.message}" to false } + .getOrElse { + when (it.problem()) { + Problem.Unpaired -> "Unpaired. Scan its code again." + Problem.Outdated -> "Update Zendr there to use this app." + Problem.Offline -> "Not connected. Tap to see why." + else -> "Zendr there ran into a problem." + } to false + } } delay(5000) } @@ -353,6 +360,8 @@ fun PairScreen(store: Store, link: String?, onDone: (Computer?) -> Unit) { var text by remember { mutableStateOf(link.orEmpty()) } var busy by remember { mutableStateOf(false) } var error by remember { mutableStateOf(null) } + // Couldn't reach the computer: what to check pops up. + var help by remember { mutableStateOf(null) } fun pair(raw: String) { val c = Computer.fromLink(raw) @@ -369,6 +378,7 @@ fun PairScreen(store: Store, link: String?, onDone: (Computer?) -> Unit) { onDone(saved) } catch (e: Exception) { error = e.message + if (e.problem() == Problem.Offline) help = c } finally { busy = false } @@ -402,6 +412,10 @@ fun PairScreen(store: Store, link: String?, onDone: (Computer?) -> Unit) { OutlinedButton(onClick = { pair(text) }, enabled = !busy && text.isNotBlank(), modifier = Modifier.fillMaxWidth().height(48.dp), shape = RoundedCornerShape(16.dp)) { Text("Pair") } } } + + help?.let { c -> + ConnectionHelp(c.name, onRetry = { help = null; pair(text) }, onClose = { help = null }) + } } @Composable @@ -421,11 +435,18 @@ private enum class Filter(val label: String) { All("All"), Need("Needs you"), Wo @OptIn(ExperimentalMaterial3Api::class) @Composable -fun SessionsScreen(store: Store, computerId: String, onBack: () -> Unit, onAbout: () -> Unit, onBell: () -> Unit, onOpen: (Session) -> Unit) { +fun SessionsScreen( + store: Store, computerId: String, onBack: () -> Unit, onAbout: () -> Unit, onBell: () -> Unit, onPair: () -> Unit, onOpen: (Session) -> Unit, +) { val scope = rememberCoroutineScope() val computer = store.computers().firstOrNull { it.id == computerId } var spaces by remember { mutableStateOf?>(null) } var error by remember { mutableStateOf(null) } + var problem by remember { mutableStateOf(null) } + // Failed looks in a row; the help pops up once per run of them. + var misses by remember { mutableIntStateOf(0) } + var help by remember { mutableStateOf(false) } + var helped by remember { mutableStateOf(false) } var refreshing by remember { mutableStateOf(false) } var filter by remember { mutableStateOf(Filter.All) } var tick by remember { mutableIntStateOf(0) } @@ -434,8 +455,13 @@ fun SessionsScreen(store: Store, computerId: String, onBack: () -> Unit, onAbout suspend fun load() { val c = store.computers().firstOrNull { it.id == computerId } ?: return runCatching { Api.state(store, c) } - .onSuccess { spaces = it; error = null; Feed.observe(store, c, it) } - .onFailure { error = it.message } + .onSuccess { spaces = it; error = null; problem = null; misses = 0; helped = false; Feed.observe(store, c, it) } + .onFailure { + error = it.message + problem = it.problem() + misses++ + if (problem == Problem.Offline && misses >= HELP_AFTER && !helped) { help = true; helped = true } + } } LaunchedEffect(computerId, tick) { @@ -460,6 +486,9 @@ fun SessionsScreen(store: Store, computerId: String, onBack: () -> Unit, onAbout Filter.Working -> s.status == "working" Filter.Done -> s.status == "done" } + val name = computer?.name ?: "the computer" + // Nothing to show but the trouble: a screen for it (an offline blip keeps the list, with a note). + val shownProblem = if (computer == null) Problem.Missing else problem?.takeIf { spaces == null || it != Problem.Offline } Scaffold(topBar = { ZendrTopBar( @@ -476,6 +505,15 @@ fun SessionsScreen(store: Store, computerId: String, onBack: () -> Unit, onAbout modifier = Modifier.fillMaxSize().padding(pad), ) { LazyColumn(Modifier.fillMaxSize(), contentPadding = PaddingValues(start = 12.dp, end = 12.dp, bottom = 24.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { + shownProblem?.let { p -> + item { + ProblemView( + p, name, error, Modifier.fillParentMaxSize(), + onRetry = { tick++ }, onHelp = { help = true }, onPair = onPair, onBack = onBack, + ) + } + return@LazyColumn + } // Which computer, and who needs you there. item { Column(Modifier.padding(start = 4.dp, top = 2.dp, bottom = 2.dp)) { @@ -495,13 +533,15 @@ fun SessionsScreen(store: Store, computerId: String, onBack: () -> Unit, onAbout } } } - error?.let { e -> + if (problem == Problem.Offline) { item { Surface(color = Red.copy(alpha = 0.12f), shape = RoundedCornerShape(12.dp), modifier = Modifier.fillMaxWidth()) { - Column(Modifier.padding(14.dp)) { - Text("Can't reach ${computer?.name ?: "it"}", color = Red, fontWeight = FontWeight.SemiBold) - Text(e, color = Red, fontSize = 13.sp) - TextButton(onClick = { tick++ }) { Text("Try again") } + Row(Modifier.padding(start = 14.dp, end = 6.dp, top = 6.dp, bottom = 6.dp), verticalAlignment = Alignment.CenterVertically) { + Column(Modifier.weight(1f)) { + Text("Lost the connection", color = Red, fontWeight = FontWeight.SemiBold) + Text("Trying $name again…", color = Red, fontSize = 13.sp) + } + TextButton(onClick = { help = true }) { Text("What to check") } } } } @@ -531,6 +571,8 @@ fun SessionsScreen(store: Store, computerId: String, onBack: () -> Unit, onAbout } } } + + if (help) ConnectionHelp(name, onRetry = { help = false; tick++ }, onClose = { help = false }) } @Composable From aead15712f0a67921e86599759186365c2debad4 Mon Sep 17 00:00:00 2001 From: Ruchira Edirisinghe Date: Sat, 28 Nov 2026 20:56:48 +0530 Subject: [PATCH 6/7] Phone: problem screens and the connection help on the terminal --- .../dev/zendr/companion/TerminalScreen.kt | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/mobile/android/app/src/main/java/dev/zendr/companion/TerminalScreen.kt b/mobile/android/app/src/main/java/dev/zendr/companion/TerminalScreen.kt index 33afd2c..911233c 100644 --- a/mobile/android/app/src/main/java/dev/zendr/companion/TerminalScreen.kt +++ b/mobile/android/app/src/main/java/dev/zendr/companion/TerminalScreen.kt @@ -63,6 +63,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberUpdatedState @@ -123,12 +124,17 @@ private const val MAX_TEXT = 22f /** One session on one computer: its screen, kept up to date, and ways to answer it. */ @OptIn(ExperimentalMaterial3Api::class) @Composable -fun TerminalScreen(store: Store, s: Screen.Terminal, onBack: () -> Unit) { +fun TerminalScreen(store: Store, s: Screen.Terminal, onBack: () -> Unit, onPair: () -> Unit) { val scope = rememberCoroutineScope() val snack = remember { SnackbarHostState() } val clipboard = LocalClipboardManager.current var screen by remember { mutableStateOf(null) } var error by remember { mutableStateOf(null) } + var problem by remember { mutableStateOf(null) } + // Failed reads in a row; the help pops up once per run of them. + var misses by remember { mutableIntStateOf(0) } + var help by remember { mutableStateOf(false) } + var helped by remember { mutableStateOf(false) } var status by remember { mutableStateOf(s.status) } var input by remember { mutableStateOf(TextFieldValue("")) } var suggestions by remember { mutableStateOf>(emptyList()) } @@ -156,10 +162,19 @@ fun TerminalScreen(store: Store, s: Screen.Terminal, onBack: () -> Unit) { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { var n = 0 while (true) { - val c = computer() ?: break + val c = computer() + if (c == null) { + problem = Problem.Missing + break + } runCatching { Api.screen(store, c, s.terminal) } - .onSuccess { if (it != screen) screen = it; error = null } - .onFailure { error = it.message } + .onSuccess { if (it != screen) screen = it; error = null; problem = null; misses = 0; helped = false } + .onFailure { + error = it.message + problem = it.problem() + misses++ + if (problem == Problem.Offline && misses >= HELP_AFTER + 1 && !helped) { help = true; helped = true } + } if (n++ % 4 == 0) { runCatching { Api.state(store, c) }.onSuccess { spaces -> spaces.flatMap { it.sessions }.firstOrNull { it.terminal == s.terminal }?.let { status = it.status } @@ -310,7 +325,12 @@ fun TerminalScreen(store: Store, s: Screen.Terminal, onBack: () -> Unit) { }, snackbarHost = { SnackbarHost(snack) }, ) { pad -> - Column(Modifier.fillMaxSize().padding(pad).consumeWindowInsets(pad).imePadding()) { + // Nothing to show but the trouble: a screen for it. An offline blip keeps the screen, with a note. + val shownProblem = problem?.takeIf { screen == null || it != Problem.Offline } + if (shownProblem != null) ProblemView( + shownProblem, computerName.ifEmpty { "the computer" }, error, Modifier.padding(pad), + onRetry = { refreshNow++ }, onHelp = { help = true }, onPair = onPair, onBack = onBack, + ) else Column(Modifier.fillMaxSize().padding(pad).consumeWindowInsets(pad).imePadding()) { // ------------------------------------------------ the screen BoxWithConstraints( Modifier.weight(1f).fillMaxWidth().padding(horizontal = 6.dp) @@ -339,15 +359,6 @@ fun TerminalScreen(store: Store, s: Screen.Terminal, onBack: () -> Unit) { val hScroll = rememberScrollState() when { - screen == null && error != null -> Column( - Modifier.fillMaxSize().padding(24.dp), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, - ) { - Text("Can't reach $computerName", color = palette.fg, fontWeight = FontWeight.SemiBold, fontSize = 16.sp) - Spacer(Modifier.height(6.dp)) - Text(error.orEmpty(), color = palette.fg.copy(alpha = 0.7f), fontSize = 13.sp) - Spacer(Modifier.height(14.dp)) - FilledTonalButton(onClick = { refreshNow++ }) { Text("Try again") } - } screen == null -> Text("Connecting…", color = palette.fg.copy(alpha = 0.7f), modifier = Modifier.align(Alignment.Center)) else -> SelectionContainer { Box(if (wrap) Modifier.fillMaxSize() else Modifier.fillMaxSize().horizontalScroll(hScroll)) { @@ -391,8 +402,11 @@ fun TerminalScreen(store: Store, s: Screen.Terminal, onBack: () -> Unit) { } if (error != null && screen != null) { Text( - "Reconnecting… $error", color = Color.White, fontSize = 12.sp, maxLines = 1, overflow = TextOverflow.Ellipsis, - modifier = Modifier.align(Alignment.TopCenter).fillMaxWidth().background(Red.copy(alpha = 0.85f), RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp)).padding(horizontal = 12.dp, vertical = 4.dp), + "Reconnecting to ${computerName.ifEmpty { "the computer" }}… Tap for help", color = Color.White, fontSize = 12.sp, + maxLines = 1, overflow = TextOverflow.Ellipsis, + modifier = Modifier.align(Alignment.TopCenter).fillMaxWidth() + .background(Red.copy(alpha = 0.85f), RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp)) + .clickable { help = true }.padding(horizontal = 12.dp, vertical = 4.dp), ) } } @@ -478,6 +492,8 @@ fun TerminalScreen(store: Store, s: Screen.Terminal, onBack: () -> Unit) { } } } + + if (help) ConnectionHelp(computerName.ifEmpty { "the computer" }, onRetry = { help = false; refreshNow++ }, onClose = { help = false }) } /** The rest of a suggested command after what's typed, in grey (Warp's ghost text). */ From 9eda650c36f439bb61479f27216dade1b5c2c885 Mon Sep 17 00:00:00 2001 From: Ruchira Edirisinghe Date: Sat, 28 Nov 2026 20:57:01 +0530 Subject: [PATCH 7/7] README: connection help and problem screens on the phone --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e92c780..2fae71c 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,7 @@ The **Zendr app for Android** shows every session on your computers, says which - **When an agent asks a question** (allow this command? 1. Yes 2. No), its choices are buttons. - **The key bar** has **Esc**, **Tab**, the arrows, **Enter** and **Ctrl+C**. Send with an empty box presses Enter. - **At a shell prompt**, the same command suggestions as on your computer show as you type. Tap one, or press → or Tab on a keyboard. +- **When it can't connect**, a popup lists what to check (Tailscale on, on this phone and the computer, with the same account, and Zendr open there), says if Tailscale looks off on the phone, and has **Open Tailscale** and **Try again**. A closed session, an unpaired phone, an older Zendr on the computer and Zendr's own errors each get a screen that says what happened and the way out. - **The bell** at the top keeps a list of what happened: agents that needed you or finished, and new versions. A dot marks the ones you haven't seen. Tap one to open that session, swipe it away to remove it. - **Light or dark**: the moon and sun at the top left switch it, and **About** (ⓘ) can also follow your phone's setting. - **New versions** pop up by themselves with what's new and a **Download** button. **About** shows your version, checks for updates and links to the source, the releases and the issue tracker.