WeatherInformation: preferences activity WIP
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 7 Oct 2014 02:53:39 +0000 (04:53 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 7 Oct 2014 02:53:39 +0000 (04:53 +0200)
Android/WeatherInformation/res/values/arrays.xml
Android/WeatherInformation/res/values/strings.xml
Android/WeatherInformation/res/xml/weather_preferences.xml
Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java

index 5900023..ef2b6d3 100644 (file)
         <item>14</item>
     </string-array>
     <string-array name="weather_preferences_day_forecast_human_value">
-        <item>5-Day Forecast</item>
-        <item>10-Day Forecast</item>
-        <item>14-Day Forecast</item>
+        <item>5 day forecast</item>
+        <item>10 day forecast</item>
+        <item>14 day forecast</item>
     </string-array>
     <string-array name="weather_preferences_update_time_rate">
-        <item>0</item>
         <item>900</item>
         <item>1800</item>
         <item>3600</item>       
         <item>86400</item>    
     </string-array>
     <string-array name="weather_preferences_update_time_rate_human_value">
-        <item>no updates</item>
         <item>fifteen minutes</item>
         <item>half hour</item>
         <item>one hour</item>   
         <item>half day</item>
         <item>one day</item>     
     </string-array>
+        <string-array name="weather_preferences_refresh_interval">
+        <item>300000</item>
+        <item>900000</item>
+        <item>1800000</item>
+        <item>3600000</item>
+        <item>7200000</item>
+        <item>43200000</item>
+        <item>86400000</item>
+    </string-array>
+    <string-array name="weather_preferences_refresh_interval_human_value">
+        <item>five minutes</item>
+        <item>fifteen minutes</item>
+        <item>half hour</item>
+        <item>one hour</item>
+        <item>two hours</item>
+        <item>half day</item>
+        <item>one day</item>
+    </string-array>
 </resources>
index 210e158..e4153b8 100644 (file)
     <string name="text_field_humidity">HUMIDITY</string>
     <string name="button_ok">OK</string>
     <string name="icon_weather_description">Icon weather</string>
+    <string name="weather_preferences_actionbar_title">Settings</string>
+    <string name="weather_preferences_units">Units</string>
     <string name="weather_preferences_units_key">weather_preferences_units</string>
     <string name="weather_preferences_units_fahrenheit">Fahrenheit</string>
     <string name="weather_preferences_units_celsius">Celsius</string>
-    <string name="weather_preferences_units">Unit of measurement for temperature</string>
+    <string name="weather_preferences_temperature_units">Temperature</string>
+    <string name="weather_preferences_notifications">Notifications</string>
+    <string name="weather_preferences_notifications_switch_off_summary">Disabled</string>
+    <string name="weather_preferences_notifications_switch_on_summary">Enabled</string>
+    <string name="weather_preferences_notifications_switch_off">OFF</string>
+    <string name="weather_preferences_notifications_switch_on">ON</string>
+    <string name="weather_preferences_notifications_switch_key">weather_preferences_notifications_switch</string>
     <string name="weather_preferences_update_time_rate_key">weather_preferences_update_time_rate</string>
-    <string name="weather_preferences_update_time_rate">Update Time Rate</string>
+    <string name="weather_preferences_update_time_rate">Update time rate</string>
     <string name="weather_preferences_day_forecast_key">weather_preferences_day_forecast</string>
     <string name="weather_preferences_day_forecast">Forecast days number</string>
+    <string name="weather_preferences_refresh_interval_key">weather_preferences_refresh_interval</string>
+    <string name="weather_preferences_refresh_interval">Refresh interval</string>
     <string name="city_not_found">city not found</string>
     <string name="country_not_found">country not found</string>
     <string name="progress_dialog_generic_message">Please wait&#8230;</string>
index c3449ab..04751af 100644 (file)
@@ -1,31 +1,50 @@
 <?xml version="1.0" encoding="utf-8"?>
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
-    <ListPreference
-        android:key="@string/weather_preferences_units_key"
-        android:title="@string/weather_preferences_units"
-        android:summary="@string/weather_preferences_units_celsius"
-        android:entries="@array/weather_preferences_units_human_value"
-        android:entryValues="@array/weather_preferences_units_value"
-        android:selectable="true"
-        android:persistent="true"
-        android:defaultValue="Celsius" />
-    <ListPreference
-        android:entries="@array/weather_preferences_update_time_rate_human_value"
-        android:entryValues="@array/weather_preferences_update_time_rate"
-        android:key="@string/weather_preferences_update_time_rate_key"
-        android:title="@string/weather_preferences_update_time_rate"
-        android:defaultValue="0"
-        android:persistent="true"
-        android:selectable="true"
-        android:summary="no updates"/>
-    <ListPreference
-        android:key="@string/weather_preferences_day_forecast_key"
+    <ListPreference android:key="@string/weather_preferences_day_forecast_key"
         android:title="@string/weather_preferences_day_forecast"
         android:entries="@array/weather_preferences_day_forecast_human_value"
         android:entryValues="@array/weather_preferences_day_forecast"
         android:selectable="true"
         android:persistent="true"
         android:defaultValue="14"
-        android:summary="14-Day Forecast" />
-
+        android:summary="14 day forecast" />
+    <ListPreference android:key="@string/weather_preferences_refresh_interval_key"
+        android:title="@string/weather_preferences_refresh_interval"
+        android:entries="@array/weather_preferences_refresh_interval_human_value"
+        android:entryValues="@array/weather_preferences_refresh_interval"
+        android:selectable="true"
+        android:persistent="true"
+        android:defaultValue="300000"
+        android:summary="five minutes" />
+    <PreferenceCategory android:title="@string/weather_preferences_units">
+        <ListPreference android:key="@string/weather_preferences_units_key"
+            android:title="@string/weather_preferences_temperature_units"
+            android:summary="@string/weather_preferences_units_celsius"
+            android:entries="@array/weather_preferences_units_human_value"
+            android:entryValues="@array/weather_preferences_units_value"
+            android:selectable="true"
+            android:persistent="true"
+            android:defaultValue="Celsius" />
+    </PreferenceCategory>
+    <PreferenceCategory android:title="@string/weather_preferences_notifications">
+        <SwitchPreference android:key="@string/weather_preferences_notifications_switch_key"
+            android:summaryOn="@string/weather_preferences_notifications_switch_on_summary"
+            android:summaryOff="@string/weather_preferences_notifications_switch_off_summary"
+            android:switchTextOff="@string/weather_preferences_notifications_switch_off"
+            android:switchTextOn="@string/weather_preferences_notifications_switch_on"
+            android:selectable="false"
+            android:enabled="true"
+            android:defaultValue="On"
+            android:disableDependentsState="false"
+            android:persistent="true"/>
+        <ListPreference android:key="@string/weather_preferences_update_time_rate_key"
+               android:entries="@array/weather_preferences_update_time_rate_human_value"
+               android:entryValues="@array/weather_preferences_update_time_rate"
+               android:title="@string/weather_preferences_update_time_rate"
+               android:defaultValue="900"
+               android:persistent="true"
+               android:selectable="true"
+               android:summary="fifteen minutes"
+               android:dependency="@string/weather_preferences_notifications_switch_key"/>
+    </PreferenceCategory>
 </PreferenceScreen>
index e70cfc1..1ba899e 100644 (file)
@@ -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));
+    }
 }
index d3b60ae..4d21465 100644 (file)
@@ -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;
        }
        
index fd746e0..f413122 100644 (file)
@@ -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;
        }
        
index 2b665e9..1315c88 100644 (file)
@@ -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;
         }