Skip to content
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions mobile/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<!-- To tell whether Tailscale (a VPN) is on when a computer can't be reached. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<!-- "Open Tailscale" from the connection help. -->
<queries>
<package android:name="com.tailscale.ipn" />
</queries>

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
Expand Down
33 changes: 29 additions & 4 deletions mobile/android/app/src/main/java/dev/zendr/companion/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<String?>(null) }
// Couldn't reach the computer: what to check pops up.
var help by remember { mutableStateOf<Computer?>(null) }

fun pair(raw: String) {
val c = Computer.fromLink(raw)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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<List<Space>?>(null) }
var error by remember { mutableStateOf<String?>(null) }
var problem by remember { mutableStateOf<Problem?>(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) }
Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -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)) {
Expand All @@ -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") }
}
}
}
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading