OLD | NEW |
(Empty) | |
| 1 package org.adblockplus.android; |
| 2 |
| 3 import android.content.SharedPreferences; |
| 4 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
| 5 import android.preference.EditTextPreference; |
| 6 import android.preference.ListPreference; |
| 7 import android.preference.Preference; |
| 8 import android.preference.PreferenceActivity; |
| 9 import android.preference.PreferenceGroup; |
| 10 import android.preference.PreferenceScreen; |
| 11 |
| 12 /** |
| 13 * PreferencesActivity which automatically sets preference summaries according |
| 14 * to its current values. |
| 15 */ |
| 16 public class SummarizedPreferences extends PreferenceActivity implements OnShare
dPreferenceChangeListener |
| 17 { |
| 18 @Override |
| 19 public void onResume() |
| 20 { |
| 21 super.onResume(); |
| 22 initSummaries(getPreferenceScreen()); |
| 23 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChang
eListener(this); |
| 24 } |
| 25 |
| 26 @Override |
| 27 public void onPause() |
| 28 { |
| 29 super.onPause(); |
| 30 getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceCha
ngeListener(this); |
| 31 } |
| 32 |
| 33 @Override |
| 34 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str
ing key) |
| 35 { |
| 36 Preference pref = findPreference(key); |
| 37 setPrefSummary(pref); |
| 38 } |
| 39 |
| 40 protected void setPrefSummary(Preference pref) |
| 41 { |
| 42 if (pref instanceof ListPreference) |
| 43 { |
| 44 CharSequence summary = ((ListPreference) pref).getEntry(); |
| 45 if (summary != null) |
| 46 { |
| 47 pref.setSummary(summary); |
| 48 } |
| 49 } |
| 50 if (pref instanceof EditTextPreference) |
| 51 { |
| 52 CharSequence summary = ((EditTextPreference) pref).getText(); |
| 53 if (summary != null) |
| 54 { |
| 55 pref.setSummary(summary); |
| 56 } |
| 57 } |
| 58 } |
| 59 |
| 60 protected void initSummaries(PreferenceGroup preference) |
| 61 { |
| 62 for (int i = preference.getPreferenceCount() - 1; i >= 0; i--) |
| 63 { |
| 64 Preference pref = preference.getPreference(i); |
| 65 |
| 66 if (pref instanceof PreferenceGroup || pref instanceof PreferenceScreen) |
| 67 { |
| 68 initSummaries((PreferenceGroup) pref); |
| 69 } |
| 70 else |
| 71 { |
| 72 setPrefSummary(pref); |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 } |
OLD | NEW |