1 package de
.cwde
.freeshisen
;
3 import android
.app
.AlertDialog
;
4 import android
.app
.Application
;
5 import android
.content
.DialogInterface
;
6 import android
.content
.SharedPreferences
;
7 import android
.preference
.PreferenceManager
;
8 import android
.util
.Log
;
10 public class ShisenSho
extends Application
{
11 private static ShisenSho instance
= null;
12 private ShisenShoView view
= null;
13 public ShisenShoActivity activity
= null;
16 public int[] boardSize
=new int[2];
17 public int difficulty
=1; // 1=Easy, 2=Hard
18 public int size
=3; // 1=Small, 2=Medium, 3=Big
19 public String tilesetid
= "classic";
20 public boolean gravity
=true;
21 public boolean timeCounter
=true;
23 public void newPlay() {
25 board
.buildRandomBoard(boardSize
[0],boardSize
[1],difficulty
,gravity
);
28 public void setSize(int s
) {
38 boardSize
[1] = 12 + 2;
43 boardSize
[1] = 16 + 2;
48 public void sleep(int deciSeconds
) {
50 Thread
.sleep(deciSeconds
*100);
51 } catch (InterruptedException e
) { }
59 public static synchronized ShisenSho
app() {
63 public ShisenShoView
getView() {
64 if (view
== null) view
= new ShisenShoView(this);
68 /** Called when the activity is first created. */
70 public void onCreate() {
72 PreferenceManager
.setDefaultValues(this, R
.xml
.preferences
, false);
73 Log
.d("ShisenSho", "starting up...");
77 private void loadOptions() {
78 SharedPreferences sp
= PreferenceManager
.getDefaultSharedPreferences(this);
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", "");
88 public void checkForChangedOptions() {
89 SharedPreferences sp
= PreferenceManager
.getDefaultSharedPreferences(this);
91 // FIXME: handle NumberFormatException here?
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", "");
98 boolean needsReset
= false;
100 if (size
!= this.size
) {
104 if (difficulty
!= this.difficulty
) {
108 if (gravity
!= this.gravity
) {
112 if (timeCounter
!= this.timeCounter
) {
116 if ((tilesetid
!= this.tilesetid
) && (view
!= null)) {
117 // tileset can be changed without a reset
118 this.tilesetid
= tilesetid
;
122 if (needsReset
&& (view
!= null) && (activity
!= null)) {
123 new AlertDialog
.Builder(this)
124 .setTitle("Preferences changed!") // FIXME: hardcoded string
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();
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
140 Log
.d("ShisenSho", "Preferences changed, but no view or activity online - huh?");