d055b2fed36ee62b4e5415cba9ed6a44080ec581
[FreeShisen] / src / de / cwde / freeshisen / SettingsActivity.java
1 package de.cwde.freeshisen;
2
3 import android.content.SharedPreferences;
4 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
5 import android.content.res.Resources;
6 import android.os.Bundle;
7 import android.preference.*;
8
9 public class SettingsActivity extends PreferenceActivity
10 implements OnSharedPreferenceChangeListener {
11
12 private ShisenSho app;
13
14 private static final String KEY_PREF_DIFF = "pref_diff";
15 private static final String KEY_PREF_SIZE = "pref_size";
16 //private static final String KEY_PREF_GRAV = "pref_grav";
17 //private static final String KEY_PREF_TIME = "pref_time";
18
19 @SuppressWarnings("deprecation")
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 app = ShisenSho.app();
24 addPreferencesFromResource(R.xml.preferences);
25 SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
26
27 sharedPreferences.registerOnSharedPreferenceChangeListener(this);
28 updateSummary(sharedPreferences, KEY_PREF_DIFF, KEY_PREF_DIFF, R.array.difficulties);
29 updateSummary(sharedPreferences, KEY_PREF_SIZE, KEY_PREF_SIZE, R.array.sizes);
30 }
31
32 @Override
33 public void onBackPressed() {
34 app.setOptions();
35 super.onBackPressed();
36 }
37
38 @SuppressWarnings("deprecation")
39 @Override
40 protected void onResume() {
41 super.onResume();
42 getPreferenceScreen().getSharedPreferences()
43 .registerOnSharedPreferenceChangeListener(this);
44 }
45
46 @SuppressWarnings("deprecation")
47 @Override
48 protected void onPause() {
49 super.onPause();
50 getPreferenceScreen().getSharedPreferences()
51 .unregisterOnSharedPreferenceChangeListener(this);
52 }
53
54 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
55 updateSummary(sharedPreferences, key, KEY_PREF_DIFF, R.array.difficulties);
56 updateSummary(sharedPreferences, key, KEY_PREF_SIZE, R.array.sizes);
57 }
58
59 private void updateSummary(SharedPreferences sharedPreferences, String changedkey, String mykey, int myresource) {
60 if (changedkey.equals(mykey)) {
61 // FIXME: handle NumberFormatException here?
62 int i = Integer.parseInt(sharedPreferences.getString(changedkey, "1"));
63
64 Resources res = getResources();
65 String[] mystrings = res.getStringArray(myresource);
66 String name = mystrings[i-1];
67
68 @SuppressWarnings("deprecation")
69 Preference myPref = findPreference(changedkey);
70 myPref.setSummary("Currently: " + name);
71 }
72 }
73 }
Impressum, Datenschutz