From 9b90c4b25c1a64a9ffc909695b78c6c8066343eb Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 7 Oct 2014 04:53:39 +0200 Subject: [PATCH] WeatherInformation: preferences activity WIP --- Android/WeatherInformation/res/values/arrays.xml | 26 +++- Android/WeatherInformation/res/values/strings.xml | 14 ++- .../res/xml/weather_preferences.xml | 63 ++++++---- .../WeatherInformationPreferencesActivity.java | 8 ++ .../fragment/current/CurrentFragment.java | 11 +- .../fragment/overview/OverviewFragment.java | 11 +- .../WeatherInformationPreferencesFragment.java | 139 +++++++++++++++------ 7 files changed, 196 insertions(+), 76 deletions(-) diff --git a/Android/WeatherInformation/res/values/arrays.xml b/Android/WeatherInformation/res/values/arrays.xml index 5900023..ef2b6d3 100644 --- a/Android/WeatherInformation/res/values/arrays.xml +++ b/Android/WeatherInformation/res/values/arrays.xml @@ -64,12 +64,11 @@ 14 - 5-Day Forecast - 10-Day Forecast - 14-Day Forecast + 5 day forecast + 10 day forecast + 14 day forecast - 0 900 1800 3600 @@ -77,11 +76,28 @@ 86400 - no updates fifteen minutes half hour one hour half day one day + + 300000 + 900000 + 1800000 + 3600000 + 7200000 + 43200000 + 86400000 + + + five minutes + fifteen minutes + half hour + one hour + two hours + half day + one day + diff --git a/Android/WeatherInformation/res/values/strings.xml b/Android/WeatherInformation/res/values/strings.xml index 210e158..e4153b8 100644 --- a/Android/WeatherInformation/res/values/strings.xml +++ b/Android/WeatherInformation/res/values/strings.xml @@ -18,14 +18,24 @@ HUMIDITY OK Icon weather + Settings + Units weather_preferences_units Fahrenheit Celsius - Unit of measurement for temperature + Temperature + Notifications + Disabled + Enabled + OFF + ON + weather_preferences_notifications_switch weather_preferences_update_time_rate - Update Time Rate + Update time rate weather_preferences_day_forecast Forecast days number + weather_preferences_refresh_interval + Refresh interval city not found country not found Please wait… diff --git a/Android/WeatherInformation/res/xml/weather_preferences.xml b/Android/WeatherInformation/res/xml/weather_preferences.xml index c3449ab..04751af 100644 --- a/Android/WeatherInformation/res/xml/weather_preferences.xml +++ b/Android/WeatherInformation/res/xml/weather_preferences.xml @@ -1,31 +1,50 @@ - - - - + android:summary="14 day forecast" /> + + + + + + + + diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java index e70cfc1..1ba899e 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java @@ -1,5 +1,6 @@ package de.example.exampletdd; +import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import de.example.exampletdd.fragment.preferences.WeatherInformationPreferencesFragment; @@ -16,4 +17,11 @@ public class WeatherInformationPreferencesActivity extends Activity { new WeatherInformationPreferencesFragment()).commit(); } + @Override + public void onResume() { + super.onResume(); + + final ActionBar actionBar = this.getActionBar(); + actionBar.setTitle(this.getString(R.string.weather_preferences_actionbar_title)); + } } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java index d3b60ae..4d21465 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -390,9 +390,14 @@ public class CurrentFragment extends Fragment { return false; } - final Date currentTime = new Date(); - // TODO: user settings instead of 120000L - if (((currentTime.getTime() - lastUpdate.getTime())) < 120000L) { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences( + this.getActivity().getApplicationContext()); + final String keyPreference = this.getString(R.string.weather_preferences_refresh_interval_key); + final String refresh = sharedPreferences.getString( + keyPreference, + this.getResources().getStringArray(R.array.weather_preferences_refresh_interval)[0]); + final Date currentTime = new Date(); + if (((currentTime.getTime() - lastUpdate.getTime())) < Long.valueOf(refresh)) { return true; } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java index fd746e0..f413122 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java @@ -298,9 +298,14 @@ public class OverviewFragment extends ListFragment { return false; } - final Date currentTime = new Date(); - // TODO: user settings instead of 120000L - if (((currentTime.getTime() - lastUpdate.getTime())) < 120000L) { + final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences( + this.getActivity().getApplicationContext()); + final String keyPreference = this.getString(R.string.weather_preferences_refresh_interval_key); + final String refresh = sharedPreferences.getString( + keyPreference, + this.getResources().getStringArray(R.array.weather_preferences_refresh_interval)[0]); + final Date currentTime = new Date(); + if (((currentTime.getTime() - lastUpdate.getTime())) < Long.valueOf(refresh)) { return true; } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java index 2b665e9..1315c88 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java @@ -32,39 +32,66 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment .getSharedPreferences().getString(keyPreference, "")); // Update Time Rate + String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate); + String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value); keyPreference = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_update_time_rate_key); connectionPref = this.findPreference(keyPreference); String value = this.getPreferenceManager().getSharedPreferences() .getString(keyPreference, ""); String humanValue = ""; - if (value.equals("0")) { - humanValue = "no updates"; - } else if (value.equals("900")) { - humanValue = "fifteen minutes"; - } else if (value.equals("1800")) { - humanValue = "half hour"; - } else if (value.equals("3600")) { - humanValue = "one hour"; - } else if (value.equals("43200")) { - humanValue = "half day"; - } else if (value.equals("86400")) { - humanValue = "one day"; + 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]; + } else if (value.equals(values[3])) { + humanValue = humanValues[3]; + } else if (value.equals(values[4])) { + humanValue = humanValues[4]; } connectionPref.setSummary(humanValue); // Forecast days number + values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value); keyPreference = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_day_forecast_key); connectionPref = this.findPreference(keyPreference); value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, ""); humanValue = ""; - if (value.equals("5")) { - humanValue = "5-Day Forecast"; - } else if (value.equals("10")) { - humanValue = "10-Day Forecast"; - } else if (value.equals("14")) { - humanValue = "14-Day Forecast"; + 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); + + // Refresh interval + values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value); + keyPreference = this.getActivity().getApplicationContext().getString( + R.string.weather_preferences_refresh_interval_key); + connectionPref = this.findPreference(keyPreference); + value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, ""); + 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]; + } else if (value.equals(values[3])) { + humanValue = humanValues[3]; + } else if (value.equals(values[4])) { + humanValue = humanValues[4]; + } else if (value.equals(values[5])) { + humanValue = humanValues[5]; + } else if (value.equals(values[6])) { + humanValue = humanValues[6]; } connectionPref.setSummary(humanValue); } @@ -99,24 +126,24 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment } // Update Time Rate + String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate); + String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value); keyValue = this.getActivity().getApplicationContext().getString( R.string.weather_preferences_update_time_rate_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); final String value = sharedPreferences.getString(key, ""); String humanValue = ""; - if (value.equals("0")) { - humanValue = "no updates"; - } else if (value.equals("900")) { - humanValue = "fifteen minutes"; - } else if (value.equals("1800")) { - humanValue = "half hour"; - } else if (value.equals("3600")) { - humanValue = "one hour"; - } else if (value.equals("43200")) { - humanValue = "half day"; - } else if (value.equals("86400")) { - humanValue = "one day"; + 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]; + } else if (value.equals(values[3])) { + humanValue = humanValues[3]; + } else if (value.equals(values[4])) { + humanValue = humanValues[4]; } this.updateAlarm(value); @@ -125,36 +152,66 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment } // Forecast days number + values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value); keyValue = this.getActivity().getString( R.string.weather_preferences_day_forecast_key); if (key.equals(keyValue)) { final Preference connectionPref = this.findPreference(key); final String value = sharedPreferences.getString(key, ""); String humanValue = ""; - if (value.equals("5")) { - humanValue = "5-Day Forecast"; - } else if (value.equals("10")) { - humanValue = "10-Day Forecast"; - } else if (value.equals("14")) { - humanValue = "14-Day Forecast"; + 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; } + // Refresh interval + values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval); + humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value); + keyValue = this.getActivity().getApplicationContext().getString( + R.string.weather_preferences_refresh_interval_key); + if (key.equals(keyValue)) { + final Preference connectionPref = this.findPreference(key); + final String value = sharedPreferences.getString(key, ""); + 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]; + } else if (value.equals(values[3])) { + humanValue = humanValues[3]; + } else if (value.equals(values[4])) { + humanValue = humanValues[4]; + } else if (value.equals(values[5])) { + humanValue = humanValues[5]; + } else if (value.equals(values[6])) { + humanValue = humanValues[6]; + } + connectionPref.setSummary(humanValue); + return; + } } private void updateAlarm(final String updateTimeRate) { + final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate); long chosenInterval = 0; - if (updateTimeRate.equals("900")) { + if (updateTimeRate.equals(values[0])) { chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES; - } else if (updateTimeRate.equals("1800")) { + } else if (updateTimeRate.equals(values[1])) { chosenInterval = AlarmManager.INTERVAL_HALF_HOUR; - } else if (updateTimeRate.equals("3600")) { + } else if (updateTimeRate.equals(values[2])) { chosenInterval = AlarmManager.INTERVAL_HOUR; - } else if (updateTimeRate.equals("43200")) { + } else if (updateTimeRate.equals(values[3])) { chosenInterval = AlarmManager.INTERVAL_HALF_DAY; - } else if (updateTimeRate.equals("86400")) { + } else if (updateTimeRate.equals(values[4])) { chosenInterval = AlarmManager.INTERVAL_DAY; } -- 2.1.4