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_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];
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().getString(keyPreference, "");
53 if (value.equals(values[0])) {
54 humanValue = humanValues[0];
55 } else if (value.equals(values[1])) {
56 humanValue = humanValues[1];
58 connectionPref.setSummary(humanValue);
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, "");
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];
75 connectionPref.setSummary(humanValue);
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, "");
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];
100 connectionPref.setSummary(humanValue);
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, "");
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];
122 connectionPref.setSummary(humanValue);
126 public void onResume() {
128 this.getPreferenceManager().getSharedPreferences()
129 .registerOnSharedPreferenceChangeListener(this);
134 public void onPause() {
136 this.getPreferenceManager().getSharedPreferences()
137 .unregisterOnSharedPreferenceChangeListener(this);
141 public void onSharedPreferenceChanged(
142 final SharedPreferences sharedPreferences, final String key) {
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];
161 connectionPref.setSummary(humanValue);
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];
179 connectionPref.setSummary(humanValue);
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];
199 connectionPref.setSummary(humanValue);
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];
227 connectionPref.setSummary(humanValue);
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())
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);
243 this.cancelNotification();
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];
267 this.updateNotification(value);
268 connectionPref.setSummary(humanValue);
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;
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(),
299 PendingIntent.FLAG_UPDATE_CURRENT);
300 if (chosenInterval != 0) {
301 alarmMgr.setInexactRepeating(
302 AlarmManager.ELAPSED_REALTIME,
303 SystemClock.elapsedRealtime(),
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(),
319 PendingIntent.FLAG_UPDATE_CURRENT);
320 alarmMgr.cancel(alarmIntent);