WeatherInformation: improvements in settings activities
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 21 Oct 2014 01:18:10 +0000 (03:18 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 21 Oct 2014 01:18:10 +0000 (03:18 +0200)
Android/WeatherInformation/app/src/main/java/de/example/exampletdd/NotificationIntentService.java
Android/WeatherInformation/app/src/main/java/de/example/exampletdd/WidgetIntentService.java
Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java
Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/overview/OverviewFragment.java
Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java
Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/specific/SpecificFragment.java
Android/WeatherInformation/app/src/main/res/values/strings.xml
Android/WeatherInformation/app/src/main/res/xml/weather_preferences.xml

index 596b43f..6f4f311 100644 (file)
@@ -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];
index d0af228..32228fe 100644 (file)
@@ -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);
         }
 
index 13f9a77..d3a3d1e 100644 (file)
@@ -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(){
index 682e5bd..139afe9 100644 (file)
@@ -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(){
index ba19c5e..b207fc6 100644 (file)
@@ -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) {
index e355c71..4912fe2 100644 (file)
@@ -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(){
index dea5420..ec32311 100644 (file)
@@ -39,6 +39,8 @@
     <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_notifications_temperature_key">weather_preferences_notifications_temperature</string>
+    <string name="weather_preferences_notifications_temperature">Temperature</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>
@@ -57,6 +59,7 @@
     <string name="weather_preferences_pressure_human_value_standard_atm">standard atmosphere</string>
 
 
+
     <!-- Widget Preferences Activity -->
     <string name="widget_preferences_action_settings">Settings</string>
     <string name="widget_preferences_button_refresh">Refresh</string>
index baaa006..7034716 100644 (file)
                android:selectable="true"
                android:summary="fifteen minutes"
                android:dependency="@string/weather_preferences_notifications_switch_key"/>
+        <ListPreference android:key="@string/weather_preferences_notifications_temperature_key"
+            android:title="@string/weather_preferences_notifications_temperature"
+            android:summary="@string/weather_preferences_temperature_celsius_human_value"
+            android:entries="@array/weather_preferences_temperature_human_value"
+            android:entryValues="@array/weather_preferences_temperature"
+            android:selectable="true"
+            android:persistent="true"
+            android:defaultValue="@string/weather_preferences_temperature_celsius"
+            android:dependency="@string/weather_preferences_notifications_switch_key"/>
     </PreferenceCategory>
 </PreferenceScreen>