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