1 package uk
.me
.njae
.sunshine
;
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
;
9 import uk
.me
.njae
.sunshine
.data
.WeatherContract
;
12 * A {@link PreferenceActivity} that presents a set of application settings.
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.
19 public class SettingsActivity
extends PreferenceActivity
20 implements Preference
.OnPreferenceChangeListener
{
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
;
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
);
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
)));
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
44 private void bindPreferenceSummaryToValue(Preference preference
) {
45 mBindingPreference
= true;
47 // Set the listener to watch for value changes.
48 preference
.setOnPreferenceChangeListener(this);
50 // Trigger the listener immediately with the preference's
52 onPreferenceChange(preference
,
54 .getDefaultSharedPreferences(preference
.getContext())
55 .getString(preference
.getKey(), ""));
57 mBindingPreference
= false;
61 public boolean onPreferenceChange(Preference preference
, Object value
) {
62 String stringValue
= value
.toString().trim();
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
);
71 // notify code that weather may be impacted
72 getContentResolver().notifyChange(WeatherContract
.WeatherEntry
.CONTENT_URI
, null);
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
);
82 preference
.setSummary(listPreference
.getEntries()[prefIndex
]);
85 // For other preferences, set the summary to the value's simple string representation.
86 preference
.setSummary(stringValue
);