Skip to content

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

Launcher: game_day's rotation runs on the steady clock, so changing the TV's time never stalls a turn

Justin Newman9f6c62135608
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/GameDay.java b/launcher/app/src/main/java/com/gathin/tvhome/GameDay.java
index 49e55db6d..8a3bf5817 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/GameDay.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/GameDay.java
@@ -203,6 +203,12 @@ final class GameDay {
return (int) ((Math.max(0, sinceMs) / (turnSeconds * 1000L)) % count);
}
+ /** Milliseconds until the next turn, {@code sinceMs} after the rotation started. */
+ static long delayToNextTurn(long sinceMs, int turnSeconds) {
+ long turnMs = turnSeconds * 1000L;
+ return turnMs - Math.floorMod(sinceMs, turnMs);
+ }
+
static boolean live(Turn t, long nowMs) {
return nowMs >= t.startMs && nowMs < t.endMs;
}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
index ffc5f6461..2d954a738 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -506,7 +506,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
raceBar = findViewById(R.id.raceBar);
racePager = findViewById(R.id.racePager);
gameDay = GameDay.create(this, cfg.gameDay, handler, this::updatePanel);
- rotationStart = System.currentTimeMillis();
+ // The rotation runs on the clock that never jumps: setting the TV's time must not stall or stretch a turn.
+ rotationStart = SystemClock.elapsedRealtime();
showStats = cfg.showStats;
inputsConnectedOnly = cfg.inputsConnectedOnly;
shamsi = cfg.shamsiDate;
@@ -2584,7 +2585,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
return;
}
int seconds = gameDay.turnSeconds();
- int on = GameDay.current(turns.size(), now - rotationStart, seconds);
+ long since = SystemClock.elapsedRealtime() - rotationStart;
+ int on = GameDay.current(turns.size(), since, seconds);
GameDay.Turn t = turns.get(on);
if (t.f1) {
raceBar.setBackgroundResource(R.drawable.race_bar);
@@ -2607,10 +2609,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
racePanel.setVisibility(View.VISIBLE);
}
showPager(turns.size(), on);
- if (turns.size() > 1 && !stopped) {
- long turnMs = seconds * 1000L;
- handler.postDelayed(turnTick, turnMs - (now - rotationStart) % turnMs);
- }
+ if (turns.size() > 1 && !stopped) handler.postDelayed(turnTick, GameDay.delayToNextTurn(since, seconds));
}
/** One dot per turn, the one on screen bright; none for a single turn. */
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/GameDayTest.java b/launcher/app/src/test/java/com/gathin/tvhome/GameDayTest.java
index b5ad76941..5e05c25a6 100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/GameDayTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/GameDayTest.java
@@ -75,6 +75,15 @@ public class GameDayTest {
assertEquals(0, GameDay.current(1, 500_000, 30));
}
+ @Test public void nextTurnIsNeverMoreThanOneTurnAway() {
+ assertEquals(30_000, GameDay.delayToNextTurn(0, 30));
+ assertEquals(1_000, GameDay.delayToNextTurn(29_000, 30));
+ assertEquals(30_000, GameDay.delayToNextTurn(60_000, 30));
+ // A clock set back while Home is open must not stretch the turn.
+ assertTrue(GameDay.delayToNextTurn(-45_000, 30) <= 30_000);
+ assertTrue(GameDay.delayToNextTurn(-45_000, 30) > 0);
+ }
+
@Test public void turnIsLiveOnlyBetweenStartAndEnd() {
GameDay.Turn t = game("Game", HOUR, 3 * HOUR);
assertFalse(GameDay.live(t, t.startMs - 1));