Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
addons/game-day: the calendar list, masked links and the setup file format
Justin Newmanc565c5bce801
diff --git a/addons/game-day/app/build.gradle b/addons/game-day/app/build.gradleindex a51563dcb..623739461 100644--- a/addons/game-day/app/build.gradle+++ b/addons/game-day/app/build.gradle@@ -33,4 +33,6 @@ android {dependencies {// Test only (never in the APK): plain JVM tests for the calendar reading and the schedule, src/test.testImplementation 'junit:junit:4.13.2'+ // Android's own org.json, which the JVM tests cannot reach through android.jar's stubs.+ testImplementation 'org.json:json:20231013'}diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Calendar.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Calendar.javanew file mode 100644index 000000000..bdd50d39f--- /dev/null+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Calendar.java@@ -0,0 +1,26 @@+package io.github.justinnewman.gameday;++/** One calendar the person added: usually one team. */+final class Calendar {+ /** Links.id(link): stable, so a cached copy survives the setup script replacing the list. */+ final String id;+ final String name;+ /** A secret: never logged or shown in full. */+ final String link;+ final Sport sport;+ /** A Palette index. */+ final int colour;++ Calendar(String id, String name, String link, Sport sport, int colour) {+ this.id = id;+ this.name = name;+ this.link = link;+ this.sport = sport;+ this.colour = colour;+ }++ @Override+ public String toString() {+ return name + " (" + Links.mask(link) + ")";+ }+}diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/CalendarStore.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/CalendarStore.javanew file mode 100644index 000000000..7c8093efe--- /dev/null+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/CalendarStore.java@@ -0,0 +1,99 @@+package io.github.justinnewman.gameday;++import java.util.ArrayList;+import java.util.HashSet;+import java.util.List;+import java.util.Set;++import org.json.JSONArray;+import org.json.JSONException;+import org.json.JSONObject;++/**+ * The calendar list: the setup file the script sends ({@code sport | name | link}, optionally {@code | colour}, one+ * per line, # for comments) and the JSON the app keeps it in. Error messages name the line, never the link.+ */+final class CalendarStore {+ static final int MAX = 20;+ static final int MAX_NAME = 40;++ /** A line the setup file cannot use. {@link #line} counts from 1. */+ static final class BadLine extends Exception {+ final int line;++ BadLine(int line, String message) {+ super("Line " + line + ": " + message);+ this.line = line;+ }+ }++ private CalendarStore() {+ }++ static List<Calendar> parseSetupFile(String text) throws BadLine {+ List<Calendar> out = new ArrayList<>();+ Set<String> links = new HashSet<>();+ Set<Integer> colours = new HashSet<>();+ String[] lines = text.split("\r?\n|\r", -1);+ for (int n = 1; n <= lines.length; n++) {+ String line = lines[n - 1].trim();+ if (line.isEmpty() || line.startsWith("#")) continue;+ String[] f = line.split("\\|", -1);+ if (f.length < 3 || f.length > 4) throw new BadLine(n, "expected sport | name | link, optionally | colour");+ Sport sport = Sport.byLabel(f[0]);+ if (sport == null) {+ throw new BadLine(n, "unknown sport \"" + f[0].trim() + "\". Choose one of: " + Sport.labels());+ }+ String name = f[1].trim();+ if (name.isEmpty()) throw new BadLine(n, "the name is empty");+ if (name.length() > MAX_NAME) throw new BadLine(n, "the name is over " + MAX_NAME + " characters");+ String link = Links.normalise(f[2]);+ if (link == null) throw new BadLine(n, "not a calendar link (it starts with https://, http:// or webcal://)");+ int colour;+ if (f.length == 4 && !f[3].trim().isEmpty()) {+ colour = Palette.byName(f[3]);+ if (colour < 0) throw new BadLine(n, "unknown colour \"" + f[3].trim() + "\", for example \"Green, dark\"");+ } else {+ colour = Palette.firstUnused(colours);+ }+ if (!links.add(link)) continue;+ if (out.size() == MAX) throw new BadLine(n, "more than " + MAX + " calendars");+ colours.add(colour);+ out.add(new Calendar(Links.id(link), name, link, sport, colour));+ }+ return out;+ }++ static String toJson(List<Calendar> calendars) {+ JSONArray a = new JSONArray();+ try {+ for (Calendar c : calendars) {+ a.put(new JSONObject().put("id", c.id).put("name", c.name).put("link", c.link)+ .put("sport", c.sport.label()).put("colour", c.colour));+ }+ } catch (JSONException impossible) {+ throw new IllegalStateException(impossible);+ }+ return a.toString();+ }++ /** The saved list; entries it cannot read (an unknown sport after an update) are left out. */+ static List<Calendar> fromJson(String json) {+ List<Calendar> out = new ArrayList<>();+ try {+ JSONArray a = new JSONArray(json);+ for (int i = 0; i < a.length(); i++) {+ JSONObject o = a.optJSONObject(i);+ if (o == null) continue;+ Sport sport = Sport.byLabel(o.optString("sport"));+ String link = Links.normalise(o.optString("link"));+ int colour = o.optInt("colour", Palette.COLUMNS);+ if (sport == null || link == null || colour < 0 || colour >= Palette.COUNT) continue;+ out.add(new Calendar(o.optString("id", Links.id(link)), o.optString("name"), link, sport, colour));+ }+ } catch (JSONException unreadable) {+ return new ArrayList<>();+ }+ return out;+ }+}diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Links.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Links.javanew file mode 100644index 000000000..4580a4599--- /dev/null+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Links.java@@ -0,0 +1,59 @@+package io.github.justinnewman.gameday;++import java.net.URI;+import java.net.URISyntaxException;+import java.nio.charset.StandardCharsets;+import java.security.MessageDigest;+import java.security.NoSuchAlgorithmException;+import java.util.Locale;++/**+ * Calendar links. Many are secrets (Google Calendar's "secret address in iCal format" lets anyone who has it read+ * the calendar), so a link is only ever shown masked, never logged and never written outside the app's own files.+ */+final class Links {+ static final int MAX_LENGTH = 2000;++ private Links() {+ }++ /** A fetchable https:// or http:// link: trimmed, webcal:// read as https://; null when it is not a link. */+ static String normalise(String raw) {+ if (raw == null) return null;+ String link = raw.trim();+ if (link.isEmpty() || link.length() > MAX_LENGTH) return null;+ String lower = link.toLowerCase(Locale.ROOT);+ if (lower.startsWith("webcal://")) link = "https://" + link.substring("webcal://".length());+ else if (lower.startsWith("https://")) link = "https://" + link.substring("https://".length());+ else if (lower.startsWith("http://")) link = "http://" + link.substring("http://".length());+ else return null;+ try {+ String host = new URI(link).getHost();+ return host == null || host.isEmpty() ? null : link;+ } catch (URISyntaxException bad) {+ return null;+ }+ }++ /** "calendar.google.com/…": enough to recognise a link, never enough to use it. */+ static String mask(String link) {+ try {+ String host = link == null ? null : new URI(link).getHost();+ return host == null ? "…" : host + "/…";+ } catch (URISyntaxException bad) {+ return "…";+ }+ }++ /** A short id that stays the same for the same link and cannot be turned back into it. */+ static String id(String link) {+ try {+ byte[] hash = MessageDigest.getInstance("SHA-256").digest(link.getBytes(StandardCharsets.UTF_8));+ StringBuilder b = new StringBuilder();+ for (int i = 0; i < 4; i++) b.append(String.format(Locale.ROOT, "%02x", hash[i]));+ return b.toString();+ } catch (NoSuchAlgorithmException impossible) {+ throw new IllegalStateException(impossible);+ }+ }+}diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/CalendarStoreTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/CalendarStoreTest.javanew file mode 100644index 000000000..cfb0ba6a5--- /dev/null+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/CalendarStoreTest.java@@ -0,0 +1,93 @@+package io.github.justinnewman.gameday;++import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertTrue;+import static org.junit.Assert.fail;++import java.util.List;++import org.junit.Test;++public class CalendarStoreTest {+ @Test public void setupFile() throws Exception {+ List<Calendar> c = CalendarStore.parseSetupFile(+ "# comment\nHockey | Minnesota Wild | https://h.example/a.ics\n\nSoccer|Utah Royals|webcal://h.example/b.ics|Amber, light\n");+ assertEquals(2, c.size());+ assertEquals("Minnesota Wild", c.get(0).name);+ assertEquals(Sport.byLabel("Hockey"), c.get(0).sport);+ assertEquals("https://h.example/a.ics", c.get(0).link);+ assertEquals("Utah Royals", c.get(1).name);+ assertEquals("https://h.example/b.ics", c.get(1).link);+ assertEquals(Palette.byName("Amber, light"), c.get(1).colour);+ assertEquals(12, c.get(0).colour);+ assertEquals(Links.id("https://h.example/a.ics"), c.get(0).id);+ }++ @Test public void omittedColoursAreAllDifferent() throws Exception {+ List<Calendar> c = CalendarStore.parseSetupFile("Hockey|A|https://h.example/a.ics\r\nHockey|B|https://h.example/b.ics");+ assertEquals(12, c.get(0).colour);+ assertEquals(13, c.get(1).colour);+ }++ @Test public void unknownSportNamesTheChoices() {+ try {+ CalendarStore.parseSetupFile("Basketbal | Jazz | https://h.example/c.ics");+ fail();+ } catch (CalendarStore.BadLine e) {+ assertEquals(1, e.line);+ assertTrue(e.getMessage(), e.getMessage().contains("Basketbal"));+ assertTrue(e.getMessage(), e.getMessage().contains("Choose one of: American football"));+ }+ }++ @Test public void badLinksAndColoursAreRefusedWithTheirLine() {+ assertBad("# x\nHockey | Wild | ftp://h.example/a.ics", 2, "link");+ assertBad("Hockey | Wild | https://h.example/a.ics | Chartreuse", 1, "colour");+ assertBad("Hockey | https://h.example/a.ics", 1, "sport | name | link");+ assertBad("Hockey | | https://h.example/a.ics", 1, "name");+ assertBad("Hockey | " + "W".repeat(41) + " | https://h.example/a.ics", 1, "40");+ }++ @Test public void theSameLinkTwiceIsKeptOnce() throws Exception {+ assertEquals(1, CalendarStore.parseSetupFile("Hockey|A|https://h.example/a.ics\nHockey|B|webcal://h.example/a.ics").size());+ }++ @Test public void errorsNeverQuoteTheLink() {+ try {+ CalendarStore.parseSetupFile("Basketbal | Jazz | https://h.example/secret-token");+ fail();+ } catch (CalendarStore.BadLine e) {+ assertTrue(!e.getMessage().contains("secret-token"));+ }+ }++ @Test public void jsonRoundTrip() throws Exception {+ List<Calendar> c = CalendarStore.parseSetupFile("Hockey|Minnesota Wild|https://h.example/a.ics\nGolf|Majors|https://h.example/b.ics|Grey, dark");+ List<Calendar> back = CalendarStore.fromJson(CalendarStore.toJson(c));+ assertEquals(2, back.size());+ for (int i = 0; i < 2; i++) {+ assertEquals(c.get(i).id, back.get(i).id);+ assertEquals(c.get(i).name, back.get(i).name);+ assertEquals(c.get(i).link, back.get(i).link);+ assertEquals(c.get(i).sport, back.get(i).sport);+ assertEquals(c.get(i).colour, back.get(i).colour);+ }+ assertTrue(CalendarStore.fromJson("not json").isEmpty());+ }++ @Test public void moreThanTwentyIsRefused() {+ StringBuilder b = new StringBuilder();+ for (int i = 0; i < 21; i++) b.append("Hockey|T").append(i).append("|https://h.example/").append(i).append(".ics\n");+ assertBad(b.toString(), 21, "20");+ }++ private static void assertBad(String text, int line, String inMessage) {+ try {+ CalendarStore.parseSetupFile(text);+ fail("accepted: " + text);+ } catch (CalendarStore.BadLine e) {+ assertEquals(line, e.line);+ assertTrue(e.getMessage(), e.getMessage().contains(inMessage));+ }+ }+}diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/LinksTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/LinksTest.javanew file mode 100644index 000000000..5c6c547be--- /dev/null+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/LinksTest.java@@ -0,0 +1,35 @@+package io.github.justinnewman.gameday;++import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertFalse;+import static org.junit.Assert.assertNull;++import org.junit.Test;++public class LinksTest {+ @Test public void linksAreNormalised() {+ assertEquals("https://x.example/a.ics", Links.normalise(" webcal://x.example/a.ics \n"));+ assertEquals("https://x.example/a.ics", Links.normalise("WEBCAL://x.example/a.ics"));+ assertEquals("https://x.example/a.ics", Links.normalise("HTTPS://x.example/a.ics"));+ assertEquals("http://x.example/a.ics", Links.normalise("http://x.example/a.ics"));+ assertNull(Links.normalise("ftp://x.example/a.ics"));+ assertNull(Links.normalise("not a link"));+ assertNull(Links.normalise("https:///no-host"));+ assertNull(Links.normalise(null));+ assertNull(Links.normalise("https://x.example/" + "a".repeat(2000)));+ }++ @Test public void masked() {+ assertEquals("calendar.google.com/…", Links.mask("https://calendar.google.com/calendar/ical/abc/private-123/basic.ics"));+ assertEquals("…", Links.mask("nonsense"));+ Calendar c = new Calendar("a1b2c3d4", "Wild", "https://h.example/secret-token", Sport.byLabel("Hockey"), 12);+ assertFalse(c.toString().contains("secret-token"));+ }++ @Test public void idIsStableAndHidesTheLink() {+ String id = Links.id("https://h.example/secret-token");+ assertEquals(8, id.length());+ assertEquals(id, Links.id("https://h.example/secret-token"));+ assertFalse(id.equals(Links.id("https://h.example/other-token")));+ }+}