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: read iCalendar feeds (Stanza, Google, any RFC 5545 file)
Justin Newmanc971d1b73007
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Ics.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Ics.javanew file mode 100644index 000000000..0de77e8b1--- /dev/null+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Ics.java@@ -0,0 +1,228 @@+package io.github.justinnewman.gameday;++import java.io.BufferedReader;+import java.io.IOException;+import java.io.Reader;+import java.time.DateTimeException;+import java.time.Duration;+import java.time.Instant;+import java.time.LocalDate;+import java.time.LocalDateTime;+import java.time.ZoneId;+import java.time.ZoneOffset;+import java.time.format.DateTimeFormatter;+import java.util.ArrayDeque;+import java.util.ArrayList;+import java.util.Deque;+import java.util.List;+import java.util.Locale;++/**+ * Reads the events of an iCalendar file (RFC 5545), as calendar apps subscribe to them: Stanza, Google Calendar's+ * secret address, a team's own feed. Only what Game Day needs: each event's title, start and end. Line by line,+ * keeping only the events in a window, so a season's file (Stanza's is about 130 KB) never sits in memory whole.+ * Repeating events (RRULE) are read as their first occurrence; sports calendars list every game on its own.+ */+final class Ics {++ static final class Event {+ /** As written, emoji included; "" when the event has no SUMMARY. */+ final String title;+ final Instant start;+ /** Null when the calendar gives no end: the schedule then uses a typical length for the sport. */+ final Instant end;+ final boolean allDay;++ Event(String title, Instant start, Instant end, boolean allDay) {+ this.title = title;+ this.start = start;+ this.end = end;+ this.allDay = allDay;+ }+ }++ /** The body is not an iCalendar file: a web page (a sign-in page after a link was reset), an error, nothing. */+ static final class NotACalendar extends Exception {+ NotACalendar() {+ super("Not a calendar link");+ }+ }++ /** An event without an end is kept when it started less than this long before the window. */+ static final Duration NO_END_GRACE = Duration.ofHours(24);++ private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss", Locale.ROOT);+ private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyyMMdd", Locale.ROOT);++ private Ics() {+ }++ /**+ * The events that are still on or to come in the window: their end is after {@code from} and their start is+ * before {@code to}. Floating times and all-day dates are read in {@code zone}, the TV's time zone.+ */+ static List<Event> parse(Reader in, ZoneId zone, Instant from, Instant to) throws NotACalendar, IOException {+ BufferedReader r = in instanceof BufferedReader ? (BufferedReader) in : new BufferedReader(in);+ List<Event> events = new ArrayList<>();+ Deque<String> components = new ArrayDeque<>();+ boolean started = false;+ Draft draft = null;+ String pending = null;+ String raw;+ while (true) {+ raw = r.readLine();+ // A line that starts with a space or a tab continues the one before it (RFC 5545, 3.1).+ if (raw != null && pending != null && !raw.isEmpty() && (raw.charAt(0) == ' ' || raw.charAt(0) == '\t')) {+ pending += raw.substring(1);+ continue;+ }+ if (pending != null) {+ String line = pending;+ pending = null;+ if (!started) {+ if (line.startsWith("ο»Ώ")) line = line.substring(1);+ if (line.trim().isEmpty()) {+ if (raw == null) break;+ pending = raw;+ continue;+ }+ if (!line.trim().equalsIgnoreCase("BEGIN:VCALENDAR")) throw new NotACalendar();+ started = true;+ }+ draft = take(line, components, draft, events, zone, from, to);+ }+ if (raw == null) break;+ pending = raw;+ }+ if (!started) throw new NotACalendar();+ return events;+ }++ /** One unfolded content line. Returns the event being read, if any. */+ private static Draft take(String line, Deque<String> components, Draft draft, List<Event> out,+ ZoneId zone, Instant from, Instant to) {+ int colon = valueStart(line);+ if (colon < 0) return draft;+ String head = line.substring(0, colon);+ String value = line.substring(colon + 1);+ int semi = head.indexOf(';');+ String name = (semi < 0 ? head : head.substring(0, semi)).trim().toUpperCase(Locale.ROOT);+ String params = semi < 0 ? "" : head.substring(semi + 1);++ if (name.equals("BEGIN")) {+ String component = value.trim().toUpperCase(Locale.ROOT);+ components.push(component);+ return component.equals("VEVENT") && components.size() == 2 ? new Draft() : draft;+ }+ if (name.equals("END")) {+ String component = components.isEmpty() ? "" : components.pop();+ if (component.equals("VEVENT") && draft != null) {+ Event e = draft.finish(zone);+ if (e != null && inWindow(e, from, to)) out.add(e);+ return null;+ }+ return draft;+ }+ // Only the event's own properties: a VALARM inside it has a DESCRIPTION and TRIGGER of its own.+ if (draft == null || components.isEmpty() || !components.peek().equals("VEVENT")) return draft;+ switch (name) {+ case "SUMMARY":+ draft.title = unescape(value);+ break;+ case "DTSTART":+ draft.start = value.trim();+ draft.startParams = params;+ break;+ case "DTEND":+ draft.end = value.trim();+ draft.endParams = params;+ break;+ case "STATUS":+ draft.cancelled = value.trim().equalsIgnoreCase("CANCELLED");+ break;+ default:+ break;+ }+ return draft;+ }++ private static boolean inWindow(Event e, Instant from, Instant to) {+ if (!e.start.isBefore(to)) return false;+ Instant end = e.end != null ? e.end : e.start.plus(NO_END_GRACE);+ return end.isAfter(from);+ }++ /** The colon that ends the name and parameters; a colon inside a quoted parameter value does not count. */+ private static int valueStart(String line) {+ boolean quoted = false;+ for (int i = 0; i < line.length(); i++) {+ char c = line.charAt(i);+ if (c == '"') quoted = !quoted;+ else if (c == ':' && !quoted) return i;+ }+ return -1;+ }++ private static String unescape(String v) {+ StringBuilder b = new StringBuilder(v.length());+ for (int i = 0; i < v.length(); i++) {+ char c = v.charAt(i);+ if (c == '\\' && i + 1 < v.length()) {+ char n = v.charAt(++i);+ b.append(n == 'n' || n == 'N' ? '\n' : n);+ } else {+ b.append(c);+ }+ }+ return b.toString().trim();+ }++ private static final class Draft {+ String title = "";+ String start, startParams = "";+ String end, endParams = "";+ boolean cancelled;++ Event finish(ZoneId zone) {+ if (cancelled || start == null) return null;+ try {+ boolean allDay = isDate(start, startParams);+ Instant s = instant(start, startParams, zone);+ Instant e = end == null ? null : instant(end, endParams, zone);+ if (allDay && e == null) e = LocalDate.parse(start, DATE).plusDays(1).atStartOfDay(zone).toInstant();+ if (e != null && !e.isAfter(s)) e = null;+ return new Event(title, s, e, allDay);+ } catch (DateTimeException bad) {+ return null;+ }+ }+ }++ private static boolean isDate(String value, String params) {+ return value.length() == 8 || params.toUpperCase(Locale.ROOT).contains("VALUE=DATE") && !params.toUpperCase(Locale.ROOT).contains("VALUE=DATE-TIME");+ }++ private static Instant instant(String value, String params, ZoneId zone) {+ if (isDate(value, params)) return LocalDate.parse(value.substring(0, 8), DATE).atStartOfDay(zone).toInstant();+ if (value.endsWith("Z") || value.endsWith("z")) {+ return LocalDateTime.parse(value.substring(0, value.length() - 1), DATE_TIME).toInstant(ZoneOffset.UTC);+ }+ return LocalDateTime.parse(value, DATE_TIME).atZone(tzid(params, zone)).toInstant();+ }++ /** The TZID parameter's zone; a name Java does not know (Outlook's "Mountain Standard Time") falls back. */+ private static ZoneId tzid(String params, ZoneId fallback) {+ for (String p : params.split(";")) {+ int eq = p.indexOf('=');+ if (eq > 0 && p.substring(0, eq).trim().equalsIgnoreCase("TZID")) {+ String id = p.substring(eq + 1).trim().replace("\"", "");+ try {+ return ZoneId.of(id);+ } catch (DateTimeException unknown) {+ return fallback;+ }+ }+ }+ return fallback;+ }+}diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/IcsTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/IcsTest.javanew file mode 100644index 000000000..a00bb1233--- /dev/null+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/IcsTest.java@@ -0,0 +1,117 @@+package io.github.justinnewman.gameday;++import static org.junit.Assert.assertEquals;+import static org.junit.Assert.assertFalse;+import static org.junit.Assert.assertNotNull;+import static org.junit.Assert.assertTrue;++import java.io.InputStreamReader;+import java.io.Reader;+import java.io.StringReader;+import java.nio.charset.StandardCharsets;+import java.time.Instant;+import java.time.ZoneId;+import java.time.ZoneOffset;+import java.util.List;++import org.junit.Test;++public class IcsTest {+ private static final ZoneId UTC = ZoneOffset.UTC;+ private static final ZoneId DENVER = ZoneId.of("America/Denver");+ private static final Instant FROM = Instant.parse("2027-01-01T00:00:00Z");+ private static final Instant TO = Instant.parse("2028-01-01T00:00:00Z");++ private static String calendar(String... eventLines) {+ StringBuilder b = new StringBuilder("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\n");+ for (String l : eventLines) b.append(l).append("\r\n");+ return b.append("END:VEVENT\r\nEND:VCALENDAR\r\n").toString();+ }++ private static List<Ics.Event> parse(String text, ZoneId zone, Instant from, Instant to) throws Exception {+ return Ics.parse(new StringReader(text), zone, from, to);+ }++ @Test public void readsStanzaEvents() throws Exception {+ try (Reader r = new InputStreamReader(getClass().getResourceAsStream("/stanza-sample.ics"), StandardCharsets.UTF_8)) {+ List<Ics.Event> events = Ics.parse(r, UTC, FROM, TO);+ assertEquals(5, events.size());+ Ics.Event first = events.get(0);+ assertEquals("π Dallas Stars @ Minnesota Wild", first.title);+ assertEquals(Instant.parse("2027-04-03T00:00:00Z"), first.start);+ assertEquals(Instant.parse("2027-04-03T03:00:00Z"), first.end);+ assertFalse(first.allDay);+ assertEquals("π Colorado Avalanche @ Minnesota Wild", events.get(1).title);+ }+ }++ @Test public void unfoldsLongLines() throws Exception {+ List<Ics.Event> e = parse(calendar("DTSTART:20270403T000000Z", "SUMMARY:π Minnesota Wild @ Dal", " las Stars"), UTC, FROM, TO);+ assertEquals("π Minnesota Wild @ Dallas Stars", e.get(0).title);+ }++ @Test public void unescapesText() throws Exception {+ List<Ics.Event> e = parse(calendar("DTSTART:20270403T000000Z", "SUMMARY:Wild\\, Stars\\; and more"), UTC, FROM, TO);+ assertEquals("Wild, Stars; and more", e.get(0).title);+ }++ @Test public void tzidAndFloatingTimes() throws Exception {+ Instant from = Instant.parse("2026-10-01T00:00:00Z");+ Instant to = Instant.parse("2026-11-01T00:00:00Z");+ Instant expected = Instant.parse("2026-10-28T00:00:00Z");+ assertEquals(expected, parse(calendar("DTSTART;TZID=America/Denver:20261027T180000", "SUMMARY:A"), UTC, from, to).get(0).start);+ assertEquals(expected, parse(calendar("DTSTART;TZID=\"America/Denver\":20261027T180000", "SUMMARY:A"), UTC, from, to).get(0).start);+ assertEquals(expected, parse(calendar("DTSTART:20261027T180000", "SUMMARY:A"), DENVER, from, to).get(0).start);+ // A time zone name Java does not know falls back to the TV's zone.+ assertEquals(expected, parse(calendar("DTSTART;TZID=Mountain Standard Time:20261027T180000", "SUMMARY:A"), DENVER, from, to).get(0).start);+ }++ @Test public void allDayEvents() throws Exception {+ Instant from = Instant.parse("2026-10-01T00:00:00Z");+ Instant to = Instant.parse("2026-11-01T00:00:00Z");+ Ics.Event one = parse(calendar("DTSTART;VALUE=DATE:20261027", "SUMMARY:Draft"), DENVER, from, to).get(0);+ assertTrue(one.allDay);+ assertEquals(Instant.parse("2026-10-27T06:00:00Z"), one.start);+ assertEquals(Instant.parse("2026-10-28T06:00:00Z"), one.end);+ Ics.Event two = parse(calendar("DTSTART;VALUE=DATE:20261027", "DTEND;VALUE=DATE:20261029", "SUMMARY:Camp"), DENVER, from, to).get(0);+ assertEquals(Instant.parse("2026-10-29T06:00:00Z"), two.end);+ }++ @Test public void cancelledAreSkipped() throws Exception {+ assertTrue(parse(calendar("DTSTART:20270403T000000Z", "STATUS:CANCELLED", "SUMMARY:A"), UTC, FROM, TO).isEmpty());+ }++ @Test public void onlyTheWindowIsKept() throws Exception {+ Instant from = Instant.parse("2027-04-03T12:00:00Z");+ Instant to = Instant.parse("2027-04-10T12:00:00Z");+ assertTrue("ended before the window", parse(calendar("DTSTART:20270403T080000Z", "DTEND:20270403T110000Z", "SUMMARY:A"), UTC, from, to).isEmpty());+ assertTrue("starts at the window's end", parse(calendar("DTSTART:20270410T120000Z", "SUMMARY:A"), UTC, from, to).isEmpty());+ assertEquals("still on at the window's start", 1, parse(calendar("DTSTART:20270403T100000Z", "DTEND:20270403T130000Z", "SUMMARY:A"), UTC, from, to).size());+ // Without an end the schedule decides how long it runs, so a start up to a day back is kept.+ assertEquals(1, parse(calendar("DTSTART:20270403T100000Z", "SUMMARY:A"), UTC, from, to).size());+ assertTrue(parse(calendar("DTSTART:20270401T100000Z", "SUMMARY:A"), UTC, from, to).isEmpty());+ }++ @Test(expected = Ics.NotACalendar.class)+ public void notACalendarIsRefused() throws Exception {+ parse("<!DOCTYPE html><html>Sign in</html>", UTC, FROM, TO);+ }++ @Test(expected = Ics.NotACalendar.class)+ public void emptyBodyIsRefused() throws Exception {+ parse("", UTC, FROM, TO);+ }++ @Test public void crlfAndLfBothWork() throws Exception {+ String crlf = calendar("DTSTART:20270403T000000Z", "SUMMARY:A");+ assertEquals(1, parse(crlf, UTC, FROM, TO).size());+ assertEquals(1, parse(crlf.replace("\r\n", "\n"), UTC, FROM, TO).size());+ assertEquals(1, parse("ο»Ώ" + crlf, UTC, FROM, TO).size());+ }++ @Test public void eventWithoutSummaryHasAnEmptyTitle() throws Exception {+ Ics.Event e = parse(calendar("DTSTART:20270403T000000Z"), UTC, FROM, TO).get(0);+ assertNotNull(e.title);+ assertEquals("", e.title);+ }+}diff --git a/addons/game-day/app/src/test/resources/stanza-sample.ics b/addons/game-day/app/src/test/resources/stanza-sample.icsnew file mode 100644index 000000000..2963c865a--- /dev/null+++ b/addons/game-day/app/src/test/resources/stanza-sample.ics@@ -0,0 +1,55 @@+BEGIN:VCALENDAR+PRODID:-//Google Inc//Google Calendar 70.9054//EN+VERSION:2.0+CALSCALE:GREGORIAN+METHOD:PUBLISH+X-WR-CALNAME:NHL+X-WR-TIMEZONE:UTC+BEGIN:VEVENT+DTSTART:20270403T000000Z+DTEND:20270403T030000Z+DTSTAMP:20261026T120000Z+UID:[email protected]+DESCRIPTION:Regular Season\, Grand Casino Arena\nTickets\, merchandise and more+LOCATION:Grand Casino Arena+SEQUENCE:0+STATUS:CONFIRMED+SUMMARY:π Dallas Stars @ Minnesota Wild+TRANSP:TRANSPARENT+END:VEVENT+BEGIN:VEVENT+DTSTART:20270404T180000Z+DTEND:20270404T210000Z+UID:[email protected]+STATUS:CONFIRMED+SUMMARY:π Colorado Avalanche @ Minnesota Wild+BEGIN:VALARM+ACTION:DISPLAY+TRIGGER:-PT30M+DESCRIPTION:Reminder+END:VALARM+END:VEVENT+BEGIN:VEVENT+DTSTART:20270410T190000Z+DTEND:20270410T220000Z+UID:[email protected]+STATUS:CONFIRMED+DESCRIPTION:Post-Season - a description long enough that Google folds it onto a second line like th+ is one+SUMMARY:π Minnesota Wild @ Colorado Avalanche+END:VEVENT+BEGIN:VEVENT+DTSTART:20270625T230000Z+DTEND:20270626T030000Z+UID:[email protected]+STATUS:CONFIRMED+SUMMARY:π 2027 NHL Draft+END:VEVENT+BEGIN:VEVENT+DTSTART:20270101T120000Z+DTEND:20270101T130000Z+UID:[email protected]+STATUS:CONFIRMED+SUMMARY:Welcome to the Minnesota Wild calendar!+END:VEVENT+END:VCALENDAR