6ad22ba33b20cd0252fc67e63c895726b19a0d38
[JavaForFun] /
1 package de.example.exampletdd.fragment.preferences;
2
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;
15
16 public class WeatherInformationPreferencesFragment extends PreferenceFragment 
17                                                                                                         implements OnSharedPreferenceChangeListener {
18
19     @Override
20     public void onCreate(final Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22
23         // Load the preferences from an XML resource
24         this.addPreferencesFromResource(R.xml.weather_preferences);
25         
26         
27         // Temperature units
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];
42         }
43         connectionPref.setSummary(humanValue);
44         
45         // Update Time Rate
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, "");
53         humanValue = "";
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];
64         }
65         connectionPref.setSummary(humanValue);
66         
67         // Forecast days number
68         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
69         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
70         keyPreference = this.getActivity().getApplicationContext().getString(
71                 R.string.weather_preferences_day_forecast_key);
72         connectionPref = this.findPreference(keyPreference);
73         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
74         humanValue = "";
75         if (value.equals(values[0])) {
76             humanValue = humanValues[0];
77         } else if (value.equals(values[1])) {
78             humanValue = humanValues[1];
79         } else if (value.equals(values[2])) {
80             humanValue = humanValues[2];
81         }
82         connectionPref.setSummary(humanValue);
83
84         // Refresh interval
85         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
86         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
87         keyPreference = this.getActivity().getApplicationContext().getString(
88                 R.string.weather_preferences_refresh_interval_key);
89         connectionPref = this.findPreference(keyPreference);
90         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
91         humanValue = "";
92         if (value.equals(values[0])) {
93             humanValue = humanValues[0];
94         } else if (value.equals(values[1])) {
95             humanValue = humanValues[1];
96         } else if (value.equals(values[2])) {
97             humanValue = humanValues[2];
98         } else if (value.equals(values[3])) {
99             humanValue = humanValues[3];
100         } else if (value.equals(values[4])) {
101             humanValue = humanValues[4];
102         } else if (value.equals(values[5])) {
103             humanValue = humanValues[5];
104         } else if (value.equals(values[6])) {
105             humanValue = humanValues[6];
106         }
107         connectionPref.setSummary(humanValue);
108     }
109
110     @Override
111     public void onResume() {
112         super.onResume();
113         this.getPreferenceManager().getSharedPreferences()
114         .registerOnSharedPreferenceChangeListener(this);
115
116     }
117
118     @Override
119     public void onPause() {
120         super.onPause();
121         this.getPreferenceManager().getSharedPreferences()
122         .unregisterOnSharedPreferenceChangeListener(this);
123     }
124
125     @Override
126     public void onSharedPreferenceChanged(
127             final SharedPreferences sharedPreferences, final String key) {
128         
129         // Units of Measurement
130         String keyValue = this.getActivity().getApplicationContext().getString(
131                 R.string.weather_preferences_units_key);
132
133         if (key.equals(keyValue)) {
134             final Preference connectionPref = this.findPreference(key);
135             connectionPref.setSummary(sharedPreferences.getString(key, ""));
136             return;
137         }
138
139         // Update Time Rate
140         String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
141         String[] humanValues = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate_human_value);
142         keyValue = this.getActivity().getApplicationContext().getString(
143                         R.string.weather_preferences_update_time_rate_key);
144         if (key.equals(keyValue)) {
145             final Preference connectionPref = this.findPreference(key);
146             final String value = sharedPreferences.getString(key, "");
147             String humanValue = "";
148             if (value.equals(values[0])) {
149                 humanValue = humanValues[0];
150             } else if (value.equals(values[1])) {
151                 humanValue = humanValues[1];
152             } else if (value.equals(values[2])) {
153                 humanValue = humanValues[2];
154             } else if (value.equals(values[3])) {
155                 humanValue = humanValues[3];
156             } else if (value.equals(values[4])) {
157                 humanValue = humanValues[4];
158             }
159             
160             this.updateAlarm(value);
161             connectionPref.setSummary(humanValue);
162             return;
163         }
164
165         // Forecast days number
166         values = this.getResources().getStringArray(R.array.weather_preferences_day_forecast);
167         humanValues = this.getResources().getStringArray(R.array.weather_preferences_day_forecast_human_value);
168         keyValue = this.getActivity().getString(
169                 R.string.weather_preferences_day_forecast_key);
170         if (key.equals(keyValue)) {
171             final Preference connectionPref = this.findPreference(key);
172             final String value = sharedPreferences.getString(key, "");
173             String humanValue = "";
174             if (value.equals(values[0])) {
175                 humanValue = humanValues[0];
176             } else if (value.equals(values[1])) {
177                 humanValue = humanValues[1];
178             } else if (value.equals(values[2])) {
179                 humanValue = humanValues[2];
180             }
181             connectionPref.setSummary(humanValue);
182             return;
183         }
184
185         // Refresh interval
186         values = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval);
187         humanValues = this.getResources().getStringArray(R.array.weather_preferences_refresh_interval_human_value);
188         keyValue = this.getActivity().getApplicationContext().getString(
189                 R.string.weather_preferences_refresh_interval_key);
190         if (key.equals(keyValue)) {
191                 final Preference connectionPref = this.findPreference(key);
192             final String value = sharedPreferences.getString(key, "");
193             String humanValue = "";
194             if (value.equals(values[0])) {
195                 humanValue = humanValues[0];
196             } else if (value.equals(values[1])) {
197                 humanValue = humanValues[1];
198             } else if (value.equals(values[2])) {
199                 humanValue = humanValues[2];
200             } else if (value.equals(values[3])) {
201                 humanValue = humanValues[3];
202             } else if (value.equals(values[4])) {
203                 humanValue = humanValues[4];
204             } else if (value.equals(values[5])) {
205                 humanValue = humanValues[5];
206             } else if (value.equals(values[6])) {
207                 humanValue = humanValues[6];
208             }
209             connectionPref.setSummary(humanValue);
210             return;
211         }
212     }
213
214     private void updateAlarm(final String updateTimeRate) {
215         final String[] values = this.getResources().getStringArray(R.array.weather_preferences_update_time_rate);
216         long chosenInterval = 0;
217         if (updateTimeRate.equals(values[0])) {
218                 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
219         } else if (updateTimeRate.equals(values[1])) {
220                 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
221         } else if (updateTimeRate.equals(values[2])) {
222                 chosenInterval = AlarmManager.INTERVAL_HOUR;
223         } else if (updateTimeRate.equals(values[3])) {
224                 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
225         } else if (updateTimeRate.equals(values[4])) {
226                 chosenInterval = AlarmManager.INTERVAL_DAY;
227         }
228
229         final AlarmManager alarmMgr =
230                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
231         // TODO: better use some string instead of .class? In case I change the service class
232         // this could be a problem (I guess)
233         final Intent serviceIntent =
234                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
235         final PendingIntent alarmIntent =
236                         PendingIntent.getService(
237                                         this.getActivity().getApplicationContext(),
238                                         0,
239                                         serviceIntent,
240                                         PendingIntent.FLAG_UPDATE_CURRENT);
241         if (chosenInterval != 0) {   
242             alarmMgr.setInexactRepeating(
243                         AlarmManager.ELAPSED_REALTIME,
244                         SystemClock.elapsedRealtime(),
245                         chosenInterval,
246                         alarmIntent);
247         } else {
248                 alarmMgr.cancel(alarmIntent);
249         }
250     }
251 }