ed0c662845cc1f49480dd4f09ff2a484614dd5f6
[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_units_value);
30         String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_units_human_value);
31         String keyPreference = this.getActivity().getApplicationContext().getString(
32                 R.string.weather_preferences_units_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         // Forecast days number
61         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
62         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
63         keyPreference = this.getActivity().getApplicationContext().getString(
64                 R.string.weather_preferences_day_forecast_key);
65         connectionPref = this.findPreference(keyPreference);
66         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
67         humanValue = "";
68         if (value.equals(values[0])) {
69             humanValue = humanValues[0];
70         } else if (value.equals(values[1])) {
71             humanValue = humanValues[1];
72         } else if (value.equals(values[2])) {
73             humanValue = humanValues[2];
74         }
75         connectionPref.setSummary(humanValue);
76
77         // Refresh interval
78         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
79         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
80         keyPreference = this.getActivity().getApplicationContext().getString(
81                 R.string.weather_preferences_refresh_interval_key);
82         connectionPref = this.findPreference(keyPreference);
83         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
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         } else if (value.equals(values[3])) {
92             humanValue = humanValues[3];
93         } else if (value.equals(values[4])) {
94             humanValue = humanValues[4];
95         } else if (value.equals(values[5])) {
96             humanValue = humanValues[5];
97         } else if (value.equals(values[6])) {
98             humanValue = humanValues[6];
99         }
100         connectionPref.setSummary(humanValue);
101
102         // Update Time Rate
103         values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
104         humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
105         keyPreference = this.getActivity().getApplicationContext().getString(
106                 R.string.weather_preferences_update_time_rate_key);
107         connectionPref = this.findPreference(keyPreference);
108         value = this.getPreferenceManager().getSharedPreferences()
109                 .getString(keyPreference, "");
110         humanValue = "";
111         if (value.equals(values[0])) {
112             humanValue = humanValues[0];
113         } else if (value.equals(values[1])) {
114             humanValue = humanValues[1];
115         } else if (value.equals(values[2])) {
116             humanValue = humanValues[2];
117         } else if (value.equals(values[3])) {
118             humanValue = humanValues[3];
119         } else if (value.equals(values[4])) {
120             humanValue = humanValues[4];
121         }
122         connectionPref.setSummary(humanValue);
123     }
124
125     @Override
126     public void onResume() {
127         super.onResume();
128         this.getPreferenceManager().getSharedPreferences()
129         .registerOnSharedPreferenceChangeListener(this);
130
131     }
132
133     @Override
134     public void onPause() {
135         super.onPause();
136         this.getPreferenceManager().getSharedPreferences()
137         .unregisterOnSharedPreferenceChangeListener(this);
138     }
139
140     @Override
141     public void onSharedPreferenceChanged(
142             final SharedPreferences sharedPreferences, final String key) {
143         
144         // Temperature units
145         String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value);
146         String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_units_human_value);
147         String keyValue = this.getActivity().getApplicationContext().getString(
148                 R.string.weather_preferences_units_key);
149         if (key.equals(keyValue)) {
150                 final Preference connectionPref = this.findPreference(key);
151             final String value = sharedPreferences.getString(key, "");
152                 String humanValue = "";
153                 if (value.equals(values[0])) {
154                         humanValue = humanValues[0];
155                 } else if (value.equals(values[1])) {
156                         humanValue = humanValues[1];
157                 } else if (value.equals(values[2])) {
158                         humanValue = humanValues[2];
159                 }
160
161                 connectionPref.setSummary(humanValue);
162                 return;
163         }
164
165         // Wind
166         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
167         humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
168         keyValue = this.getString(R.string.weather_preferences_wind_key);
169         if (key.equals(keyValue)) {
170             final Preference connectionPref = this.findPreference(key);
171             final String value = sharedPreferences.getString(key, "");
172             String humanValue = "";
173             if (value.equals(values[0])) {
174                 humanValue = humanValues[0];
175             } else if (value.equals(values[1])) {
176                 humanValue = humanValues[1];
177             }
178         
179                 connectionPref.setSummary(humanValue);
180                 return;
181         }
182         
183         // Forecast days number
184         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
185         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
186         keyValue = this.getActivity().getString(
187                 R.string.weather_preferences_day_forecast_key);
188         if (key.equals(keyValue)) {
189             final Preference connectionPref = this.findPreference(key);
190             final String value = sharedPreferences.getString(key, "");
191             String humanValue = "";
192             if (value.equals(values[0])) {
193                 humanValue = humanValues[0];
194             } else if (value.equals(values[1])) {
195                 humanValue = humanValues[1];
196             } else if (value.equals(values[2])) {
197                 humanValue = humanValues[2];
198             }
199             connectionPref.setSummary(humanValue);
200             return;
201         }
202
203         // Refresh interval
204         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
205         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
206         keyValue = this.getActivity().getApplicationContext().getString(
207                 R.string.weather_preferences_refresh_interval_key);
208         if (key.equals(keyValue)) {
209                 final Preference connectionPref = this.findPreference(key);
210             final String value = sharedPreferences.getString(key, "");
211             String humanValue = "";
212             if (value.equals(values[0])) {
213                 humanValue = humanValues[0];
214             } else if (value.equals(values[1])) {
215                 humanValue = humanValues[1];
216             } else if (value.equals(values[2])) {
217                 humanValue = humanValues[2];
218             } else if (value.equals(values[3])) {
219                 humanValue = humanValues[3];
220             } else if (value.equals(values[4])) {
221                 humanValue = humanValues[4];
222             } else if (value.equals(values[5])) {
223                 humanValue = humanValues[5];
224             } else if (value.equals(values[6])) {
225                 humanValue = humanValues[6];
226             }
227             connectionPref.setSummary(humanValue);
228             return;
229         }
230
231         // Notification switch
232         keyValue = this.getActivity().getApplicationContext().getString(
233                         R.string.weather_preferences_notifications_switch_key);
234         if (key.equals(keyValue)) {
235                 final SwitchPreference preference = (SwitchPreference)this.findPreference(key);
236                 if (preference.isChecked())
237                 {
238                         keyValue = this.getActivity().getApplicationContext().getString(
239                                 R.string.weather_preferences_update_time_rate_key);
240                         final String value = sharedPreferences.getString(keyValue, "");
241                         this.updateNotification(value);
242                 } else {
243                         this.cancelNotification();
244                 }
245         }
246         // Update Time Rate
247         values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
248         humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
249         keyValue = this.getActivity().getApplicationContext().getString(
250                         R.string.weather_preferences_update_time_rate_key);
251         if (key.equals(keyValue)) {
252             final Preference connectionPref = this.findPreference(key);
253             final String value = sharedPreferences.getString(key, "");
254             String humanValue = "";
255             if (value.equals(values[0])) {
256                 humanValue = humanValues[0];
257             } else if (value.equals(values[1])) {
258                 humanValue = humanValues[1];
259             } else if (value.equals(values[2])) {
260                 humanValue = humanValues[2];
261             } else if (value.equals(values[3])) {
262                 humanValue = humanValues[3];
263             } else if (value.equals(values[4])) {
264                 humanValue = humanValues[4];
265             }
266
267             this.updateNotification(value);
268             connectionPref.setSummary(humanValue);
269             return;
270         }
271     }
272
273     private void updateNotification(final String updateTimeRate) {
274         final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
275         long chosenInterval = 0;
276         if (updateTimeRate.equals(values[0])) {
277                 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
278         } else if (updateTimeRate.equals(values[1])) {
279                 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
280         } else if (updateTimeRate.equals(values[2])) {
281                 chosenInterval = AlarmManager.INTERVAL_HOUR;
282         } else if (updateTimeRate.equals(values[3])) {
283                 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
284         } else if (updateTimeRate.equals(values[4])) {
285                 chosenInterval = AlarmManager.INTERVAL_DAY;
286         }
287
288         final AlarmManager alarmMgr =
289                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
290         // TODO: better use some string instead of .class? In case I change the service class
291         // this could be a problem (I guess)
292         final Intent serviceIntent =
293                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
294         final PendingIntent alarmIntent =
295                         PendingIntent.getService(
296                                         this.getActivity().getApplicationContext(),
297                                         0,
298                                         serviceIntent,
299                                         PendingIntent.FLAG_UPDATE_CURRENT);
300         if (chosenInterval != 0) {   
301             alarmMgr.setInexactRepeating(
302                         AlarmManager.ELAPSED_REALTIME,
303                         SystemClock.elapsedRealtime(),
304                         chosenInterval,
305                         alarmIntent);
306         }
307     }
308
309     private void cancelNotification() {
310         final AlarmManager alarmMgr =
311                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
312         final Intent serviceIntent =
313                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
314         final PendingIntent alarmIntent =
315                         PendingIntent.getService(
316                                         this.getActivity().getApplicationContext(),
317                                         0,
318                                         serviceIntent,
319                                         PendingIntent.FLAG_UPDATE_CURRENT);
320         alarmMgr.cancel(alarmIntent);
321     }
322 }