mirror of
https://github.com/nerzhul/nrz-androidlib.git
synced 2025-06-08 00:16:12 +00:00
Add many methods to make preference activity simpler
This commit is contained in:
parent
d9f5dd4f5a
commit
f4497ce993
@ -28,26 +28,31 @@ package fr.nrz.androidlib.activities;
|
|||||||
* either expressed or implied, of the FreeBSD Project.
|
* either expressed or implied, of the FreeBSD Project.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.preference.CheckBoxPreference;
|
||||||
|
import android.preference.ListPreference;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.preference.PreferenceActivity;
|
import android.preference.PreferenceActivity;
|
||||||
import android.preference.PreferenceFragment;
|
import android.preference.PreferenceFragment;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
public abstract class NrzSettingsActivity extends PreferenceActivity {
|
public class NrzSettingsActivity extends PreferenceActivity {
|
||||||
private static final boolean ALWAYS_SIMPLE_PREFS = false;
|
private static final boolean ALWAYS_SIMPLE_PREFS = false;
|
||||||
protected static Context _context;
|
protected static Context _context;
|
||||||
protected static int _prefsRessourceFile;
|
protected static int _prefsRessourceFile;
|
||||||
protected static Preference.OnPreferenceChangeListener sBindPreferenceListener;
|
protected static Vector<BindObjectPref> _boolPrefs = new Vector<BindObjectPref>();
|
||||||
|
protected static Vector<BindObjectPref> _stringPrefs = new Vector<BindObjectPref>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostCreate(final Bundle savedInstanceState) {
|
protected void onPostCreate(final Bundle savedInstanceState) {
|
||||||
super.onPostCreate(savedInstanceState);
|
super.onPostCreate(savedInstanceState);
|
||||||
_context = getBaseContext();
|
_context = getBaseContext();
|
||||||
|
|
||||||
setupSimplePreferencesScreen();
|
setupSimplePreferencesScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,8 +73,6 @@ public abstract class NrzSettingsActivity extends PreferenceActivity {
|
|||||||
bindPreferences();
|
bindPreferences();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void bindPreferences();
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public boolean onIsMultiPane() {
|
public boolean onIsMultiPane() {
|
||||||
@ -109,11 +112,11 @@ public abstract class NrzSettingsActivity extends PreferenceActivity {
|
|||||||
public static void bindPreferenceBooleanToValue(final Preference preference, final Boolean defValue) {
|
public static void bindPreferenceBooleanToValue(final Preference preference, final Boolean defValue) {
|
||||||
// Set the listener to watch for value changes.
|
// Set the listener to watch for value changes.
|
||||||
preference
|
preference
|
||||||
.setOnPreferenceChangeListener(sBindPreferenceListener);
|
.setOnPreferenceChangeListener(_bindPreferenceListener);
|
||||||
|
|
||||||
// Trigger the listener immediately with the preference's
|
// Trigger the listener immediately with the preference's
|
||||||
// current value.
|
// current value.
|
||||||
sBindPreferenceListener.onPreferenceChange(
|
_bindPreferenceListener.onPreferenceChange(
|
||||||
preference,
|
preference,
|
||||||
PreferenceManager.getDefaultSharedPreferences(
|
PreferenceManager.getDefaultSharedPreferences(
|
||||||
preference.getContext()).getBoolean(
|
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.
|
// Set the listener to watch for value changes.
|
||||||
preference
|
preference
|
||||||
.setOnPreferenceChangeListener(sBindPreferenceListener);
|
.setOnPreferenceChangeListener(_bindPreferenceListener);
|
||||||
|
|
||||||
// Trigger the listener immediately with the preference's
|
// Trigger the listener immediately with the preference's
|
||||||
// current value.
|
// current value.
|
||||||
sBindPreferenceListener.onPreferenceChange(
|
_bindPreferenceListener.onPreferenceChange(
|
||||||
preference,
|
preference,
|
||||||
PreferenceManager.getDefaultSharedPreferences(
|
PreferenceManager.getDefaultSharedPreferences(
|
||||||
preference.getContext()).getString(
|
preference.getContext()).getString(
|
||||||
preference.getKey(),
|
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) {}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user