package de.devland.masterpassword.ui.preferences;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
/**
* Created by David Kunzler on 26.11.2014.
*/
public abstract class BaseSettingsFragment extends PreferenceFragment {
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
protected Preference.OnPreferenceChangeListener bindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value != null ? value.toString() : "";
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #bindPreferenceSummaryToValueListener
*/
protected void bindPreferenceSummaryToValue(Preference preference) {
if (preference != null) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(bindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
bindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getAll().get(preference.getKey()));
}
}
public AppCompatActivity getAppCompatActivity() {
return (AppCompatActivity) getActivity();
}
}