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 de.example.exampletdd.R;
14 import de.example.exampletdd.NotificationIntentService;
16 public class WeatherInformationPreferencesFragment extends PreferenceFragment
17 implements OnSharedPreferenceChangeListener {
20 public void onCreate(final Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
23 // Load the preferences from an XML resource
24 this.addPreferencesFromResource(R.xml.weather_preferences);
28 String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value);
29 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_units_human_value);
30 String keyPreference = this.getActivity().getApplicationContext().getString(
31 R.string.weather_preferences_units_key);
32 Preference connectionPref = this.findPreference(keyPreference);
33 String value = this.getPreferenceManager().getSharedPreferences()
34 .getString(keyPreference, "");
35 String humanValue = "";
36 if (value.equals(values[0])) {
37 humanValue = humanValues[0];
38 } else if (value.equals(values[1])) {
39 humanValue = humanValues[1];
40 } else if (value.equals(values[2])) {
41 humanValue = humanValues[2];
43 connectionPref.setSummary(humanValue);
46 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
47 humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
48 keyPreference = this.getActivity().getApplicationContext().getString(
49 R.string.weather_preferences_update_time_rate_key);
50 connectionPref = this.findPreference(keyPreference);
51 value = this.getPreferenceManager().getSharedPreferences()
52 .getString(keyPreference, "");
54 if (value.equals(values[0])) {
55 humanValue = humanValues[0];
56 } else if (value.equals(values[1])) {
57 humanValue = humanValues[1];
58 } else if (value.equals(values[2])) {
59 humanValue = humanValues[2];
60 } else if (value.equals(values[3])) {
61 humanValue = humanValues[3];
62 } else if (value.equals(values[4])) {
63 humanValue = humanValues[4];
65 connectionPref.setSummary(humanValue);
68 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
69 humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
70 keyPreference = this.getString(R.string.weather_preferences_wind_key);
71 connectionPref = this.findPreference(keyPreference);
72 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
74 if (value.equals(values[0])) {
75 humanValue = humanValues[0];
76 } else if (value.equals(values[1])) {
77 humanValue = humanValues[1];
79 connectionPref.setSummary(humanValue);
81 // Forecast days number
82 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
83 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
84 keyPreference = this.getActivity().getApplicationContext().getString(
85 R.string.weather_preferences_day_forecast_key);
86 connectionPref = this.findPreference(keyPreference);
87 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
89 if (value.equals(values[0])) {
90 humanValue = humanValues[0];
91 } else if (value.equals(values[1])) {
92 humanValue = humanValues[1];
93 } else if (value.equals(values[2])) {
94 humanValue = humanValues[2];
96 connectionPref.setSummary(humanValue);
99 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
100 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
101 keyPreference = this.getActivity().getApplicationContext().getString(
102 R.string.weather_preferences_refresh_interval_key);
103 connectionPref = this.findPreference(keyPreference);
104 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
106 if (value.equals(values[0])) {
107 humanValue = humanValues[0];
108 } else if (value.equals(values[1])) {
109 humanValue = humanValues[1];
110 } else if (value.equals(values[2])) {
111 humanValue = humanValues[2];
112 } else if (value.equals(values[3])) {
113 humanValue = humanValues[3];
114 } else if (value.equals(values[4])) {
115 humanValue = humanValues[4];
116 } else if (value.equals(values[5])) {
117 humanValue = humanValues[5];
118 } else if (value.equals(values[6])) {
119 humanValue = humanValues[6];
121 connectionPref.setSummary(humanValue);
125 public void onResume() {
127 this.getPreferenceManager().getSharedPreferences()
128 .registerOnSharedPreferenceChangeListener(this);
133 public void onPause() {
135 this.getPreferenceManager().getSharedPreferences()
136 .unregisterOnSharedPreferenceChangeListener(this);
140 public void onSharedPreferenceChanged(
141 final SharedPreferences sharedPreferences, final String key) {
144 String[] values = this.getResources().getStringArray(R.array.weather_preferences_units_value);
145 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_units_human_value);
146 String keyValue = this.getActivity().getApplicationContext().getString(
147 R.string.weather_preferences_units_key);
148 if (key.equals(keyValue)) {
149 final Preference connectionPref = this.findPreference(key);
150 final String value = sharedPreferences.getString(key, "");
151 String humanValue = "";
152 if (value.equals(values[0])) {
153 humanValue = humanValues[0];
154 } else if (value.equals(values[1])) {
155 humanValue = humanValues[1];
156 } else if (value.equals(values[2])) {
157 humanValue = humanValues[2];
160 connectionPref.setSummary(humanValue);
165 values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
166 humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
167 keyValue = this.getActivity().getApplicationContext().getString(
168 R.string.weather_preferences_update_time_rate_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];
177 } else if (value.equals(values[2])) {
178 humanValue = humanValues[2];
179 } else if (value.equals(values[3])) {
180 humanValue = humanValues[3];
181 } else if (value.equals(values[4])) {
182 humanValue = humanValues[4];
185 this.updateAlarm(value);
186 connectionPref.setSummary(humanValue);
191 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
192 humanValues = this.getResources().getStringArray(R.array.weather_preferences_wind_human_value);
193 keyValue = this.getString(R.string.weather_preferences_wind_key);
194 if (key.equals(keyValue)) {
195 final Preference connectionPref = this.findPreference(key);
196 final String value = sharedPreferences.getString(key, "");
197 String humanValue = "";
198 if (value.equals(values[0])) {
199 humanValue = humanValues[0];
200 } else if (value.equals(values[1])) {
201 humanValue = humanValues[1];
204 connectionPref.setSummary(humanValue);
208 // Forecast days number
209 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
210 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
211 keyValue = this.getActivity().getString(
212 R.string.weather_preferences_day_forecast_key);
213 if (key.equals(keyValue)) {
214 final Preference connectionPref = this.findPreference(key);
215 final String value = sharedPreferences.getString(key, "");
216 String humanValue = "";
217 if (value.equals(values[0])) {
218 humanValue = humanValues[0];
219 } else if (value.equals(values[1])) {
220 humanValue = humanValues[1];
221 } else if (value.equals(values[2])) {
222 humanValue = humanValues[2];
224 connectionPref.setSummary(humanValue);
229 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
230 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
231 keyValue = this.getActivity().getApplicationContext().getString(
232 R.string.weather_preferences_refresh_interval_key);
233 if (key.equals(keyValue)) {
234 final Preference connectionPref = this.findPreference(key);
235 final String value = sharedPreferences.getString(key, "");
236 String humanValue = "";
237 if (value.equals(values[0])) {
238 humanValue = humanValues[0];
239 } else if (value.equals(values[1])) {
240 humanValue = humanValues[1];
241 } else if (value.equals(values[2])) {
242 humanValue = humanValues[2];
243 } else if (value.equals(values[3])) {
244 humanValue = humanValues[3];
245 } else if (value.equals(values[4])) {
246 humanValue = humanValues[4];
247 } else if (value.equals(values[5])) {
248 humanValue = humanValues[5];
249 } else if (value.equals(values[6])) {
250 humanValue = humanValues[6];
252 connectionPref.setSummary(humanValue);
257 private void updateAlarm(final String updateTimeRate) {
258 final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
259 long chosenInterval = 0;
260 if (updateTimeRate.equals(values[0])) {
261 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
262 } else if (updateTimeRate.equals(values[1])) {
263 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
264 } else if (updateTimeRate.equals(values[2])) {
265 chosenInterval = AlarmManager.INTERVAL_HOUR;
266 } else if (updateTimeRate.equals(values[3])) {
267 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
268 } else if (updateTimeRate.equals(values[4])) {
269 chosenInterval = AlarmManager.INTERVAL_DAY;
272 final AlarmManager alarmMgr =
273 (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
274 // TODO: better use some string instead of .class? In case I change the service class
275 // this could be a problem (I guess)
276 final Intent serviceIntent =
277 new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
278 final PendingIntent alarmIntent =
279 PendingIntent.getService(
280 this.getActivity().getApplicationContext(),
283 PendingIntent.FLAG_UPDATE_CURRENT);
284 if (chosenInterval != 0) {
285 alarmMgr.setInexactRepeating(
286 AlarmManager.ELAPSED_REALTIME,
287 SystemClock.elapsedRealtime(),
291 alarmMgr.cancel(alarmIntent);