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);
27 // Units of Measurement
28 String keyPreference = this.getActivity().getApplicationContext().getString(
29 R.string.weather_preferences_units_key);
30 Preference connectionPref = this.findPreference(keyPreference);
31 connectionPref.setSummary(this.getPreferenceManager()
32 .getSharedPreferences().getString(keyPreference, ""));
35 String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
36 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
37 keyPreference = this.getActivity().getApplicationContext().getString(
38 R.string.weather_preferences_update_time_rate_key);
39 connectionPref = this.findPreference(keyPreference);
40 String value = this.getPreferenceManager().getSharedPreferences()
41 .getString(keyPreference, "");
42 String humanValue = "";
43 if (value.equals(values[0])) {
44 humanValue = humanValues[0];
45 } else if (value.equals(values[1])) {
46 humanValue = humanValues[1];
47 } else if (value.equals(values[2])) {
48 humanValue = humanValues[2];
49 } else if (value.equals(values[3])) {
50 humanValue = humanValues[3];
51 } else if (value.equals(values[4])) {
52 humanValue = humanValues[4];
54 connectionPref.setSummary(humanValue);
56 // Forecast days number
57 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
58 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
59 keyPreference = this.getActivity().getApplicationContext().getString(
60 R.string.weather_preferences_day_forecast_key);
61 connectionPref = this.findPreference(keyPreference);
62 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
64 if (value.equals(values[0])) {
65 humanValue = humanValues[0];
66 } else if (value.equals(values[1])) {
67 humanValue = humanValues[1];
68 } else if (value.equals(values[2])) {
69 humanValue = humanValues[2];
71 connectionPref.setSummary(humanValue);
74 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
75 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
76 keyPreference = this.getActivity().getApplicationContext().getString(
77 R.string.weather_preferences_refresh_interval_key);
78 connectionPref = this.findPreference(keyPreference);
79 value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
81 if (value.equals(values[0])) {
82 humanValue = humanValues[0];
83 } else if (value.equals(values[1])) {
84 humanValue = humanValues[1];
85 } else if (value.equals(values[2])) {
86 humanValue = humanValues[2];
87 } else if (value.equals(values[3])) {
88 humanValue = humanValues[3];
89 } else if (value.equals(values[4])) {
90 humanValue = humanValues[4];
91 } else if (value.equals(values[5])) {
92 humanValue = humanValues[5];
93 } else if (value.equals(values[6])) {
94 humanValue = humanValues[6];
96 connectionPref.setSummary(humanValue);
100 public void onResume() {
102 this.getPreferenceManager().getSharedPreferences()
103 .registerOnSharedPreferenceChangeListener(this);
108 public void onPause() {
110 this.getPreferenceManager().getSharedPreferences()
111 .unregisterOnSharedPreferenceChangeListener(this);
115 public void onSharedPreferenceChanged(
116 final SharedPreferences sharedPreferences, final String key) {
118 // Units of Measurement
119 String keyValue = this.getActivity().getApplicationContext().getString(
120 R.string.weather_preferences_units_key);
122 if (key.equals(keyValue)) {
123 final Preference connectionPref = this.findPreference(key);
124 connectionPref.setSummary(sharedPreferences.getString(key, ""));
129 String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
130 String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
131 keyValue = this.getActivity().getApplicationContext().getString(
132 R.string.weather_preferences_update_time_rate_key);
133 if (key.equals(keyValue)) {
134 final Preference connectionPref = this.findPreference(key);
135 final String value = sharedPreferences.getString(key, "");
136 String humanValue = "";
137 if (value.equals(values[0])) {
138 humanValue = humanValues[0];
139 } else if (value.equals(values[1])) {
140 humanValue = humanValues[1];
141 } else if (value.equals(values[2])) {
142 humanValue = humanValues[2];
143 } else if (value.equals(values[3])) {
144 humanValue = humanValues[3];
145 } else if (value.equals(values[4])) {
146 humanValue = humanValues[4];
149 this.updateAlarm(value);
150 connectionPref.setSummary(humanValue);
154 // Forecast days number
155 values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
156 humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
157 keyValue = this.getActivity().getString(
158 R.string.weather_preferences_day_forecast_key);
159 if (key.equals(keyValue)) {
160 final Preference connectionPref = this.findPreference(key);
161 final String value = sharedPreferences.getString(key, "");
162 String humanValue = "";
163 if (value.equals(values[0])) {
164 humanValue = humanValues[0];
165 } else if (value.equals(values[1])) {
166 humanValue = humanValues[1];
167 } else if (value.equals(values[2])) {
168 humanValue = humanValues[2];
170 connectionPref.setSummary(humanValue);
175 values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
176 humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
177 keyValue = this.getActivity().getApplicationContext().getString(
178 R.string.weather_preferences_refresh_interval_key);
179 if (key.equals(keyValue)) {
180 final Preference connectionPref = this.findPreference(key);
181 final String value = sharedPreferences.getString(key, "");
182 String humanValue = "";
183 if (value.equals(values[0])) {
184 humanValue = humanValues[0];
185 } else if (value.equals(values[1])) {
186 humanValue = humanValues[1];
187 } else if (value.equals(values[2])) {
188 humanValue = humanValues[2];
189 } else if (value.equals(values[3])) {
190 humanValue = humanValues[3];
191 } else if (value.equals(values[4])) {
192 humanValue = humanValues[4];
193 } else if (value.equals(values[5])) {
194 humanValue = humanValues[5];
195 } else if (value.equals(values[6])) {
196 humanValue = humanValues[6];
198 connectionPref.setSummary(humanValue);
203 private void updateAlarm(final String updateTimeRate) {
204 final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
205 long chosenInterval = 0;
206 if (updateTimeRate.equals(values[0])) {
207 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
208 } else if (updateTimeRate.equals(values[1])) {
209 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
210 } else if (updateTimeRate.equals(values[2])) {
211 chosenInterval = AlarmManager.INTERVAL_HOUR;
212 } else if (updateTimeRate.equals(values[3])) {
213 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
214 } else if (updateTimeRate.equals(values[4])) {
215 chosenInterval = AlarmManager.INTERVAL_DAY;
218 final AlarmManager alarmMgr =
219 (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
220 // TODO: better use some string instead of .class? In case I change the service class
221 // this could be a problem (I guess)
222 final Intent serviceIntent =
223 new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
224 final PendingIntent alarmIntent =
225 PendingIntent.getService(
226 this.getActivity().getApplicationContext(),
229 PendingIntent.FLAG_UPDATE_CURRENT);
230 if (chosenInterval != 0) {
231 alarmMgr.setInexactRepeating(
232 AlarmManager.ELAPSED_REALTIME,
233 SystemClock.elapsedRealtime(),
237 alarmMgr.cancel(alarmIntent);