Skip to content

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

addons/weather: options for a 7-day forecast, humidity and wind

majnioui6868934796e0
diff --git a/addons/weather/README.md b/addons/weather/README.md
index 7d303869f..fdf89b4db 100644
--- a/addons/weather/README.md
+++ b/addons/weather/README.md
@@ -18,6 +18,18 @@ sky-blue glow; on any other Android TV home it is an ordinary app.
- It reads Open-Meteo only while the page is on screen, every 30 minutes, or 5 minutes after a failure.
- Degrees Celsius, English words; day names follow the TV's language.
+## Options
+
+Set in `app/src/main/res/values/options.xml` before building. With the defaults the page is as above.
+
+| Option | Default | What it does |
+|---|---|---|
+| `forecast_days` | `3` | Days in the forecast row after today, 3 to 7. Above 3 the day cards share the row, with short day names, so a week fits on screen. |
+| `show_humidity` | `false` | The relative humidity now, after "Feels like". |
+| `show_wind` | `false` | The wind speed now in km/h, after "Feels like". |
+
+![A week, with humidity and wind](https://raw.githubusercontent.com/majnioui/cleanyourtv-contrib/pr-assets/addon-weather-week/weather-options.png)
+
## Permissions
| Permission | Why |
@@ -49,6 +61,8 @@ with your own key if you hand them out; the launcher pins its Show answer to the
Hisense SmartTV 4K FFM (lushan, MediaTek MT9676), Google TV 12 / Android 12: city search with the TV keyboard,
the weather page, the card prompt, and the card shown in the launcher's dock.
+The options: Xiaomi TV (MiTV-MOOR2), Google TV 11, with `forecast_days` 7, `show_humidity` and `show_wind` on.
+
## Maintainer
[@Sumyabazar1](https://github.com/Sumyabazar1)
diff --git a/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/Forecast.java b/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/Forecast.java
index 4b64211e3..ccbf5c044 100644
--- a/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/Forecast.java
+++ b/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/Forecast.java
@@ -20,7 +20,7 @@ import java.util.List;
import java.util.Locale;
/**
- * Open-Meteo, which needs no key: the place search, the weather now and for the next three days, and the US air
+ * Open-Meteo, which needs no key: the place search, the weather now and for the next days, and the US air
* quality index. The only thing sent is the place name typed in the search, then that place's latitude and
* longitude. Every call blocks, so WeatherActivity makes them off the UI thread.
*/
@@ -28,6 +28,8 @@ final class Forecast {
static final long REFRESH_MS = 30 * 60_000L;
static final long RETRY_MS = 5 * 60_000L;
private static final int TIMEOUT_MS = 8_000;
+ /** Days after today the forecast row can show (options.xml, forecast_days). */
+ static final int MIN_DAYS = 3, MAX_DAYS = 7;
private Forecast() { }
@@ -46,7 +48,10 @@ final class Forecast {
}
}
- /** The weather now: whole degrees Celsius (air and feels-like), the WMO weather code, day or night. */
+ /**
+ * The weather now: whole degrees Celsius (air and feels-like), the WMO weather code, day or night, the relative
+ * humidity in % and the wind speed in km/h.
+ */
static final class Report {
final int tempC;
final int feelsC;
@@ -57,14 +62,18 @@ final class Forecast {
final List<Day> days;
/** US AQI, or -1 when the air quality read failed. */
final int aqi;
+ final int humidity;
+ final int windKmh;
- Report(int tempC, int feelsC, int code, boolean day, List<Day> days, int aqi) {
+ Report(int tempC, int feelsC, int code, boolean day, List<Day> days, int aqi, int humidity, int windKmh) {
this.tempC = tempC;
this.feelsC = feelsC;
this.code = code;
this.day = day;
this.days = days;
this.aqi = aqi;
+ this.humidity = humidity;
+ this.windKmh = windKmh;
}
}
@@ -100,20 +109,31 @@ final class Forecast {
return out;
}
- /** The weather and air quality at a place. The air quality is a separate service: if it fails, aqi is -1. */
- static Report fetch(double latitude, double longitude) throws IOException, JSONException {
+ /** Days after today to show: the option, kept between {@link #MIN_DAYS} and {@link #MAX_DAYS}. */
+ static int forecastDays(int wanted) {
+ return Math.max(MIN_DAYS, Math.min(MAX_DAYS, wanted));
+ }
+
+ /**
+ * The weather and air quality at a place, with this many days after today. The air quality is a separate
+ * service: if it fails, aqi is -1.
+ */
+ static Report fetch(double latitude, double longitude, int days) throws IOException, JSONException {
String where = String.format(Locale.ROOT, "latitude=%.4f&longitude=%.4f", latitude, longitude);
// DWD's ICON model: Open-Meteo's default blend called light snow over Ulaanbaatar "overcast" while ICON,
// and Google, had snow falling.
JSONObject reply = get("https://api.open-meteo.com/v1/forecast?" + where + "&models=icon_seamless"
- + "&current=temperature_2m,relative_humidity_2m,apparent_temperature,weather_code,is_day"
- + "&daily=weather_code,temperature_2m_max,temperature_2m_min&timezone=auto&forecast_days=4");
+ + "&current=temperature_2m,relative_humidity_2m,apparent_temperature,weather_code,is_day,wind_speed_10m"
+ + "&daily=weather_code,temperature_2m_max,temperature_2m_min&timezone=auto&forecast_days="
+ + (forecastDays(days) + 1));
JSONObject current = reply.getJSONObject("current");
double temp = current.getDouble("temperature_2m");
- int code = snowIfCold(current.getInt("weather_code"), temp, current.getDouble("relative_humidity_2m"));
+ double humidity = current.getDouble("relative_humidity_2m");
+ int code = snowIfCold(current.getInt("weather_code"), temp, humidity);
return new Report((int) Math.round(temp), (int) Math.round(current.getDouble("apparent_temperature")),
code, current.getInt("is_day") == 1,
- days(reply.getJSONObject("daily")), aqiOrUnknown(where));
+ days(reply.getJSONObject("daily")), aqiOrUnknown(where),
+ (int) Math.round(humidity), (int) Math.round(current.optDouble("wind_speed_10m", 0)));
}
/**
diff --git a/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/WeatherActivity.java b/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/WeatherActivity.java
index d25aba249..a95f7ac88 100644
--- a/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/WeatherActivity.java
+++ b/addons/weather/app/src/main/java/io/github/sumyabazar1/weather/WeatherActivity.java
@@ -25,7 +25,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
- * The weather for one place, full screen: now, the air quality and the next three days. It reads Open-Meteo only
+ * The weather for one place, full screen: now, the air quality and the next days (options.xml). It reads Open-Meteo only
* while it is on screen (every {@link Forecast#REFRESH_MS}, or {@link Forecast#RETRY_MS} after a failure) and
* does nothing in the background. The place is chosen once with a search and kept in the app's preferences.
*/
@@ -40,12 +40,19 @@ public final class WeatherActivity extends Activity {
private View content;
private TextView message;
private boolean shown;
+ /** options.xml: days after today, and the extras on the "Feels like" line. */
+ private int forecastDays;
+ private boolean showHumidity;
+ private boolean showWind;
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_weather);
place = getSharedPreferences(PREFS, MODE_PRIVATE);
+ forecastDays = Forecast.forecastDays(getResources().getInteger(R.integer.forecast_days));
+ showHumidity = getResources().getBoolean(R.bool.show_humidity);
+ showWind = getResources().getBoolean(R.bool.show_wind);
root = findViewById(R.id.root);
content = findViewById(R.id.content);
message = findViewById(R.id.message);
@@ -83,7 +90,7 @@ public final class WeatherActivity extends Activity {
io.execute(() -> {
Forecast.Report report;
try {
- report = Forecast.fetch(lat, lon);
+ report = Forecast.fetch(lat, lon, forecastDays);
} catch (Exception e) {
report = null;
}
@@ -103,7 +110,10 @@ public final class WeatherActivity extends Activity {
((TextView) findViewById(R.id.place)).setText(place.getString("name", ""));
((TextView) findViewById(R.id.temp)).setText(r.tempC + "°");
((TextView) findViewById(R.id.sky)).setText(Forecast.sky(r.code, r.day));
- ((TextView) findViewById(R.id.feels)).setText(getString(R.string.feels_like, r.feelsC));
+ StringBuilder feels = new StringBuilder(getString(R.string.feels_like, r.feelsC));
+ if (showHumidity) feels.append(" · ").append(getString(R.string.humidity, r.humidity));
+ if (showWind) feels.append(" · ").append(getString(R.string.wind, r.windKmh));
+ ((TextView) findViewById(R.id.feels)).setText(feels);
TextView air = findViewById(R.id.airQuality);
if (r.aqi >= 0) {
@@ -121,12 +131,29 @@ public final class WeatherActivity extends Activity {
LinearLayout days = findViewById(R.id.days);
days.removeAllViews();
LayoutInflater inflater = getLayoutInflater();
- for (Forecast.Day d : r.days) {
+ // More than three days: the cards share the row, with short day names, so a week fits on screen.
+ boolean compact = r.days.size() > Forecast.MIN_DAYS;
+ if (compact) days.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
+ for (int i = 0; i < r.days.size(); i++) {
+ Forecast.Day d = r.days.get(i);
View card = inflater.inflate(R.layout.item_day, days, false);
- ((TextView) card.findViewById(R.id.dayName))
- .setText(d.date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault()));
+ ((TextView) card.findViewById(R.id.dayName)).setText(d.date.getDayOfWeek()
+ .getDisplayName(compact ? TextStyle.SHORT : TextStyle.FULL, Locale.getDefault()));
((TextView) card.findViewById(R.id.daySky)).setText(Forecast.sky(d.code, true));
- ((TextView) card.findViewById(R.id.dayTemps)).setText(getString(R.string.high_low, d.maxC, d.minC));
+ TextView temps = card.findViewById(R.id.dayTemps);
+ temps.setText(getString(R.string.high_low, d.maxC, d.minC));
+ if (compact) {
+ int pad = Math.round(14 * getResources().getDisplayMetrics().density);
+ card.setPadding(pad, pad, pad, pad);
+ TextView sky = card.findViewById(R.id.daySky);
+ sky.setTextSize(15);
+ sky.setSingleLine(false); // "Partly cloudy" on two lines rather than cut; every card two lines tall
+ sky.setLines(2);
+ temps.setTextSize(21);
+ LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
+ lp.setMarginEnd(i < r.days.size() - 1 ? Math.round(12 * getResources().getDisplayMetrics().density) : 0);
+ card.setLayoutParams(lp);
+ }
days.addView(card);
}
diff --git a/addons/weather/app/src/main/res/values/options.xml b/addons/weather/app/src/main/res/values/options.xml
new file mode 100644
index 000000000..a93a258c5
--- /dev/null
+++ b/addons/weather/app/src/main/res/values/options.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Options, set before building. With the defaults the page is as it always was. -->
+<resources>
+ <!-- Days in the forecast row after today, 3 to 7. Above 3 the day cards share the row and get more compact. -->
+ <integer name="forecast_days">3</integer>
+ <!-- The relative humidity now, after "Feels like". -->
+ <bool name="show_humidity">false</bool>
+ <!-- The wind speed now, in km/h, after "Feels like". -->
+ <bool name="show_wind">false</bool>
+</resources>
diff --git a/addons/weather/app/src/main/res/values/strings.xml b/addons/weather/app/src/main/res/values/strings.xml
index 9984e21eb..e24643636 100644
--- a/addons/weather/app/src/main/res/values/strings.xml
+++ b/addons/weather/app/src/main/res/values/strings.xml
@@ -3,6 +3,8 @@
<string name="app_name">Weather</string>
<string name="feels_like">Feels like %1$d°</string>
+ <string name="humidity">Humidity %1$d%%</string>
+ <string name="wind">Wind %1$d km/h</string>
<string name="high_low">%1$d° / %2$d°</string>
<string name="aqi_line">Air quality %1$d · %2$s</string>
<string name="updated">Updated %1$s · Open-Meteo</string>
diff --git a/addons/weather/app/src/test/java/io/github/sumyabazar1/weather/ForecastTest.java b/addons/weather/app/src/test/java/io/github/sumyabazar1/weather/ForecastTest.java
index 246f11ab0..00b896fae 100644
--- a/addons/weather/app/src/test/java/io/github/sumyabazar1/weather/ForecastTest.java
+++ b/addons/weather/app/src/test/java/io/github/sumyabazar1/weather/ForecastTest.java
@@ -49,6 +49,14 @@ public class ForecastTest {
assertEquals(3, Forecast.snowIfCold(3, -5, 60));
}
+ @Test
+ public void forecastDaysStayBetweenThreeAndSeven() {
+ assertEquals(3, Forecast.forecastDays(3));
+ assertEquals(7, Forecast.forecastDays(7));
+ assertEquals(3, Forecast.forecastDays(0));
+ assertEquals(7, Forecast.forecastDays(16));
+ }
+
@Test
public void wetBulbMatchesStull() {
// Stull (2011): 20 °C and 50 % give a wet-bulb of about 13.7 °C.