From f4497ce993ff4f8894de26c89665c0f999fa3ec1 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sun, 29 Mar 2015 21:39:34 +0200 Subject: [PATCH] Add many methods to make preference activity simpler --- .../activities/NrzSettingsActivity.java | 101 ++++++++++++++++-- 1 file changed, 90 insertions(+), 11 deletions(-) diff --git a/src/fr/nrz/androidlib/activities/NrzSettingsActivity.java b/src/fr/nrz/androidlib/activities/NrzSettingsActivity.java index 731685f..749b627 100644 --- a/src/fr/nrz/androidlib/activities/NrzSettingsActivity.java +++ b/src/fr/nrz/androidlib/activities/NrzSettingsActivity.java @@ -28,26 +28,31 @@ package fr.nrz.androidlib.activities; * either expressed or implied, of the FreeBSD Project. */ +import java.util.Vector; + +import android.annotation.TargetApi; import android.content.Context; import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; -public abstract class NrzSettingsActivity extends PreferenceActivity { +public class NrzSettingsActivity extends PreferenceActivity { private static final boolean ALWAYS_SIMPLE_PREFS = false; protected static Context _context; protected static int _prefsRessourceFile; - protected static Preference.OnPreferenceChangeListener sBindPreferenceListener; + protected static Vector _boolPrefs = new Vector(); + protected static Vector _stringPrefs = new Vector(); @Override protected void onPostCreate(final Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); _context = getBaseContext(); - setupSimplePreferencesScreen(); } @@ -68,8 +73,6 @@ public abstract class NrzSettingsActivity extends PreferenceActivity { bindPreferences(); } - protected abstract void bindPreferences(); - /** {@inheritDoc} */ @Override public boolean onIsMultiPane() { @@ -109,11 +112,11 @@ public abstract class NrzSettingsActivity extends PreferenceActivity { public static void bindPreferenceBooleanToValue(final Preference preference, final Boolean defValue) { // Set the listener to watch for value changes. preference - .setOnPreferenceChangeListener(sBindPreferenceListener); + .setOnPreferenceChangeListener(_bindPreferenceListener); // Trigger the listener immediately with the preference's // current value. - sBindPreferenceListener.onPreferenceChange( + _bindPreferenceListener.onPreferenceChange( preference, PreferenceManager.getDefaultSharedPreferences( preference.getContext()).getBoolean( @@ -123,20 +126,96 @@ public abstract class NrzSettingsActivity extends PreferenceActivity { ); } - public static void bindPreferenceStringToValue(final Preference preference) { + public static void bindPreferenceStringToValue(final Preference preference, final String defValue) { // Set the listener to watch for value changes. preference - .setOnPreferenceChangeListener(sBindPreferenceListener); + .setOnPreferenceChangeListener(_bindPreferenceListener); // Trigger the listener immediately with the preference's // current value. - sBindPreferenceListener.onPreferenceChange( + _bindPreferenceListener.onPreferenceChange( preference, PreferenceManager.getDefaultSharedPreferences( preference.getContext()).getString( preference.getKey(), - "" + defValue ) ); } + + /** + * This fragment shows data and sync preferences only. It is used when the + * activity is showing a two-pane settings UI. + */ + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static class DataSyncPreferenceFragment extends PreferenceFragment { + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(_prefsRessourceFile); + + // Bind the summaries of EditText/List/Dialog/Ringtone preferences + // to their values. When their values change, their summaries are + // updated to reflect the new value, per the Android Design + // guidelines. + for (int i=0; i < _stringPrefs.size(); i++) { + bindPreferenceStringToValue(findPreference(_stringPrefs.get(i).name), + (String) _stringPrefs.get(i).value); + } + for (int i=0; i < _boolPrefs.size(); i++) { + bindPreferenceBooleanToValue(findPreference(_boolPrefs.get(i).name), + (Boolean) _boolPrefs.get(i).value); + } + } + } + + private void bindPreferences() { + // Bind the summaries of EditText/List/Dialog/Ringtone preferences to + // their values. When their values change, their summaries are updated + // to reflect the new value, per the Android Design guidelines. + for (int i=0; i < _stringPrefs.size(); i++) { + bindPreferenceStringToValue(findPreference(_stringPrefs.get(i).name), + (String) _stringPrefs.get(i).value); + } + + for (int i=0; i < _boolPrefs.size(); i++) { + bindPreferenceBooleanToValue(findPreference(_boolPrefs.get(i).name), + (Boolean) _boolPrefs.get(i).value); + } + } + + // The preference object, it's only a key value pair + protected class BindObjectPref { + public String name; + public Object value; + public BindObjectPref(final String prefName, final Object prefVal) { + name = prefName; + value = prefVal; + } + } + + /** + * A preference value change listener that updates the preference's summary + * to reflect its new value. + */ + private static Preference.OnPreferenceChangeListener _bindPreferenceListener = new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(final Preference preference, final Object value) { + if (preference instanceof ListPreference) { + handleListPreference(preference.getKey(), value.toString(), (ListPreference) preference); + + } else if (preference instanceof CheckBoxPreference) { + handleCheckboxPreference(preference.getKey(), (Boolean)value); + } else { + // For all other preferences, set the summary to the value's + // simple string representation. + //preference.setSummary(boolValue); + } + return true; + } + }; + + protected static void handleCheckboxPreference(final String key, final Boolean value) {} + protected static void handleListPreference(final String key, final String value, + final ListPreference preference) {} }