c4a757328650600c81dd6c3f5529ea1ed8f55ed2
[JavaForFun] /
1 package de.example.exampletdd.fragment.preferences;
2
3 import android.content.SharedPreferences;
4 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
5 import android.os.Bundle;
6 import android.preference.Preference;
7 import android.preference.PreferenceFragment;
8 import de.example.exampletdd.R;
9
10 public class WeatherInformationPreferencesFragment extends PreferenceFragment
11 implements OnSharedPreferenceChangeListener {
12
13     @Override
14     public void onCreate(final Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16
17         // Load the preferences from an XML resource
18         this.addPreferencesFromResource(R.xml.weather_preferences);
19
20         String keyPreference = this.getActivity().getString(
21                 R.string.weather_preferences_units_key);
22         Preference connectionPref = this.findPreference(keyPreference);
23         connectionPref.setSummary(this.getPreferenceManager()
24                 .getSharedPreferences().getString(keyPreference, ""));
25
26         keyPreference = this.getActivity().getString(
27                 R.string.weather_preferences_update_time_rate_key);
28         connectionPref = this.findPreference(keyPreference);
29         String value = this.getPreferenceManager().getSharedPreferences()
30                 .getString(keyPreference, "");
31         String humanValue = "";
32         if (value.equals("60")) {
33             humanValue = "1 minute";
34         } else if (value.equals("120")) {
35             humanValue = "2 minutes";
36         } else if (value.equals("300")) {
37             humanValue = "5 minutes";
38         } else if (value.equals("600")) {
39             humanValue = "10 minutes";
40         } else if (value.equals("900")) {
41             humanValue = "15 minutes";
42         }
43         connectionPref.setSummary(humanValue);
44
45         keyPreference = this.getActivity().getString(
46                 R.string.weather_preferences_day_forecast_key);
47         connectionPref = this.findPreference(keyPreference);
48         value = this.getPreferenceManager().getSharedPreferences().getString(keyPreference, "");
49         humanValue = "";
50         if (value.equals("5")) {
51             humanValue = "5-Day Forecast";
52         } else if (value.equals("10")) {
53             humanValue = "10-Day Forecast";
54         } else if (value.equals("14")) {
55             humanValue = "14-Day Forecast";
56         }
57         connectionPref.setSummary(humanValue);
58     }
59
60     @Override
61     public void onResume() {
62         super.onResume();
63         this.getPreferenceManager().getSharedPreferences()
64         .registerOnSharedPreferenceChangeListener(this);
65
66     }
67
68     @Override
69     public void onPause() {
70         super.onPause();
71         this.getPreferenceManager().getSharedPreferences()
72         .unregisterOnSharedPreferenceChangeListener(this);
73     }
74
75     @Override
76     public void onSharedPreferenceChanged(
77             final SharedPreferences sharedPreferences, final String key) {
78         String keyValue = this.getActivity().getString(
79                 R.string.weather_preferences_units_key);
80
81         if (key.equals(keyValue)) {
82             final Preference connectionPref = this.findPreference(key);
83             connectionPref.setSummary(sharedPreferences.getString(key, ""));
84             return;
85         }
86
87         keyValue = this.getActivity().getString(R.string.weather_preferences_update_time_rate_key);
88         if (key.equals(keyValue)) {
89             final Preference connectionPref = this.findPreference(key);
90             final String value = sharedPreferences.getString(key, "");
91             String humanValue = "";
92             if (value.equals("60")) {
93                 humanValue = "1 minute";
94             } else if (value.equals("120")) {
95                 humanValue = "2 minutes";
96             } else if (value.equals("300")) {
97                 humanValue = "5 minutes";
98             } else if (value.equals("600")) {
99                 humanValue = "10 minutes";
100             } else if (value.equals("900")) {
101                 humanValue = "15 minutes";
102             }
103             connectionPref.setSummary(humanValue);
104             return;
105         }
106
107         keyValue = this.getActivity().getString(
108                 R.string.weather_preferences_day_forecast_key);
109         if (key.equals(keyValue)) {
110             final Preference connectionPref = this.findPreference(key);
111             final String value = sharedPreferences.getString(key, "");
112             String humanValue = "";
113             if (value.equals("5")) {
114                 humanValue = "5-Day Forecast";
115             } else if (value.equals("10")) {
116                 humanValue = "10-Day Forecast";
117             } else if (value.equals("14")) {
118                 humanValue = "14-Day Forecast";
119             }
120             connectionPref.setSummary(humanValue);
121             return;
122         }
123
124     }
125
126 }