Finished lesson 4
[Sunshine.git] / app / src / main / java / uk / me / njae / sunshine / SettingsActivity.java
1 package uk.me.njae.sunshine;
2
3 import android.os.Bundle;
4 import android.preference.ListPreference;
5 import android.preference.Preference;
6 import android.preference.PreferenceActivity;
7 import android.preference.PreferenceManager;
8
9 import uk.me.njae.sunshine.data.WeatherContract;
10
11 /**
12 * A {@link PreferenceActivity} that presents a set of application settings.
13 * <p>
14 * See <a href="http://developer.android.com/design/patterns/settings.html">
15 * Android Design: Settings</a> for design guidelines and the <a
16 * href="http://developer.android.com/guide/topics/ui/settings.html">Settings
17 * API Guide</a> for more information on developing a Settings UI.
18 */
19 public class SettingsActivity extends PreferenceActivity
20 implements Preference.OnPreferenceChangeListener {
21
22 // since we use the preference change initially to populate the summary
23 // field, we'll ignore that change at start of the activity
24 boolean mBindingPreference;
25
26 @Override
27 public void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 // Add 'general' preferences, defined in the XML file
30 addPreferencesFromResource(R.xml.pref_general);
31
32 // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
33 // updated when the preference changes.
34 bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
35 bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
36
37 }
38
39 /**
40 * Attaches a listener so the summary is always updated with the preference value.
41 * Also fires the listener once, to initialize the summary (so it shows up before the value
42 * is changed.)
43 */
44 private void bindPreferenceSummaryToValue(Preference preference) {
45 mBindingPreference = true;
46
47 // Set the listener to watch for value changes.
48 preference.setOnPreferenceChangeListener(this);
49
50 // Trigger the listener immediately with the preference's
51 // current value.
52 onPreferenceChange(preference,
53 PreferenceManager
54 .getDefaultSharedPreferences(preference.getContext())
55 .getString(preference.getKey(), ""));
56
57 mBindingPreference = false;
58 }
59
60 @Override
61 public boolean onPreferenceChange(Preference preference, Object value) {
62 String stringValue = value.toString().trim();
63
64 // are we starting the preference activity?
65 if ( !mBindingPreference ) {
66 if (preference.getKey().equals(getString(R.string.pref_location_key))) {
67 FetchWeatherTask weatherTask = new FetchWeatherTask(this);
68 String location = value.toString();
69 weatherTask.execute(location);
70 } else {
71 // notify code that weather may be impacted
72 getContentResolver().notifyChange(WeatherContract.WeatherEntry.CONTENT_URI, null);
73 }
74 }
75
76 if (preference instanceof ListPreference) {
77 // For list preferences, look up the correct display value in
78 // the preference's 'entries' list (since they have separate labels/values).
79 ListPreference listPreference = (ListPreference) preference;
80 int prefIndex = listPreference.findIndexOfValue(stringValue);
81 if (prefIndex >= 0) {
82 preference.setSummary(listPreference.getEntries()[prefIndex]);
83 }
84 } else {
85 // For other preferences, set the summary to the value's simple string representation.
86 preference.setSummary(stringValue);
87 }
88 return true;
89 }
90
91 }