Skip to content

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

addons: change weather options with the TV remote

DVOpenLabs1794ecc8cfc8
diff --git a/addons/weather/README.md b/addons/weather/README.md
index 8a427f36a..1848e8224 100644
--- a/addons/weather/README.md
+++ b/addons/weather/README.md
@@ -20,7 +20,16 @@ sky-blue glow; on any other Android TV home it is an ordinary app.
## Options
-Set in `app/src/main/res/values/options.xml` before building. With the defaults the page is as above.
+Open **Weather settings** beside **Change city** at the bottom of the page. Use Up/Down and OK to change
+units, forecast length, both units, humidity and wind. Back cancels a unit/day picker; **Done** or Back
+closes settings. Choices are saved on this TV and survive closing the app and restarting the TV.
+
+Display changes use the current reading, without another network request or changing its update time.
+Changing the forecast length fetches once when settings closes. **Reset weather settings** restores the
+build defaults without changing the city.
+
+`app/src/main/res/values/options.xml` still sets the defaults for a fresh installation and for Reset.
+Saved choices override these defaults. With no saved choices the page is as above.
| Option | Default | What it does |
|---|---|---|
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 06a6b1c6c..1858e39a2 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
@@ -12,6 +12,7 @@ import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
+import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
@@ -36,15 +37,20 @@ public final class WeatherActivity extends Activity {
private final Handler main = new Handler(Looper.getMainLooper());
private final Runnable tick = this::refresh;
private SharedPreferences place;
+ private SharedPreferences options;
private View root;
private View content;
private TextView message;
private boolean shown;
- /** options.xml: days after today, and the extras on the "Feels like" line. */
+ private boolean resumed;
+ private int requestId;
+ private Forecast.Report lastReport;
+ private Date updatedAt;
+ /** Saved choices, falling back to options.xml until the owner changes them. */
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. */
+ /** Degrees Fahrenheit (and mph), and the other unit on the "Feels like" line. */
private boolean fahrenheit;
private boolean bothUnits;
@@ -53,16 +59,14 @@ public final class WeatherActivity extends Activity {
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);
- fahrenheit = "f".equalsIgnoreCase(getString(R.string.temperature_unit).trim());
- bothUnits = getResources().getBoolean(R.bool.show_both_units);
+ options = getSharedPreferences("options", MODE_PRIVATE);
+ readOptions();
root = findViewById(R.id.root);
content = findViewById(R.id.content);
message = findViewById(R.id.message);
View changeCity = findViewById(R.id.changeCity);
changeCity.setOnClickListener(v -> pickCity());
+ findViewById(R.id.weatherSettings).setOnClickListener(v -> settings());
changeCity.requestFocus();
if (!place.contains("name")) {
message.setText(R.string.pick_first);
@@ -73,12 +77,15 @@ public final class WeatherActivity extends Activity {
@Override
protected void onResume() {
super.onResume();
+ resumed = true;
if (place.contains("name")) refresh();
}
@Override
protected void onPause() {
super.onPause();
+ resumed = false;
+ requestId++;
main.removeCallbacks(tick); // nothing runs while the page is not on screen
}
@@ -90,19 +97,26 @@ public final class WeatherActivity extends Activity {
private void refresh() {
main.removeCallbacks(tick);
+ if (!resumed || !place.contains("name")) return;
+ int request = ++requestId;
+ int requestedDays = forecastDays;
double lat = Double.longBitsToDouble(place.getLong("lat", 0));
double lon = Double.longBitsToDouble(place.getLong("lon", 0));
io.execute(() -> {
Forecast.Report report;
try {
- report = Forecast.fetch(lat, lon, forecastDays);
+ report = Forecast.fetch(lat, lon, requestedDays);
} catch (Exception e) {
report = null;
}
Forecast.Report result = report;
main.post(() -> {
- if (isDestroyed()) return;
- if (result != null) show(result);
+ if (isDestroyed() || !resumed || request != requestId) return;
+ if (result != null) {
+ lastReport = result;
+ updatedAt = new Date();
+ show(result);
+ }
else if (!shown) message.setText(R.string.offline);
main.postDelayed(tick, result != null ? Forecast.REFRESH_MS : Forecast.RETRY_MS);
});
@@ -145,9 +159,11 @@ public final class WeatherActivity extends Activity {
days.removeAllViews();
LayoutInflater inflater = getLayoutInflater();
// 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++) {
+ int count = Math.min(forecastDays, r.days.size());
+ boolean compact = count > Forecast.MIN_DAYS;
+ days.getLayoutParams().width = compact ? LinearLayout.LayoutParams.MATCH_PARENT
+ : LinearLayout.LayoutParams.WRAP_CONTENT;
+ for (int i = 0; i < count; 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()
@@ -165,18 +181,103 @@ public final class WeatherActivity extends Activity {
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);
+ lp.setMarginEnd(i < count - 1 ? Math.round(12 * getResources().getDisplayMetrics().density) : 0);
card.setLayoutParams(lp);
}
days.addView(card);
}
((TextView) findViewById(R.id.updated))
- .setText(getString(R.string.updated, DateFormat.getTimeFormat(this).format(new Date())));
+ .setText(getString(R.string.updated, DateFormat.getTimeFormat(this).format(updatedAt)));
message.setVisibility(View.GONE);
content.setVisibility(View.VISIBLE);
}
+ private void readOptions() {
+ forecastDays = Forecast.forecastDays(options.getInt("forecast_days",
+ getResources().getInteger(R.integer.forecast_days)));
+ showHumidity = options.getBoolean("show_humidity", getResources().getBoolean(R.bool.show_humidity));
+ showWind = options.getBoolean("show_wind", getResources().getBoolean(R.bool.show_wind));
+ fahrenheit = options.getBoolean("fahrenheit", "f".equalsIgnoreCase(getString(R.string.temperature_unit).trim()));
+ bothUnits = options.getBoolean("show_both_units", getResources().getBoolean(R.bool.show_both_units));
+ }
+
+ private String optionRow(int label, boolean enabled) {
+ return getString(R.string.setting_value, getString(label), getString(enabled ? R.string.option_on : R.string.option_off));
+ }
+
+ private String[] optionRows() {
+ return new String[] {
+ getString(R.string.setting_value, getString(R.string.setting_units),
+ getString(fahrenheit ? R.string.unit_f : R.string.unit_c)),
+ optionRow(R.string.setting_both_units, bothUnits),
+ getString(R.string.setting_value, getString(R.string.setting_days),
+ dayCount(forecastDays)),
+ optionRow(R.string.setting_humidity, showHumidity),
+ optionRow(R.string.setting_wind, showWind),
+ getString(R.string.setting_reset)
+ };
+ }
+
+ private String dayCount(int days) {
+ return getResources().getQuantityString(R.plurals.day_count, days, days);
+ }
+
+ /** Native lists keep D-pad focus, checked values and Back handling without a new dependency. */
+ private void settings() {
+ int initialDays = forecastDays;
+ ArrayAdapter<String> rows = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, optionRows());
+ AlertDialog dialog = new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_Alert)
+ .setTitle(R.string.weather_settings)
+ .setAdapter(rows, null)
+ .setNegativeButton(R.string.settings_done, null)
+ .create();
+ Runnable changed = () -> {
+ readOptions();
+ rows.setNotifyOnChange(false);
+ rows.clear();
+ rows.addAll(optionRows());
+ rows.notifyDataSetChanged();
+ if (lastReport != null) show(lastReport); // Display choices do not make another network request.
+ };
+ dialog.setOnDismissListener(d -> {
+ if (forecastDays != initialDays) refresh(); // One fetch after all choices, including Back.
+ });
+ dialog.show();
+ dialog.getListView().setOnItemClickListener((parent, view, position, id) -> {
+ if (position == 0) {
+ new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_Alert)
+ .setTitle(R.string.setting_units)
+ .setSingleChoiceItems(new String[] {getString(R.string.unit_c), getString(R.string.unit_f)},
+ fahrenheit ? 1 : 0, (picker, which) -> {
+ options.edit().putBoolean("fahrenheit", which == 1).apply();
+ changed.run();
+ picker.dismiss();
+ })
+ .setNegativeButton(android.R.string.cancel, null).show();
+ } else if (position == 2) {
+ String[] days = new String[Forecast.MAX_DAYS - Forecast.MIN_DAYS + 1];
+ for (int i = 0; i < days.length; i++) days[i] = dayCount(i + Forecast.MIN_DAYS);
+ new AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Dialog_Alert)
+ .setTitle(R.string.setting_days)
+ .setSingleChoiceItems(days, forecastDays - Forecast.MIN_DAYS, (picker, which) -> {
+ options.edit().putInt("forecast_days", which + Forecast.MIN_DAYS).apply();
+ changed.run();
+ picker.dismiss();
+ })
+ .setNegativeButton(android.R.string.cancel, null).show();
+ } else {
+ if (position == 1) options.edit().putBoolean("show_both_units", !bothUnits).apply();
+ if (position == 3) options.edit().putBoolean("show_humidity", !showHumidity).apply();
+ if (position == 4) options.edit().putBoolean("show_wind", !showWind).apply();
+ if (position == 5) options.edit().clear().apply(); // Never clears the chosen city.
+ changed.run();
+ }
+ });
+ dialog.getListView().requestFocus();
+ dialog.getListView().setSelection(0);
+ }
+
/** Type a city, pick it from the matches, keep it. Back leaves the current place as it was. */
private void pickCity() {
EditText name = new EditText(this);
@@ -246,6 +347,10 @@ public final class WeatherActivity extends Activity {
.putLong("lat", Double.doubleToRawLongBits(p.latitude))
.putLong("lon", Double.doubleToRawLongBits(p.longitude))
.apply();
+ lastReport = null;
+ shown = false;
+ content.setVisibility(View.INVISIBLE);
+ message.setVisibility(View.VISIBLE);
message.setText(R.string.loading);
refresh();
})
diff --git a/addons/weather/app/src/main/res/layout/activity_weather.xml b/addons/weather/app/src/main/res/layout/activity_weather.xml
index bd353914a..b6c5a6e26 100644
--- a/addons/weather/app/src/main/res/layout/activity_weather.xml
+++ b/addons/weather/app/src/main/res/layout/activity_weather.xml
@@ -113,7 +113,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pill"
+ android:minHeight="48dp"
+ android:gravity="center"
android:focusable="true"
+ android:nextFocusRight="@id/weatherSettings"
android:fontFamily="sans-serif-medium"
android:paddingStart="20dp"
android:paddingTop="10dp"
@@ -123,6 +126,25 @@
android:textColor="@color/pill_text"
android:textSize="16sp" />
+ <TextView
+ android:id="@+id/weatherSettings"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="12dp"
+ android:minHeight="48dp"
+ android:gravity="center"
+ android:background="@drawable/pill"
+ android:focusable="true"
+ android:nextFocusLeft="@id/changeCity"
+ android:fontFamily="sans-serif-medium"
+ android:paddingStart="20dp"
+ android:paddingTop="10dp"
+ android:paddingEnd="20dp"
+ android:paddingBottom="10dp"
+ android:text="@string/weather_settings"
+ android:textColor="@color/pill_text"
+ android:textSize="16sp" />
+
<TextView
android:id="@+id/updated"
android:layout_width="wrap_content"
diff --git a/addons/weather/app/src/main/res/values-it/strings.xml b/addons/weather/app/src/main/res/values-it/strings.xml
index de83b6ea6..28177f301 100644
--- a/addons/weather/app/src/main/res/values-it/strings.xml
+++ b/addons/weather/app/src/main/res/values-it/strings.xml
@@ -1,5 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
+ <string name="weather_settings">Impostazioni meteo</string>
+ <string name="settings_done">Fine</string>
+ <string name="setting_units">Unità di temperatura</string>
+ <string name="unit_c">Celsius (°C)</string>
+ <string name="unit_f">Fahrenheit (°F)</string>
+ <string name="setting_both_units">Mostra entrambe le unità</string>
+ <string name="setting_days">Durata delle previsioni</string>
+ <plurals name="day_count">
+ <item quantity="one">%1$d giorno</item>
+ <item quantity="many">%1$d giorni</item>
+ <item quantity="other">%1$d giorni</item>
+ </plurals>
+ <string name="setting_humidity">Mostra umidità</string>
+ <string name="setting_wind">Mostra vento</string>
+ <string name="setting_reset">Ripristina impostazioni meteo</string>
+ <string name="setting_value">%1$s · %2$s</string>
+ <string name="option_on">Attivo</string>
+ <string name="option_off">Disattivo</string>
<string name="app_name">Meteo</string>
<string name="feels_like">Percepita %1$d°</string>
diff --git a/addons/weather/app/src/main/res/values-ko/strings.xml b/addons/weather/app/src/main/res/values-ko/strings.xml
index dcfcc0bb1..8c1e098cd 100644
--- a/addons/weather/app/src/main/res/values-ko/strings.xml
+++ b/addons/weather/app/src/main/res/values-ko/strings.xml
@@ -1,5 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
+ <string name="weather_settings">날씨 설정</string>
+ <string name="settings_done">완료</string>
+ <string name="setting_units">온도 단위</string>
+ <string name="unit_c">섭씨 (°C)</string>
+ <string name="unit_f">화씨 (°F)</string>
+ <string name="setting_both_units">두 단위 모두 표시</string>
+ <string name="setting_days">예보 기간</string>
+ <plurals name="day_count">
+ <item quantity="other">%1$d일</item>
+ </plurals>
+ <string name="setting_humidity">습도 표시</string>
+ <string name="setting_wind">풍속 표시</string>
+ <string name="setting_reset">날씨 설정 초기화</string>
+ <string name="setting_value">%1$s · %2$s</string>
+ <string name="option_on">켜짐</string>
+ <string name="option_off">꺼짐</string>
<string name="app_name">날씨</string>
<string name="feels_like">체감 %1$d°</string>
diff --git a/addons/weather/app/src/main/res/values-tr/strings.xml b/addons/weather/app/src/main/res/values-tr/strings.xml
index 27c9aaa8e..1730fab6a 100644
--- a/addons/weather/app/src/main/res/values-tr/strings.xml
+++ b/addons/weather/app/src/main/res/values-tr/strings.xml
@@ -1,5 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
+ <string name="weather_settings">Hava durumu ayarları</string>
+ <string name="settings_done">Bitti</string>
+ <string name="setting_units">Sıcaklık birimi</string>
+ <string name="unit_c">Santigrat (°C)</string>
+ <string name="unit_f">Fahrenhayt (°F)</string>
+ <string name="setting_both_units">Her iki birimi göster</string>
+ <string name="setting_days">Tahmin süresi</string>
+ <plurals name="day_count">
+ <item quantity="one">%1$d gün</item>
+ <item quantity="other">%1$d gün</item>
+ </plurals>
+ <string name="setting_humidity">Nemi göster</string>
+ <string name="setting_wind">Rüzgârı göster</string>
+ <string name="setting_reset">Hava durumu ayarlarını sıfırla</string>
+ <string name="setting_value">%1$s · %2$s</string>
+ <string name="option_on">Açık</string>
+ <string name="option_off">Kapalı</string>
<string name="app_name">Weather</string>
<string name="feels_like">Hissedilen %1$d°</string>
diff --git a/addons/weather/app/src/main/res/values/options.xml b/addons/weather/app/src/main/res/values/options.xml
index dfada416a..3046432c2 100644
--- a/addons/weather/app/src/main/res/values/options.xml
+++ b/addons/weather/app/src/main/res/values/options.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
-<!-- Options, set before building. With the defaults the page is as it always was. -->
+<!-- Build defaults. Saved choices in Weather settings override them; Reset restores these defaults. -->
<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>
diff --git a/addons/weather/app/src/main/res/values/strings.xml b/addons/weather/app/src/main/res/values/strings.xml
index acd3d5fa7..813cd00a8 100644
--- a/addons/weather/app/src/main/res/values/strings.xml
+++ b/addons/weather/app/src/main/res/values/strings.xml
@@ -12,6 +12,23 @@
<string name="aqi_line">Air quality %1$d · %2$s</string>
<string name="updated">Updated %1$s · Open-Meteo</string>
<string name="change_city">Change city</string>
+ <string name="weather_settings">Weather settings</string>
+ <string name="settings_done">Done</string>
+ <string name="setting_units">Temperature units</string>
+ <string name="unit_c">Celsius (°C)</string>
+ <string name="unit_f">Fahrenheit (°F)</string>
+ <string name="setting_both_units">Show both units</string>
+ <string name="setting_days">Forecast length</string>
+ <plurals name="day_count">
+ <item quantity="one">%1$d day</item>
+ <item quantity="other">%1$d days</item>
+ </plurals>
+ <string name="setting_humidity">Show humidity</string>
+ <string name="setting_wind">Show wind</string>
+ <string name="setting_reset">Reset weather settings</string>
+ <string name="setting_value">%1$s · %2$s</string>
+ <string name="option_on">On</string>
+ <string name="option_off">Off</string>
<string name="loading">Loading the weather…</string>
<string name="offline">Can\'t reach Open-Meteo. Check the TV\'s internet connection; it tries again in 5 minutes.</string>