Skip to content

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

addons/weather: temperature_unit f for Fahrenheit, and show_both_units

Justin Newmane49b09fafe40
diff --git a/addons/weather/README.md b/addons/weather/README.md
index fdf89b4db..d31df6e81 100644
--- a/addons/weather/README.md
+++ b/addons/weather/README.md
@@ -16,7 +16,11 @@ sky-blue glow; on any other Android TV home it is an ordinary app.
"light rain" at 1 °C (ICON and GFS did, in a Ulaanbaatar blizzard). The app decides by the wet-bulb
temperature instead, which counts how dry the air is: at 1 °C or below, rain and drizzle are shown as snow.
- 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.
+- Degrees Celsius by default, English words; day names follow the TV's language. For Fahrenheit set
+ `temperature_unit` to `f` in `app/src/main/res/values/options.xml` before building: the temperature now,
+ feels-like and the days' highs and lows turn to °F, converted from the reading before rounding, and the wind to
+ mph. `show_both_units` adds the temperature now in the other unit at the start of the "Feels like" line
+ ("22°C · Feels like 70°").
## Options
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 d7b2b050f..d156c8320 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
@@ -49,12 +49,12 @@ final class Forecast {
}
/**
- * 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.
+ * The weather now: degrees Celsius as read (air and feels-like; {@link #degrees} rounds them for the page), 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;
+ final double tempC;
+ final double feelsC;
/** The WMO code, rain turned into snow where the air is cold enough for it (see {@link #snowIfCold}). */
final int code;
final boolean day;
@@ -65,7 +65,7 @@ final class Forecast {
final int humidity;
final int windKmh;
- Report(int tempC, int feelsC, int code, boolean day, List<Day> days, int aqi, int humidity, int windKmh) {
+ Report(double tempC, double feelsC, int code, boolean day, List<Day> days, int aqi, int humidity, int windKmh) {
this.tempC = tempC;
this.feelsC = feelsC;
this.code = code;
@@ -77,14 +77,14 @@ final class Forecast {
}
}
- /** One forecast day: its date, the WMO code and the high and low in whole degrees Celsius. */
+ /** One forecast day: its date, the WMO code and the high and low in degrees Celsius as read. */
static final class Day {
final LocalDate date;
final int code;
- final int maxC;
- final int minC;
+ final double maxC;
+ final double minC;
- Day(LocalDate date, int code, int maxC, int minC) {
+ Day(LocalDate date, int code, double maxC, double minC) {
this.date = date;
this.code = code;
this.maxC = maxC;
@@ -130,7 +130,7 @@ final class Forecast {
double temp = current.getDouble("temperature_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")),
+ return new Report(temp, current.getDouble("apparent_temperature"),
code, current.getInt("is_day") == 1,
days(reply.getJSONObject("daily")), aqiOrUnknown(where),
(int) Math.round(humidity), (int) Math.round(current.optDouble("wind_speed_10m", 0)));
@@ -171,7 +171,7 @@ final class Forecast {
// A day whose high stays near freezing gets the same rain-to-snow correction, with the high and
// saturated air, which is the warm end of the day's precipitation.
out.add(new Day(LocalDate.parse(dates.getString(i)), snowIfCold(codes.getInt(i), max.getDouble(i), 100),
- (int) Math.round(max.getDouble(i)), (int) Math.round(min.getDouble(i))));
+ max.getDouble(i), min.getDouble(i)));
}
return out;
}
@@ -200,6 +200,16 @@ final class Forecast {
}
}
+ /** Whole degrees for the page: Celsius as read, or Fahrenheit converted before rounding (options.xml temperature_unit). */
+ static int degrees(double celsius, boolean fahrenheit) {
+ return (int) Math.round(fahrenheit ? celsius * 9 / 5 + 32 : celsius);
+ }
+
+ /** Whole miles per hour from km/h. */
+ static int mph(int kmh) {
+ return (int) Math.round(kmh / 1.609344);
+ }
+
// ---- words and colours -------------------------------------------------------------------------
/** The words for a WMO weather code, as fine as the code goes ("Light snow", not "Snow"). */
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 a95f7ac88..06a6b1c6c 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
@@ -44,6 +44,9 @@ public final class WeatherActivity extends Activity {
private int forecastDays;
private boolean showHumidity;
private boolean showWind;
+ /** options.xml: degrees Fahrenheit (and mph) instead of Celsius, and the other unit on the "Feels like" line. */
+ private boolean fahrenheit;
+ private boolean bothUnits;
@Override
protected void onCreate(Bundle state) {
@@ -53,6 +56,8 @@ public final class WeatherActivity extends Activity {
forecastDays = Forecast.forecastDays(getResources().getInteger(R.integer.forecast_days));
showHumidity = getResources().getBoolean(R.bool.show_humidity);
showWind = getResources().getBoolean(R.bool.show_wind);
+ fahrenheit = "f".equalsIgnoreCase(getString(R.string.temperature_unit).trim());
+ bothUnits = getResources().getBoolean(R.bool.show_both_units);
root = findViewById(R.id.root);
content = findViewById(R.id.content);
message = findViewById(R.id.message);
@@ -108,11 +113,19 @@ public final class WeatherActivity extends Activity {
shown = true;
root.setBackground(Forecast.backdrop(r.code, r.day));
((TextView) findViewById(R.id.place)).setText(place.getString("name", ""));
- ((TextView) findViewById(R.id.temp)).setText(r.tempC + "°");
+ ((TextView) findViewById(R.id.temp)).setText(Forecast.degrees(r.tempC, fahrenheit) + "°");
((TextView) findViewById(R.id.sky)).setText(Forecast.sky(r.code, r.day));
- StringBuilder feels = new StringBuilder(getString(R.string.feels_like, r.feelsC));
+ StringBuilder feels = new StringBuilder();
+ if (bothUnits) {
+ feels.append(getString(fahrenheit ? R.string.other_unit_c : R.string.other_unit_f,
+ Forecast.degrees(r.tempC, !fahrenheit))).append(" · ");
+ }
+ feels.append(getString(R.string.feels_like, Forecast.degrees(r.feelsC, fahrenheit)));
if (showHumidity) feels.append(" · ").append(getString(R.string.humidity, r.humidity));
- if (showWind) feels.append(" · ").append(getString(R.string.wind, r.windKmh));
+ if (showWind) {
+ feels.append(" · ").append(fahrenheit ? getString(R.string.wind_mph, Forecast.mph(r.windKmh))
+ : getString(R.string.wind, r.windKmh));
+ }
((TextView) findViewById(R.id.feels)).setText(feels);
TextView air = findViewById(R.id.airQuality);
@@ -141,7 +154,8 @@ public final class WeatherActivity extends Activity {
.getDisplayName(compact ? TextStyle.SHORT : TextStyle.FULL, Locale.getDefault()));
((TextView) card.findViewById(R.id.daySky)).setText(Forecast.sky(d.code, true));
TextView temps = card.findViewById(R.id.dayTemps);
- temps.setText(getString(R.string.high_low, d.maxC, d.minC));
+ temps.setText(getString(R.string.high_low, Forecast.degrees(d.maxC, fahrenheit),
+ Forecast.degrees(d.minC, fahrenheit)));
if (compact) {
int pad = Math.round(14 * getResources().getDisplayMetrics().density);
card.setPadding(pad, pad, pad, pad);
diff --git a/addons/weather/app/src/main/res/values/options.xml b/addons/weather/app/src/main/res/values/options.xml
index a93a258c5..dfada416a 100644
--- a/addons/weather/app/src/main/res/values/options.xml
+++ b/addons/weather/app/src/main/res/values/options.xml
@@ -5,6 +5,11 @@
<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". -->
+ <!-- The wind speed now, after "Feels like": km/h, or mph with temperature_unit f. -->
<bool name="show_wind">false</bool>
+ <!-- c: degrees Celsius. f: degrees Fahrenheit, for the temperature now, feels-like, the days' highs and lows
+ and the wind (mph). -->
+ <string name="temperature_unit" translatable="false">c</string>
+ <!-- The temperature now in the other unit too, at the start of the "Feels like" line ("22°C · Feels like 70°"). -->
+ <bool name="show_both_units">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 e24643636..acd3d5fa7 100644
--- a/addons/weather/app/src/main/res/values/strings.xml
+++ b/addons/weather/app/src/main/res/values/strings.xml
@@ -5,6 +5,9 @@
<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="wind_mph">Wind %1$d mph</string>
+ <string name="other_unit_c">%1$d°C</string>
+ <string name="other_unit_f">%1$d°F</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 00b896fae..3fb54ffe2 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
@@ -63,4 +63,20 @@ public class ForecastTest {
assertEquals(13.7, Forecast.wetBulb(20, 50), 0.1);
assertEquals(true, Forecast.wetBulb(1.1, 86) < 1.0);
}
+
+ @Test
+ public void degreesRoundOnlyOnceInEitherUnit() {
+ assertEquals(22, Forecast.degrees(21.6, false));
+ assertEquals(71, Forecast.degrees(21.6, true)); // 70.88, not 22 °C rounded first (71.6 -> 72)
+ assertEquals(32, Forecast.degrees(0, true));
+ assertEquals(-40, Forecast.degrees(-40, true));
+ assertEquals(212, Forecast.degrees(100, true));
+ }
+
+ @Test
+ public void windInMilesPerHour() {
+ assertEquals(0, Forecast.mph(0));
+ assertEquals(10, Forecast.mph(16));
+ assertEquals(62, Forecast.mph(100));
+ }
}