]>
Commit | Line | Data |
---|---|---|
1 | package de.cwde.freeshisen; | |
2 | ||
3 | import android.os.Bundle; | |
4 | import android.preference.PreferenceManager; | |
5 | import android.view.View; | |
6 | import android.widget.TextView; | |
7 | import android.app.Activity; | |
8 | import android.app.AlertDialog; | |
9 | import android.content.DialogInterface; | |
10 | import android.content.SharedPreferences; | |
11 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; | |
12 | ||
13 | public class HighscoreActivity extends Activity implements | |
14 | OnSharedPreferenceChangeListener { | |
15 | ||
16 | @Override | |
17 | protected void onCreate(Bundle savedInstanceState) { | |
18 | super.onCreate(savedInstanceState); | |
19 | setContentView(R.layout.highscore); | |
20 | updateTextViews(); | |
21 | ||
22 | PreferenceManager.getDefaultSharedPreferences(this) | |
23 | .registerOnSharedPreferenceChangeListener(this); | |
24 | } | |
25 | ||
26 | @Override | |
27 | protected void onResume() { | |
28 | super.onResume(); | |
29 | PreferenceManager.getDefaultSharedPreferences(this) | |
30 | .registerOnSharedPreferenceChangeListener(this); | |
31 | } | |
32 | ||
33 | @Override | |
34 | protected void onPause() { | |
35 | super.onPause(); | |
36 | PreferenceManager.getDefaultSharedPreferences(this) | |
37 | .unregisterOnSharedPreferenceChangeListener(this); | |
38 | } | |
39 | ||
40 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, | |
41 | String key) { | |
42 | updateTextViews(); | |
43 | } | |
44 | ||
45 | private void updateTextViews() { | |
46 | // argh... | |
47 | SharedPreferences sp = PreferenceManager | |
48 | .getDefaultSharedPreferences(this); | |
49 | fillTextView(sp, R.id.textViewHL1, "hiscore_HL1"); | |
50 | fillTextView(sp, R.id.textViewHL2, "hiscore_HL2"); | |
51 | fillTextView(sp, R.id.textViewHM1, "hiscore_HM1"); | |
52 | fillTextView(sp, R.id.textViewHM2, "hiscore_HM2"); | |
53 | fillTextView(sp, R.id.textViewHS1, "hiscore_HS1"); | |
54 | fillTextView(sp, R.id.textViewHS2, "hiscore_HS2"); | |
55 | fillTextView(sp, R.id.textViewEL1, "hiscore_EL1"); | |
56 | fillTextView(sp, R.id.textViewEL2, "hiscore_EL2"); | |
57 | fillTextView(sp, R.id.textViewEM1, "hiscore_EM1"); | |
58 | fillTextView(sp, R.id.textViewEM2, "hiscore_EM2"); | |
59 | fillTextView(sp, R.id.textViewES1, "hiscore_ES1"); | |
60 | fillTextView(sp, R.id.textViewES2, "hiscore_ES2"); | |
61 | } | |
62 | ||
63 | private void fillTextView(SharedPreferences sp, int id, String key) { | |
64 | TextView tv = (TextView) findViewById(id); | |
65 | tv.setText(sp.getString(key, "9:99:99")); | |
66 | } | |
67 | ||
68 | public void clearHiscore(View view) { | |
69 | AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
70 | ||
71 | builder.setMessage(R.string.clearhiscore_confirm_text); | |
72 | builder.setTitle(R.string.clearhiscore_confirm_title); | |
73 | ||
74 | builder.setPositiveButton(android.R.string.ok, | |
75 | new DialogInterface.OnClickListener() { | |
76 | public void onClick(DialogInterface dialog, int id) { | |
77 | // User clicked OK button - delete hiscores | |
78 | SharedPreferences sp = PreferenceManager | |
79 | .getDefaultSharedPreferences(((AlertDialog) dialog) | |
80 | .getContext()); | |
81 | SharedPreferences.Editor editor = sp.edit(); | |
82 | editor.remove("hiscore_HL1"); | |
83 | editor.remove("hiscore_HL2"); | |
84 | editor.remove("hiscore_HM1"); | |
85 | editor.remove("hiscore_HM2"); | |
86 | editor.remove("hiscore_HS1"); | |
87 | editor.remove("hiscore_HS2"); | |
88 | editor.remove("hiscore_EL1"); | |
89 | editor.remove("hiscore_EL2"); | |
90 | editor.remove("hiscore_EM1"); | |
91 | editor.remove("hiscore_EM2"); | |
92 | editor.remove("hiscore_ES1"); | |
93 | editor.remove("hiscore_ES2"); | |
94 | editor.commit(); | |
95 | } | |
96 | }); | |
97 | builder.setNegativeButton(android.R.string.cancel, null); | |
98 | ||
99 | AlertDialog dialog = builder.create(); | |
100 | dialog.show(); | |
101 | updateTextViews(); | |
102 | } | |
103 | } |