From 324b600f5795735c74eae81a27beb4953f40653e Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 22 Oct 2026 11:09:59 +0200 Subject: Add post parsing, URL helpers, health check and double-OK logic with tests Co-Authored-By: Claude Opus 5.5 --- app/src/main/java/mygram/droid/Api.java | 39 ++++++++++++++ app/src/main/java/mygram/droid/Discovery.java | 35 +++++++++++++ app/src/main/java/mygram/droid/DoubleOk.java | 32 ++++++++++++ app/src/main/java/mygram/droid/Post.java | 73 +++++++++++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 app/src/main/java/mygram/droid/Api.java create mode 100644 app/src/main/java/mygram/droid/Discovery.java create mode 100644 app/src/main/java/mygram/droid/DoubleOk.java create mode 100644 app/src/main/java/mygram/droid/Post.java (limited to 'app/src/main/java') 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. + * + * 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. + * + * 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. + * + * 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. + * + * 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 = 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 list(JSONObject page, String base) { + List 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); + } +} -- cgit v1.2.3