Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

home/android: refuse a launch request it cannot meet, and let a reload claim the document again (#642)

nyblnet06d0ad9fe1e4
diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md
index a8a1c3ce..e8215970 100644
--- a/docs/DECISIONS.md
+++ b/docs/DECISIONS.md
@@ -14,6 +14,42 @@ Decision. Why. Pointers.
---
+## 2026-10-05 — Android meets the `launchQueue` rules, and a reload also forgets its Save-As copies
+
+**Decision.** Android implements the two host rules from the entry below: a
+`begin` marked `launch: true` that it cannot meet — a read-only grant (the usual
+`ACTION_VIEW` from mail) or a document already handed out — is answered **no**,
+before the export branch; and a main-frame `onPageStarted` (WebView's
+equivalent of iOS's `didCommit`; fragment changes and `pushState` do not fire it)
+lets the new page claim the open document again.
+
+**The same hook forgets the old page's Save-As copies** — the second reset in
+the entry below, which both hosts make. On Android the copies are #595's
+`exportTargets`, keyed by the name a handle was vended under; a new page holds
+no such handle, so without the reset its first export vended under the same name
+would be written straight into the previous page's copy, without a picker. Found
+on Android first (case 4 below); iOS made the same reset in #635.
+
+**The symptom, measured rather than predicted** (emulator, Android 16, the
+#635 bridge, `setConsumer` called the way the kernel will). Without the refusal
+a read-only document does not prompt *at open*: `begin` only vends a name, so
+the page is handed an export handle (`deck.bento.html`) and the **first write**
+— the kernel's first autosave after any edit — opens a save dialog unprompted.
+Without the reset, a writable document's first ⌘S after a reload opens a
+picker instead of saving in place. With both, every case passed: read-only
+refuses (consumer never called, no picker, ⌘S still offers Save-As, original
+untouched); writable hands over `viewtest.bento.html` and a write through it
+lands in place; after a reload the consumer is handed the document again and
+⌘S saves in place; after a reload a read-only page's save asks again rather than
+writing the previous copy.
+
+**Guarded by** `scripts/test-home-bridge.ts` — four Android shape checks beside
+iOS's: the refusal exists, precedes the export branch, and the page-start hook
+resets both the hand-over and the remembered copies. All four fail against the
+pre-change `EditorActivity.kt`.
+
+---
+
## 2026-10-05 — A native host hands the page the file it opened, through `launchQueue`
**Decision.** `home/bridge.js` (iOS and Android) defines `window.launchQueue`. When
diff --git a/home/README.md b/home/README.md
index 6ed281e3..e16792d3 100644
--- a/home/README.md
+++ b/home/README.md
@@ -463,8 +463,8 @@ here rather than discovered later.
| bridge reachable only from the document | every message checked: main frame + own origin (`BridgeSender`) | `allowedOriginRules` + `isMainFrame` | same |
| first `begin` = open document, later = export | ✓ | ✓ | same |
| the open document offered at boot via `launchQueue`, only to a page that asks | ✓ (`home/bridge.js`) | ✓ (`home/bridge.js`) | same |
-| a launch request it cannot meet is refused, never exported | ✓ | **not yet** — a read-only grant would prompt at the first autosave | differs |
-| a reload may claim the open document again | resets on main-frame commit | **not yet** | differs |
+| a launch request it cannot meet is refused, never exported | ✓ | ✓ — read-only grant or already handed out | same |
+| a reload may claim the open document again, and forgets the old page's Save-As copies | resets both on main-frame commit (`didCommit`) | resets both on main-frame `onPageStarted` | same |
| an export can never address the open file | `exportName` + `targetsOpenDocument` | identical logic | same |
| page-supplied filenames sanitised | `safeFileName` | `safeFileName` | same |
| in-place write | `UIDocument.save(.forOverwriting)` | `openOutputStream(uri, "wt")` | same |
diff --git a/home/android/app/src/main/java/page/bento/home/EditorActivity.kt b/home/android/app/src/main/java/page/bento/home/EditorActivity.kt
index 4a5ba128..16f31d3b 100644
--- a/home/android/app/src/main/java/page/bento/home/EditorActivity.kt
+++ b/home/android/app/src/main/java/page/bento/home/EditorActivity.kt
@@ -8,6 +8,7 @@ import android.app.AlertDialog
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
+import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Bundle
@@ -303,6 +304,27 @@ class EditorActivity : Activity() {
}
return true
}
+
+ /**
+ * A new page holds no handles, so everything handed to the old one
+ * is forgotten here. onPageStarted fires for MAIN-FRAME navigations
+ * only — a fragment change or pushState is not one — which makes it
+ * the equivalent of iOS's didCommit.
+ *
+ * - The open document is the new page's to claim again. Without
+ * this, a reload spent the first begin on the old page, and the
+ * new page's first save (or its launchQueue handle) was taken for
+ * an export and prompted for a destination.
+ * - Remembered copies are dropped too. They are keyed by the name a
+ * handle was vended under, and the new page has no such handle: a
+ * later export it happens to vend under the same name would
+ * otherwise go straight into a file the user picked for the
+ * previous page, without asking.
+ */
+ override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
+ openDocumentVended = false
+ exportTargets.clear()
+ }
}
web.webChromeClient = TrayChromeClient()
@@ -506,6 +528,17 @@ class EditorActivity : Activity() {
if (!openDocumentVended && canWriteInPlace) {
openDocumentVended = true
reply(id, true, docName)
+ } else if (m.optBoolean("launch", false)) {
+ // The page asked for the file it was opened with (bridge.js
+ // launchQueue) and this host cannot hand it over: the grant
+ // is read-only — the usual case for a document from mail —
+ // or it was already handed out. That is a NO, never an
+ // export. Falling through would put a save dialog on screen
+ // the moment the document opened; refused, the page keeps
+ // today's path and its ⌘S behaves exactly as before.
+ reply(id, false,
+ if (!canWriteInPlace) "the open document is read-only here"
+ else "the open document was already handed out")
} else {
// Either a copy/template/read-only export, or a document we
// hold no write grant on. Both must end at a picker, and
diff --git a/scripts/test-home-bridge.ts b/scripts/test-home-bridge.ts
index 1b478fff..439ee190 100644
--- a/scripts/test-home-bridge.ts
+++ b/scripts/test-home-bridge.ts
@@ -132,6 +132,28 @@ ok(exportCopy.includes('hasPrefix(') && exportCopy.includes('safeFileName(name)'
"a main-frame commit forgets the old page's Save-As copies")
}
+// The same rules on Android (EditorActivity.kt): the launch refusal, and both
+// resets on a main-frame page start — the hand-over, and the Save-As copies
+// #595 remembers by vended name (a new page holds no such handle, so its first
+// export under the same name would otherwise land in the old page's copy).
+{
+ const KT = join(dirname(SRC), '../android/app/src/main/java/page/bento/home/EditorActivity.kt')
+ const kt = readFileSync(KT, 'utf8')
+ const begin = kt.slice(kt.indexOf('"begin" ->'), kt.indexOf('"read" ->'))
+ const refuse = begin.indexOf('m.optBoolean("launch"')
+ const exportAt = begin.indexOf('exportName(')
+ ok(refuse > 0 && /reply\(id, false/.test(begin.slice(refuse, refuse + 900)),
+ 'android: a launch request it cannot meet (read-only, or already vended) is refused')
+ ok(refuse > 0 && exportAt > refuse,
+ 'android: the launch refusal comes before the export branch, so it can never reach a picker')
+ // Not slice(): its error names the Swift file. A missing hook is a FAIL here.
+ const started = kt.includes('override fun onPageStarted(') ? slice(kt, 'override fun onPageStarted(') : ''
+ ok(/openDocumentVended = false/.test(started),
+ 'android: a main-frame page start lets the new page claim the open document again')
+ ok(/exportTargets\.clear\(\)/.test(started),
+ 'android: a main-frame page start forgets remembered Save-As copies')
+}
+
if (spawnSync('swiftc', ['--version']).status !== 0) {
console.log(' skip no Swift toolchain on this machine — behaviour checks not run')
console.log(`\n${checks - failures}/${checks} checks passed`)