643c6d3533c4c93da9dc0e649291af18f338bda2
[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_TILE = "pref_tile";
17 //private static final String KEY_PREF_GRAV = "pref_grav";
18 //private static final String KEY_PREF_TIME = "pref_time";
19
20 @SuppressWarnings("deprecation")
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 app = ShisenSho.app();
25 addPreferencesFromResource(R.xml.preferences);
26 SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
27
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);
31 updateTileSummary(sharedPreferences, KEY_PREF_TILE);
32 }
33
34 @Override
35 public void onBackPressed() {
36 app.setOptions();
37 super.onBackPressed();
38 }
39
40 @SuppressWarnings("deprecation")
41 @Override
42 protected void onResume() {
43 super.onResume();
44 getPreferenceScreen().getSharedPreferences()
45 .registerOnSharedPreferenceChangeListener(this);
46 }
47
48 @SuppressWarnings("deprecation")
49 @Override
50 protected void onPause() {
51 super.onPause();
52 getPreferenceScreen().getSharedPreferences()
53 .unregisterOnSharedPreferenceChangeListener(this);
54 }
55
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);
59 updateTileSummary(sharedPreferences, key);
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 }
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
87 }
Impressum, Datenschutz