b207fc6a5058835080700123d3bcfd97ad48598f
[JavaForFun] /
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, this.getString(R.string.weather_preferences_temperature_celsius));
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()
52                 .getString(keyPreference, this.getString(R.string.weather_preferences_wind_meters));
53         humanValue = "";
54         if (value.equals(values[0])) {
55             humanValue = humanValues[0];
56         } else if (value.equals(values[1])) {
57             humanValue = humanValues[1];
58         }
59         connectionPref.setSummary(humanValue);
60
61         // Pressure
62         values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
63         humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
64         keyPreference = this.getString(R.string.weather_preferences_pressure_key);
65         connectionPref = this.findPreference(keyPreference);
66         value = this.getPreferenceManager().getSharedPreferences()
67                 .getString(keyPreference, this.getString(R.string.weather_preferences_pressure_pascal));
68         humanValue = "";
69         if (value.equals(values[0])) {
70             humanValue = humanValues[0];
71         } else if (value.equals(values[1])) {
72             humanValue = humanValues[1];
73         }
74         connectionPref.setSummary(humanValue);
75
76         // Forecast days number
77         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
78         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
79         keyPreference = this.getActivity().getApplicationContext().getString(
80                 R.string.weather_preferences_day_forecast_key);
81         connectionPref = this.findPreference(keyPreference);
82         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]);
83         humanValue = "";
84         if (value.equals(values[0])) {
85             humanValue = humanValues[0];
86         } else if (value.equals(values[1])) {
87             humanValue = humanValues[1];
88         } else if (value.equals(values[2])) {
89             humanValue = humanValues[2];
90         }
91         connectionPref.setSummary(humanValue);
92
93         // Refresh interval
94         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
95         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
96         keyPreference = this.getActivity().getApplicationContext().getString(
97                 R.string.weather_preferences_refresh_interval_key);
98         connectionPref = this.findPreference(keyPreference);
99         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]);
100         humanValue = "";
101         if (value.equals(values[0])) {
102             humanValue = humanValues[0];
103         } else if (value.equals(values[1])) {
104             humanValue = humanValues[1];
105         } else if (value.equals(values[2])) {
106             humanValue = humanValues[2];
107         } else if (value.equals(values[3])) {
108             humanValue = humanValues[3];
109         } else if (value.equals(values[4])) {
110             humanValue = humanValues[4];
111         } else if (value.equals(values[5])) {
112             humanValue = humanValues[5];
113         } else if (value.equals(values[6])) {
114             humanValue = humanValues[6];
115         }
116         connectionPref.setSummary(humanValue);
117
118         // Update Time Rate
119         values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
120         humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
121         keyPreference = this.getActivity().getApplicationContext().getString(
122                 R.string.weather_preferences_update_time_rate_key);
123         connectionPref = this.findPreference(keyPreference);
124         value = this.getPreferenceManager().getSharedPreferences()
125                 .getString(keyPreference, values[0]);
126         humanValue = "";
127         if (value.equals(values[0])) {
128             humanValue = humanValues[0];
129         } else if (value.equals(values[1])) {
130             humanValue = humanValues[1];
131         } else if (value.equals(values[2])) {
132             humanValue = humanValues[2];
133         } else if (value.equals(values[3])) {
134             humanValue = humanValues[3];
135         } else if (value.equals(values[4])) {
136             humanValue = humanValues[4];
137         }
138         connectionPref.setSummary(humanValue);
139
140         // Notifications temperature units
141         values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
142         humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
143         keyPreference = this.getActivity().getApplicationContext().getString(
144                 R.string.weather_preferences_notifications_temperature_key);
145         connectionPref = this.findPreference(keyPreference);
146         value = this.getPreferenceManager().getSharedPreferences()
147                 .getString(keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
148         humanValue = "";
149         if (value.equals(values[0])) {
150             humanValue = humanValues[0];
151         } else if (value.equals(values[1])) {
152             humanValue = humanValues[1];
153         } else if (value.equals(values[2])) {
154             humanValue = humanValues[2];
155         }
156         connectionPref.setSummary(humanValue);
157     }
158
159     @Override
160     public void onResume() {
161         super.onResume();
162         this.getPreferenceManager().getSharedPreferences()
163         .registerOnSharedPreferenceChangeListener(this);
164
165     }
166
167     @Override
168     public void onPause() {
169         super.onPause();
170         this.getPreferenceManager().getSharedPreferences()
171         .unregisterOnSharedPreferenceChangeListener(this);
172     }
173
174     @Override
175     public void onSharedPreferenceChanged(
176             final SharedPreferences sharedPreferences, final String key) {
177         
178         // Temperature units
179         String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
180         String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
181         String keyValue = this.getActivity().getApplicationContext().getString(
182                 R.string.weather_preferences_temperature_key);
183         if (key.equals(keyValue)) {
184                 final Preference connectionPref = this.findPreference(key);
185             final String value = sharedPreferences.getString(key, values[0]);
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                 } else if (value.equals(values[2])) {
192                         humanValue = humanValues[2];
193                 }
194
195                 connectionPref.setSummary(humanValue);
196                 return;
197         }
198
199         // Wind
200         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
201         humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
202         keyValue = this.getString(R.string.weather_preferences_wind_key);
203         if (key.equals(keyValue)) {
204             final Preference connectionPref = this.findPreference(key);
205             final String value = sharedPreferences.getString(key, values[0]);
206             String humanValue = "";
207             if (value.equals(values[0])) {
208                 humanValue = humanValues[0];
209             } else if (value.equals(values[1])) {
210                 humanValue = humanValues[1];
211             }
212         
213                 connectionPref.setSummary(humanValue);
214                 return;
215         }
216
217         // Pressure
218         values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
219         humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
220         keyValue = this.getString(R.string.weather_preferences_pressure_key);
221         if (key.equals(keyValue)) {
222             final Preference connectionPref = this.findPreference(key);
223             final String value = sharedPreferences.getString(key, values[0]);
224             String humanValue = "";
225             if (value.equals(values[0])) {
226                 humanValue = humanValues[0];
227             } else if (value.equals(values[1])) {
228                 humanValue = humanValues[1];
229             }
230         
231                 connectionPref.setSummary(humanValue);
232                 return;
233         }
234
235         // Forecast days number
236         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
237         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
238         keyValue = this.getActivity().getString(
239                 R.string.weather_preferences_day_forecast_key);
240         if (key.equals(keyValue)) {
241             final Preference connectionPref = this.findPreference(key);
242             final String value = sharedPreferences.getString(key, values[0]);
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             }
251             connectionPref.setSummary(humanValue);
252             return;
253         }
254
255         // Refresh interval
256         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
257         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
258         keyValue = this.getActivity().getApplicationContext().getString(
259                 R.string.weather_preferences_refresh_interval_key);
260         if (key.equals(keyValue)) {
261                 final Preference connectionPref = this.findPreference(key);
262             final String value = sharedPreferences.getString(key, values[0]);
263             String humanValue = "";
264             if (value.equals(values[0])) {
265                 humanValue = humanValues[0];
266             } else if (value.equals(values[1])) {
267                 humanValue = humanValues[1];
268             } else if (value.equals(values[2])) {
269                 humanValue = humanValues[2];
270             } else if (value.equals(values[3])) {
271                 humanValue = humanValues[3];
272             } else if (value.equals(values[4])) {
273                 humanValue = humanValues[4];
274             } else if (value.equals(values[5])) {
275                 humanValue = humanValues[5];
276             } else if (value.equals(values[6])) {
277                 humanValue = humanValues[6];
278             }
279             connectionPref.setSummary(humanValue);
280             return;
281         }
282
283         // Notification switch
284         values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
285         keyValue = this.getActivity().getApplicationContext().getString(
286                         R.string.weather_preferences_notifications_switch_key);
287         if (key.equals(keyValue)) {
288                 final SwitchPreference preference = (SwitchPreference)this.findPreference(key);
289                 if (preference.isChecked())
290                 {
291                         keyValue = this.getActivity().getApplicationContext().getString(
292                                 R.string.weather_preferences_update_time_rate_key);
293                         final String value = sharedPreferences.getString(keyValue, values[0]);
294                         this.updateNotification(value);
295                 } else {
296                         this.cancelNotification();
297                 }
298         }
299         // Update Time Rate
300         values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
301         humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
302         keyValue = this.getActivity().getApplicationContext().getString(
303                         R.string.weather_preferences_update_time_rate_key);
304         if (key.equals(keyValue)) {
305             final Preference connectionPref = this.findPreference(key);
306             final String value = sharedPreferences.getString(key, values[0]);
307             String humanValue = "";
308             if (value.equals(values[0])) {
309                 humanValue = humanValues[0];
310             } else if (value.equals(values[1])) {
311                 humanValue = humanValues[1];
312             } else if (value.equals(values[2])) {
313                 humanValue = humanValues[2];
314             } else if (value.equals(values[3])) {
315                 humanValue = humanValues[3];
316             } else if (value.equals(values[4])) {
317                 humanValue = humanValues[4];
318             }
319
320             this.updateNotification(value);
321             connectionPref.setSummary(humanValue);
322             return;
323         }
324
325         // Temperature units
326         values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
327         humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
328         keyValue = this.getActivity().getApplicationContext().getString(
329                 R.string.weather_preferences_notifications_temperature_key);
330         if (key.equals(keyValue)) {
331             final Preference connectionPref = this.findPreference(key);
332             final String value = sharedPreferences.getString(key, values[0]);
333             String humanValue = "";
334             if (value.equals(values[0])) {
335                 humanValue = humanValues[0];
336             } else if (value.equals(values[1])) {
337                 humanValue = humanValues[1];
338             } else if (value.equals(values[2])) {
339                 humanValue = humanValues[2];
340             }
341
342             connectionPref.setSummary(humanValue);
343             return;
344         }
345     }
346
347     private void updateNotification(final String updateTimeRate) {
348         final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
349         long chosenInterval = 0;
350         if (updateTimeRate.equals(values[0])) {
351                 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
352         } else if (updateTimeRate.equals(values[1])) {
353                 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
354         } else if (updateTimeRate.equals(values[2])) {
355                 chosenInterval = AlarmManager.INTERVAL_HOUR;
356         } else if (updateTimeRate.equals(values[3])) {
357                 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
358         } else if (updateTimeRate.equals(values[4])) {
359                 chosenInterval = AlarmManager.INTERVAL_DAY;
360         }
361
362         final AlarmManager alarmMgr =
363                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
364         // TODO: better use some string instead of .class? In case I change the service class
365         // this could be a problem (I guess)
366         final Intent serviceIntent =
367                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
368         final PendingIntent alarmIntent =
369                         PendingIntent.getService(
370                                         this.getActivity().getApplicationContext(),
371                                         0,
372                                         serviceIntent,
373                                         PendingIntent.FLAG_UPDATE_CURRENT);
374         if (chosenInterval != 0) {   
375             alarmMgr.setInexactRepeating(
376                         AlarmManager.ELAPSED_REALTIME,
377                         SystemClock.elapsedRealtime(),
378                         chosenInterval,
379                         alarmIntent);
380         }
381     }
382
383     private void cancelNotification() {
384         final AlarmManager alarmMgr =
385                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
386         final Intent serviceIntent =
387                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
388         final PendingIntent alarmIntent =
389                         PendingIntent.getService(
390                                         this.getActivity().getApplicationContext(),
391                                         0,
392                                         serviceIntent,
393                                         PendingIntent.FLAG_UPDATE_CURRENT);
394         alarmMgr.cancel(alarmIntent);
395     }
396 }