WeatherInformation: preference cancel/start notification
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 8 Oct 2014 02:31:48 +0000 (04:31 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 8 Oct 2014 02:31:48 +0000 (04:31 +0200)
Android/WeatherInformation/res/xml/weather_preferences.xml
Android/WeatherInformation/src/de/example/exampletdd/fragment/preferences/WeatherInformationPreferencesFragment.java

index a2754f8..6bf3bb0 100644 (file)
@@ -40,9 +40,9 @@
             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:selectable="true"
             android:enabled="true"
-            android:defaultValue="On"
+            android:defaultValue="@string/weather_preferences_notifications_switch_off"
             android:disableDependentsState="false"
             android:persistent="true"/>
         <ListPreference android:key="@string/weather_preferences_update_time_rate_key"
index b730ea1..ed0c662 100644 (file)
@@ -10,6 +10,7 @@ import android.os.Bundle;
 import android.os.SystemClock;
 import android.preference.Preference;
 import android.preference.PreferenceFragment;
+import android.preference.SwitchPreference;
 import de.example.exampletdd.R;
 import de.example.exampletdd.NotificationIntentService;
 
@@ -42,28 +43,6 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment
         }
         connectionPref.setSummary(humanValue);
         
-        // Update Time Rate
-        values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
-        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);
-        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];
-        }
-        connectionPref.setSummary(humanValue);
-        
         // Wind
         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
         humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
@@ -119,6 +98,28 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment
             humanValue = humanValues[6];
         }
         connectionPref.setSummary(humanValue);
+
+        // Update Time Rate
+        values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
+        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);
+        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];
+        }
+        connectionPref.setSummary(humanValue);
     }
 
     @Override
@@ -161,32 +162,6 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment
                return;
         }
 
-        // Update Time Rate
-        values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
-        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(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);
-            connectionPref.setSummary(humanValue);
-            return;
-        }
-
         // Wind
         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
         humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
@@ -252,9 +227,50 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment
             connectionPref.setSummary(humanValue);
             return;
         }
+
+        // Notification switch
+        keyValue = this.getActivity().getApplicationContext().getString(
+                       R.string.weather_preferences_notifications_switch_key);
+        if (key.equals(keyValue)) {
+               final SwitchPreference preference = (SwitchPreference)this.findPreference(key);
+               if (preference.isChecked())
+               {
+                       keyValue = this.getActivity().getApplicationContext().getString(
+                               R.string.weather_preferences_update_time_rate_key);
+                       final String value = sharedPreferences.getString(keyValue, "");
+                       this.updateNotification(value);
+               } else {
+                       this.cancelNotification();
+               }
+        }
+        // Update Time Rate
+        values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
+        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(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.updateNotification(value);
+            connectionPref.setSummary(humanValue);
+            return;
+        }
     }
 
-    private void updateAlarm(final String updateTimeRate) {
+    private void updateNotification(final String updateTimeRate) {
        final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
         long chosenInterval = 0;
         if (updateTimeRate.equals(values[0])) {
@@ -287,8 +303,20 @@ public class WeatherInformationPreferencesFragment extends PreferenceFragment
                        SystemClock.elapsedRealtime(),
                        chosenInterval,
                        alarmIntent);
-        } else {
-               alarmMgr.cancel(alarmIntent);
         }
     }
+
+    private void cancelNotification() {
+       final AlarmManager alarmMgr =
+                       (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
+       final Intent serviceIntent =
+                       new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
+       final PendingIntent alarmIntent =
+                       PendingIntent.getService(
+                                       this.getActivity().getApplicationContext(),
+                                       0,
+                                       serviceIntent,
+                                       PendingIntent.FLAG_UPDATE_CURRENT);
+       alarmMgr.cancel(alarmIntent);
+    }
 }