From 421876b69e1bd71999f418c986555eb4fb86e84d Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 21 Oct 2014 03:18:10 +0200 Subject: [PATCH] WeatherInformation: improvements in settings activities --- .../exampletdd/NotificationIntentService.java | 9 +-- .../de/example/exampletdd/WidgetIntentService.java | 5 +- .../fragment/current/CurrentFragment.java | 9 ++- .../fragment/overview/OverviewFragment.java | 5 +- .../WeatherInformationPreferencesFragment.java | 68 +++++++++++++++++----- .../fragment/specific/SpecificFragment.java | 9 ++- app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/weather_preferences.xml | 9 +++ 8 files changed, 91 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/de/example/exampletdd/NotificationIntentService.java b/app/src/main/java/de/example/exampletdd/NotificationIntentService.java index 596b43f..6f4f311 100644 --- a/app/src/main/java/de/example/exampletdd/NotificationIntentService.java +++ b/app/src/main/java/de/example/exampletdd/NotificationIntentService.java @@ -113,9 +113,10 @@ public class NotificationIntentService extends IntentService { // 1.1 Temperature String tempSymbol; UnitsConversor tempUnitsConversor; - String keyPreference = this.getResources().getString(R.string.weather_preferences_temperature_key); - String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); - String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + final String keyPreference = this.getApplicationContext().getString(R.string.weather_preferences_notifications_temperature_key); + final String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); if (unitsPreferenceValue.equals(values[0])) { tempSymbol = values[0]; tempUnitsConversor = new UnitsConversor(){ @@ -124,7 +125,7 @@ public class NotificationIntentService extends IntentService { public double doConversion(final double value) { return value - 273.15; } - + }; } else if (unitsPreferenceValue.equals(values[1])) { tempSymbol = values[1]; diff --git a/app/src/main/java/de/example/exampletdd/WidgetIntentService.java b/app/src/main/java/de/example/exampletdd/WidgetIntentService.java index d0af228..32228fe 100644 --- a/app/src/main/java/de/example/exampletdd/WidgetIntentService.java +++ b/app/src/main/java/de/example/exampletdd/WidgetIntentService.java @@ -166,7 +166,7 @@ public class WidgetIntentService extends IntentService { // What was saved to permanent storage (or default values if it is the first time) final String tempValue = this.getSharedPreferences("WIDGET_PREFERENCES", Context.MODE_PRIVATE) .getString(realKeyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); - String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + final String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); if (tempValue.equals(values[0])) { tempSymbol = values[0]; tempUnitsConversor = new UnitsConversor(){ @@ -249,6 +249,9 @@ public class WidgetIntentService extends IntentService { if (!isCountry) { remoteView.setViewVisibility(R.id.weather_appwidget_country, View.GONE); } else { + // TODO: It is as if Android has a view cache. If I did not set VISIBLE value, + // the country field would be gone forever... :/ + remoteView.setViewVisibility(R.id.weather_appwidget_country, View.VISIBLE); remoteView.setTextViewText(R.id.weather_appwidget_country, country); } diff --git a/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java b/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java index 13f9a77..d3a3d1e 100644 --- a/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -208,8 +208,9 @@ public class CurrentFragment extends Fragment { String tempSymbol; UnitsConversor tempUnitsConversor; String keyPreference = this.getResources().getString(R.string.weather_preferences_temperature_key); - String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); if (unitsPreferenceValue.equals(values[0])) { tempSymbol = values[0]; tempUnitsConversor = new UnitsConversor(){ @@ -246,8 +247,9 @@ public class CurrentFragment extends Fragment { String windSymbol; UnitsConversor windUnitsConversor; keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key); - unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); values = this.getResources().getStringArray(R.array.weather_preferences_wind); + unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_wind_meters)); if (unitsPreferenceValue.equals(values[0])) { windSymbol = values[0]; windUnitsConversor = new UnitsConversor(){ @@ -272,8 +274,9 @@ public class CurrentFragment extends Fragment { String pressureSymbol; UnitsConversor pressureUnitsConversor; keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key); - unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); values = this.getResources().getStringArray(R.array.weather_preferences_pressure); + unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_pressure_pascal)); if (unitsPreferenceValue.equals(values[0])) { pressureSymbol = values[0]; pressureUnitsConversor = new UnitsConversor(){ diff --git a/app/src/main/java/de/example/exampletdd/fragment/overview/OverviewFragment.java b/app/src/main/java/de/example/exampletdd/fragment/overview/OverviewFragment.java index 682e5bd..139afe9 100644 --- a/app/src/main/java/de/example/exampletdd/fragment/overview/OverviewFragment.java +++ b/app/src/main/java/de/example/exampletdd/fragment/overview/OverviewFragment.java @@ -214,8 +214,9 @@ public class OverviewFragment extends ListFragment { UnitsConversor unitsConversor; String keyPreference = this.getResources().getString( R.string.weather_preferences_temperature_key); - final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); - String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + final String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + final String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); if (unitsPreferenceValue.equals(values[0])) { symbol = values[0]; unitsConversor = new UnitsConversor(){ diff --git a/app/src/main/java/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java b/app/src/main/java/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java index ba19c5e..b207fc6 100644 --- a/app/src/main/java/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java +++ b/app/src/main/java/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java @@ -32,7 +32,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment R.string.weather_preferences_temperature_key); Preference connectionPref = this.findPreference(keyPreference); String value = this.getPreferenceManager().getSharedPreferences() - .getString(keyPreference, ""); + .getString(keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); String humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -48,7 +48,8 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value); keyPreference = this.getString(R.string.weather_preferences_wind_key); connectionPref = this.findPreference(keyPreference); - value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, ""); + value = this.getPreferenceManager().getSharedPreferences() + .getString(keyPreference, this.getString(R.string.weather_preferences_wind_meters)); humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -62,7 +63,8 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value); keyPreference = this.getString(R.string.weather_preferences_pressure_key); connectionPref = this.findPreference(keyPreference); - value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, ""); + value = this.getPreferenceManager().getSharedPreferences() + .getString(keyPreference, this.getString(R.string.weather_preferences_pressure_pascal)); humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -77,7 +79,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment keyPreference = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_day_forecast_key); connectionPref = this.findPreference(keyPreference); - value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, ""); + value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]); humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -94,7 +96,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment keyPreference = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_refresh_interval_key); connectionPref = this.findPreference(keyPreference); - value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, ""); + value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]); humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -120,7 +122,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment R.string.weather_preferences_update_time_rate_key); connectionPref = this.findPreference(keyPreference); value = this.getPreferenceManager().getSharedPreferences() - .getString(keyPreference, ""); + .getString(keyPreference, values[0]); humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -134,6 +136,24 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment humanValue = humanValues[4]; } connectionPref.setSummary(humanValue); + + // Notifications temperature units + values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value); + keyPreference = this.getActivity().getApplicationContext().getString( + R.string.weather_preferences_notifications_temperature_key); + connectionPref = this.findPreference(keyPreference); + value = this.getPreferenceManager().getSharedPreferences() + .getString(keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); + humanValue = ""; + if (value.equals(values[0])) { + humanValue = humanValues[0]; + } else if (value.equals(values[1])) { + humanValue = humanValues[1]; + } else if (value.equals(values[2])) { + humanValue = humanValues[2]; + } + connectionPref.setSummary(humanValue); } @Override @@ -162,7 +182,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment R.string.weather_preferences_temperature_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); - final String value = sharedPreferences.getString(key, ""); + final String value = sharedPreferences.getString(key, values[0]); String humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -182,7 +202,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment keyValue = this.getString(R.string.weather_preferences_wind_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); - final String value = sharedPreferences.getString(key, ""); + final String value = sharedPreferences.getString(key, values[0]); String humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -200,7 +220,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment keyValue = this.getString(R.string.weather_preferences_pressure_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); - final String value = sharedPreferences.getString(key, ""); + final String value = sharedPreferences.getString(key, values[0]); String humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -219,7 +239,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment R.string.weather_preferences_day_forecast_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); - final String value = sharedPreferences.getString(key, ""); + final String value = sharedPreferences.getString(key, values[0]); String humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -239,7 +259,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment R.string.weather_preferences_refresh_interval_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); - final String value = sharedPreferences.getString(key, ""); + final String value = sharedPreferences.getString(key, values[0]); String humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -261,6 +281,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment } // Notification switch + values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate); keyValue = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_notifications_switch_key); if (key.equals(keyValue)) { @@ -269,7 +290,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment { keyValue = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_update_time_rate_key); - final String value = sharedPreferences.getString(keyValue, ""); + final String value = sharedPreferences.getString(keyValue, values[0]); this.updateNotification(value); } else { this.cancelNotification(); @@ -282,7 +303,7 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment R.string.weather_preferences_update_time_rate_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); - final String value = sharedPreferences.getString(key, ""); + final String value = sharedPreferences.getString(key, values[0]); String humanValue = ""; if (value.equals(values[0])) { humanValue = humanValues[0]; @@ -300,6 +321,27 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment connectionPref.setSummary(humanValue); return; } + + // Temperature units + values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value); + keyValue = this.getActivity().getApplicationContext().getString( + R.string.weather_preferences_notifications_temperature_key); + if (key.equals(keyValue)) { + final Preference connectionPref = this.findPreference(key); + final String value = sharedPreferences.getString(key, values[0]); + String humanValue = ""; + if (value.equals(values[0])) { + humanValue = humanValues[0]; + } else if (value.equals(values[1])) { + humanValue = humanValues[1]; + } else if (value.equals(values[2])) { + humanValue = humanValues[2]; + } + + connectionPref.setSummary(humanValue); + return; + } } private void updateNotification(final String updateTimeRate) { diff --git a/app/src/main/java/de/example/exampletdd/fragment/specific/SpecificFragment.java b/app/src/main/java/de/example/exampletdd/fragment/specific/SpecificFragment.java index e355c71..4912fe2 100644 --- a/app/src/main/java/de/example/exampletdd/fragment/specific/SpecificFragment.java +++ b/app/src/main/java/de/example/exampletdd/fragment/specific/SpecificFragment.java @@ -121,8 +121,9 @@ public class SpecificFragment extends Fragment { UnitsConversor tempUnitsConversor; String keyPreference = this.getResources().getString( R.string.weather_preferences_temperature_key); - String unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature); + String unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_temperature_celsius)); if (unitsPreferenceValue.equals(values[0])) { tempSymbol = values[0]; tempUnitsConversor = new UnitsConversor(){ @@ -159,8 +160,9 @@ public class SpecificFragment extends Fragment { String windSymbol; UnitsConversor windUnitsConversor; keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key); - unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); values = this.getResources().getStringArray(R.array.weather_preferences_wind); + unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_wind_meters)); if (unitsPreferenceValue.equals(values[0])) { windSymbol = values[0]; windUnitsConversor = new UnitsConversor(){ @@ -185,8 +187,9 @@ public class SpecificFragment extends Fragment { String pressureSymbol; UnitsConversor pressureUnitsConversor; keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key); - unitsPreferenceValue = sharedPreferences.getString(keyPreference, ""); values = this.getResources().getStringArray(R.array.weather_preferences_pressure); + unitsPreferenceValue = sharedPreferences.getString( + keyPreference, this.getString(R.string.weather_preferences_pressure_pascal)); if (unitsPreferenceValue.equals(values[0])) { pressureSymbol = values[0]; pressureUnitsConversor = new UnitsConversor(){ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dea5420..ec32311 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -39,6 +39,8 @@ weather_preferences_notifications_switch weather_preferences_update_time_rate Update time rate + weather_preferences_notifications_temperature + Temperature weather_preferences_day_forecast Forecast days number weather_preferences_refresh_interval @@ -57,6 +59,7 @@ standard atmosphere + Settings Refresh diff --git a/app/src/main/res/xml/weather_preferences.xml b/app/src/main/res/xml/weather_preferences.xml index baaa006..7034716 100644 --- a/app/src/main/res/xml/weather_preferences.xml +++ b/app/src/main/res/xml/weather_preferences.xml @@ -62,5 +62,14 @@ android:selectable="true" android:summary="fifteen minutes" android:dependency="@string/weather_preferences_notifications_switch_key"/> + -- 2.1.4