mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-28 01:12:10 +01:00
102 lines
3.8 KiB
Java
102 lines
3.8 KiB
Java
|
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.util.Log;
|
||
|
import android.view.MenuItem;
|
||
|
|
||
|
public class AbstractSettingsActivity extends PreferenceActivity {
|
||
|
|
||
|
private static final String TAG = "AbstractSettingsAct";
|
||
|
|
||
|
@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 {
|
||
|
Log.e(TAG, "Unknown preference key: " + prefKey + ", unable to display value.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* A preference value change listener that updates the preference's summary
|
||
|
* to reflect its new value.
|
||
|
*/
|
||
|
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
|
||
|
@Override
|
||
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
||
|
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);
|
||
|
}
|
||
|
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 #sBindPreferenceSummaryToValueListener
|
||
|
*/
|
||
|
private static void bindPreferenceSummaryToValue(Preference preference) {
|
||
|
// Set the listener to watch for value changes.
|
||
|
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
|
||
|
|
||
|
// Trigger the listener immediately with the preference's
|
||
|
// current value.
|
||
|
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
|
||
|
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);
|
||
|
}
|
||
|
}
|