1 package de.example.exampletdd.fragment.preferences;
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;
17 public class WeatherInformationPreferencesFragment extends PreferenceFragment
18 implements OnSharedPreferenceChangeListener {
21 public void onCreate(final Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
24 // Load the preferences from an XML resource
25 this.addPreferencesFromResource(R.xml.weather_preferences);
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];
44 connectionPref.setSummary(humanValue);
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));
54 if (value.equals(values[0])) {
55 humanValue = humanValues[0];
56 } else if (value.equals(values[1])) {
57 humanValue = humanValues[1];
59 connectionPref.setSummary(humanValue);
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));
69 if (value.equals(values[0])) {
70 humanValue = humanValues[0];
71 } else if (value.equals(values[1])) {
72 humanValue = humanValues[1];
74 connectionPref.setSummary(humanValue);
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]);
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];
91 connectionPref.setSummary(humanValue);
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]);
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];
116 connectionPref.setSummary(humanValue);
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]);
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];
138 connectionPref.setSummary(humanValue);
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));
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];
156 connectionPref.setSummary(humanValue);
160 public void onResume() {
162 this.getPreferenceManager().getSharedPreferences()
163 .registerOnSharedPreferenceChangeListener(this);
168 public void onPause() {
170 this.getPreferenceManager().getSharedPreferences()
171 .unregisterOnSharedPreferenceChangeListener(this);
175 public void onSharedPreferenceChanged(
176 final SharedPreferences sharedPreferences, final String key) {
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];
195 connectionPref.setSummary(humanValue);
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];
213 connectionPref.setSummary(humanValue);
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];
231 connectionPref.setSummary(humanValue);
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];
251 connectionPref.setSummary(humanValue);
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];
279 connectionPref.setSummary(humanValue);
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())
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);
296 this.cancelNotification();
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];
320 this.updateNotification(value);
321 connectionPref.setSummary(humanValue);
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];
342 connectionPref.setSummary(humanValue);
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;
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(),
373 PendingIntent.FLAG_UPDATE_CURRENT);
374 if (chosenInterval != 0) {
375 alarmMgr.setInexactRepeating(
376 AlarmManager.ELAPSED_REALTIME,
377 SystemClock.elapsedRealtime(),
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(),
393 PendingIntent.FLAG_UPDATE_CURRENT);
394 alarmMgr.cancel(alarmIntent);