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 WeatherInformationPreferencesFragment extends PreferenceFragment
34 implements OnSharedPreferenceChangeListener {
37 public void onCreate(final Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
40 // Load the preferences from an XML resource
41 this.addPreferencesFromResource(R.xml.weather_preferences);
45 String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
46 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
47 String keyPreference = this.getActivity().getApplicationContext().getString(
48 R.string.weather_preferences_temperature_key);
49 Preference connectionPref = this.findPreference(keyPreference);
50 String value = this.getPreferenceManager().getSharedPreferences()
51 .getString(keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
52 String humanValue = "";
53 if (value.equals(values[0])) {
54 humanValue = humanValues[0];
55 } else if (value.equals(values[1])) {
56 humanValue = humanValues[1];
57 } else if (value.equals(values[2])) {
58 humanValue = humanValues[2];
60 connectionPref.setSummary(humanValue);
63 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
64 humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
65 keyPreference = this.getString(R.string.weather_preferences_wind_key);
66 connectionPref = this.findPreference(keyPreference);
67 value = this.getPreferenceManager().getSharedPreferences()
68 .getString(keyPreference, this.getString(R.string.weather_preferences_wind_meters));
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);
78 values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
79 humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
80 keyPreference = this.getString(R.string.weather_preferences_pressure_key);
81 connectionPref = this.findPreference(keyPreference);
82 value = this.getPreferenceManager().getSharedPreferences()
83 .getString(keyPreference, this.getString(R.string.weather_preferences_pressure_pascal));
85 if (value.equals(values[0])) {
86 humanValue = humanValues[0];
87 } else if (value.equals(values[1])) {
88 humanValue = humanValues[1];
90 connectionPref.setSummary(humanValue);
92 // Forecast days number
93 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
94 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
95 keyPreference = this.getActivity().getApplicationContext().getString(
96 R.string.weather_preferences_day_forecast_key);
97 connectionPref = this.findPreference(keyPreference);
98 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]);
100 if (value.equals(values[0])) {
101 humanValue = humanValues[0];
102 } else if (value.equals(values[1])) {
103 humanValue = humanValues[1];
104 } else if (value.equals(values[2])) {
105 humanValue = humanValues[2];
107 connectionPref.setSummary(humanValue);
110 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
111 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
112 keyPreference = this.getActivity().getApplicationContext().getString(
113 R.string.weather_preferences_refresh_interval_key);
114 connectionPref = this.findPreference(keyPreference);
115 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, values[0]);
117 if (value.equals(values[0])) {
118 humanValue = humanValues[0];
119 } else if (value.equals(values[1])) {
120 humanValue = humanValues[1];
121 } else if (value.equals(values[2])) {
122 humanValue = humanValues[2];
123 } else if (value.equals(values[3])) {
124 humanValue = humanValues[3];
125 } else if (value.equals(values[4])) {
126 humanValue = humanValues[4];
127 } else if (value.equals(values[5])) {
128 humanValue = humanValues[5];
129 } else if (value.equals(values[6])) {
130 humanValue = humanValues[6];
132 connectionPref.setSummary(humanValue);
135 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
136 humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
137 keyPreference = this.getActivity().getApplicationContext().getString(
138 R.string.weather_preferences_update_time_rate_key);
139 connectionPref = this.findPreference(keyPreference);
140 value = this.getPreferenceManager().getSharedPreferences()
141 .getString(keyPreference, values[0]);
143 if (value.equals(values[0])) {
144 humanValue = humanValues[0];
145 } else if (value.equals(values[1])) {
146 humanValue = humanValues[1];
147 } else if (value.equals(values[2])) {
148 humanValue = humanValues[2];
149 } else if (value.equals(values[3])) {
150 humanValue = humanValues[3];
151 } else if (value.equals(values[4])) {
152 humanValue = humanValues[4];
154 connectionPref.setSummary(humanValue);
156 // Notifications temperature units
157 values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
158 humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
159 keyPreference = this.getActivity().getApplicationContext().getString(
160 R.string.weather_preferences_notifications_temperature_key);
161 connectionPref = this.findPreference(keyPreference);
162 value = this.getPreferenceManager().getSharedPreferences()
163 .getString(keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
165 if (value.equals(values[0])) {
166 humanValue = humanValues[0];
167 } else if (value.equals(values[1])) {
168 humanValue = humanValues[1];
169 } else if (value.equals(values[2])) {
170 humanValue = humanValues[2];
172 connectionPref.setSummary(humanValue);
176 public void onResume() {
178 this.getPreferenceManager().getSharedPreferences()
179 .registerOnSharedPreferenceChangeListener(this);
184 public void onPause() {
186 this.getPreferenceManager().getSharedPreferences()
187 .unregisterOnSharedPreferenceChangeListener(this);
191 public void onSharedPreferenceChanged(
192 final SharedPreferences sharedPreferences, final String key) {
195 String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
196 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
197 String keyValue = this.getActivity().getApplicationContext().getString(
198 R.string.weather_preferences_temperature_key);
199 if (key.equals(keyValue)) {
200 final Preference connectionPref = this.findPreference(key);
201 final String value = sharedPreferences.getString(key, values[0]);
202 String humanValue = "";
203 if (value.equals(values[0])) {
204 humanValue = humanValues[0];
205 } else if (value.equals(values[1])) {
206 humanValue = humanValues[1];
207 } else if (value.equals(values[2])) {
208 humanValue = humanValues[2];
211 connectionPref.setSummary(humanValue);
216 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
217 humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
218 keyValue = this.getString(R.string.weather_preferences_wind_key);
219 if (key.equals(keyValue)) {
220 final Preference connectionPref = this.findPreference(key);
221 final String value = sharedPreferences.getString(key, values[0]);
222 String humanValue = "";
223 if (value.equals(values[0])) {
224 humanValue = humanValues[0];
225 } else if (value.equals(values[1])) {
226 humanValue = humanValues[1];
229 connectionPref.setSummary(humanValue);
234 values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
235 humanValues = this.getResources().getStringArray(R.array.weather_preferences_pressure_human_value);
236 keyValue = this.getString(R.string.weather_preferences_pressure_key);
237 if (key.equals(keyValue)) {
238 final Preference connectionPref = this.findPreference(key);
239 final String value = sharedPreferences.getString(key, values[0]);
240 String humanValue = "";
241 if (value.equals(values[0])) {
242 humanValue = humanValues[0];
243 } else if (value.equals(values[1])) {
244 humanValue = humanValues[1];
247 connectionPref.setSummary(humanValue);
251 // Forecast days number
252 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
253 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
254 keyValue = this.getActivity().getString(
255 R.string.weather_preferences_day_forecast_key);
256 if (key.equals(keyValue)) {
257 final Preference connectionPref = this.findPreference(key);
258 final String value = sharedPreferences.getString(key, values[0]);
259 String humanValue = "";
260 if (value.equals(values[0])) {
261 humanValue = humanValues[0];
262 } else if (value.equals(values[1])) {
263 humanValue = humanValues[1];
264 } else if (value.equals(values[2])) {
265 humanValue = humanValues[2];
267 connectionPref.setSummary(humanValue);
272 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
273 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
274 keyValue = this.getActivity().getApplicationContext().getString(
275 R.string.weather_preferences_refresh_interval_key);
276 if (key.equals(keyValue)) {
277 final Preference connectionPref = this.findPreference(key);
278 final String value = sharedPreferences.getString(key, values[0]);
279 String humanValue = "";
280 if (value.equals(values[0])) {
281 humanValue = humanValues[0];
282 } else if (value.equals(values[1])) {
283 humanValue = humanValues[1];
284 } else if (value.equals(values[2])) {
285 humanValue = humanValues[2];
286 } else if (value.equals(values[3])) {
287 humanValue = humanValues[3];
288 } else if (value.equals(values[4])) {
289 humanValue = humanValues[4];
290 } else if (value.equals(values[5])) {
291 humanValue = humanValues[5];
292 } else if (value.equals(values[6])) {
293 humanValue = humanValues[6];
295 connectionPref.setSummary(humanValue);
299 // Notification switch
300 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
301 keyValue = this.getActivity().getApplicationContext().getString(
302 R.string.weather_preferences_notifications_switch_key);
303 if (key.equals(keyValue)) {
304 final SwitchPreference preference = (SwitchPreference)this.findPreference(key);
305 if (preference.isChecked())
307 keyValue = this.getActivity().getApplicationContext().getString(
308 R.string.weather_preferences_update_time_rate_key);
309 final String value = sharedPreferences.getString(keyValue, values[0]);
310 this.updateNotification(value);
312 this.cancelNotification();
316 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
317 humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
318 keyValue = this.getActivity().getApplicationContext().getString(
319 R.string.weather_preferences_update_time_rate_key);
320 if (key.equals(keyValue)) {
321 final Preference connectionPref = this.findPreference(key);
322 final String value = sharedPreferences.getString(key, values[0]);
323 String humanValue = "";
324 if (value.equals(values[0])) {
325 humanValue = humanValues[0];
326 } else if (value.equals(values[1])) {
327 humanValue = humanValues[1];
328 } else if (value.equals(values[2])) {
329 humanValue = humanValues[2];
330 } else if (value.equals(values[3])) {
331 humanValue = humanValues[3];
332 } else if (value.equals(values[4])) {
333 humanValue = humanValues[4];
336 this.updateNotification(value);
337 connectionPref.setSummary(humanValue);
342 values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
343 humanValues = this.getResources().getStringArray(R.array.weather_preferences_temperature_human_value);
344 keyValue = this.getActivity().getApplicationContext().getString(
345 R.string.weather_preferences_notifications_temperature_key);
346 if (key.equals(keyValue)) {
347 final Preference connectionPref = this.findPreference(key);
348 final String value = sharedPreferences.getString(key, values[0]);
349 String humanValue = "";
350 if (value.equals(values[0])) {
351 humanValue = humanValues[0];
352 } else if (value.equals(values[1])) {
353 humanValue = humanValues[1];
354 } else if (value.equals(values[2])) {
355 humanValue = humanValues[2];
358 connectionPref.setSummary(humanValue);
363 private void updateNotification(final String updateTimeRate) {
364 final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
365 long chosenInterval = 0;
366 if (updateTimeRate.equals(values[0])) {
367 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
368 } else if (updateTimeRate.equals(values[1])) {
369 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
370 } else if (updateTimeRate.equals(values[2])) {
371 chosenInterval = AlarmManager.INTERVAL_HOUR;
372 } else if (updateTimeRate.equals(values[3])) {
373 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
374 } else if (updateTimeRate.equals(values[4])) {
375 chosenInterval = AlarmManager.INTERVAL_DAY;
378 final AlarmManager alarmMgr =
379 (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
380 // TODO: better use some string instead of .class? In case I change the service class
381 // this could be a problem (I guess)
382 final Intent serviceIntent =
383 new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
384 final PendingIntent alarmIntent =
385 PendingIntent.getService(
386 this.getActivity().getApplicationContext(),
389 PendingIntent.FLAG_UPDATE_CURRENT);
390 if (chosenInterval != 0) {
391 alarmMgr.setInexactRepeating(
392 AlarmManager.ELAPSED_REALTIME,
393 SystemClock.elapsedRealtime(),
399 private void cancelNotification() {
400 final AlarmManager alarmMgr =
401 (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
402 final Intent serviceIntent =
403 new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
404 final PendingIntent alarmIntent =
405 PendingIntent.getService(
406 this.getActivity().getApplicationContext(),
409 PendingIntent.FLAG_UPDATE_CURRENT);
410 alarmMgr.cancel(alarmIntent);