]> git.zerfleddert.de Git - FreeShisen/blame - src/de/cwde/freeshisen/ShisenSho.java
Handle settings differently. Should fix issue #1.
[FreeShisen] / src / de / cwde / freeshisen / ShisenSho.java
CommitLineData
92b19250 1package de.cwde.freeshisen;
c6f3dff3 2
03f986ee 3import android.app.AlertDialog;
c6f3dff3 4import android.app.Application;
03f986ee 5import android.content.DialogInterface;
42aa846a 6import android.content.SharedPreferences;
7import android.preference.PreferenceManager;
c6f3dff3 8import android.util.Log;
9
10public class ShisenSho extends Application {
11 private static ShisenSho instance = null;
12 private ShisenShoView view = null;
13 public ShisenShoActivity activity = null;
655c3517 14
c6f3dff3 15 public Board board;
16 public int[] boardSize=new int[2];
29a01301 17 public int difficulty=1; // 1=Easy, 2=Hard
c6f3dff3 18 public int size=3; // 1=Small, 2=Medium, 3=Big
e1331c9c 19 public String tilesetid = "classic";
c6f3dff3 20 public boolean gravity=true;
21 public boolean timeCounter=true;
22
c6f3dff3 23 public void newPlay() {
24 board = new Board();
25 board.buildRandomBoard(boardSize[0],boardSize[1],difficulty,gravity);
26 }
27
28 public void setSize(int s) {
03f986ee 29 size = s;
30
c6f3dff3 31 switch (s) {
32 case 1:
03f986ee 33 boardSize[0] = 6 + 2;
34 boardSize[1] = 8 + 2;
c6f3dff3 35 break;
36 case 2:
03f986ee 37 boardSize[0] = 6 + 2;
38 boardSize[1] = 12 + 2;
c6f3dff3 39 break;
40 case 3:
41 default:
03f986ee 42 boardSize[0] = 6 + 2;
43 boardSize[1] = 16 + 2;
c6f3dff3 44 break;
45 }
46 }
47
48 public void sleep(int deciSeconds) {
49 try {
50 Thread.sleep(deciSeconds*100);
51 } catch (InterruptedException e) { }
52 }
53
54 public ShisenSho() {
55 instance = this;
56 setSize(size);
57 }
655c3517 58
c6f3dff3 59 public static synchronized ShisenSho app() {
60 return instance;
61 }
655c3517 62
c6f3dff3 63 public ShisenShoView getView() {
64 if (view == null) view = new ShisenShoView(this);
65 return view;
66 }
655c3517 67
c6f3dff3 68 /** Called when the activity is first created. */
29a01301 69 @Override
70 public void onCreate() {
71 super.onCreate();
72 PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
03f986ee 73 Log.d("ShisenSho", "starting up...");
74 loadOptions();
75 }
76
77 private void loadOptions() {
78 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
79
80 // FIXME: handle NumberFormatException here?
81 setSize(Integer.parseInt(sp.getString("pref_size", "1")));
82 difficulty = Integer.parseInt(sp.getString("pref_diff", "1"));
83 gravity = sp.getBoolean("pref_grav", true);
84 timeCounter = sp.getBoolean("pref_time", true);
85 tilesetid = sp.getString("pref_tile", "");
29a01301 86 }
87
03f986ee 88 public void checkForChangedOptions() {
89 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
655c3517 90
9054c57f 91 // FIXME: handle NumberFormatException here?
03f986ee 92 int size = Integer.parseInt(sp.getString("pref_size", "1"));
93 int difficulty = Integer.parseInt(sp.getString("pref_diff", "1"));
94 boolean gravity = sp.getBoolean("pref_grav", true);
95 boolean timeCounter = sp.getBoolean("pref_time", true);
96 String tilesetid = sp.getString("pref_tile", "");
c6f3dff3 97
98 boolean needsReset = false;
655c3517 99
c6f3dff3 100 if (size != this.size) {
c6f3dff3 101 needsReset = true;
102 }
655c3517 103
c6f3dff3 104 if (difficulty != this.difficulty) {
c6f3dff3 105 needsReset = true;
106 }
107
108 if (gravity != this.gravity) {
c6f3dff3 109 needsReset = true;
110 }
655c3517 111
03f986ee 112 if (timeCounter != this.timeCounter) {
113 needsReset = true;
c6f3dff3 114 }
115
07fd4c9f 116 if ((tilesetid != this.tilesetid) && (view != null)) {
03f986ee 117 // tileset can be changed without a reset
07fd4c9f 118 this.tilesetid = tilesetid;
119 view.loadTileset();
120 }
121
03f986ee 122 if (needsReset && (view != null) && (activity != null)) {
123 new AlertDialog.Builder(this)
124 .setTitle("Preferences changed!") // FIXME: hardcoded string
125 .setCancelable(true)
126 .setIcon(R.drawable.icon)
127 .setPositiveButton(android.R.string.yes,
128 new DialogInterface.OnClickListener() {
129 public void onClick(DialogInterface dialog, int id) {
130 // User clicked OK button - reset game
131 ((ShisenSho) ((AlertDialog) dialog).getContext()).view.reset();
132 }
133 })
134 .setNegativeButton(android.R.string.no, null)
135 .setMessage("Changes in Preferences will only have an effect if" +
136 " a new game is started. Abort current game and start" +
137 " a new one?").create() // FIXME: hardcoded string
138 .show();
139 } else {
140 Log.d("ShisenSho", "Preferences changed, but no view or activity online - huh?");
c6f3dff3 141 }
07fd4c9f 142
c6f3dff3 143 }
c6f3dff3 144}
Impressum, Datenschutz