2 * Copyright 2014 Gustavo Martin Morcuende
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package name.gumartinm.weather.information.fragment.preferences;
18 import android.app.AlarmManager;
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
24 import android.os.Bundle;
25 import android.os.SystemClock;
26 import android.preference.Preference;
27 import android.preference.PreferenceFragment;
28 import android.preference.SwitchPreference;
30 import name.gumartinm.weather.information.R;
31 import name.gumartinm.weather.information.notification.NotificationIntentService;
33 public class PreferencesFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
36 public void onCreate(final Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
39 // Load the preferences from an XML resource
40 this.addPreferencesFromResource(R.xml.weather_preferences);
44 String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
45 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
46 String keyPreference = this.getActivity().getApplicationContext().getString(
47 R.string.weather_preferences_temperature_key);
48 Preference connectionPref = this.findPreference(keyPreference);
49 String value = this.getPreferenceManager().getSharedPreferences()
50 .getString(keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
51 String humanValue = "";
52 if (value.equals(values[0])) {
53 humanValue = humanValues[0];
54 } else if (value.equals(values[1])) {
55 humanValue = humanValues[1];
56 } else if (value.equals(values[2])) {
57 humanValue = humanValues[2];
59 connectionPref.setSummary(humanValue);
62 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
63 humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
64 keyPreference = this.getString(R.string.weather_preferences_wind_key);
65 connectionPref = this.findPreference(keyPreference);
66 value = this.getPreferenceManager().getSharedPreferences()
67 .getString(keyPreference, this.getString(R.string.weather_preferences_wind_meters));
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);
77 values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
78 humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
79 keyPreference = this.getString(R.string.weather_preferences_pressure_key);
80 connectionPref = this.findPreference(keyPreference);
81 value = this.getPreferenceManager().getSharedPreferences()
82 .getString(keyPreference, this.getString(R.string.weather_preferences_pressure_pascal));
84 if (value.equals(values[0])) {
85 humanValue = humanValues[0];
86 } else if (value.equals(values[1])) {
87 humanValue = humanValues[1];
89 connectionPref.setSummary(humanValue);
91 // Forecast days number
92 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
93 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
94 keyPreference = this.getActivity().getApplicationContext().getString(
95 R.string.weather_preferences_day_forecast_key);
96 connectionPref = this.findPreference(keyPreference);
97 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]);
99 if (value.equals(values[0])) {
100 humanValue = humanValues[0];
101 } else if (value.equals(values[1])) {
102 humanValue = humanValues[1];
103 } else if (value.equals(values[2])) {
104 humanValue = humanValues[2];
106 connectionPref.setSummary(humanValue);
109 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
110 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
111 keyPreference = this.getActivity().getApplicationContext().getString(
112 R.string.weather_preferences_refresh_interval_key);
113 connectionPref = this.findPreference(keyPreference);
114 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]);
116 if (value.equals(values[0])) {
117 humanValue = humanValues[0];
118 } else if (value.equals(values[1])) {
119 humanValue = humanValues[1];
120 } else if (value.equals(values[2])) {
121 humanValue = humanValues[2];
122 } else if (value.equals(values[3])) {
123 humanValue = humanValues[3];
124 } else if (value.equals(values[4])) {
125 humanValue = humanValues[4];
126 } else if (value.equals(values[5])) {
127 humanValue = humanValues[5];
128 } else if (value.equals(values[6])) {
129 humanValue = humanValues[6];
131 connectionPref.setSummary(humanValue);
134 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
135 humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
136 keyPreference = this.getActivity().getApplicationContext().getString(
137 R.string.weather_preferences_update_time_rate_key);
138 connectionPref = this.findPreference(keyPreference);
139 value = this.getPreferenceManager().getSharedPreferences()
140 .getString(keyPreference, values[0]);
142 if (value.equals(values[0])) {
143 humanValue = humanValues[0];
144 } else if (value.equals(values[1])) {
145 humanValue = humanValues[1];
146 } else if (value.equals(values[2])) {
147 humanValue = humanValues[2];
148 } else if (value.equals(values[3])) {
149 humanValue = humanValues[3];
150 } else if (value.equals(values[4])) {
151 humanValue = humanValues[4];
153 connectionPref.setSummary(humanValue);
155 // Notifications temperature units
156 values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
157 humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
158 keyPreference = this.getActivity().getApplicationContext().getString(
159 R.string.weather_preferences_notifications_temperature_key);
160 connectionPref = this.findPreference(keyPreference);
161 value = this.getPreferenceManager().getSharedPreferences()
162 .getString(keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
164 if (value.equals(values[0])) {
165 humanValue = humanValues[0];
166 } else if (value.equals(values[1])) {
167 humanValue = humanValues[1];
168 } else if (value.equals(values[2])) {
169 humanValue = humanValues[2];
171 connectionPref.setSummary(humanValue);
175 public void onResume() {
177 this.getPreferenceManager().getSharedPreferences()
178 .registerOnSharedPreferenceChangeListener(this);
183 public void onPause() {
185 this.getPreferenceManager().getSharedPreferences()
186 .unregisterOnSharedPreferenceChangeListener(this);
190 public void onSharedPreferenceChanged(
191 final SharedPreferences sharedPreferences, final String key) {
194 String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
195 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
196 String keyValue = this.getActivity().getApplicationContext().getString(
197 R.string.weather_preferences_temperature_key);
198 if (key.equals(keyValue)) {
199 final Preference connectionPref = this.findPreference(key);
200 final String value = sharedPreferences.getString(key, values[0]);
201 String humanValue = "";
202 if (value.equals(values[0])) {
203 humanValue = humanValues[0];
204 } else if (value.equals(values[1])) {
205 humanValue = humanValues[1];
206 } else if (value.equals(values[2])) {
207 humanValue = humanValues[2];
210 connectionPref.setSummary(humanValue);
215 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
216 humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
217 keyValue = this.getString(R.string.weather_preferences_wind_key);
218 if (key.equals(keyValue)) {
219 final Preference connectionPref = this.findPreference(key);
220 final String value = sharedPreferences.getString(key, values[0]);
221 String humanValue = "";
222 if (value.equals(values[0])) {
223 humanValue = humanValues[0];
224 } else if (value.equals(values[1])) {
225 humanValue = humanValues[1];
228 connectionPref.setSummary(humanValue);
233 values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
234 humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
235 keyValue = this.getString(R.string.weather_preferences_pressure_key);
236 if (key.equals(keyValue)) {
237 final Preference connectionPref = this.findPreference(key);
238 final String value = sharedPreferences.getString(key, values[0]);
239 String humanValue = "";
240 if (value.equals(values[0])) {
241 humanValue = humanValues[0];
242 } else if (value.equals(values[1])) {
243 humanValue = humanValues[1];
246 connectionPref.setSummary(humanValue);
250 // Forecast days number
251 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
252 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
253 keyValue = this.getActivity().getString(
254 R.string.weather_preferences_day_forecast_key);
255 if (key.equals(keyValue)) {
256 final Preference connectionPref = this.findPreference(key);
257 final String value = sharedPreferences.getString(key, values[0]);
258 String humanValue = "";
259 if (value.equals(values[0])) {
260 humanValue = humanValues[0];
261 } else if (value.equals(values[1])) {
262 humanValue = humanValues[1];
263 } else if (value.equals(values[2])) {
264 humanValue = humanValues[2];
266 connectionPref.setSummary(humanValue);
271 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
272 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
273 keyValue = this.getActivity().getApplicationContext().getString(
274 R.string.weather_preferences_refresh_interval_key);
275 if (key.equals(keyValue)) {
276 final Preference connectionPref = this.findPreference(key);
277 final String value = sharedPreferences.getString(key, values[0]);
278 String humanValue = "";
279 if (value.equals(values[0])) {
280 humanValue = humanValues[0];
281 } else if (value.equals(values[1])) {
282 humanValue = humanValues[1];
283 } else if (value.equals(values[2])) {
284 humanValue = humanValues[2];
285 } else if (value.equals(values[3])) {
286 humanValue = humanValues[3];
287 } else if (value.equals(values[4])) {
288 humanValue = humanValues[4];
289 } else if (value.equals(values[5])) {
290 humanValue = humanValues[5];
291 } else if (value.equals(values[6])) {
292 humanValue = humanValues[6];
294 connectionPref.setSummary(humanValue);
298 // Notification switch
299 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
300 keyValue = this.getActivity().getApplicationContext().getString(
301 R.string.weather_preferences_notifications_switch_key);
302 if (key.equals(keyValue)) {
303 final SwitchPreference preference = (SwitchPreference)this.findPreference(key);
304 if (preference.isChecked())
306 keyValue = this.getActivity().getApplicationContext().getString(
307 R.string.weather_preferences_update_time_rate_key);
308 final String value = sharedPreferences.getString(keyValue, values[0]);
309 this.updateNotification(value);
311 this.cancelNotification();
315 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
316 humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
317 keyValue = this.getActivity().getApplicationContext().getString(
318 R.string.weather_preferences_update_time_rate_key);
319 if (key.equals(keyValue)) {
320 final Preference connectionPref = this.findPreference(key);
321 final String value = sharedPreferences.getString(key, values[0]);
322 String humanValue = "";
323 if (value.equals(values[0])) {
324 humanValue = humanValues[0];
325 } else if (value.equals(values[1])) {
326 humanValue = humanValues[1];
327 } else if (value.equals(values[2])) {
328 humanValue = humanValues[2];
329 } else if (value.equals(values[3])) {
330 humanValue = humanValues[3];
331 } else if (value.equals(values[4])) {
332 humanValue = humanValues[4];
335 this.updateNotification(value);
336 connectionPref.setSummary(humanValue);
341 values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
342 humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
343 keyValue = this.getActivity().getApplicationContext().getString(
344 R.string.weather_preferences_notifications_temperature_key);
345 if (key.equals(keyValue)) {
346 final Preference connectionPref = this.findPreference(key);
347 final String value = sharedPreferences.getString(key, values[0]);
348 String humanValue = "";
349 if (value.equals(values[0])) {
350 humanValue = humanValues[0];
351 } else if (value.equals(values[1])) {
352 humanValue = humanValues[1];
353 } else if (value.equals(values[2])) {
354 humanValue = humanValues[2];
357 connectionPref.setSummary(humanValue);
362 private void updateNotification(final String updateTimeRate) {
363 final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
364 long chosenInterval = 0;
365 if (updateTimeRate.equals(values[0])) {
366 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
367 } else if (updateTimeRate.equals(values[1])) {
368 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
369 } else if (updateTimeRate.equals(values[2])) {
370 chosenInterval = AlarmManager.INTERVAL_HOUR;
371 } else if (updateTimeRate.equals(values[3])) {
372 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
373 } else if (updateTimeRate.equals(values[4])) {
374 chosenInterval = AlarmManager.INTERVAL_DAY;
377 final AlarmManager alarmMgr =
378 (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
379 // TODO: better use some string instead of .class? In case I change the service class
380 // this could be a problem (I guess)
381 final Intent serviceIntent =
382 new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
383 final PendingIntent alarmIntent =
384 PendingIntent.getService(
385 this.getActivity().getApplicationContext(),
388 PendingIntent.FLAG_UPDATE_CURRENT);
389 if (chosenInterval != 0) {
390 alarmMgr.setInexactRepeating(
391 AlarmManager.ELAPSED_REALTIME,
392 SystemClock.elapsedRealtime(),
398 private void cancelNotification() {
399 final AlarmManager alarmMgr =
400 (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
401 final Intent serviceIntent =
402 new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
403 final PendingIntent alarmIntent =
404 PendingIntent.getService(
405 this.getActivity().getApplicationContext(),
408 PendingIntent.FLAG_UPDATE_CURRENT);
409 alarmMgr.cancel(alarmIntent);