]>
Commit | Line | Data |
---|---|---|
1 | package de.cwde.freeshisen; | |
2 | ||
3 | import android.app.Application; | |
4 | import android.content.SharedPreferences; | |
5 | import android.preference.PreferenceManager; | |
6 | import android.util.Log; | |
7 | ||
8 | public class ShisenSho extends Application { | |
9 | private static ShisenSho instance = null; | |
10 | private ShisenShoView view = null; | |
11 | public ShisenShoActivity activity = null; | |
12 | ||
13 | public Board board; | |
14 | public int[] boardSize=new int[2]; | |
15 | public int difficulty=1; // 1=Easy, 2=Hard | |
16 | public int size=3; // 1=Small, 2=Medium, 3=Big | |
17 | public String tilesetid = "classic"; | |
18 | public boolean gravity=true; | |
19 | public boolean timeCounter=true; | |
20 | ||
21 | public void newPlay() { | |
22 | loadOptions(); | |
23 | board = new Board(); | |
24 | board.buildRandomBoard(boardSize[0],boardSize[1],difficulty,gravity); | |
25 | } | |
26 | ||
27 | public void setSize(int s) { | |
28 | size = s; | |
29 | ||
30 | switch (s) { | |
31 | case 1: | |
32 | boardSize[0] = 6 + 2; | |
33 | boardSize[1] = 8 + 2; | |
34 | break; | |
35 | case 2: | |
36 | boardSize[0] = 6 + 2; | |
37 | boardSize[1] = 12 + 2; | |
38 | break; | |
39 | case 3: | |
40 | default: | |
41 | boardSize[0] = 6 + 2; | |
42 | boardSize[1] = 16 + 2; | |
43 | break; | |
44 | } | |
45 | } | |
46 | ||
47 | public void sleep(int deciSeconds) { | |
48 | try { | |
49 | Thread.sleep(deciSeconds*100); | |
50 | } catch (InterruptedException e) { } | |
51 | } | |
52 | ||
53 | public ShisenSho() { | |
54 | instance = this; | |
55 | setSize(size); | |
56 | } | |
57 | ||
58 | public static synchronized ShisenSho app() { | |
59 | return instance; | |
60 | } | |
61 | ||
62 | public ShisenShoView getView() { | |
63 | if (view == null) view = new ShisenShoView(this); | |
64 | return view; | |
65 | } | |
66 | ||
67 | /** Called when the activity is first created. */ | |
68 | @Override | |
69 | public void onCreate() { | |
70 | super.onCreate(); | |
71 | PreferenceManager.setDefaultValues(this, R.xml.preferences, false); | |
72 | Log.d("ShisenSho", "starting up..."); | |
73 | loadOptions(); | |
74 | } | |
75 | ||
76 | private void loadOptions() { | |
77 | SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); | |
78 | ||
79 | // FIXME: handle NumberFormatException here? | |
80 | setSize(Integer.parseInt(sp.getString("pref_size", "1"))); | |
81 | difficulty = Integer.parseInt(sp.getString("pref_diff", "1")); | |
82 | gravity = sp.getBoolean("pref_grav", true); | |
83 | timeCounter = sp.getBoolean("pref_time", true); | |
84 | tilesetid = sp.getString("pref_tile", ""); | |
85 | } | |
86 | ||
87 | public void checkForChangedOptions() { | |
88 | SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); | |
89 | ||
90 | // FIXME: handle NumberFormatException here? | |
91 | int size = Integer.parseInt(sp.getString("pref_size", "1")); | |
92 | int difficulty = Integer.parseInt(sp.getString("pref_diff", "1")); | |
93 | boolean gravity = sp.getBoolean("pref_grav", true); | |
94 | boolean timeCounter = sp.getBoolean("pref_time", true); | |
95 | String tilesetid = sp.getString("pref_tile", ""); | |
96 | ||
97 | boolean needsReset = false; | |
98 | ||
99 | if (size != this.size) { | |
100 | needsReset = true; | |
101 | } | |
102 | ||
103 | if (difficulty != this.difficulty) { | |
104 | needsReset = true; | |
105 | } | |
106 | ||
107 | if (gravity != this.gravity) { | |
108 | needsReset = true; | |
109 | } | |
110 | ||
111 | if (timeCounter != this.timeCounter) { | |
112 | needsReset = true; | |
113 | } | |
114 | ||
115 | if ((tilesetid != this.tilesetid) && (view != null)) { | |
116 | // tileset can be changed without a reset | |
117 | this.tilesetid = tilesetid; | |
118 | view.loadTileset(); | |
119 | } | |
120 | ||
121 | if (needsReset) { | |
122 | if ((view != null) && (activity != null)) { | |
123 | activity.onOptionsChanged(); | |
124 | } else { | |
125 | Log.d("ShisenSho", "Preferences changed, but no view or activity online - huh?"); | |
126 | } | |
127 | } | |
128 | ||
129 | } | |
130 | } |