1 package name.gumartinm.weather.information.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;
15 import name.gumartinm.weather.information.R;
16 import name.gumartinm.weather.information.notification.NotificationIntentService;
18 public class WeatherInformationPreferencesFragment extends PreferenceFragment
19 implements OnSharedPreferenceChangeListener {
22 public void onCreate(final Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
25 // Load the preferences from an XML resource
26 this.addPreferencesFromResource(R.xml.weather_preferences);
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];
45 connectionPref.setSummary(humanValue);
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));
55 if (value.equals(values[0])) {
56 humanValue = humanValues[0];
57 } else if (value.equals(values[1])) {
58 humanValue = humanValues[1];
60 connectionPref.setSummary(humanValue);
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));
70 if (value.equals(values[0])) {
71 humanValue = humanValues[0];
72 } else if (value.equals(values[1])) {
73 humanValue = humanValues[1];
75 connectionPref.setSummary(humanValue);
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]);
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];
92 connectionPref.setSummary(humanValue);
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]);
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];
117 connectionPref.setSummary(humanValue);
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]);
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];
139 connectionPref.setSummary(humanValue);
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));
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];
157 connectionPref.setSummary(humanValue);
161 public void onResume() {
163 this.getPreferenceManager().getSharedPreferences()
164 .registerOnSharedPreferenceChangeListener(this);
169 public void onPause() {
171 this.getPreferenceManager().getSharedPreferences()
172 .unregisterOnSharedPreferenceChangeListener(this);
176 public void onSharedPreferenceChanged(
177 final SharedPreferences sharedPreferences, final String key) {
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];
196 connectionPref.setSummary(humanValue);
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];
214 connectionPref.setSummary(humanValue);
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];
232 connectionPref.setSummary(humanValue);
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];
252 connectionPref.setSummary(humanValue);
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];
280 connectionPref.setSummary(humanValue);
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())
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);
297 this.cancelNotification();
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];
321 this.updateNotification(value);
322 connectionPref.setSummary(humanValue);
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];
343 connectionPref.setSummary(humanValue);
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;
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(),
374 PendingIntent.FLAG_UPDATE_CURRENT);
375 if (chosenInterval != 0) {
376 alarmMgr.setInexactRepeating(
377 AlarmManager.ELAPSED_REALTIME,
378 SystemClock.elapsedRealtime(),
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(),
394 PendingIntent.FLAG_UPDATE_CURRENT);
395 alarmMgr.cancel(alarmIntent);