Skip to content

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

launcher: follow the real sunrise and sunset when a location is set

Arturas Radzeviciusc2d8c0f1c4fd
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/Sky.java b/launcher/app/src/main/java/com/gathin/tvhome/Sky.java
index 04d789d8f..55d1c3ed4 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/Sky.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Sky.java
@@ -54,7 +54,10 @@ final class Sky {
private static final int NIGHT_BOTTOM = 0xFF0B1326;
// Anchors were picked around a temperate evening: full night until half past five, dawn before
- // seven, the warmest glow between seven in the evening and a quarter to eight.
+ // seven, the warmest glow between seven in the evening and a quarter to eight. With a location
+ // set, at(minute, sunrise, sunset) stretches this reference day onto the real one.
+ static final int REF_SUNRISE = 450;
+ static final int REF_SUNSET = 1140;
private static final Stop[] STOPS = {
new Stop(0, NIGHT_TOP, NIGHT_MID, NIGHT_BOTTOM, 0, 0.30f),
new Stop(320, NIGHT_TOP, NIGHT_MID, NIGHT_BOTTOM, 0, 0.30f),
@@ -73,6 +76,21 @@ final class Sky {
private Sky() {
}
+ /**
+ * The palette at {@code minuteOfDay} on a day whose sun rises at {@code sunrise} and sets at
+ * {@code sunset} (minutes of the day): night, day and evening are each stretched piecewise so
+ * the reference sunrise and sunset glows land on the real ones.
+ */
+ static Palette at(int minuteOfDay, int sunrise, int sunset) {
+ if (sunrise <= 0 || sunset >= 1440 || sunrise >= sunset) return at(minuteOfDay);
+ int m = Math.max(0, Math.min(1440, minuteOfDay));
+ float ref;
+ if (m < sunrise) ref = m * REF_SUNRISE / (float) sunrise;
+ else if (m < sunset) ref = REF_SUNRISE + (m - sunrise) * (REF_SUNSET - REF_SUNRISE) / (float) (sunset - sunrise);
+ else ref = REF_SUNSET + (m - sunset) * (1440 - REF_SUNSET) / (float) (1440 - sunset);
+ return at(Math.round(ref));
+ }
+
/** The palette at {@code minuteOfDay}, clamped to a day. */
static Palette at(int minuteOfDay) {
int m = Math.max(0, Math.min(1440, minuteOfDay));
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/SkyView.java b/launcher/app/src/main/java/com/gathin/tvhome/SkyView.java
index 8cf84e640..96be4f76d 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/SkyView.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/SkyView.java
@@ -14,6 +14,8 @@ import android.view.View;
import androidx.annotation.Nullable;
+import java.time.LocalDate;
+import java.time.ZoneId;
import java.util.Calendar;
/**
@@ -67,13 +69,28 @@ public class SkyView extends View {
if (intensity > 0) render(true);
}
+ /** Today's sunrise and sunset at the configured place, or null without one (fixed reference day). */
+ @Nullable
+ private int[] sunTimes() {
+ String lat = getContext().getString(R.string.latitude);
+ String lon = getContext().getString(R.string.longitude);
+ if (lat.isEmpty() || lon.isEmpty()) return null;
+ try {
+ return Sun.riseAndSet(LocalDate.now(), Double.parseDouble(lat), Double.parseDouble(lon),
+ ZoneId.systemDefault());
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ }
+
private void render(boolean force) {
Calendar now = Calendar.getInstance();
int minute = Sky.minuteOfDay(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE));
if (!force && minute == lastMinute) return;
lastMinute = minute;
- Sky.Palette sky = Sky.at(minute);
+ int[] sun = sunTimes();
+ Sky.Palette sky = sun == null ? Sky.at(minute) : Sky.at(minute, sun[0], sun[1]);
if (texture == null) texture = Bitmap.createBitmap(TEX_W, TEX_H, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(texture);
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/Sun.java b/launcher/app/src/main/java/com/gathin/tvhome/Sun.java
new file mode 100644
index 000000000..426cf1542
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Sun.java
@@ -0,0 +1,38 @@
+package com.gathin.tvhome;
+
+import java.time.LocalDate;
+import java.time.ZoneId;
+
+/**
+ * Sunrise and sunset for a place, computed offline with NOAA's general solar position equations
+ * (accurate to a few minutes, which is plenty for a backdrop). Pure maths, no Android types, so
+ * it runs in JVM tests; no network and no location permission, the place comes from config.xml.
+ */
+final class Sun {
+
+ private Sun() {
+ }
+
+ /**
+ * Local minutes of the day of sunrise and sunset, as {@code {sunrise, sunset}}, or null when the
+ * sun does not rise or does not set that day (polar night or midnight sun).
+ */
+ static int[] riseAndSet(LocalDate date, double latitude, double longitude, ZoneId zone) {
+ double g = 2 * Math.PI / 365 * (date.getDayOfYear() - 1);
+ double eqTime = 229.18 * (0.000075 + 0.001868 * Math.cos(g) - 0.032077 * Math.sin(g)
+ - 0.014615 * Math.cos(2 * g) - 0.040849 * Math.sin(2 * g));
+ double decl = 0.006918 - 0.399912 * Math.cos(g) + 0.070257 * Math.sin(g)
+ - 0.006758 * Math.cos(2 * g) + 0.000907 * Math.sin(2 * g)
+ - 0.002697 * Math.cos(3 * g) + 0.00148 * Math.sin(3 * g);
+ double lat = Math.toRadians(latitude);
+ // 90.833 degrees: the sun's upper edge on the horizon, with refraction.
+ double cosHa = Math.cos(Math.toRadians(90.833)) / (Math.cos(lat) * Math.cos(decl))
+ - Math.tan(lat) * Math.tan(decl);
+ if (cosHa > 1 || cosHa < -1) return null;
+ double ha = Math.toDegrees(Math.acos(cosHa));
+ int offset = zone.getRules().getOffset(date.atTime(12, 0)).getTotalSeconds() / 60;
+ return new int[]{
+ (int) Math.round(720 - 4 * (longitude + ha) - eqTime + offset),
+ (int) Math.round(720 - 4 * (longitude - ha) - eqTime + offset)};
+ }
+}
diff --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xml
index 3deebbea4..f2a320e15 100644
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@ -153,6 +153,11 @@
sets its opacity. Around 60 reads clearly without hiding a wallpaper. Can also be set with
"sky_intensity": 60 in a theme's config.json. -->
<integer name="sky_intensity">0</integer>
+ <!-- Where the TV is, in decimal degrees, so the sky's sunrise and sunset glows follow the real sun
+ (computed on the TV, no network). Empty: a fixed temperate day, sunset glow around 19:00.
+ Vilnius: 54.69 and 25.28. -->
+ <string name="latitude" translatable="false"></string>
+ <string name="longitude" translatable="false"></string>
<!-- Cinematic top shelf: the focused dock app's artwork, softened, fills the top of the screen in its
colour; the shelf gets a larger name, a "Last opened" line (from the apps Home opened) and dock position
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/SunTest.java b/launcher/app/src/test/java/com/gathin/tvhome/SunTest.java
new file mode 100644
index 000000000..98f67005f
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/SunTest.java
@@ -0,0 +1,61 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+
+import java.time.LocalDate;
+import java.time.ZoneId;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/** Sunrise/sunset maths and the sky's stretch onto a real day. */
+public class SunTest {
+
+ private static final ZoneId VILNIUS = ZoneId.of("Europe/Vilnius");
+
+ @Test
+ public void vilniusWinterDayIsShortAndLate() {
+ int[] t = Sun.riseAndSet(LocalDate.of(2026, 12, 21), 54.69, 25.28, VILNIUS);
+ assertTrue("sunrise " + t[0], t[0] > 8 * 60 + 30 && t[0] < 9 * 60); // about 08:40
+ assertTrue("sunset " + t[1], t[1] > 15 * 60 + 40 && t[1] < 16 * 60 + 5); // about 15:55
+ }
+
+ @Test
+ public void vilniusSummerDayIsLongInLocalSummerTime() {
+ int[] t = Sun.riseAndSet(LocalDate.of(2026, 6, 21), 54.69, 25.28, VILNIUS);
+ assertTrue("sunrise " + t[0], t[0] > 4 * 60 + 30 && t[0] < 4 * 60 + 55); // about 04:42
+ assertTrue("sunset " + t[1], t[1] > 21 * 60 + 50 && t[1] < 22 * 60 + 10); // about 22:00
+ }
+
+ @Test
+ public void noSunriseOrSunsetAtThePoles() {
+ assertNull(Sun.riseAndSet(LocalDate.of(2026, 12, 21), 78.2, 15.6, ZoneId.of("Arctic/Longyearbyen")));
+ assertNull(Sun.riseAndSet(LocalDate.of(2026, 6, 21), 78.2, 15.6, ZoneId.of("Arctic/Longyearbyen")));
+ }
+
+ @Test
+ public void skyGlowsLandOnTheRealSunriseAndSunset() {
+ int sunrise = 8 * 60 + 40, sunset = 15 * 60 + 55;
+ assertEquals(Sky.at(Sky.REF_SUNRISE).sun, Sky.at(sunrise, sunrise, sunset).sun);
+ assertEquals(Sky.at(Sky.REF_SUNSET).bottom, Sky.at(sunset, sunrise, sunset).bottom);
+ assertEquals(Sky.at(0).top, Sky.at(0, sunrise, sunset).top);
+ assertEquals(Sky.at(1440).top, Sky.at(1440, sunrise, sunset).top);
+ // Five o'clock on that winter day is past sunset: the glow has gone down on the right,
+ // where the reference day still has the sun in the afternoon sky.
+ assertTrue(Sky.at(17 * 60, sunrise, sunset).sunX > Sky.at(17 * 60).sunX + 0.1f);
+ }
+
+ @Test
+ public void theReferenceDayMapsOntoItself() {
+ for (int m = 0; m <= 1440; m += 30) {
+ assertEquals(Sky.at(m).bottom, Sky.at(m, Sky.REF_SUNRISE, Sky.REF_SUNSET).bottom);
+ }
+ }
+
+ @Test
+ public void nonsenseSunTimesFallBackToTheReferenceDay() {
+ assertEquals(Sky.at(600).bottom, Sky.at(600, 900, 800).bottom);
+ assertEquals(Sky.at(600).bottom, Sky.at(600, 0, 1440).bottom);
+ }
+}