2015-05-07 01:30:40 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.ListPreference;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceActivity;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.support.v4.app.NavUtils;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
/**
|
|
|
|
* A settings activity with support for preferences directly displaying their value.
|
|
|
|
* If you combine such preferences with a custom OnPreferenceChangeListener, you have
|
|
|
|
* to set that listener in #onCreate, *not* in #onPostCreate, otherwise the value will
|
|
|
|
* not be displayed.
|
|
|
|
*/
|
2015-05-07 01:30:40 +02:00
|
|
|
public class AbstractSettingsActivity extends PreferenceActivity {
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AbstractSettingsActivity.class);
|
2015-05-07 01:30:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A preference value change listener that updates the preference's summary
|
|
|
|
* to reflect its new value.
|
|
|
|
*/
|
2015-05-10 21:12:10 +02:00
|
|
|
private static class SimpleSetSummaryOnChangeListener implements Preference.OnPreferenceChangeListener {
|
2015-05-07 01:30:40 +02:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
2015-05-10 21:12:10 +02:00
|
|
|
updateSummary(preference, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateSummary(Preference preference, Object value) {
|
2015-05-07 01:30:40 +02:00
|
|
|
String stringValue = 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
private static class ExtraSetSummaryOnChangeListener extends SimpleSetSummaryOnChangeListener {
|
|
|
|
private Preference.OnPreferenceChangeListener delegate;
|
|
|
|
|
|
|
|
public ExtraSetSummaryOnChangeListener(Preference.OnPreferenceChangeListener delegate) {
|
|
|
|
this.delegate = delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
|
|
|
boolean result = delegate.onPreferenceChange(preference, value);
|
|
|
|
if (result) {
|
|
|
|
return super.onPreferenceChange(preference, value);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static SimpleSetSummaryOnChangeListener sBindPreferenceSummaryToValueListener = new SimpleSetSummaryOnChangeListener();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostCreate(Bundle savedInstanceState) {
|
|
|
|
super.onPostCreate(savedInstanceState);
|
|
|
|
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
for (String prefKey : getPreferenceKeysWithSummary()) {
|
|
|
|
final Preference pref = findPreference(prefKey);
|
|
|
|
if (pref != null) {
|
|
|
|
bindPreferenceSummaryToValue(pref);
|
|
|
|
} else {
|
2015-05-12 06:28:11 +02:00
|
|
|
LOG.error("Unknown preference key: " + prefKey + ", unable to display value.");
|
2015-05-10 21:12:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclasses should reimplement this to return the keys of those
|
|
|
|
* preferences which should print its values as a summary below the
|
|
|
|
* preference name.
|
|
|
|
*/
|
|
|
|
protected String[] getPreferenceKeysWithSummary() {
|
|
|
|
return new String[0];
|
|
|
|
}
|
|
|
|
|
2015-05-07 01:30:40 +02:00
|
|
|
/**
|
|
|
|
* 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 #sBindPreferenceSummaryToValueListener
|
|
|
|
*/
|
|
|
|
private static void bindPreferenceSummaryToValue(Preference preference) {
|
|
|
|
// Set the listener to watch for value changes.
|
2015-05-10 21:12:10 +02:00
|
|
|
SimpleSetSummaryOnChangeListener listener = null;
|
|
|
|
Preference.OnPreferenceChangeListener existingListener = preference.getOnPreferenceChangeListener();
|
|
|
|
if (existingListener != null) {
|
|
|
|
listener = new ExtraSetSummaryOnChangeListener(existingListener);
|
|
|
|
} else {
|
|
|
|
listener = sBindPreferenceSummaryToValueListener;
|
|
|
|
}
|
|
|
|
preference.setOnPreferenceChangeListener(listener);
|
2015-05-07 01:30:40 +02:00
|
|
|
|
2015-05-10 21:12:10 +02:00
|
|
|
// Trigger the listener immediately with the preference's current value.
|
|
|
|
listener.updateSummary(preference,
|
2015-05-07 01:30:40 +02:00
|
|
|
PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(preference.getContext())
|
|
|
|
.getString(preference.getKey(), ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|