Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

addons/game-day: each calendar's next entry within a week, one turn each

Justin Newman4d36a5f11759
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Schedule.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Schedule.java
new file mode 100644
index 000000000..302b756f2
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Schedule.java
@@ -0,0 +1,84 @@
+package io.github.justinnewman.gameday;
+
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * What Home rotates through: one turn per calendar, its next entry that has not ended, if that starts within a
+ * week. A game in two calendars (following both teams) is one turn, in the colour of the calendar listed first.
+ */
+final class Schedule {
+ static final Duration WINDOW = Duration.ofDays(7);
+
+ static final class Turn {
+ final String topLine;
+ final String word;
+ final String calendar;
+ final long startMs;
+ final long endMs;
+ /** ARGB. */
+ final int colour;
+
+ Turn(String topLine, String word, String calendar, long startMs, long endMs, int colour) {
+ this.topLine = topLine;
+ this.word = word;
+ this.calendar = calendar;
+ this.startMs = startMs;
+ this.endMs = endMs;
+ this.colour = colour;
+ }
+ }
+
+ private Schedule() {
+ }
+
+ static List<Turn> turns(List<Calendar> calendars, Map<String, List<Ics.Event>> eventsByCalendarId, Instant now,
+ ZoneId zone) {
+ List<Turn> out = new ArrayList<>();
+ Set<String> seen = new HashSet<>();
+ Instant horizon = now.plus(WINDOW);
+ for (Calendar c : calendars) {
+ List<Ics.Event> events = eventsByCalendarId.get(c.id);
+ if (events == null || events.isEmpty()) continue;
+ List<Ics.Event> sorted = new ArrayList<>(events);
+ sorted.sort(Comparator.comparing(e -> e.start));
+ for (Ics.Event e : sorted) {
+ Instant end = endOf(e, c.sport, zone);
+ if (!end.isAfter(now)) continue;
+ if (e.start.isBefore(horizon)) {
+ String top = Titles.topLine(e.title, c.name);
+ if (seen.add(top.toLowerCase(Locale.ROOT) + "|" + e.start.toEpochMilli())) {
+ out.add(new Turn(top, Titles.word(c.sport, e.title), c.name, e.start.toEpochMilli(),
+ end.toEpochMilli(), Palette.colour(c.colour)));
+ }
+ }
+ break;
+ }
+ }
+ Collections.sort(out, Comparator.comparingLong(t -> t.startMs));
+ return out;
+ }
+
+ /** The calendar's end, or a typical length: 3 hours, 2 for motor racing, the rest of the day for golf. */
+ static Instant endOf(Ics.Event e, Sport sport, ZoneId zone) {
+ if (e.end != null) return e.end;
+ switch (sport) {
+ case GOLF:
+ return e.start.atZone(zone).toLocalDate().plusDays(1).atStartOfDay(zone).toInstant();
+ case MOTOR_FLAG:
+ case MOTOR_LIGHTS:
+ return e.start.plus(Duration.ofHours(2));
+ default:
+ return e.start.plus(Duration.ofHours(3));
+ }
+ }
+}
diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/ScheduleTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/ScheduleTest.java
new file mode 100644
index 000000000..1378aeafb
--- /dev/null
+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/ScheduleTest.java
@@ -0,0 +1,123 @@
+package io.github.justinnewman.gameday;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+
+public class ScheduleTest {
+ private static final Instant NOW = Instant.parse("2027-04-02T12:00:00Z");
+ private static final ZoneId UTC = ZoneOffset.UTC;
+ private static final Calendar WILD = cal("Minnesota Wild", "Hockey", 16);
+ private static final Calendar STARS = cal("Dallas Stars", "Hockey", 28);
+ private static final Calendar ROYALS = cal("Utah Royals", "Soccer", 14);
+
+ private static Calendar cal(String name, String sport, int colour) {
+ String link = "https://h.example/" + name.replace(' ', '-');
+ return new Calendar(Links.id(link), name, link, Sport.byLabel(sport), colour);
+ }
+
+ private static Ics.Event ev(String title, Duration fromNow, Duration length) {
+ Instant start = NOW.plus(fromNow);
+ return new Ics.Event(title, start, length == null ? null : start.plus(length), false);
+ }
+
+ private static List<Schedule.Turn> turns(Map<String, List<Ics.Event>> events, Calendar... calendars) {
+ return Schedule.turns(Arrays.asList(calendars), events, NOW, UTC);
+ }
+
+ private static Map<String, List<Ics.Event>> events(Object... calendarAndEvents) {
+ Map<String, List<Ics.Event>> m = new HashMap<>();
+ for (int i = 0; i < calendarAndEvents.length; i += 2) {
+ @SuppressWarnings("unchecked") List<Ics.Event> list = (List<Ics.Event>) calendarAndEvents[i + 1];
+ m.put(((Calendar) calendarAndEvents[i]).id, list);
+ }
+ return m;
+ }
+
+ @Test public void oneTurnPerCalendarItsNextEntry() {
+ List<Schedule.Turn> t = turns(events(WILD, Arrays.asList(
+ ev("πŸ’ Minnesota Wild @ Dallas Stars", Duration.ofDays(2), Duration.ofHours(3)),
+ ev("πŸ’ Colorado Avalanche @ Minnesota Wild", Duration.ofHours(1), Duration.ofHours(3)))), WILD);
+ assertEquals(1, t.size());
+ Schedule.Turn turn = t.get(0);
+ assertEquals("Colorado Avalanche at Minnesota Wild", turn.topLine);
+ assertEquals("Puck drop", turn.word);
+ assertEquals(NOW.plus(Duration.ofHours(1)).toEpochMilli(), turn.startMs);
+ assertEquals(NOW.plus(Duration.ofHours(4)).toEpochMilli(), turn.endMs);
+ assertEquals(Palette.colour(16), turn.colour);
+ assertEquals("Minnesota Wild", turn.calendar);
+ }
+
+ @Test public void liveEntryStaysUntilItsEnd() {
+ List<Schedule.Turn> t = turns(events(WILD, Arrays.asList(
+ ev("πŸ’ Minnesota Wild @ Dallas Stars", Duration.ofHours(-1), Duration.ofHours(3)),
+ ev("πŸ’ Minnesota Wild @ Utah Mammoth", Duration.ofDays(2), Duration.ofHours(3)))), WILD);
+ assertEquals("Minnesota Wild at Dallas Stars", t.get(0).topLine);
+ }
+
+ @Test public void endedEntryGivesWayToTheNext() {
+ List<Schedule.Turn> t = turns(events(WILD, Arrays.asList(
+ ev("πŸ’ Minnesota Wild @ Dallas Stars", Duration.ofHours(-3).minusMinutes(5), Duration.ofHours(3)),
+ ev("πŸ’ Minnesota Wild @ Utah Mammoth", Duration.ofDays(2), Duration.ofHours(3)))), WILD);
+ assertEquals("Minnesota Wild at Utah Mammoth", t.get(0).topLine);
+ }
+
+ @Test public void eightDaysOutIsLeftOut() {
+ assertTrue(turns(events(WILD, Arrays.asList(ev("πŸ’ A @ B", Duration.ofDays(8), Duration.ofHours(3)))), WILD).isEmpty());
+ assertEquals(1, turns(events(WILD, Arrays.asList(ev("πŸ’ A @ B", Duration.ofDays(7).minusMinutes(1), Duration.ofHours(3)))), WILD).size());
+ }
+
+ @Test public void pastOnlyCalendarHasNoTurn() {
+ assertTrue(turns(events(WILD, Arrays.asList(ev("πŸ’ A @ B", Duration.ofDays(-30), Duration.ofHours(3)))), WILD).isEmpty());
+ assertTrue(turns(events(), WILD).isEmpty());
+ }
+
+ @Test public void defaultLengths() {
+ Ics.Event noEnd = ev("x", Duration.ZERO, null);
+ assertEquals(NOW.plus(Duration.ofHours(3)), Schedule.endOf(noEnd, Sport.byLabel("Hockey"), UTC));
+ assertEquals(NOW.plus(Duration.ofHours(2)), Schedule.endOf(noEnd, Sport.byLabel("Motor racing, light start"), UTC));
+ assertEquals(NOW.plus(Duration.ofHours(2)), Schedule.endOf(noEnd, Sport.byLabel("Motor racing, flag start"), UTC));
+ assertEquals(NOW.plus(Duration.ofHours(3)), Schedule.endOf(noEnd, Sport.byLabel("Darts"), UTC));
+ assertEquals(Instant.parse("2027-04-03T00:00:00Z"), Schedule.endOf(noEnd, Sport.byLabel("Golf"), UTC));
+ assertEquals(Instant.parse("2027-04-03T06:00:00Z"), Schedule.endOf(noEnd, Sport.byLabel("Golf"), ZoneId.of("America/Denver")));
+ Ics.Event withEnd = ev("x", Duration.ZERO, Duration.ofMinutes(150));
+ assertEquals(NOW.plus(Duration.ofMinutes(150)), Schedule.endOf(withEnd, Sport.byLabel("Golf"), UTC));
+ }
+
+ @Test public void sameGameInTwoCalendarsShowsOnceInTheFirstColour() {
+ Ics.Event game = ev("πŸ’ Minnesota Wild @ Dallas Stars", Duration.ofHours(5), Duration.ofHours(3));
+ List<Schedule.Turn> t = turns(events(WILD, Arrays.asList(game), STARS, Arrays.asList(
+ new Ics.Event("Minnesota Wild at Dallas Stars", game.start, game.end, false))), WILD, STARS);
+ assertEquals(1, t.size());
+ assertEquals(Palette.colour(16), t.get(0).colour);
+ assertEquals("Minnesota Wild", t.get(0).calendar);
+ }
+
+ @Test public void sortedBySoonestStart() {
+ List<Schedule.Turn> t = turns(events(
+ WILD, Arrays.asList(ev("πŸ’ Minnesota Wild @ Dallas Stars", Duration.ofDays(3), Duration.ofHours(3))),
+ ROYALS, Arrays.asList(ev("⚽ Utah Royals vs Portland Thorns", Duration.ofHours(6), Duration.ofHours(2)))),
+ WILD, ROYALS);
+ assertEquals(2, t.size());
+ assertEquals("Portland Thorns at Utah Royals", t.get(0).topLine);
+ assertEquals("Kickoff", t.get(0).word);
+ assertEquals("Minnesota Wild at Dallas Stars", t.get(1).topLine);
+ }
+
+ @Test public void entriesNeedNotBeInOrder() {
+ List<Schedule.Turn> t = turns(events(WILD, Arrays.asList(
+ ev("πŸ’ Late @ Game", Duration.ofDays(4), Duration.ofHours(3)),
+ ev("πŸ’ Early @ Game", Duration.ofDays(1), Duration.ofHours(3)))), WILD);
+ assertEquals("Early at Game", t.get(0).topLine);
+ }
+}