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