2b665e9398a42cd51d47e30c714dd38520add1e7
[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         // 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, ""));
33         
34         // Update Time Rate
35         keyPreference = this.getActivity().getApplicationContext().getString(
36                 R.string.weather_preferences_update_time_rate_key);
37         connectionPref = this.findPreference(keyPreference);
38         String value = this.getPreferenceManager().getSharedPreferences()
39                 .getString(keyPreference, "");
40         String humanValue = "";
41         if (value.equals("0")) {
42             humanValue = "no updates";
43         } else if (value.equals("900")) {
44             humanValue = "fifteen minutes";
45         } else if (value.equals("1800")) {
46             humanValue = "half hour";
47         } else if (value.equals("3600")) {
48             humanValue = "one hour";
49         } else if (value.equals("43200")) {
50             humanValue = "half day";
51         } else if (value.equals("86400")) {
52             humanValue = "one day";
53         }
54         connectionPref.setSummary(humanValue);
55         
56         // Forecast days number
57         keyPreference = this.getActivity().getApplicationContext().getString(
58                 R.string.weather_preferences_day_forecast_key);
59         connectionPref = this.findPreference(keyPreference);
60         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
61         humanValue = "";
62         if (value.equals("5")) {
63             humanValue = "5-Day Forecast";
64         } else if (value.equals("10")) {
65             humanValue = "10-Day Forecast";
66         } else if (value.equals("14")) {
67             humanValue = "14-Day Forecast";
68         }
69         connectionPref.setSummary(humanValue);
70     }
71
72     @Override
73     public void onResume() {
74         super.onResume();
75         this.getPreferenceManager().getSharedPreferences()
76         .registerOnSharedPreferenceChangeListener(this);
77
78     }
79
80     @Override
81     public void onPause() {
82         super.onPause();
83         this.getPreferenceManager().getSharedPreferences()
84         .unregisterOnSharedPreferenceChangeListener(this);
85     }
86
87     @Override
88     public void onSharedPreferenceChanged(
89             final SharedPreferences sharedPreferences, final String key) {
90         
91         // Units of Measurement
92         String keyValue = this.getActivity().getApplicationContext().getString(
93                 R.string.weather_preferences_units_key);
94
95         if (key.equals(keyValue)) {
96             final Preference connectionPref = this.findPreference(key);
97             connectionPref.setSummary(sharedPreferences.getString(key, ""));
98             return;
99         }
100
101         // Update Time Rate
102         keyValue = this.getActivity().getApplicationContext().getString(
103                         R.string.weather_preferences_update_time_rate_key);
104         if (key.equals(keyValue)) {
105             final Preference connectionPref = this.findPreference(key);
106             final String value = sharedPreferences.getString(key, "");
107             String humanValue = "";
108             if (value.equals("0")) {
109                 humanValue = "no updates";
110             } else if (value.equals("900")) {
111                 humanValue = "fifteen minutes";
112             } else if (value.equals("1800")) {
113                 humanValue = "half hour";
114             } else if (value.equals("3600")) {
115                 humanValue = "one hour";
116             } else if (value.equals("43200")) {
117                 humanValue = "half day";
118             } else if (value.equals("86400")) {
119                 humanValue = "one day";
120             }
121             
122             this.updateAlarm(value);
123             connectionPref.setSummary(humanValue);
124             return;
125         }
126
127         // Forecast days number
128         keyValue = this.getActivity().getString(
129                 R.string.weather_preferences_day_forecast_key);
130         if (key.equals(keyValue)) {
131             final Preference connectionPref = this.findPreference(key);
132             final String value = sharedPreferences.getString(key, "");
133             String humanValue = "";
134             if (value.equals("5")) {
135                 humanValue = "5-Day Forecast";
136             } else if (value.equals("10")) {
137                 humanValue = "10-Day Forecast";
138             } else if (value.equals("14")) {
139                 humanValue = "14-Day Forecast";
140             }
141             connectionPref.setSummary(humanValue);
142             return;
143         }
144
145     }
146
147     private void updateAlarm(final String updateTimeRate) {
148         long chosenInterval = 0;
149         if (updateTimeRate.equals("900")) {
150                 chosenInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
151         } else if (updateTimeRate.equals("1800")) {
152                 chosenInterval = AlarmManager.INTERVAL_HALF_HOUR;
153         } else if (updateTimeRate.equals("3600")) {
154                 chosenInterval = AlarmManager.INTERVAL_HOUR;
155         } else if (updateTimeRate.equals("43200")) {
156                 chosenInterval = AlarmManager.INTERVAL_HALF_DAY;
157         } else if (updateTimeRate.equals("86400")) {
158                 chosenInterval = AlarmManager.INTERVAL_DAY;
159         }
160
161         final AlarmManager alarmMgr =
162                         (AlarmManager) this.getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
163         // TODO: better use some string instead of .class? In case I change the service class
164         // this could be a problem (I guess)
165         final Intent serviceIntent =
166                         new Intent(this.getActivity().getApplicationContext(), NotificationIntentService.class);
167         final PendingIntent alarmIntent =
168                         PendingIntent.getService(
169                                         this.getActivity().getApplicationContext(),
170                                         0,
171                                         serviceIntent,
172                                         PendingIntent.FLAG_UPDATE_CURRENT);
173         if (chosenInterval != 0) {   
174             alarmMgr.setInexactRepeating(
175                         AlarmManager.ELAPSED_REALTIME,
176                         SystemClock.elapsedRealtime(),
177                         chosenInterval,
178                         alarmIntent);
179         } else {
180                 alarmMgr.cancel(alarmIntent);
181         }
182     }
183 }