Skip to content

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

addons/game-day: settings on the TV, adding a calendar with the remote, 36 colours

Justin Newmanf4fa75078304
diff --git a/addons/game-day/app/src/main/AndroidManifest.xml b/addons/game-day/app/src/main/AndroidManifest.xml
index a6c22f6a8..a31a4bf42 100644
--- a/addons/game-day/app/src/main/AndroidManifest.xml
+++ b/addons/game-day/app/src/main/AndroidManifest.xml
@@ -9,8 +9,31 @@
<application
android:allowBackup="false"
+ android:banner="@drawable/banner"
+ android:icon="@drawable/icon"
android:label="@string/app_name"
- android:supportsRtl="false">
+ android:supportsRtl="false"
+ android:theme="@style/Screen">
+
+ <activity
+ android:name=".SettingsActivity"
+ android:exported="true"
+ android:screenOrientation="landscape">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
+ </intent-filter>
+ <!-- Clean Your TV module card (docs/specs/card.md): the tile reads "Game Day" once the owner says Show. -->
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="com.gathin.tvhome.category.DOCK_CARD" />
+ </intent-filter>
+ <meta-data android:name="com.gathin.tvhome.card" android:resource="@xml/module_card" />
+ </activity>
+ <activity android:name=".CalendarActivity" android:exported="false" android:screenOrientation="landscape" />
+ <activity android:name=".AddCalendarActivity" android:exported="false" android:screenOrientation="landscape"
+ android:windowSoftInputMode="adjustNothing" />
+ <activity android:name=".SecondsActivity" android:exported="false" android:screenOrientation="landscape" />
<!-- Clean Your TV launcher, game_day (docs/modules.md): where Home reads each team's next game. -->
<meta-data android:name="com.gathin.tvhome.games" android:value="io.github.justinnewman.gameday.games" />
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/AddCalendarActivity.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/AddCalendarActivity.java
new file mode 100644
index 000000000..a309d1a53
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/AddCalendarActivity.java
@@ -0,0 +1,341 @@
+package io.github.justinnewman.gameday;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.graphics.drawable.GradientDrawable;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.text.InputType;
+import android.view.Gravity;
+import android.view.KeyEvent;
+import android.view.View;
+import android.view.inputmethod.EditorInfo;
+import android.widget.EditText;
+import android.widget.LinearLayout;
+import android.widget.ScrollView;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import java.time.ZoneId;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Adding a calendar with the remote: 1 the link (fetched and checked before going on), 2 a name, 3 the sport,
+ * 4 a colour, with a preview of the turn on Home. Opened with {@link #edit} it shows one step to change one thing.
+ */
+public final class AddCalendarActivity extends Activity {
+ static final int STEP_LINK = 0;
+ static final int STEP_NAME = 1;
+ static final int STEP_SPORT = 2;
+ static final int STEP_COLOUR = 3;
+ private static final String EXTRA_ID = "id";
+ private static final String EXTRA_STEP = "step";
+
+ private final Handler main = new Handler(Looper.getMainLooper());
+ private Repository repo;
+ private TextView note;
+ private EditText field;
+ private ScrollView sportScroll;
+ private LinearLayout sports;
+ private ColourGrid grid;
+ private TextView colourName;
+ private View preview;
+
+ /** Null when adding. */
+ private String editId;
+ /** The one step to show when changing a calendar; -1 for the four steps. */
+ private int only = -1;
+ private int step;
+ private String link;
+ private String name = "";
+ private Sport sport = Sport.OTHER;
+ private int colour = Palette.COLUMNS;
+ private int entries;
+ private Ics.Event nextEvent;
+ private boolean busy;
+
+ static Intent edit(Context ctx, String id, int step) {
+ return new Intent(ctx, AddCalendarActivity.class).putExtra(EXTRA_ID, id).putExtra(EXTRA_STEP, step);
+ }
+
+ @Override
+ protected void onCreate(Bundle saved) {
+ super.onCreate(saved);
+ setContentView(R.layout.activity_add);
+ repo = new Repository(this);
+ note = findViewById(R.id.note);
+ field = findViewById(R.id.field);
+ sportScroll = findViewById(R.id.sportScroll);
+ sports = findViewById(R.id.sports);
+ grid = findViewById(R.id.grid);
+ colourName = findViewById(R.id.colourName);
+ preview = findViewById(R.id.preview);
+ findViewById(R.id.next).setOnClickListener(v -> next());
+ field.setOnEditorActionListener((v, action, event) -> {
+ if (action == EditorInfo.IME_ACTION_NEXT || action == EditorInfo.IME_ACTION_DONE
+ || event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
+ next();
+ return true;
+ }
+ return false;
+ });
+ buildSports();
+ editId = getIntent().getStringExtra(EXTRA_ID);
+ if (editId == null) {
+ Background.read(() -> {
+ Set<Integer> used = usedColours(null);
+ colour = Palette.firstUnused(used);
+ main.post(() -> show(STEP_LINK));
+ });
+ return;
+ }
+ only = getIntent().getIntExtra(EXTRA_STEP, STEP_NAME);
+ Background.read(() -> {
+ Calendar c = repo.find(editId);
+ if (c == null) {
+ main.post(this::finish);
+ return;
+ }
+ link = c.link;
+ name = c.name;
+ sport = c.sport;
+ colour = c.colour;
+ nextEvent = repo.nextEvent(c, System.currentTimeMillis(), ZoneId.systemDefault());
+ main.post(() -> {
+ ((TextView) findViewById(R.id.title)).setText(c.name);
+ findViewById(R.id.steps).setVisibility(View.GONE);
+ show(only);
+ });
+ });
+ }
+
+ private void show(int s) {
+ step = s;
+ int[] labels = {R.id.step1, R.id.step2, R.id.step3, R.id.step4};
+ for (int i = 0; i < labels.length; i++) {
+ ((TextView) findViewById(labels[i])).setTextColor(i == s ? 0xFFFFFFFF : i < s ? 0xB3FFFFFF : 0x66FFFFFF);
+ }
+ boolean typing = s == STEP_LINK || s == STEP_NAME;
+ field.setVisibility(typing ? View.VISIBLE : View.GONE);
+ findViewById(R.id.next).setVisibility(typing ? View.VISIBLE : View.GONE);
+ sportScroll.setVisibility(s == STEP_SPORT ? View.VISIBLE : View.GONE);
+ grid.setVisibility(s == STEP_COLOUR ? View.VISIBLE : View.GONE);
+ colourName.setVisibility(s == STEP_COLOUR ? View.VISIBLE : View.GONE);
+ preview.setVisibility(s == STEP_SPORT || s == STEP_COLOUR ? View.VISIBLE : View.GONE);
+ note.setTextColor(0x8CFFFFFF);
+ switch (s) {
+ case STEP_LINK:
+ note.setText("");
+ field.setText("");
+ field.setHint(R.string.link_hint);
+ field.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
+ field.requestFocus();
+ break;
+ case STEP_NAME:
+ note.setText(only < 0 ? getString(R.string.checked,
+ getResources().getQuantityString(R.plurals.entries, entries, entries)) : "");
+ field.setText(name);
+ field.setSelection(name.length());
+ field.setHint("");
+ field.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
+ field.requestFocus();
+ break;
+ case STEP_SPORT:
+ note.setText("");
+ for (int i = 0; i < sports.getChildCount(); i++) {
+ View row = sports.getChildAt(i);
+ if (row.getTag() == sport) row.requestFocus();
+ }
+ updatePreview(sport);
+ break;
+ default:
+ note.setText(R.string.colour_hint);
+ Background.read(() -> {
+ Set<Integer> used = usedColours(editId);
+ main.post(() -> {
+ grid.set(colour, used, new ColourGrid.Listener() {
+ @Override
+ public void onMoved(int index) {
+ colour = index;
+ colourName.setText(Palette.name(index));
+ updatePreview(sport);
+ }
+
+ @Override
+ public void onChosen(int index) {
+ colour = index;
+ save();
+ }
+ });
+ colourName.setText(Palette.name(colour));
+ grid.requestFocus();
+ updatePreview(sport);
+ });
+ });
+ break;
+ }
+ }
+
+ private void buildSports() {
+ float d = getResources().getDisplayMetrics().density;
+ for (Sport s : Sport.values()) {
+ LinearLayout row = new LinearLayout(this);
+ row.setOrientation(LinearLayout.HORIZONTAL);
+ row.setBackgroundResource(R.drawable.row);
+ row.setFocusable(true);
+ row.setClickable(true);
+ row.setPadding(Math.round(16 * d), Math.round(9 * d), Math.round(16 * d), Math.round(9 * d));
+ row.setTag(s);
+ TextView label = new TextView(this);
+ label.setText(s.label());
+ label.setTextSize(16);
+ label.setTextColor(0xE6FFFFFF);
+ TextView word = new TextView(this);
+ word.setText(s.word());
+ word.setTextSize(14);
+ word.setTextColor(0x80FFFFFF);
+ word.setGravity(Gravity.END);
+ row.addView(label, new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
+ row.addView(word, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
+ row.setOnFocusChangeListener((v, focused) -> {
+ if (focused) updatePreview(s);
+ });
+ row.setOnClickListener(v -> {
+ sport = s;
+ if (only >= 0) save();
+ else show(STEP_COLOUR);
+ });
+ LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
+ LinearLayout.LayoutParams.WRAP_CONTENT);
+ lp.bottomMargin = Math.round(6 * d);
+ sports.addView(row, lp);
+ }
+ }
+
+ private void updatePreview(Sport s) {
+ GradientDrawable bar = new GradientDrawable();
+ bar.setColor(Palette.colour(colour));
+ bar.setCornerRadius(2 * getResources().getDisplayMetrics().density);
+ findViewById(R.id.previewBar).setBackground(bar);
+ TextView top = findViewById(R.id.previewTop);
+ TextView word = findViewById(R.id.previewWord);
+ if (nextEvent == null) {
+ top.setText(name);
+ word.setText(R.string.no_entry);
+ return;
+ }
+ top.setText(Titles.topLine(nextEvent.title, name));
+ long until = nextEvent.start.toEpochMilli() - System.currentTimeMillis();
+ word.setText(until <= 0 ? getString(R.string.live_now) : Titles.word(s, nextEvent.title) + " in " + Countdown.text(until));
+ }
+
+ private void next() {
+ if (busy) return;
+ if (step == STEP_LINK) checkLink();
+ else if (step == STEP_NAME) {
+ String n = field.getText().toString().trim();
+ if (n.isEmpty() || n.length() > CalendarStore.MAX_NAME) {
+ error(getString(R.string.name_bad));
+ return;
+ }
+ name = n;
+ if (only >= 0) save();
+ else show(STEP_SPORT);
+ }
+ }
+
+ private void checkLink() {
+ String l = Links.normalise(field.getText().toString());
+ if (l == null) {
+ error(getString(R.string.link_bad));
+ return;
+ }
+ busy = true;
+ note.setTextColor(0x8CFFFFFF);
+ note.setText(R.string.checking);
+ Background.execute(() -> {
+ String id = Links.id(l);
+ if (repo.find(id) != null && !id.equals(editId)) {
+ main.post(() -> {
+ busy = false;
+ error(getString(R.string.link_twice));
+ });
+ return;
+ }
+ long now = System.currentTimeMillis();
+ ZoneId zone = ZoneId.systemDefault();
+ Calendar probe = new Calendar(id, name, l, sport, colour);
+ FetchStatus s = new Fetcher().fetch(probe, repo.dir(), null, now);
+ if (s.error != null) {
+ main.post(() -> {
+ busy = false;
+ error(s.error);
+ });
+ return;
+ }
+ repo.saveStatus(id, s);
+ List<String> titles = repo.titles(probe, now, zone);
+ Ics.Event next = repo.nextEvent(probe, now, zone);
+ String team = Titles.likelyTeam(titles);
+ Sport guess = null;
+ for (String t : titles) {
+ guess = Sport.fromEmoji(t);
+ if (guess != null) break;
+ }
+ Sport guessed = guess;
+ main.post(() -> {
+ busy = false;
+ link = l;
+ entries = s.entries;
+ nextEvent = next;
+ if (only >= 0) {
+ save();
+ return;
+ }
+ if (team != null) name = team;
+ if (guessed != null) sport = guessed;
+ show(STEP_NAME);
+ });
+ });
+ }
+
+ /** Adds the new calendar, or puts the changed one back in its place. */
+ private void save() {
+ Calendar c = new Calendar(Links.id(link), name, link, sport, colour);
+ Background.read(() -> {
+ boolean ok = true;
+ if (editId == null) ok = repo.add(c);
+ else repo.replace(editId, c);
+ GamesProvider.changed(getApplicationContext());
+ boolean added = ok;
+ main.post(() -> {
+ if (!added) Toast.makeText(this, R.string.full, Toast.LENGTH_SHORT).show();
+ finish();
+ });
+ });
+ }
+
+ private void error(String text) {
+ note.setTextColor(0xFFFF8A80);
+ note.setText(text);
+ }
+
+ /** Colours other calendars use (not the one being changed). */
+ private Set<Integer> usedColours(String except) {
+ Set<Integer> used = new HashSet<>();
+ for (Calendar c : repo.calendars()) {
+ if (!c.id.equals(except)) used.add(c.colour);
+ }
+ return used;
+ }
+
+ @Override
+ public void onBackPressed() {
+ if (only < 0 && step > STEP_LINK && !busy) show(step - 1);
+ else super.onBackPressed();
+ }
+}
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Ago.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Ago.java
new file mode 100644
index 000000000..1a4ed92b8
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Ago.java
@@ -0,0 +1,25 @@
+package io.github.justinnewman.gameday;
+
+/** "2 hours ago", for the settings screens. */
+final class Ago {
+ private static final long MINUTE = 60_000L;
+ private static final long HOUR = 60 * MINUTE;
+ private static final long DAY = 24 * HOUR;
+
+ private Ago() {
+ }
+
+ static String text(long nowMs, long thenMs) {
+ if (thenMs <= 0) return "never";
+ long d = Math.max(0, nowMs - thenMs);
+ if (d < MINUTE) return "just now";
+ if (d < HOUR) return plural(d / MINUTE, "minute");
+ if (d < DAY) return plural(d / HOUR, "hour");
+ if (d < 2 * DAY) return "yesterday";
+ return plural(d / DAY, "day");
+ }
+
+ private static String plural(long n, String unit) {
+ return n + " " + unit + (n == 1 ? "" : "s") + " ago";
+ }
+}
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Background.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Background.java
index 7640dfb95..ac048a50f 100644
--- a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Background.java
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Background.java
@@ -18,15 +18,23 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/
final class Background {
private static final ExecutorService ONE = Executors.newSingleThreadExecutor();
+ /** Reading the saved files for a screen; never waits behind a slow fetch. */
+ private static final ExecutorService READ = Executors.newSingleThreadExecutor();
private static final AtomicBoolean REFRESHING = new AtomicBoolean();
private Background() {
}
+ /** Network work: fetches. */
static void execute(Runnable r) {
ONE.execute(r);
}
+ /** File work for a screen. */
+ static void read(Runnable r) {
+ READ.execute(r);
+ }
+
/** Fetch the calendars that are due; tell the launcher when one came in. */
static void refresh(Context ctx, long nowMs) {
Context app = ctx.getApplicationContext();
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.java
index bdd50d39f..8584222e8 100644
--- 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.java
@@ -19,6 +19,23 @@ final class Calendar {
this.colour = colour;
}
+ Calendar withName(String n) {
+ return new Calendar(id, n, link, sport, colour);
+ }
+
+ Calendar withSport(Sport s) {
+ return new Calendar(id, name, link, s, colour);
+ }
+
+ Calendar withColour(int c) {
+ return new Calendar(id, name, link, sport, c);
+ }
+
+ /** A new link is a new calendar to fetch: its id follows the link. */
+ Calendar withLink(String l) {
+ return new Calendar(Links.id(l), name, l, sport, colour);
+ }
+
@Override
public String toString() {
return name + " (" + Links.mask(link) + ")";
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/CalendarActivity.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/CalendarActivity.java
new file mode 100644
index 000000000..7b00f45a9
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/CalendarActivity.java
@@ -0,0 +1,104 @@
+package io.github.justinnewman.gameday;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.widget.TextView;
+
+import java.time.ZoneId;
+
+/** One calendar: its sport, colour, name and link to change, fetching it, moving it up, removing it. */
+public final class CalendarActivity extends Activity {
+ static final String EXTRA_ID = "id";
+
+ private final Handler main = new Handler(Looper.getMainLooper());
+ private Repository repo;
+ private String id;
+
+ @Override
+ protected void onCreate(Bundle saved) {
+ super.onCreate(saved);
+ setContentView(R.layout.activity_calendar);
+ repo = new Repository(this);
+ id = getIntent().getStringExtra(EXTRA_ID);
+ findViewById(R.id.sport).setOnClickListener(v -> startActivity(AddCalendarActivity.edit(this, id, AddCalendarActivity.STEP_SPORT)));
+ findViewById(R.id.colour).setOnClickListener(v -> startActivity(AddCalendarActivity.edit(this, id, AddCalendarActivity.STEP_COLOUR)));
+ findViewById(R.id.name).setOnClickListener(v -> startActivity(AddCalendarActivity.edit(this, id, AddCalendarActivity.STEP_NAME)));
+ findViewById(R.id.link).setOnClickListener(v -> {
+ startActivity(AddCalendarActivity.edit(this, id, AddCalendarActivity.STEP_LINK));
+ // A new link is a new id: this screen's calendar is gone once it is saved.
+ finish();
+ });
+ findViewById(R.id.fetch).setOnClickListener(v -> Background.execute(() -> {
+ Calendar c = repo.find(id);
+ if (c == null) return;
+ repo.fetch(new Fetcher(), c, System.currentTimeMillis());
+ GamesProvider.changed(getApplicationContext());
+ main.post(this::load);
+ }));
+ findViewById(R.id.up).setOnClickListener(v -> Background.read(() -> {
+ repo.moveUp(id);
+ GamesProvider.changed(getApplicationContext());
+ }));
+ findViewById(R.id.remove).setOnClickListener(v -> confirmRemove());
+ findViewById(R.id.sport).requestFocus();
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ load();
+ }
+
+ private void load() {
+ Background.read(() -> {
+ Calendar c = repo.find(id);
+ if (c == null) {
+ main.post(this::finish);
+ return;
+ }
+ long now = System.currentTimeMillis();
+ ZoneId zone = ZoneId.systemDefault();
+ FetchStatus s = repo.status(c.id);
+ String sub;
+ if (s != null && s.error != null) {
+ sub = s.lastSuccessMs > 0 ? getString(R.string.failed, Ago.text(now, s.lastSuccessMs), s.error)
+ : getString(R.string.failed_never, s.error);
+ } else {
+ Schedule.Turn t = repo.next(c, now, zone);
+ String next = t == null ? Repository.NOTHING : Repository.when(t, zone) + ", " + t.topLine;
+ int n = s == null ? 0 : s.entries;
+ sub = getString(R.string.loaded, getResources().getQuantityString(R.plurals.entries, n, n), next);
+ }
+ main.post(() -> show(c, sub));
+ });
+ }
+
+ private void show(Calendar c, String sub) {
+ ((TextView) findViewById(R.id.title)).setText(c.name);
+ ((TextView) findViewById(R.id.sub)).setText(sub);
+ ((TextView) findViewById(R.id.sport)).setText(getString(R.string.sport) + " " + c.sport.label() + " · " + c.sport.word());
+ ((TextView) findViewById(R.id.colour)).setText(getString(R.string.colour) + " " + Palette.name(c.colour));
+ ((TextView) findViewById(R.id.name)).setText(getString(R.string.name) + " " + c.name);
+ ((TextView) findViewById(R.id.link)).setText(getString(R.string.link) + " " + Links.mask(c.link));
+ }
+
+ private void confirmRemove() {
+ Calendar c = repo.find(id);
+ if (c == null) return;
+ AlertDialog d = new AlertDialog.Builder(this)
+ .setTitle(getString(R.string.remove_title, c.name))
+ .setMessage(R.string.remove_text)
+ .setPositiveButton(R.string.keep, null)
+ .setNegativeButton(R.string.remove_short, (x, w) -> Background.read(() -> {
+ repo.remove(id);
+ GamesProvider.changed(getApplicationContext());
+ main.post(this::finish);
+ }))
+ .show();
+ // A stray OK keeps it.
+ d.getButton(AlertDialog.BUTTON_POSITIVE).requestFocus();
+ }
+}
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/ColourGrid.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/ColourGrid.java
new file mode 100644
index 000000000..b3c7911f2
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/ColourGrid.java
@@ -0,0 +1,110 @@
+package io.github.justinnewman.gameday;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.util.AttributeSet;
+import android.view.KeyEvent;
+import android.view.View;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * The 36 colours as a 12 by 3 grid: Left and Right change the colour, Up and Down the shade, OK chooses.
+ * A small dark dot marks colours other calendars already use.
+ */
+public final class ColourGrid extends View {
+ interface Listener {
+ void onMoved(int index);
+
+ void onChosen(int index);
+ }
+
+ private final Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG);
+ private final Paint ring = new Paint(Paint.ANTI_ALIAS_FLAG);
+ private Set<Integer> used = new HashSet<>();
+ private int selected = Palette.COLUMNS;
+ private Listener listener;
+
+ public ColourGrid(Context ctx, AttributeSet attrs) {
+ super(ctx, attrs);
+ setFocusable(true);
+ ring.setStyle(Paint.Style.STROKE);
+ }
+
+ /** Where a D-pad key moves the selection; the grid's edges stop it. */
+ static int move(int index, int keyCode) {
+ int col = index % Palette.COLUMNS;
+ int row = index / Palette.COLUMNS;
+ int rows = Palette.COUNT / Palette.COLUMNS;
+ switch (keyCode) {
+ case KeyEvent.KEYCODE_DPAD_LEFT:
+ return col > 0 ? index - 1 : index;
+ case KeyEvent.KEYCODE_DPAD_RIGHT:
+ return col < Palette.COLUMNS - 1 ? index + 1 : index;
+ case KeyEvent.KEYCODE_DPAD_UP:
+ return row > 0 ? index - Palette.COLUMNS : index;
+ case KeyEvent.KEYCODE_DPAD_DOWN:
+ return row < rows - 1 ? index + Palette.COLUMNS : index;
+ default:
+ return index;
+ }
+ }
+
+ void set(int selectedIndex, Set<Integer> usedColours, Listener l) {
+ selected = selectedIndex;
+ used = usedColours;
+ listener = l;
+ invalidate();
+ }
+
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
+ if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
+ if (listener != null) listener.onChosen(selected);
+ return true;
+ }
+ int next = move(selected, keyCode);
+ if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
+ || keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
+ if (next != selected) {
+ selected = next;
+ invalidate();
+ if (listener != null) listener.onMoved(selected);
+ }
+ return true;
+ }
+ return super.onKeyDown(keyCode, event);
+ }
+
+ @Override
+ protected void onMeasure(int widthSpec, int heightSpec) {
+ int w = MeasureSpec.getSize(widthSpec);
+ float cell = w / (float) Palette.COLUMNS;
+ setMeasuredDimension(w, Math.round(cell * Palette.COUNT / Palette.COLUMNS));
+ }
+
+ @Override
+ protected void onDraw(Canvas canvas) {
+ float cell = getWidth() / (float) Palette.COLUMNS;
+ float r = cell * 0.36f;
+ ring.setStrokeWidth(cell * 0.06f);
+ for (int i = 0; i < Palette.COUNT; i++) {
+ float cx = (i % Palette.COLUMNS + 0.5f) * cell;
+ float cy = (i / Palette.COLUMNS + 0.5f) * cell;
+ boolean focused = i == selected && isFocused();
+ float radius = focused ? r * 1.18f : r;
+ fill.setColor(Palette.colour(i));
+ canvas.drawCircle(cx, cy, radius, fill);
+ if (i == selected) {
+ ring.setColor(focused ? 0xFFFFFFFF : 0x80FFFFFF);
+ canvas.drawCircle(cx, cy, radius + ring.getStrokeWidth() * 1.5f, ring);
+ }
+ if (used.contains(i) && i != selected) {
+ fill.setColor(0xFF000000);
+ canvas.drawCircle(cx + r * 0.62f, cy + r * 0.62f, r * 0.28f, fill);
+ }
+ }
+ }
+}
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Countdown.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Countdown.java
new file mode 100644
index 000000000..f283c96bd
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Countdown.java
@@ -0,0 +1,17 @@
+package io.github.justinnewman.gameday;
+
+import java.util.Locale;
+
+/** "46 min", "2h 18m", "1d 3h": the launcher's countdown, for the settings preview. */
+final class Countdown {
+ private Countdown() {
+ }
+
+ static String text(long ms) {
+ long minutes = Math.max(1, (ms / 1000 + 59) / 60);
+ if (minutes < 60) return minutes + " min";
+ long hours = minutes / 60;
+ if (hours < 24) return String.format(Locale.US, "%dh %dm", hours, minutes % 60);
+ return String.format(Locale.US, "%dd %dh", hours / 24, hours % 24);
+ }
+}
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.java
index 0de77e8b1..bad47c2ce 100644
--- 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.java
@@ -51,6 +51,9 @@ final class Ics {
/** 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);
+ /** Some servers start the file with one (U+FEFF); it is not part of BEGIN:VCALENDAR. */
+ private static final char BYTE_ORDER_MARK = (char) 0xFEFF;
+
private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss", Locale.ROOT);
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyyMMdd", Locale.ROOT);
@@ -80,7 +83,7 @@ final class Ics {
String line = pending;
pending = null;
if (!started) {
- if (line.startsWith("")) line = line.substring(1);
+ if (!line.isEmpty() && line.charAt(0) == BYTE_ORDER_MARK) line = line.substring(1);
if (line.trim().isEmpty()) {
if (raw == null) break;
pending = raw;
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Repository.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Repository.java
index a54eda4cf..c4cb423f7 100644
--- a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Repository.java
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Repository.java
@@ -73,6 +73,52 @@ final class Repository {
}
}
+ /** Adds a calendar at the end; false when its link is already on the list or the list is full. */
+ boolean add(Calendar c) {
+ List<Calendar> list = calendars();
+ if (list.size() >= CalendarStore.MAX) return false;
+ for (Calendar o : list) {
+ if (o.id.equals(c.id)) return false;
+ }
+ list.add(c);
+ save(list);
+ return true;
+ }
+
+ /** Puts {@code c} where the calendar {@code id} was (its id changes when its link does). */
+ void replace(String id, Calendar c) {
+ List<Calendar> list = calendars();
+ for (int i = 0; i < list.size(); i++) {
+ if (list.get(i).id.equals(id)) list.set(i, c);
+ }
+ save(list);
+ }
+
+ /** One place up: the higher calendar's colour wins when two share a game. */
+ void moveUp(String id) {
+ List<Calendar> list = calendars();
+ for (int i = 1; i < list.size(); i++) {
+ if (list.get(i).id.equals(id)) {
+ list.add(i - 1, list.remove(i));
+ break;
+ }
+ }
+ save(list);
+ }
+
+ void remove(String id) {
+ List<Calendar> list = calendars();
+ list.removeIf(c -> c.id.equals(id));
+ save(list);
+ }
+
+ Calendar find(String id) {
+ for (Calendar c : calendars()) {
+ if (c.id.equals(id)) return c;
+ }
+ return null;
+ }
+
int turnSeconds() {
String json = read(new File(dir, "settings.json"));
if (json == null) return TURN_DEFAULT;
@@ -131,8 +177,50 @@ final class Repository {
/** "Sat 1:00 AM", or "nothing in the next 7 days". */
String nextLine(Calendar c, long nowMs, ZoneId zone) {
+ Schedule.Turn t = next(c, nowMs, zone);
+ return t == null ? NOTHING : when(t, zone);
+ }
+
+ /** "Sat 1:00 AM". */
+ static String when(Schedule.Turn t, ZoneId zone) {
+ return NEXT.format(Instant.ofEpochMilli(t.startMs).atZone(zone));
+ }
+
+ /** The calendar's turn on Home right now, or null. */
+ Schedule.Turn next(Calendar c, long nowMs, ZoneId zone) {
List<Schedule.Turn> t = turns(Collections.singletonList(c), nowMs, zone);
- return t.isEmpty() ? NOTHING : NEXT.format(Instant.ofEpochMilli(t.get(0).startMs).atZone(zone));
+ return t.isEmpty() ? null : t.get(0);
+ }
+
+ /** The calendar's next entry that has not ended, however far ahead: the settings preview. */
+ Ics.Event nextEvent(Calendar c, long nowMs, ZoneId zone) {
+ File f = Fetcher.cacheFile(dir, c.id);
+ if (!f.exists()) return null;
+ Instant now = Instant.ofEpochMilli(nowMs);
+ Ics.Event best = null;
+ try (Reader r = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8)) {
+ for (Ics.Event e : Ics.parse(r, zone, now, now.plus(Duration.ofDays(366)))) {
+ if (!Schedule.endOf(e, c.sport, zone).isAfter(now)) continue;
+ if (best == null || e.start.isBefore(best.start)) best = e;
+ }
+ } catch (IOException | Ics.NotACalendar unreadable) {
+ return null;
+ }
+ return best;
+ }
+
+ /** Every title in the calendar's copy from now on, for suggesting its name and sport. */
+ List<String> titles(Calendar c, long nowMs, ZoneId zone) {
+ List<String> out = new ArrayList<>();
+ File f = Fetcher.cacheFile(dir, c.id);
+ if (!f.exists()) return out;
+ Instant now = Instant.ofEpochMilli(nowMs);
+ try (Reader r = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8)) {
+ for (Ics.Event e : Ics.parse(r, zone, now, now.plus(Duration.ofDays(366)))) out.add(e.title);
+ } catch (IOException | Ics.NotACalendar unreadable) {
+ // nothing to suggest from
+ }
+ return out;
}
private List<Schedule.Turn> turns(List<Calendar> calendars, long nowMs, ZoneId zone) {
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/SecondsActivity.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/SecondsActivity.java
new file mode 100644
index 000000000..efa257323
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/SecondsActivity.java
@@ -0,0 +1,50 @@
+package io.github.justinnewman.gameday;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.KeyEvent;
+import android.widget.TextView;
+
+/** Seconds per turn on Home: Left and Right change it by 5, OK saves, Back leaves it as it was. */
+public final class SecondsActivity extends Activity {
+ private Repository repo;
+ private TextView value;
+ private int seconds;
+
+ @Override
+ protected void onCreate(Bundle saved) {
+ super.onCreate(saved);
+ setContentView(R.layout.activity_seconds);
+ repo = new Repository(this);
+ value = findViewById(R.id.value);
+ seconds = repo.turnSeconds();
+ show();
+ value.requestFocus();
+ }
+
+ private void show() {
+ value.setText("‹ " + seconds + " ›");
+ }
+
+ @Override
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
+ switch (keyCode) {
+ case KeyEvent.KEYCODE_DPAD_LEFT:
+ seconds = Math.max(Repository.TURN_MIN, seconds - 5);
+ show();
+ return true;
+ case KeyEvent.KEYCODE_DPAD_RIGHT:
+ seconds = Math.min(Repository.TURN_MAX, seconds + 5);
+ show();
+ return true;
+ case KeyEvent.KEYCODE_DPAD_CENTER:
+ case KeyEvent.KEYCODE_ENTER:
+ repo.setTurnSeconds(seconds);
+ GamesProvider.changed(getApplicationContext());
+ finish();
+ return true;
+ default:
+ return super.onKeyDown(keyCode, event);
+ }
+ }
+}
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/SettingsActivity.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/SettingsActivity.java
new file mode 100644
index 000000000..36ae28377
--- /dev/null
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/SettingsActivity.java
@@ -0,0 +1,148 @@
+package io.github.justinnewman.gameday;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.Intent;
+import android.database.ContentObserver;
+import android.graphics.drawable.GradientDrawable;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.List;
+
+/** Game Day's main screen: the calendars, and adding one, the seconds per turn, fetching, help. */
+public final class SettingsActivity extends Activity {
+ private final Handler main = new Handler(Looper.getMainLooper());
+ private Repository repo;
+ private LinearLayout list;
+ private TextView empty;
+ private TextView footer;
+ private TextView seconds;
+ private String focusId;
+
+ private final ContentObserver changes = new ContentObserver(main) {
+ @Override
+ public void onChange(boolean selfChange) {
+ load();
+ }
+ };
+
+ private static final class Row {
+ Calendar calendar;
+ String meta;
+ boolean failed;
+ }
+
+ @Override
+ protected void onCreate(Bundle saved) {
+ super.onCreate(saved);
+ setContentView(R.layout.activity_settings);
+ repo = new Repository(this);
+ list = findViewById(R.id.list);
+ empty = findViewById(R.id.empty);
+ footer = findViewById(R.id.footer);
+ seconds = findViewById(R.id.seconds);
+ empty.setText(getString(R.string.none_yet) + "\n" + getString(R.string.none_yet_more));
+ findViewById(R.id.add).setOnClickListener(v -> Background.read(() -> {
+ boolean full = repo.calendars().size() >= CalendarStore.MAX;
+ main.post(() -> {
+ if (full) Toast.makeText(this, R.string.full, Toast.LENGTH_SHORT).show();
+ else startActivity(new Intent(this, AddCalendarActivity.class));
+ });
+ }));
+ seconds.setOnClickListener(v -> startActivity(new Intent(this, SecondsActivity.class)));
+ findViewById(R.id.fetch).setOnClickListener(v -> {
+ Toast.makeText(this, R.string.fetching, Toast.LENGTH_SHORT).show();
+ Background.execute(() -> {
+ Fetcher f = new Fetcher();
+ long now = System.currentTimeMillis();
+ for (Calendar c : repo.calendars()) repo.fetch(f, c, now);
+ GamesProvider.changed(getApplicationContext());
+ });
+ });
+ findViewById(R.id.help).setOnClickListener(v -> new AlertDialog.Builder(this)
+ .setTitle(R.string.help_title).setMessage(R.string.help_text).setPositiveButton(R.string.ok, null).show());
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ getContentResolver().registerContentObserver(GamesProvider.TURNS, false, changes);
+ load();
+ Background.refresh(this, System.currentTimeMillis());
+ }
+
+ @Override
+ protected void onPause() {
+ super.onPause();
+ getContentResolver().unregisterContentObserver(changes);
+ }
+
+ private void load() {
+ Background.read(() -> {
+ long now = System.currentTimeMillis();
+ ZoneId zone = ZoneId.systemDefault();
+ List<Row> rows = new ArrayList<>();
+ long updated = 0;
+ for (Calendar c : repo.calendars()) {
+ Row r = new Row();
+ r.calendar = c;
+ FetchStatus s = repo.status(c.id);
+ if (s != null) updated = Math.max(updated, s.lastSuccessMs);
+ if (s != null && s.error != null) {
+ r.failed = true;
+ r.meta = s.lastSuccessMs > 0 ? getString(R.string.failed, Ago.text(now, s.lastSuccessMs), s.error)
+ : getString(R.string.failed_never, s.error);
+ } else {
+ Schedule.Turn t = repo.next(c, now, zone);
+ r.meta = c.sport.label() + " · " + (t == null ? Repository.NOTHING
+ : Repository.when(t, zone) + " · " + t.topLine);
+ }
+ rows.add(r);
+ }
+ int turn = repo.turnSeconds();
+ String footerText = getString(R.string.footer, Ago.text(now, updated),
+ getResources().getQuantityString(R.plurals.calendars, rows.size(), rows.size()));
+ main.post(() -> show(rows, turn, footerText));
+ });
+ }
+
+ private void show(List<Row> rows, int turn, String footerText) {
+ if (isFinishing()) return;
+ View focused = getCurrentFocus();
+ if (focused != null && focused.getTag() instanceof String) focusId = (String) focused.getTag();
+ list.removeAllViews();
+ LayoutInflater inf = getLayoutInflater();
+ View again = null;
+ for (Row r : rows) {
+ View v = inf.inflate(R.layout.item_calendar, list, false);
+ GradientDrawable bar = new GradientDrawable();
+ bar.setCornerRadius(4 * getResources().getDisplayMetrics().density);
+ bar.setColor(Palette.colour(r.calendar.colour));
+ v.findViewById(R.id.bar).setBackground(bar);
+ ((TextView) v.findViewById(R.id.name)).setText(r.calendar.name);
+ TextView meta = v.findViewById(R.id.meta);
+ meta.setText(r.meta);
+ if (r.failed) meta.setTextColor(0xFFFF8A80);
+ ((TextView) v.findViewById(R.id.link)).setText(Links.mask(r.calendar.link));
+ v.setTag(r.calendar.id);
+ v.setOnClickListener(x -> startActivity(new Intent(this, CalendarActivity.class)
+ .putExtra(CalendarActivity.EXTRA_ID, r.calendar.id)));
+ list.addView(v);
+ if (r.calendar.id.equals(focusId)) again = v;
+ }
+ empty.setVisibility(rows.isEmpty() ? View.VISIBLE : View.GONE);
+ seconds.setText(getString(R.string.seconds) + " " + turn);
+ footer.setText(footerText);
+ if (again != null) again.requestFocus();
+ else if (getCurrentFocus() == null) findViewById(rows.isEmpty() ? R.id.add : R.id.list).requestFocus();
+ }
+}
diff --git a/addons/game-day/app/src/main/res/drawable/banner.xml b/addons/game-day/app/src/main/res/drawable/banner.xml
new file mode 100644
index 000000000..7927f47e1
--- /dev/null
+++ b/addons/game-day/app/src/main/res/drawable/banner.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- The TV banner: a scoreboard with a countdown's colon on a deep green field. Drawn for this add-on. -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="320dp" android:height="180dp"
+ android:viewportWidth="320" android:viewportHeight="180">
+ <path android:fillColor="#FF0B2A1E" android:pathData="M0,0h320v180h-320z" />
+ <path android:fillColor="#FF134A33" android:pathData="M70,50h180a14,14 0,0 1,14 14v52a14,14 0,0 1,-14 14h-180a14,14 0,0 1,-14 -14v-52a14,14 0,0 1,14 -14z" />
+ <path android:fillColor="#FFFFFFFF" android:pathData="M88,70h52v40h-52z M180,70h52v40h-52z" android:fillAlpha="0.9" />
+ <path android:fillColor="#FF3CBE78" android:pathData="M160,80m-6,0a6,6 0,1 1,12 0a6,6 0,1 1,-12 0 M160,100m-6,0a6,6 0,1 1,12 0a6,6 0,1 1,-12 0" />
+</vector>
diff --git a/addons/game-day/app/src/main/res/drawable/field.xml b/addons/game-day/app/src/main/res/drawable/field.xml
new file mode 100644
index 000000000..6b3b1c0b7
--- /dev/null
+++ b/addons/game-day/app/src/main/res/drawable/field.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+ <solid android:color="#1AFFFFFF" />
+ <stroke android:width="2dp" android:color="#8CFFFFFF" />
+ <corners android:radius="10dp" />
+</shape>
diff --git a/addons/game-day/app/src/main/res/drawable/icon.xml b/addons/game-day/app/src/main/res/drawable/icon.xml
new file mode 100644
index 000000000..ac0e1fce8
--- /dev/null
+++ b/addons/game-day/app/src/main/res/drawable/icon.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- The app icon: the banner's scoreboard, square. -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp" android:height="48dp"
+ android:viewportWidth="48" android:viewportHeight="48">
+ <path android:fillColor="#FF134A33" android:pathData="M0,0h48v48h-48z" />
+ <path android:fillColor="#FFFFFFFF" android:pathData="M8,16h12v16h-12z M28,16h12v16h-12z" />
+ <path android:fillColor="#FF3CBE78" android:pathData="M24,20m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0 M24,28m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" />
+</vector>
diff --git a/addons/game-day/app/src/main/res/drawable/panel.xml b/addons/game-day/app/src/main/res/drawable/panel.xml
new file mode 100644
index 000000000..c94a1b557
--- /dev/null
+++ b/addons/game-day/app/src/main/res/drawable/panel.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+ <solid android:color="#0DFFFFFF" />
+ <corners android:radius="14dp" />
+</shape>
diff --git a/addons/game-day/app/src/main/res/drawable/row.xml b/addons/game-day/app/src/main/res/drawable/row.xml
new file mode 100644
index 000000000..0d7f48718
--- /dev/null
+++ b/addons/game-day/app/src/main/res/drawable/row.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- A row or button: faint glass, brighter with an outline when the remote is on it. -->
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+ <item android:state_focused="true">
+ <shape android:shape="rectangle">
+ <solid android:color="#29FFFFFF" />
+ <stroke android:width="2dp" android:color="#8CFFFFFF" />
+ <corners android:radius="12dp" />
+ </shape>
+ </item>
+ <item>
+ <shape android:shape="rectangle">
+ <solid android:color="#0DFFFFFF" />
+ <corners android:radius="12dp" />
+ </shape>
+ </item>
+</selector>
diff --git a/addons/game-day/app/src/main/res/drawable/screen.xml b/addons/game-day/app/src/main/res/drawable/screen.xml
new file mode 100644
index 000000000..2c38e6af8
--- /dev/null
+++ b/addons/game-day/app/src/main/res/drawable/screen.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Near black, a little lighter toward the top left. -->
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+ <gradient android:type="radial" android:centerX="0.2" android:centerY="0" android:gradientRadius="1200dp"
+ android:startColor="#FF10161D" android:endColor="#FF050607" />
+</shape>
diff --git a/addons/game-day/app/src/main/res/layout/activity_add.xml b/addons/game-day/app/src/main/res/layout/activity_add.xml
new file mode 100644
index 000000000..107dacec8
--- /dev/null
+++ b/addons/game-day/app/src/main/res/layout/activity_add.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Adding a calendar with the remote, in four steps; also opened at one step to change a calendar. -->
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent" android:layout_height="match_parent"
+ android:paddingStart="48dp" android:paddingEnd="48dp" android:paddingTop="28dp" android:paddingBottom="20dp">
+
+ <TextView android:id="@+id/title" style="@style/Title" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:text="@string/add" />
+
+ <LinearLayout android:id="@+id/steps" android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:layout_below="@id/title" android:layout_marginTop="6dp" android:orientation="horizontal">
+ <TextView android:id="@+id/step1" android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:layout_marginEnd="18dp" android:textSize="13sp" android:text="@string/step_link" />
+ <TextView android:id="@+id/step2" android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:layout_marginEnd="18dp" android:textSize="13sp" android:text="@string/step_name" />
+ <TextView android:id="@+id/step3" android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:layout_marginEnd="18dp" android:textSize="13sp" android:text="@string/step_sport" />
+ <TextView android:id="@+id/step4" android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:textSize="13sp" android:text="@string/step_colour" />
+ </LinearLayout>
+
+ <!-- How the turn will look on Home: shown for the sport and colour steps. -->
+ <LinearLayout android:id="@+id/preview" android:layout_width="300dp" android:layout_height="wrap_content"
+ android:layout_alignParentEnd="true" android:layout_below="@id/steps" android:layout_marginTop="28dp"
+ android:background="@drawable/panel" android:orientation="vertical" android:padding="18dp" android:visibility="gone">
+ <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/on_home"
+ android:textSize="11sp" android:letterSpacing="0.2" android:textAllCaps="true" android:textColor="#73FFFFFF" />
+ <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"
+ android:orientation="horizontal" android:gravity="center_vertical">
+ <View android:id="@+id/previewBar" android:layout_width="4dp" android:layout_height="14dp" android:layout_marginEnd="9dp" />
+ <TextView android:id="@+id/previewTop" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:textSize="11sp" android:letterSpacing="0.18" android:textAllCaps="true" android:textColor="#B3FFFFFF"
+ android:singleLine="true" android:ellipsize="end" />
+ </LinearLayout>
+ <TextView android:id="@+id/previewWord" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_marginTop="6dp" android:textSize="22sp" android:fontFamily="sans-serif-light" android:textColor="#FFFFFFFF"
+ android:singleLine="true" android:ellipsize="end" />
+ </LinearLayout>
+
+ <LinearLayout android:id="@+id/body" android:layout_width="match_parent" android:layout_height="match_parent"
+ android:layout_below="@id/steps" android:layout_toStartOf="@id/preview" android:layout_marginTop="24dp"
+ android:layout_marginEnd="28dp" android:orientation="vertical">
+
+ <TextView android:id="@+id/note" style="@style/Sub" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_marginBottom="10dp" />
+
+ <EditText android:id="@+id/field" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:background="@drawable/field" android:padding="12dp" android:textSize="16sp" android:textColor="#FFFFFFFF"
+ android:textColorHint="#59FFFFFF" android:singleLine="true" android:importantForAutofill="no"
+ android:inputType="textUri" android:imeOptions="actionNext|flagNoExtractUi" android:hint="@string/link_hint" />
+
+ <TextView android:id="@+id/next" style="@style/Button" android:layout_width="160dp" android:layout_height="wrap_content"
+ android:layout_marginTop="14dp" android:gravity="center" android:text="@string/next" />
+
+ <ScrollView android:id="@+id/sportScroll" android:layout_width="match_parent" android:layout_height="match_parent"
+ android:padding="4dp" android:clipToPadding="false" android:scrollbars="none" android:visibility="gone">
+ <LinearLayout android:id="@+id/sports" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:orientation="vertical" />
+ </ScrollView>
+
+ <io.github.justinnewman.gameday.ColourGrid android:id="@+id/grid" android:layout_width="match_parent"
+ android:layout_height="wrap_content" android:layout_marginTop="8dp" android:visibility="gone" />
+ <TextView android:id="@+id/colourName" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_marginTop="16dp" android:textSize="18sp" android:textColor="#FFFFFFFF" android:visibility="gone" />
+ </LinearLayout>
+</RelativeLayout>
diff --git a/addons/game-day/app/src/main/res/layout/activity_calendar.xml b/addons/game-day/app/src/main/res/layout/activity_calendar.xml
new file mode 100644
index 000000000..b5354eab4
--- /dev/null
+++ b/addons/game-day/app/src/main/res/layout/activity_calendar.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- One calendar: change its sport, colour, name or link, fetch it, move it up, remove it. -->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
+ android:paddingStart="48dp" android:paddingEnd="48dp" android:paddingTop="28dp">
+
+ <TextView android:id="@+id/title" style="@style/Title" android:layout_width="match_parent" android:layout_height="wrap_content" />
+ <TextView android:id="@+id/sub" style="@style/Sub" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_marginTop="2dp" android:singleLine="true" android:ellipsize="end" />
+
+ <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_marginTop="24dp" android:orientation="horizontal">
+ <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
+ android:layout_marginEnd="24dp" android:orientation="vertical">
+ <TextView android:id="@+id/sport" style="@style/Button" android:layout_width="match_parent" android:layout_height="wrap_content" />
+ <TextView android:id="@+id/colour" style="@style/Button" android:layout_width="match_parent" android:layout_height="wrap_content" />
+ <TextView android:id="@+id/name" style="@style/Button" android:layout_width="match_parent" android:layout_height="wrap_content" />
+ <TextView android:id="@+id/link" style="@style/Button" android:layout_width="match_parent" android:layout_height="wrap_content" />
+ </LinearLayout>
+ <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
+ android:orientation="vertical">
+ <TextView android:id="@+id/fetch" style="@style/Button" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:text="@string/fetch_one" />
+ <TextView android:id="@+id/up" style="@style/Button" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:text="@string/move_up" />
+ <TextView android:id="@+id/remove" style="@style/Button" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:text="@string/remove" android:textColor="#FFFF8A80" />
+ </LinearLayout>
+ </LinearLayout>
+</LinearLayout>
diff --git a/addons/game-day/app/src/main/res/layout/activity_seconds.xml b/addons/game-day/app/src/main/res/layout/activity_seconds.xml
new file mode 100644
index 000000000..fb21ff4a8
--- /dev/null
+++ b/addons/game-day/app/src/main/res/layout/activity_seconds.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Seconds per turn: Left and Right change it by 5, OK saves, Back leaves it as it was. -->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
+ android:paddingStart="48dp" android:paddingEnd="48dp" android:paddingTop="28dp">
+ <TextView style="@style/Title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/seconds" />
+ <TextView style="@style/Sub" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp"
+ android:text="@string/seconds_text" />
+ <TextView android:id="@+id/value" android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:layout_marginTop="40dp" android:focusable="true" android:focusableInTouchMode="true"
+ android:textSize="72sp" android:fontFamily="sans-serif-thin" android:textColor="#FFFFFFFF" />
+ <TextView style="@style/Sub" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"
+ android:text="@string/seconds_hint" />
+</LinearLayout>
diff --git a/addons/game-day/app/src/main/res/layout/activity_settings.xml b/addons/game-day/app/src/main/res/layout/activity_settings.xml
new file mode 100644
index 000000000..765c1d50a
--- /dev/null
+++ b/addons/game-day/app/src/main/res/layout/activity_settings.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Game Day's main screen: the calendars on the left, the actions on the right (mock-up section 2). -->
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent" android:layout_height="match_parent"
+ android:paddingStart="48dp" android:paddingEnd="48dp" android:paddingTop="28dp" android:paddingBottom="16dp">
+
+ <TextView android:id="@+id/title" style="@style/Title"
+ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_name" />
+
+ <TextView android:id="@+id/sub" style="@style/Sub"
+ android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_below="@id/title" android:layout_marginTop="2dp" android:text="@string/subtitle" />
+
+ <LinearLayout android:id="@+id/actions"
+ android:layout_width="300dp" android:layout_height="wrap_content"
+ android:layout_alignParentEnd="true" android:layout_below="@id/sub" android:layout_marginTop="24dp"
+ android:orientation="vertical">
+ <TextView android:id="@+id/add" style="@style/Button"
+ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/add" />
+ <TextView android:id="@+id/seconds" style="@style/Button"
+ android:layout_width="match_parent" android:layout_height="wrap_content" />
+ <TextView android:id="@+id/fetch" style="@style/Button"
+ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/fetch_now" />
+ <TextView android:id="@+id/help" style="@style/Button"
+ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/help" />
+ </LinearLayout>
+
+ <TextView android:id="@+id/footer" style="@style/Sub"
+ android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_alignParentBottom="true" android:textSize="12sp" android:textColor="#66FFFFFF" />
+
+ <ScrollView android:id="@+id/scroll"
+ android:layout_width="match_parent" android:layout_height="match_parent"
+ android:layout_below="@id/sub" android:layout_toStartOf="@id/actions" android:layout_above="@id/footer"
+ android:layout_marginTop="20dp" android:layout_marginEnd="28dp" android:clipToPadding="false"
+ android:padding="4dp" android:scrollbars="none">
+ <LinearLayout android:id="@+id/list"
+ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" />
+ </ScrollView>
+
+ <TextView android:id="@+id/empty"
+ android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_below="@id/sub" android:layout_toStartOf="@id/actions"
+ android:layout_marginTop="28dp" android:layout_marginEnd="28dp"
+ android:textSize="16sp" android:textColor="#BFFFFFFF" android:lineSpacingMultiplier="1.3"
+ android:visibility="gone" />
+</RelativeLayout>
diff --git a/addons/game-day/app/src/main/res/layout/item_calendar.xml b/addons/game-day/app/src/main/res/layout/item_calendar.xml
new file mode 100644
index 000000000..0c2933def
--- /dev/null
+++ b/addons/game-day/app/src/main/res/layout/item_calendar.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- One calendar on the main screen: its colour, name, sport and next entry, and the link masked. -->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_marginBottom="8dp" android:background="@drawable/row"
+ android:focusable="true" android:clickable="true" android:orientation="horizontal"
+ android:paddingStart="12dp" android:paddingEnd="16dp" android:paddingTop="10dp" android:paddingBottom="10dp">
+
+ <View android:id="@+id/bar" android:layout_width="5dp" android:layout_height="match_parent"
+ android:layout_marginEnd="14dp" />
+
+ <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
+ android:orientation="vertical">
+ <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:textSize="17sp" android:textColor="#FFFFFFFF" android:singleLine="true" android:ellipsize="end" />
+ <TextView android:id="@+id/meta" android:layout_width="match_parent" android:layout_height="wrap_content"
+ android:layout_marginTop="2dp" android:textSize="12sp" android:textColor="#8CFFFFFF"
+ android:maxLines="2" android:ellipsize="end" />
+ </LinearLayout>
+
+ <TextView android:id="@+id/link" android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:layout_gravity="center_vertical" android:layout_marginStart="12dp"
+ android:textSize="13sp" android:textColor="#80FFFFFF" android:singleLine="true" />
+</LinearLayout>
diff --git a/addons/game-day/app/src/main/res/values/strings.xml b/addons/game-day/app/src/main/res/values/strings.xml
index 50c5616ab..74233b081 100644
--- a/addons/game-day/app/src/main/res/values/strings.xml
+++ b/addons/game-day/app/src/main/res/values/strings.xml
@@ -1,4 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Game Day</string>
+ <string name="subtitle">Your teams\' next games on Home, from your own calendars</string>
+
+ <string name="add">Add a calendar</string>
+ <string name="seconds">Seconds per turn</string>
+ <string name="fetch_now">Fetch now</string>
+ <string name="fetching">Fetching</string>
+ <string name="help">How to add calendars from a computer</string>
+ <string name="none_yet">No calendars yet.</string>
+ <string name="none_yet_more">Add a team\'s calendar link (from Stanza, a team\'s website, or Google Calendar\'s \"Secret address in iCal format\"), or set them all at once from a computer with setup.sh.</string>
+ <string name="footer">Updated %1$s · %2$s · links are never shown in full</string>
+ <plurals name="calendars">
+ <item quantity="one">%d calendar</item>
+ <item quantity="other">%d calendars</item>
+ </plurals>
+ <plurals name="entries">
+ <item quantity="one">%d entry</item>
+ <item quantity="other">%d entries</item>
+ </plurals>
+ <string name="full">At most 20 calendars</string>
+ <string name="failed">Last loaded %1$s. %2$s. Showing that copy.</string>
+ <string name="failed_never">Could not load: %1$s.</string>
+ <string name="help_title">From a computer</string>
+ <string name="help_text">Long links are easier to paste than to type with the remote. On a computer with this repository and ADB access to the TV, put one calendar per line in addons/game-day/calendars.txt as\n\nsport | name | link\n\nthen run\n\nTV_HOST=&lt;tv ip&gt; addons/game-day/setup.sh\n\n(setup.ps1 on Windows). It replaces this list and prints how each calendar loaded. Links are never printed.</string>
+ <string name="ok">OK</string>
+
+ <string name="sport">Sport</string>
+ <string name="colour">Colour</string>
+ <string name="name">Name</string>
+ <string name="link">Link</string>
+ <string name="fetch_one">Fetch this calendar now</string>
+ <string name="move_up">Move up in the list</string>
+ <string name="remove">Remove calendar</string>
+ <string name="loaded">Loaded %1$s · next: %2$s</string>
+ <string name="remove_title">Remove %1$s?</string>
+ <string name="remove_text">Its games leave Home. The link is deleted from the TV; your calendar itself is not touched.</string>
+ <string name="keep">Keep it</string>
+ <string name="remove_short">Remove</string>
+
+ <string name="step_link">1 Link</string>
+ <string name="step_name">2 Name</string>
+ <string name="step_sport">3 Sport</string>
+ <string name="step_colour">4 Colour</string>
+ <string name="next">Next</string>
+ <string name="link_hint">Links starting with webcal:// work too</string>
+ <string name="link_bad">That is not a calendar link. It starts with https://, http:// or webcal://.</string>
+ <string name="link_twice">That calendar is already on the list.</string>
+ <string name="checking">Checking the link</string>
+ <string name="checked">Checked: %1$s found. The name is only for this list.</string>
+ <string name="name_bad">The name is empty or over 40 characters.</string>
+ <string name="colour_hint">Left and Right to choose, Up and Down for the shade, OK to save · a dot marks a colour another calendar uses</string>
+ <string name="on_home">On Home</string>
+ <string name="no_entry">Nothing in the next 7 days</string>
+ <string name="live_now">Live now</string>
+ <string name="seconds_text">How long each team stays on Home before the next one</string>
+ <string name="seconds_hint">10 to 120 seconds, in steps of 5 · OK to save</string>
</resources>
diff --git a/addons/game-day/app/src/main/res/values/styles.xml b/addons/game-day/app/src/main/res/values/styles.xml
new file mode 100644
index 000000000..543d0904f
--- /dev/null
+++ b/addons/game-day/app/src/main/res/values/styles.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <style name="Screen" parent="@android:style/Theme.Material.NoActionBar.Fullscreen">
+ <item name="android:windowBackground">@drawable/screen</item>
+ <item name="android:colorAccent">#FF3CBE78</item>
+ </style>
+
+ <style name="Title">
+ <item name="android:textSize">30sp</item>
+ <item name="android:fontFamily">sans-serif-light</item>
+ <item name="android:textColor">#FFFFFFFF</item>
+ <item name="android:singleLine">true</item>
+ <item name="android:ellipsize">end</item>
+ </style>
+
+ <style name="Sub">
+ <item name="android:textSize">14sp</item>
+ <item name="android:textColor">#8CFFFFFF</item>
+ </style>
+
+ <style name="Button">
+ <item name="android:background">@drawable/row</item>
+ <item name="android:focusable">true</item>
+ <item name="android:clickable">true</item>
+ <item name="android:paddingStart">16dp</item>
+ <item name="android:paddingEnd">16dp</item>
+ <item name="android:minHeight">44dp</item>
+ <item name="android:gravity">center_vertical</item>
+ <item name="android:textSize">16sp</item>
+ <item name="android:textColor">#E6FFFFFF</item>
+ <item name="android:layout_marginBottom">8dp</item>
+ </style>
+</resources>
diff --git a/addons/game-day/app/src/main/res/xml/module_card.xml b/addons/game-day/app/src/main/res/xml/module_card.xml
new file mode 100644
index 000000000..7ec67f31e
--- /dev/null
+++ b/addons/game-day/app/src/main/res/xml/module_card.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="utf-8"?>
+<card spec="1" label="Game Day" caption="Your teams' next games" accent="#3CBE78" />
diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/RepositoryTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/RepositoryTest.java
index 6aa20f5a1..4230b45e5 100644
--- a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/RepositoryTest.java
+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/RepositoryTest.java
@@ -82,6 +82,21 @@ public class RepositoryTest {
assertEquals("nothing in the next 7 days", repo.nextLine(cal("Nobody", "Hockey", 3), NOW.toEpochMilli(), UTC));
}
+ @Test public void titlesForSuggestions() throws Exception {
+ Calendar wild = cal("Minnesota Wild", "Hockey", 16);
+ cache(wild, "🏒 Minnesota Wild @ Dallas Stars", "20270403T010000Z");
+ assertEquals(Arrays.asList("🏒 Minnesota Wild @ Dallas Stars"), repo.titles(wild, NOW.toEpochMilli(), UTC));
+ assertTrue(repo.titles(cal("Nobody", "Hockey", 3), NOW.toEpochMilli(), UTC).isEmpty());
+ }
+
+ @Test public void nextEventIgnoresTheWeekLimit() throws Exception {
+ Calendar wild = cal("Minnesota Wild", "Hockey", 16);
+ cache(wild, "🏒 Minnesota Wild @ Dallas Stars", "20270420T010000Z");
+ Ics.Event e = repo.nextEvent(wild, NOW.toEpochMilli(), UTC);
+ assertEquals("🏒 Minnesota Wild @ Dallas Stars", e.title);
+ assertEquals(null, repo.nextEvent(cal("Nobody", "Hockey", 3), NOW.toEpochMilli(), UTC));
+ }
+
@Test public void resultFileNeverHasALink() {
String result = repo.applySetupFile("Hockey|Minnesota Wild|http://127.0.0.1:1/secret-token.ics\n"
+ "Soccer|Utah Royals|https://127.0.0.1:1/other-secret.ics", NOW.toEpochMilli(), new Fetcher(300, 300), UTC);
diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/SettingsLogicTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/SettingsLogicTest.java
new file mode 100644
index 000000000..9d35102f0
--- /dev/null
+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/SettingsLogicTest.java
@@ -0,0 +1,98 @@
+package io.github.justinnewman.gameday;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.view.KeyEvent;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class SettingsLogicTest {
+ private static final long MIN = 60_000L;
+ private static final long HOUR = 60 * MIN;
+
+ @Rule public TemporaryFolder tmp = new TemporaryFolder();
+
+ private static Calendar cal(String name) {
+ String link = "https://h.example/" + name;
+ return new Calendar(Links.id(link), name, link, Sport.byLabel("Hockey"), 12);
+ }
+
+ @Test public void colourGridMoves() {
+ assertEquals(11, ColourGrid.move(11, KeyEvent.KEYCODE_DPAD_RIGHT));
+ assertEquals(5, ColourGrid.move(4, KeyEvent.KEYCODE_DPAD_RIGHT));
+ assertEquals(16, ColourGrid.move(4, KeyEvent.KEYCODE_DPAD_DOWN));
+ assertEquals(4, ColourGrid.move(4, KeyEvent.KEYCODE_DPAD_UP));
+ assertEquals(24, ColourGrid.move(24, KeyEvent.KEYCODE_DPAD_LEFT));
+ assertEquals(35, ColourGrid.move(35, KeyEvent.KEYCODE_DPAD_DOWN));
+ assertEquals(23, ColourGrid.move(35, KeyEvent.KEYCODE_DPAD_UP));
+ }
+
+ @Test public void calendarEdits() {
+ Calendar c = cal("Wild");
+ assertEquals("Minnesota Wild", c.withName("Minnesota Wild").name);
+ assertEquals(Sport.byLabel("Soccer"), c.withSport(Sport.byLabel("Soccer")).sport);
+ assertEquals(28, c.withColour(28).colour);
+ Calendar moved = c.withLink("https://h.example/new");
+ assertEquals("https://h.example/new", moved.link);
+ assertEquals(Links.id("https://h.example/new"), moved.id);
+ assertEquals(c.name, moved.name);
+ }
+
+ @Test public void ago() {
+ long now = 1_800_000_000_000L;
+ assertEquals("just now", Ago.text(now, now - 20_000));
+ assertEquals("1 minute ago", Ago.text(now, now - MIN));
+ assertEquals("45 minutes ago", Ago.text(now, now - 45 * MIN));
+ assertEquals("1 hour ago", Ago.text(now, now - HOUR));
+ assertEquals("2 hours ago", Ago.text(now, now - 2 * HOUR - 5 * MIN));
+ assertEquals("yesterday", Ago.text(now, now - 30 * HOUR));
+ assertEquals("3 days ago", Ago.text(now, now - 3 * 24 * HOUR));
+ assertEquals("never", Ago.text(now, 0));
+ }
+
+ @Test public void countdownLikeHome() {
+ // As the launcher's RaceWeekend.countdown: rounded up to the minute, never 0.
+ assertEquals("46 min", Countdown.text(45 * MIN + 20_000));
+ assertEquals("1 min", Countdown.text(20_000));
+ assertEquals("2h 18m", Countdown.text(2 * HOUR + 18 * MIN));
+ assertEquals("1d 3h", Countdown.text(27 * HOUR + 10 * MIN));
+ assertEquals("1 min", Countdown.text(-5 * MIN));
+ }
+
+ @Test public void addReplaceMoveRemove() throws Exception {
+ File dir = tmp.newFolder();
+ Repository repo = new Repository(dir);
+ assertTrue(repo.add(cal("A")));
+ assertTrue(repo.add(cal("B")));
+ assertFalse("same link twice", repo.add(cal("A")));
+ repo.replace(cal("B").id, cal("B").withName("Bee"));
+ assertEquals("Bee", repo.calendars().get(1).name);
+ repo.moveUp(cal("B").id);
+ assertEquals(Arrays.asList("Bee", "A"), names(repo.calendars()));
+ repo.moveUp(cal("B").id);
+ assertEquals(Arrays.asList("Bee", "A"), names(repo.calendars()));
+ repo.remove(cal("A").id);
+ assertEquals(Arrays.asList("Bee"), names(repo.calendars()));
+ }
+
+ @Test public void atMostTwenty() throws Exception {
+ Repository repo = new Repository(tmp.newFolder());
+ for (int i = 0; i < CalendarStore.MAX; i++) assertTrue(repo.add(cal("T" + i)));
+ assertFalse(repo.add(cal("T20")));
+ }
+
+ private static List<String> names(List<Calendar> list) {
+ List<String> out = new ArrayList<>();
+ for (Calendar c : list) out.add(c.name);
+ return out;
+ }
+}