import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.content.res.Resources;
import android.os.Bundle;
import android.preference.*;
public class SettingsActivity extends PreferenceActivity
- implements OnSharedPreferenceChangeListener {
+ implements OnSharedPreferenceChangeListener {
private ShisenSho app;
@SuppressWarnings("deprecation")
@Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- app = ShisenSho.app();
- addPreferencesFromResource(R.xml.preferences);
- }
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ app = ShisenSho.app();
+ addPreferencesFromResource(R.xml.preferences);
+ SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
+
+ sharedPreferences.registerOnSharedPreferenceChangeListener(this);
+ updateSummary(sharedPreferences, KEY_PREF_DIFF, KEY_PREF_DIFF, R.array.difficulties);
+ updateSummary(sharedPreferences, KEY_PREF_SIZE, KEY_PREF_SIZE, R.array.sizes);
+ }
@Override
public void onBackPressed() {
@SuppressWarnings("deprecation")
@Override
protected void onResume() {
- super.onResume();
- getPreferenceScreen().getSharedPreferences()
- .registerOnSharedPreferenceChangeListener(this);
+ super.onResume();
+ getPreferenceScreen().getSharedPreferences()
+ .registerOnSharedPreferenceChangeListener(this);
}
@SuppressWarnings("deprecation")
@Override
protected void onPause() {
- super.onPause();
- getPreferenceScreen().getSharedPreferences()
- .unregisterOnSharedPreferenceChangeListener(this);
+ super.onPause();
+ getPreferenceScreen().getSharedPreferences()
+ .unregisterOnSharedPreferenceChangeListener(this);
}
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- if (key.equals(KEY_PREF_DIFF)) {
- @SuppressWarnings("deprecation")
- Preference myPref = findPreference(key);
- // Set summary to be the user-description for the selected value
- myPref.setSummary(sharedPreferences.getString(key, ""));
- }
- if (key.equals(KEY_PREF_SIZE)) {
- @SuppressWarnings("deprecation")
- Preference myPref = findPreference(key);
- // Set summary to be the user-description for the selected value
- myPref.setSummary(sharedPreferences.getString(key, ""));
- }
- }
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ updateSummary(sharedPreferences, key, KEY_PREF_DIFF, R.array.difficulties);
+ updateSummary(sharedPreferences, key, KEY_PREF_SIZE, R.array.sizes);
+ }
+
+ private void updateSummary(SharedPreferences sharedPreferences, String changedkey, String mykey, int myresource) {
+ if (changedkey.equals(mykey)) {
+ // FIXME: handle NumberFormatException here?
+ int i = Integer.parseInt(sharedPreferences.getString(changedkey, "1"));
+
+ Resources res = getResources();
+ String[] mystrings = res.getStringArray(myresource);
+ String name = mystrings[i-1];
+
+ @SuppressWarnings("deprecation")
+ Preference myPref = findPreference(changedkey);
+ myPref.setSummary("Currently: " + name);
+ }
+ }
}