Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(android): keep the offline copy to its own server and whole across a refresh

Mauricef02c4c57c322
diff --git a/client/android/app/src/main/java/com/liketrek/trek/MainActivity.java b/client/android/app/src/main/java/com/liketrek/trek/MainActivity.java
index 3491995d1..e284ee944 100644
--- a/client/android/app/src/main/java/com/liketrek/trek/MainActivity.java
+++ b/client/android/app/src/main/java/com/liketrek/trek/MainActivity.java
@@ -43,8 +43,9 @@ public class MainActivity extends BridgeActivity {
|| (!TrekOfflineShell.hasNetwork(this) && TrekOfflineShell.hasCopy(this, TrekServerStore.getUrlOrPrevious(this)));
if (offlineStart && TrekOfflineShell.hasCopy(this, TrekServerStore.getUrlOrPrevious(this))) {
TrekServerStore.restoreForOffline(this);
- TrekOfflineShell.activate();
+ TrekOfflineShell.activate(TrekServerStore.getUrl(this));
} else {
+ TrekOfflineShell.deactivate();
TrekServerStore.recheckAfterUpdate(this);
}
String server = TrekServerStore.getUrl(this);
diff --git a/client/android/app/src/main/java/com/liketrek/trek/TrekOfflineShell.java b/client/android/app/src/main/java/com/liketrek/trek/TrekOfflineShell.java
index 4ce8c52ec..821d143ef 100644
--- a/client/android/app/src/main/java/com/liketrek/trek/TrekOfflineShell.java
+++ b/client/android/app/src/main/java/com/liketrek/trek/TrekOfflineShell.java
@@ -61,8 +61,12 @@ final class TrekOfflineShell {
TYPES.put("html", "text/html");
}
- /** The UI comes from the copy in this run: the server did not answer when the app started. */
- private static volatile boolean active;
+ /**
+ * The server whose copy the UI comes from in this run: it did not answer
+ * when the app started. Null while the UI comes from the server itself.
+ * Bound to the server, so a copy never answers for another one.
+ */
+ private static volatile String activeServer;
/** Capacitor's JSInjector (package-private there) and its getInjectedStream, found once. */
private static volatile Object injector;
private static volatile Method injectMethod;
@@ -71,11 +75,27 @@ final class TrekOfflineShell {
private TrekOfflineShell() {}
static boolean isActive() {
- return active;
+ return activeServer != null;
}
- static void activate() {
- active = true;
+ static void activate(String server) {
+ activeServer = server;
+ }
+
+ /**
+ * A start that goes to the server. The flag lives in the process, and
+ * restart() recreates the activity in that same process (another server
+ * picked, the address screen, a reload once the server is back), so each
+ * start sets it anew instead of keeping the last run's copy in front.
+ */
+ static void deactivate() {
+ activeServer = null;
+ }
+
+ /** Whether the copy stands in for this server in this run. */
+ static boolean isActiveFor(String server) {
+ String current = activeServer;
+ return current != null && current.equals(server);
}
private static File root(Context context) {
@@ -84,9 +104,13 @@ final class TrekOfflineShell {
/** Whether there is a copy of this server's UI to start from. */
static boolean hasCopy(Context context, String server) {
+ return hasCopy(root(context), server);
+ }
+
+ static boolean hasCopy(File root, String server) {
if (server == null) return false;
- File page = new File(root(context), PAGE);
- return page.isFile() && server.equals(readStamp(context));
+ File page = new File(root, PAGE);
+ return page.isFile() && server.equals(readStamp(root));
}
/** Whether the device has a network at all; with none, the server cannot answer and the copy starts at once. */
@@ -103,21 +127,49 @@ final class TrekOfflineShell {
}
}
- /** Keeps the start page; a copy of another server is thrown away first. */
+ /**
+ * Before a refresh fetches the files of a build: a copy of another server
+ * is thrown away, one of this server stays as it is until savePage.
+ */
+ static boolean beginRefresh(Context context, String server) {
+ return beginRefresh(root(context), server);
+ }
+
+ static boolean beginRefresh(File root, String server) {
+ if (!server.equals(readStamp(root))) deleteTree(root);
+ return root.isDirectory() || root.mkdirs();
+ }
+
+ /**
+ * Keeps the start page, and only once every file it names is stored: the
+ * page points at the build's files by their hashes, so a refresh cut off
+ * halfway (no reception, the app ended) keeps the previous page with its
+ * complete build instead of a new page whose scripts are missing.
+ */
static boolean savePage(Context context, String server, byte[] html) {
- if (!server.equals(readStamp(context))) clear(context);
- File dir = root(context);
- if (!dir.isDirectory() && !dir.mkdirs()) return false;
- return write(new File(dir, STAMP), server.getBytes(StandardCharsets.UTF_8)) && write(new File(dir, PAGE), html);
+ return savePage(root(context), server, html);
+ }
+
+ static boolean savePage(File root, String server, byte[] html) {
+ if (!root.isDirectory() && !root.mkdirs()) return false;
+ return write(new File(root, STAMP), server.getBytes(StandardCharsets.UTF_8)) && write(new File(root, PAGE), html);
}
static boolean hasFile(Context context, String path) {
- File file = fileFor(context, path);
+ return hasFile(root(context), path);
+ }
+
+ static boolean hasFile(File root, String path) {
+ File file = fileFor(root, path);
return file != null && file.isFile();
}
static boolean saveFile(Context context, String path, byte[] data) {
- File file = fileFor(context, path);
+ return saveFile(root(context), path, data);
+ }
+
+ static boolean saveFile(File root, String path, byte[] data) {
+ File file = fileFor(root, path);
if (file == null) return false;
File parent = file.getParentFile();
if (parent != null && !parent.isDirectory() && !parent.mkdirs()) return false;
@@ -126,13 +178,12 @@ final class TrekOfflineShell {
/** Drops the files of older builds once the current one is complete. */
static void prune(Context context, Set<String> keep) {
- File base = new File(root(context), FILES);
- pruneDir(base, base, keep);
+ prune(root(context), keep);
}
- static void clear(Context context) {
- active = false;
- deleteTree(root(context));
+ static void prune(File root, Set<String> keep) {
+ File base = new File(root, FILES);
+ pruneDir(base, base, keep);
}
/**
@@ -142,7 +193,7 @@ final class TrekOfflineShell {
* the build by its path. Null for anything else, which goes on as usual.
*/
static WebResourceResponse serve(Context context, Bridge bridge, String server, WebResourceRequest request) {
- if (!active || server == null || !"GET".equalsIgnoreCase(request.getMethod())) return null;
+ if (!isActiveFor(server) || !"GET".equalsIgnoreCase(request.getMethod())) return null;
Uri url = request.getUrl();
Uri origin = Uri.parse(server);
if (!sameOrigin(url, origin)) return null;
@@ -155,7 +206,7 @@ final class TrekOfflineShell {
}
String path = url.getPath();
if (path == null) return null;
- File file = fileFor(context, path);
+ File file = fileFor(root(context), path);
if (file == null || !file.isFile()) return null;
return response(typeOf(path), new FileInputStream(file));
} catch (IOException e) {
@@ -218,10 +269,10 @@ final class TrekOfflineShell {
return "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80;
}
- private static File fileFor(Context context, String path) {
+ private static File fileFor(File root, String path) {
String clean = path.startsWith("/") ? path.substring(1) : path;
if (clean.isEmpty() || clean.contains("..") || !SAFE_PATH.matcher(clean).matches()) return null;
- return new File(new File(root(context), FILES), clean);
+ return new File(new File(root, FILES), clean);
}
private static String typeOf(String path) {
@@ -231,8 +282,8 @@ final class TrekOfflineShell {
return type == null ? "application/octet-stream" : type;
}
- private static String readStamp(Context context) {
- File stamp = new File(root(context), STAMP);
+ private static String readStamp(File root) {
+ File stamp = new File(root, STAMP);
if (!stamp.isFile()) return null;
try (FileInputStream in = new FileInputStream(stamp)) {
byte[] data = new byte[(int) Math.min(stamp.length(), 4096)];
diff --git a/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java b/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java
index da4df1069..9d609a081 100644
--- a/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java
+++ b/client/android/app/src/main/java/com/liketrek/trek/TrekShellPlugin.java
@@ -548,10 +548,9 @@ public class TrekShellPlugin extends Plugin {
new Thread(() -> {
JSObject result = new JSObject();
byte[] page = download(server, "", "text/html");
- boolean saved = page != null && TrekOfflineShell.savePage(context, server, page);
- boolean complete = saved;
- if (saved && listed != null) {
- Set<String> keep = new HashSet<>();
+ boolean complete = page != null && TrekOfflineShell.beginRefresh(context, server);
+ Set<String> keep = new HashSet<>();
+ if (complete && listed != null) {
for (int i = 0; i < listed.length(); i++) {
String path = listed.optString(i, null);
if (path == null || path.isEmpty()) continue;
@@ -561,8 +560,12 @@ public class TrekShellPlugin extends Plugin {
byte[] data = download(server, clean, null);
if (data == null || !TrekOfflineShell.saveFile(context, clean, data)) complete = false;
}
- if (complete) TrekOfflineShell.prune(context, keep);
}
+ // The new page goes in last: until all its files are there the
+ // stored page stays the previous one, whose build is still whole.
+ boolean saved = complete && TrekOfflineShell.savePage(context, server, page);
+ complete = saved;
+ if (saved && listed != null) TrekOfflineShell.prune(context, keep);
result.put("saved", saved);
result.put("complete", complete);
call.resolve(result);
diff --git a/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java b/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java
index 472b0e6a2..e7e7031d0 100644
--- a/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java
+++ b/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java
@@ -183,7 +183,7 @@ class TrekWebViewClient extends BridgeWebViewClient {
// the stored copy of the UI takes over, with the app's own offline data.
String server = TrekServerStore.getUrl(view.getContext());
if (isNetworkError(error.getErrorCode()) && !TrekOfflineShell.isActive() && TrekOfflineShell.hasCopy(view.getContext(), server)) {
- TrekOfflineShell.activate();
+ TrekOfflineShell.activate(server);
String url = request.getUrl().toString();
view.post(() -> view.loadUrl(url));
return;
diff --git a/client/android/app/src/test/java/com/liketrek/trek/TrekOfflineShellTest.kt b/client/android/app/src/test/java/com/liketrek/trek/TrekOfflineShellTest.kt
new file mode 100644
index 000000000..4671623da
--- /dev/null
+++ b/client/android/app/src/test/java/com/liketrek/trek/TrekOfflineShellTest.kt
@@ -0,0 +1,91 @@
+package com.liketrek.trek
+
+import java.io.File
+import java.nio.file.Files
+import org.junit.After
+import org.junit.Assert.assertArrayEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/** The stored copy of the server's UI (TrekOfflineShell), on a plain folder. */
+class TrekOfflineShellTest {
+ private val root: File = Files.createTempDirectory("offline-shell").toFile()
+
+ @After
+ fun cleanUp() {
+ TrekOfflineShell.deactivate()
+ root.deleteRecursively()
+ }
+
+ @Test
+ fun theCopyAnswersOnlyForTheServerItWasStartedFor() {
+ TrekOfflineShell.activate("https://a.example.com")
+ assertTrue(TrekOfflineShell.isActive())
+ assertTrue(TrekOfflineShell.isActiveFor("https://a.example.com"))
+ // Another server picked in the same process: its pages come from the server.
+ assertFalse(TrekOfflineShell.isActiveFor("https://b.example.com"))
+ assertFalse(TrekOfflineShell.isActiveFor(null))
+ // A start that reaches the server leaves the copy behind.
+ TrekOfflineShell.deactivate()
+ assertFalse(TrekOfflineShell.isActive())
+ assertFalse(TrekOfflineShell.isActiveFor("https://a.example.com"))
+ }
+
+ @Test
+ fun aRefreshCutOffHalfwayKeepsThePreviousBuildWhole() {
+ val server = "https://a.example.com"
+ val oldPage = "<script src=/assets/index-old.js>".toByteArray()
+ assertTrue(TrekOfflineShell.beginRefresh(root, server))
+ assertTrue(TrekOfflineShell.saveFile(root, "assets/index-old.js", byteArrayOf(1)))
+ assertTrue(TrekOfflineShell.savePage(root, server, oldPage))
+ assertTrue(TrekOfflineShell.hasCopy(root, server))
+
+ // The next build: one file arrives, then the connection drops before the page is taken over.
+ assertTrue(TrekOfflineShell.beginRefresh(root, server))
+ assertTrue(TrekOfflineShell.saveFile(root, "assets/index-new.js", byteArrayOf(2)))
+
+ assertTrue(TrekOfflineShell.hasCopy(root, server))
+ assertArrayEquals(oldPage, File(root, "index.html").readBytes())
+ assertTrue(TrekOfflineShell.hasFile(root, "assets/index-old.js"))
+ // The file already fetched stays for the next try.
+ assertTrue(TrekOfflineShell.hasFile(root, "assets/index-new.js"))
+ }
+
+ @Test
+ fun aCompleteRefreshTakesTheNewPageAndDropsTheOldBuild() {
+ val server = "https://a.example.com"
+ TrekOfflineShell.beginRefresh(root, server)
+ TrekOfflineShell.saveFile(root, "assets/index-old.js", byteArrayOf(1))
+ TrekOfflineShell.savePage(root, server, "old".toByteArray())
+
+ TrekOfflineShell.beginRefresh(root, server)
+ TrekOfflineShell.saveFile(root, "assets/index-new.js", byteArrayOf(2))
+ assertTrue(TrekOfflineShell.savePage(root, server, "new".toByteArray()))
+ TrekOfflineShell.prune(root, setOf("assets/index-new.js"))
+
+ assertArrayEquals("new".toByteArray(), File(root, "index.html").readBytes())
+ assertTrue(TrekOfflineShell.hasFile(root, "assets/index-new.js"))
+ assertFalse(TrekOfflineShell.hasFile(root, "assets/index-old.js"))
+ }
+
+ @Test
+ fun aRefreshForAnotherServerThrowsTheOldCopyAwayFirst() {
+ TrekOfflineShell.beginRefresh(root, "https://a.example.com")
+ TrekOfflineShell.saveFile(root, "assets/index-a.js", byteArrayOf(1))
+ TrekOfflineShell.savePage(root, "https://a.example.com", "a".toByteArray())
+
+ assertTrue(TrekOfflineShell.beginRefresh(root, "https://b.example.com"))
+
+ assertFalse(TrekOfflineShell.hasCopy(root, "https://a.example.com"))
+ assertFalse(TrekOfflineShell.hasCopy(root, "https://b.example.com"))
+ assertFalse(TrekOfflineShell.hasFile(root, "assets/index-a.js"))
+ }
+
+ @Test
+ fun pathsOutsideTheCopyAreRefused() {
+ assertFalse(TrekOfflineShell.saveFile(root, "../escape.js", byteArrayOf(1)))
+ assertFalse(TrekOfflineShell.saveFile(root, "assets/index.js?v=1", byteArrayOf(1)))
+ assertFalse(TrekOfflineShell.hasFile(root, ""))
+ }
+}