Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/org/adblockplus/android/SummarizedPreferences.java

Issue 8493083: ABP/Android UI (Closed)
Patch Set: Created Oct. 5, 2012, 9:42 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/org/adblockplus/android/SummarizedPreferences.java
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/org/adblockplus/android/SummarizedPreferences.java
@@ -0,0 +1,78 @@
+package org.adblockplus.android;
+
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.preference.EditTextPreference;
+import android.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceGroup;
+import android.preference.PreferenceScreen;
+
+/**
+ * PreferencesActivity which automatically sets preference summaries according
+ * to its current values.
+ */
+public class SummarizedPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener
+{
+ @Override
+ public void onResume()
+ {
+ super.onResume();
+ // initialize list summaries
Felix Dahlke 2012/10/09 14:27:29 Why not call initSummaries() initListSummaries() t
Andrey Novikov 2012/10/12 13:19:14 The comment was wrong, I've coped it from my old c
+ initSummaries(getPreferenceScreen());
+ getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
+ }
+
+ @Override
+ public void onPause()
+ {
+ super.onPause();
+ getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
+ }
+
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
+ {
+ Preference pref = findPreference(key);
+ setPrefSummary(pref);
+ }
+
+ protected void setPrefSummary(Preference pref)
+ {
+ if (pref instanceof ListPreference)
+ {
+ CharSequence summary = ((ListPreference) pref).getEntry();
+ if (summary != null)
+ {
+ pref.setSummary(summary);
+ }
+ }
+ if (pref instanceof EditTextPreference)
+ {
+ CharSequence summary = ((EditTextPreference) pref).getText();
+ if (summary != null)
+ {
+ pref.setSummary(summary);
+ }
+ }
+ }
+
+ protected void initSummaries(PreferenceGroup preference)
+ {
+ for (int i = preference.getPreferenceCount() - 1; i >= 0; i--)
+ {
+ Preference pref = preference.getPreference(i);
+
+ if (pref instanceof PreferenceGroup || pref instanceof PreferenceScreen)
+ {
+ initSummaries((PreferenceGroup) pref);
+ }
+ else
+ {
+ setPrefSummary(pref);
+ }
+ }
+ }
+
+}

Powered by Google App Engine
This is Rietveld