aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-10-22 11:09:59 +0200
committerDanilo M. <danix@danix.xyz>2026-10-22 11:09:59 +0200
commit324b600f5795735c74eae81a27beb4953f40653e (patch)
tree6de17d5bf33d1acfdbb09879c3c9c24e2474421d
parent3605f4e081283b2f32451c7075fe07648e76e99d (diff)
downloadmygram-droid-324b600f5795735c74eae81a27beb4953f40653e.tar.gz
mygram-droid-324b600f5795735c74eae81a27beb4953f40653e.zip
Add post parsing, URL helpers, health check and double-OK logic with tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--app/src/main/java/mygram/droid/Api.java39
-rw-r--r--app/src/main/java/mygram/droid/Discovery.java35
-rw-r--r--app/src/main/java/mygram/droid/DoubleOk.java32
-rw-r--r--app/src/main/java/mygram/droid/Post.java73
-rw-r--r--app/src/test/java/mygram/droid/LogicTest.java110
5 files changed, 289 insertions, 0 deletions
diff --git a/app/src/main/java/mygram/droid/Api.java b/app/src/main/java/mygram/droid/Api.java
new file mode 100644
index 0000000..edc4be1
--- /dev/null
+++ b/app/src/main/java/mygram/droid/Api.java
@@ -0,0 +1,39 @@
+/*
+ * mygram-droid: Android TV client for Foldergram.
+ * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+
+package mygram.droid;
+
+import java.net.URI;
+
+/** Foldergram HTTP API. */
+final class Api {
+ private Api() {
+ }
+
+ /** "192.0.2.10:4141/" or "http://host:4141//" to "http://host:4141". */
+ static String normalize(String url) {
+ String s = url.trim();
+ if (!s.contains("://"))
+ s = "http://" + s;
+ while (s.endsWith("/"))
+ s = s.substring(0, s.length() - 1);
+ return s;
+ }
+
+ /** Resolves a server path like "/thumbnails/..." against the base URL. */
+ static String resolve(String base, String path) {
+ return path == null ? null : URI.create(base + "/").resolve(path).toString();
+ }
+}
diff --git a/app/src/main/java/mygram/droid/Discovery.java b/app/src/main/java/mygram/droid/Discovery.java
new file mode 100644
index 0000000..1d3e0be
--- /dev/null
+++ b/app/src/main/java/mygram/droid/Discovery.java
@@ -0,0 +1,35 @@
+/*
+ * mygram-droid: Android TV client for Foldergram.
+ * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+
+package mygram.droid;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/** Finds Foldergram instances on the LAN. */
+final class Discovery {
+ private Discovery() {
+ }
+
+ /** True for a Foldergram /api/health reply: {"ok":true,...,"storage":{...}}. */
+ static boolean isFoldergram(String body) {
+ try {
+ JSONObject o = new JSONObject(body);
+ return o.optBoolean("ok") && o.optJSONObject("storage") != null;
+ } catch (JSONException e) {
+ return false;
+ }
+ }
+}
diff --git a/app/src/main/java/mygram/droid/DoubleOk.java b/app/src/main/java/mygram/droid/DoubleOk.java
new file mode 100644
index 0000000..d35b835
--- /dev/null
+++ b/app/src/main/java/mygram/droid/DoubleOk.java
@@ -0,0 +1,32 @@
+/*
+ * mygram-droid: Android TV client for Foldergram.
+ * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+
+package mygram.droid;
+
+/** Tells a single OK press from the second press of a double press. */
+final class DoubleOk {
+ static final long WINDOW_MS = 300;
+ private long last = Long.MIN_VALUE / 2;
+
+ /** Call on every press with its time; true if it completes a double press. */
+ boolean press(long nowMs) {
+ if (nowMs - last <= WINDOW_MS) {
+ last = Long.MIN_VALUE / 2;
+ return true;
+ }
+ last = nowMs;
+ return false;
+ }
+}
diff --git a/app/src/main/java/mygram/droid/Post.java b/app/src/main/java/mygram/droid/Post.java
new file mode 100644
index 0000000..b4211cf
--- /dev/null
+++ b/app/src/main/java/mygram/droid/Post.java
@@ -0,0 +1,73 @@
+/*
+ * mygram-droid: Android TV client for Foldergram.
+ * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+
+package mygram.droid;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** One Foldergram post: a photo, a video, or a carousel of slides. */
+final class Post {
+ /** One slide. A single post has exactly one. */
+ static final class Media {
+ final boolean video;
+ final int width, height;
+ final long durationMs;
+ final String thumbnailUrl, previewUrl;
+
+ Media(JSONObject o, String base) {
+ video = "video".equals(o.optString("mediaType"));
+ width = o.optInt("width");
+ height = o.optInt("height");
+ durationMs = o.optLong("durationMs");
+ thumbnailUrl = Api.resolve(base, str(o, "thumbnailUrl"));
+ previewUrl = Api.resolve(base, str(o, "previewUrl"));
+ }
+ }
+
+ final int id;
+ final String folderSlug, folderName;
+ final List<Media> media = new ArrayList<>();
+
+ Post(JSONObject o, String base) {
+ id = o.optInt("id");
+ folderSlug = o.optString("folderSlug");
+ folderName = o.optString("folderName");
+ JSONArray items = o.optJSONArray("mediaItems");
+ if (items != null)
+ for (int i = 0; i < items.length(); i++)
+ media.add(new Media(items.optJSONObject(i), base));
+ if (media.isEmpty())
+ media.add(new Media(o, base));
+ }
+
+ /** The posts in a paged response's "items". */
+ static List<Post> list(JSONObject page, String base) {
+ List<Post> out = new ArrayList<>();
+ JSONArray items = page.optJSONArray("items");
+ if (items != null)
+ for (int i = 0; i < items.length(); i++)
+ out.add(new Post(items.optJSONObject(i), base));
+ return out;
+ }
+
+ /** optString turns JSON null into "null"; this returns null. */
+ static String str(JSONObject o, String key) {
+ return o.isNull(key) ? null : o.optString(key);
+ }
+}
diff --git a/app/src/test/java/mygram/droid/LogicTest.java b/app/src/test/java/mygram/droid/LogicTest.java
new file mode 100644
index 0000000..e807725
--- /dev/null
+++ b/app/src/test/java/mygram/droid/LogicTest.java
@@ -0,0 +1,110 @@
+/*
+ * mygram-droid: Android TV client for Foldergram.
+ * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+
+package mygram.droid;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.json.JSONObject;
+import org.junit.Test;
+
+import java.util.List;
+
+public class LogicTest {
+ private static final String BASE = "http://192.0.2.10:4141";
+
+ @Test
+ public void parsesSinglePhoto() throws Exception {
+ Post p = new Post(new JSONObject("{\"id\":7,\"folderSlug\":\"trips\",\"folderName\":\"Trips\","
+ + "\"mediaType\":\"image\",\"width\":1080,\"height\":1920,\"durationMs\":null,"
+ + "\"thumbnailUrl\":\"/thumbnails/ab/x.webp?v=1\",\"previewUrl\":\"/previews/ab/x.webp?v=1\","
+ + "\"postType\":\"single\",\"itemCount\":1}"), BASE);
+ assertEquals(7, p.id);
+ assertEquals("trips", p.folderSlug);
+ assertEquals(1, p.media.size());
+ Post.Media m = p.media.get(0);
+ assertFalse(m.video);
+ assertEquals(0, m.durationMs);
+ assertEquals(1080, m.width);
+ assertEquals(BASE + "/thumbnails/ab/x.webp?v=1", m.thumbnailUrl);
+ assertEquals(BASE + "/previews/ab/x.webp?v=1", m.previewUrl);
+ }
+
+ @Test
+ public void parsesVideo() throws Exception {
+ Post p = new Post(new JSONObject("{\"id\":8,\"mediaType\":\"video\",\"durationMs\":41633,"
+ + "\"thumbnailUrl\":\"/thumbnails/cd/y.webp\",\"previewUrl\":\"/previews/cd/y.mp4\"}"), BASE);
+ Post.Media m = p.media.get(0);
+ assertTrue(m.video);
+ assertEquals(41633, m.durationMs);
+ assertEquals(BASE + "/previews/cd/y.mp4", m.previewUrl);
+ }
+
+ @Test
+ public void parsesCarouselSlidesInOrder() throws Exception {
+ Post p = new Post(new JSONObject("{\"id\":9,\"mediaType\":\"image\",\"postType\":\"carousel\","
+ + "\"thumbnailUrl\":\"/thumbnails/a.webp\",\"previewUrl\":\"/previews/a.webp\",\"mediaItems\":["
+ + "{\"imageId\":1,\"mediaType\":\"image\",\"thumbnailUrl\":\"/thumbnails/a.webp\",\"previewUrl\":\"/previews/a.webp\"},"
+ + "{\"imageId\":2,\"mediaType\":\"video\",\"durationMs\":5000,\"thumbnailUrl\":\"/thumbnails/b.webp\",\"previewUrl\":\"/previews/b.mp4\"}]}"),
+ BASE);
+ assertEquals(2, p.media.size());
+ assertFalse(p.media.get(0).video);
+ assertTrue(p.media.get(1).video);
+ assertEquals(BASE + "/previews/b.mp4", p.media.get(1).previewUrl);
+ }
+
+ @Test
+ public void parsesPageAndNullUrl() throws Exception {
+ List<Post> posts = Post.list(new JSONObject("{\"items\":[{\"id\":1,\"mediaType\":\"image\","
+ + "\"thumbnailUrl\":null,\"previewUrl\":\"/previews/z.webp\"}],\"hasMore\":false}"), BASE);
+ assertEquals(1, posts.size());
+ assertNull(posts.get(0).media.get(0).thumbnailUrl);
+ assertEquals(0, Post.list(new JSONObject("{}"), BASE).size());
+ }
+
+ @Test
+ public void normalizesServerUrls() {
+ assertEquals("http://192.0.2.10:4141", Api.normalize(" 192.0.2.10:4141/ "));
+ assertEquals("http://foldergram.example.org:4141", Api.normalize("http://foldergram.example.org:4141//"));
+ assertEquals("https://foldergram.example.org", Api.normalize("https://foldergram.example.org"));
+ }
+
+ @Test
+ public void resolvesMediaPaths() {
+ assertEquals(BASE + "/icon-192.png", Api.resolve(BASE, "/icon-192.png"));
+ assertNull(Api.resolve(BASE, null));
+ }
+
+ @Test
+ public void detectsFoldergramHealth() {
+ assertTrue(Discovery.isFoldergram("{\"ok\":true,\"timestamp\":\"t\",\"storage\":{\"available\":true}}"));
+ assertFalse(Discovery.isFoldergram("{\"ok\":true}"));
+ assertFalse(Discovery.isFoldergram("{\"ok\":false,\"storage\":{}}"));
+ assertFalse(Discovery.isFoldergram("<html>router login</html>"));
+ }
+
+ @Test
+ public void tellsDoublePressFromSingle() {
+ DoubleOk d = new DoubleOk();
+ assertFalse(d.press(1000));
+ assertTrue(d.press(1250));
+ assertFalse("a double press resets", d.press(1400));
+ assertFalse(d.press(2000));
+ assertFalse("301 ms is too slow", d.press(2301));
+ }
+}