ba19c5e00b35faca9b255a4121f09071db12c8d0
[AndroidWeatherInformation] /
1 package de.example.exampletdd.fragment.preferences;
2
3 import android.app.AlarmManager;
4 import android.app.PendingIntent;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.SharedPreferences;
8 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
9 import android.os.Bundle;
10 import android.os.SystemClock;
11 import android.preference.Preference;
12 import android.preference.PreferenceFragment;
13 import android.preference.SwitchPreference;
14 import de.example.exampletdd.R;
15 import de.example.exampletdd.NotificationIntentService;
16
17 public class WeatherInformationPreferencesFragment extends PreferenceFragment 
18                                                                                                         implements OnSharedPreferenceChangeListener {
19
20     @Override
21     public void onCreate(final Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23
24         // Load the preferences from an XML resource
25         this.addPreferencesFromResource(R.xml.weather_preferences);
26         
27         
28         // Temperature units
29         String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
30         String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
31         String keyPreference = this.getActivity().getApplicationContext().getString(
32                 R.string.weather_preferences_temperature_key);
33         Preference connectionPref = this.findPreference(keyPreference);
34         String value = this.getPreferenceManager().getSharedPreferences()
35                 .getString(keyPreference, "");
36         String humanValue = "";
37         if (value.equals(values[0])) {
38             humanValue = humanValues[0];
39         } else if (value.equals(values[1])) {
40             humanValue = humanValues[1];
41         } else if (value.equals(values[2])) {
42             humanValue = humanValues[2];
43         }
44         connectionPref.setSummary(humanValue);
45         
46         // Wind
47         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
48         humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
49         keyPreference = this.getString(R.string.weather_preferences_wind_key);
50         connectionPref = this.findPreference(keyPreference);
51         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
52         humanValue = "";
53         if (value.equals(values[0])) {
54             humanValue = humanValues[0];
55         } else if (value.equals(values[1])) {
56             humanValue = humanValues[1];
57         }
58         connectionPref.setSummary(humanValue);
59
60         // Pressure
61         values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
62         humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
63         keyPreference = this.getString(R.string.weather_preferences_pressure_key);
64         connectionPref = this.findPreference(keyPreference);
65         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
66         humanValue = "";
67         if (value.equals(values[0])) {
68             humanValue = humanValues[0];
69         } else if (value.equals(values[1])) {
70             humanValue = humanValues[1];
71         }
72         connectionPref.setSummary(humanValue);
73
74         // Forecast days number
75         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
76         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
77         keyPreference = this.getActivity().getApplicationContext().getString(
78                 R.string.weather_preferences_day_forecast_key);
79         connectionPref = this.findPreference(keyPreference);
80         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
81         humanValue = "";
82         if (value.equals(values[0])) {
83             humanValue = humanValues[0];
84         } else if (value.equals(values[1])) {
85             humanValue = humanValues[1];
86         } else if (value.equals(values[2])) {
87             humanValue = humanValues[2];
88         }
89         connectionPref.setSummary(humanValue);
90
91         // Refresh interval
92         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
93         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
94         keyPreference = this.getActivity().getApplicationContext().getString(
95                 R.string.weather_preferences_refresh_interval_key);
96         connectionPref = this.findPreference(keyPreference);
97         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
98         humanValue = "";
99         if (value.equals(values[0])) {
100             humanValue = humanValues[0];
101         } else if (value.equals(values[1])) {
102             humanValue = humanValues[1];
103         } else if (value.equals(values[2])) {
104             humanValue = humanValues[2];
105         } else if (value.equals(values[3])) {
106             humanValue = humanValues[3];
107         } else if (value.equals(values[4])) {
108             humanValue = humanValues[4];
109         } else if (value.equals(values[5])) {
110             humanValue = humanValues[5];
111         } else if (value.equals(values[6])) {
112             humanValue = humanValues[6];
113         }
114         connectionPref.setSummary(humanValue);
115
116         // Update Time Rate
117         values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
118         humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
119         keyPreference = this.getActivity().getApplicationContext().getString(
120                 R.string.weather_preferences_update_time_rate_key);
121         connectionPref = this.findPreference(keyPreference);
122         value = this.getPreferenceManager().getSharedPreferences()
123                 .getString(keyPreference, "");
124         humanValue = "";
125         if (value.equals(values[0])) {
126             humanValue = humanValues[0];
127         } else if (value.equals(values[1])) {
128             humanValue = humanValues[1];
129         } else if (value.equals(values[2])) {
130             humanValue = humanValues[2];
131         } else if (value.equals(values[3])) {
132             humanValue = humanValues[3];
133         } else if (value.equals(values[4])) {
134             humanValue = humanValues[4];
135         }
136         connectionPref.setSummary(humanValue);
137     }
138
139     @Override
140     public void onResume() {
141         super.onResume();
142         this.getPreferenceManager().getSharedPreferences()
143         .registerOnSharedPreferenceChangeListener(this);
144
145     }
146
147     @Override
148     public void onPause() {
149         super.onPause();
150         this.getPreferenceManager().getSharedPreferences()
151         .unregisterOnSharedPreferenceChangeListener(this);
152     }
153
154     @Override
155     public void onSharedPreferenceChanged(
156             final SharedPreferences sharedPreferences, final String key) {
157         
158         // Temperature units
159         String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
160         String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
161         String keyValue = this.getActivity().getApplicationContext().getString(
162                 R.string.weather_preferences_temperature_key);
163         if (key.equals(keyValue)) {
164                 final Preference connectionPref = this.findPreference(key);
165             final String value = sharedPreferences.getString(key, "");
166                 String humanValue = "";
167                 if (value.equals(values[0])) {
168                         humanValue = humanValues[0];
169                 } else if (value.equals(values[1])) {
170                         humanValue = humanValues[1];
171                 } else if (value.equals(values[2])) {
172                         humanValue = humanValues[2];
173                 }
174
175                 connectionPref.setSummary(humanValue);
176                 return;
177         }
178
179         // Wind
180         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
181         humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
182         keyValue = this.getString(R.string.weather_preferences_wind_key);
183         if (key.equals(keyValue)) {
184             final Preference connectionPref = this.findPreference(key);
185             final String value = sharedPreferences.getString(key, "");
186             String humanValue = "";
187             if (value.equals(values[0])) {
188                 humanValue = humanValues[0];
189             } else if (value.equals(values[1])) {
190                 humanValue = humanValues[1];
191             }
192         
193                 connectionPref.setSummary(humanValue);
194                 return;
195         }
196
197         // Pressure
198         values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
199         humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
200         keyValue = this.getString(R.string.weather_preferences_pressure_key);
201         if (key.equals(keyValue)) {
202             final Preference connectionPref = this.findPreference(key);
203             final String value = sharedPreferences.getString(key, "");
204             String humanValue = "";
205             if (value.equals(values[0])) {
206                 humanValue = humanValues[0];
207             } else if (value.equals(values[1])) {
208                 humanValue = humanValues[1];
209             }
210         
211                 connectionPref.setSummary(humanValue);
212                 return;
213         }
214
215         // Forecast days number
216         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
217         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
218         keyValue = this.getActivity().getString(
219                 R.string.weather_preferences_day_forecast_key);
220         if (key.equals(keyValue)) {
221             final Preference connectionPref = this.findPreference(key);
222             final String value = sharedPreferences.getString(key, "");
223             String humanValue = "";
224             if (value.equals(values[0])) {
225                 humanValue = humanValues[0];
226             } else if (value.equals(values[1])) {
227                 humanValue = humanValues[1];
228             } else if (value.equals(values[2])) {
229                 humanValue = humanValues[2];
230             }
231             connectionPref.setSummary(humanValue);
232             return;
233         }
234
235         // Refresh interval
236         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
237         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
238         keyValue = this.getActivity().getApplicationContext().getString(
239                 R.string.weather_preferences_refresh_interval_key);
240         if (key.equals(keyValue)) {
241                 final Preference connectionPref = this.findPreference(key);
242             final String value = sharedPreferences.getString(key, "");
243             String humanValue = "";
244             if (value.equals(values[0])) {
245                 humanValue = humanValues[0];
246             } else if (value.equals(values[1])) {
247                 humanValue = humanValues[1];
248             } else if (value.equals(values[2])) {
249                 humanValue = humanValues[2];
250             } else if (value.equals(values[3])) {
251                 humanValue = humanValues[3];
252             } else if (value.equals(values[4])) {
253                 humanValue = humanValues[4];
254             } else if (value.equals(values[5])) {
255                 humanValue = humanValues[5];
256             } else if (value.equals(values[6])) {
257                 humanValue = humanValues[6];
258             }
259             connectionPref.setSummary(humanValue);
260             return;
261         }
262
263         // Notification switch
264         keyValue = this.getActivity().getApplicationContext().getString(
265                         R.string.weather_preferences_notifications_switch_key);
266         if (key.equals(keyValue)) {
267                 final SwitchPreference preference = (SwitchPreference)this.findPreference(key);
268                 if (preference.isChecked())
269                 {
270                         keyValue = this.getActivity().getApplicationContext().getString(
271                                 R.string.weather_preferences_update_time_rate_key);
272                         final String value = sharedPreferences.getString(keyValue, "");
273                         this.updateNotification(value);
274                 } else {
275                         this.cancelNotification();
276                 }
277         }
278         // Update Time Rate
279         values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
280         humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
281         keyValue = this.getActivity().getApplicationContext().getString(
282                         R.string.weather_preferences_update_time_rate_key);
283         if (key.equals(keyValue)) {
284             final Preference connectionPref = this.findPreference(key);
285             final String value = sharedPreferences.getString(key, "");
286             String humanValue = "";
287             if (value.equals(values[0])) {
288                 humanValue = humanValues[0];
289             } else if (value.equals(values[1])) {
290                 humanValue = humanValues[1];
291             } else if (value.equals(values[2])) {
292                 humanValue = humanValues[2];
293             } else if (value.equals(values[3])) {
294                 humanValue = humanValues[3];
295             } else if (value.equals(values[4])) {
296                 humanValue = humanValues[4];
297             }
298
299             this.updateNotification(value);
300             connectionPref.setSummary(humanValue);
301             return;
302         }
303     }
304
305     private void updateNotification(final String updateTimeRate) {
306         final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
307         long chosenInterval = 0;
308         if (updateTimeRate.equals(values[0])) {
309                 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
310         } else if (updateTimeRate.equals(values[1])) {
311                 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
312         } else if (updateTimeRate.equals(values[2])) {
313                 chosenInterval = AlarmManager.INTERVAL_HOUR;
314         } else if (updateTimeRate.equals(values[3])) {
315                 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
316         } else if (updateTimeRate.equals(values[4])) {
317                 chosenInterval = AlarmManager.INTERVAL_DAY;
318         }
319
320         final AlarmManager alarmMgr =
321                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
322         // TODO: better use some string instead of .class? In case I change the service class
323         // this could be a problem (I guess)
324         final Intent serviceIntent =
325                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
326         final PendingIntent alarmIntent =
327                         PendingIntent.getService(
328                                         this.getActivity().getApplicationContext(),
329                                         0,
330                                         serviceIntent,
331                                         PendingIntent.FLAG_UPDATE_CURRENT);
332         if (chosenInterval != 0) {   
333             alarmMgr.setInexactRepeating(
334                         AlarmManager.ELAPSED_REALTIME,
335                         SystemClock.elapsedRealtime(),
336                         chosenInterval,
337                         alarmIntent);
338         }
339     }
340
341     private void cancelNotification() {
342         final AlarmManager alarmMgr =
343                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
344         final Intent serviceIntent =
345                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
346         final PendingIntent alarmIntent =
347                         PendingIntent.getService(
348                                         this.getActivity().getApplicationContext(),
349                                         0,
350                                         serviceIntent,
351                                         PendingIntent.FLAG_UPDATE_CURRENT);
352         alarmMgr.cancel(alarmIntent);
353     }
354 }