Handle settings differently. Should fix issue #1.
authorgitknilch <gitknilch@cwde.de>
Fri, 5 Apr 2013 13:17:43 +0000 (15:17 +0200)
committergitknilch <gitknilch@cwde.de>
Fri, 5 Apr 2013 13:17:43 +0000 (15:17 +0200)
Change-Id: I93176b71d7050373a5b8e6f91fdaafecbd748cf5
Signed-off-by: gitknilch <gitknilch@cwde.de>
src/de/cwde/freeshisen/SettingsActivity.java
src/de/cwde/freeshisen/ShisenSho.java

index 643c6d3533c4c93da9dc0e649291af18f338bda2..26a63f9f435efde4bed8d31c2ad5f8c2c8d781eb 100644 (file)
@@ -33,7 +33,7 @@ implements OnSharedPreferenceChangeListener {
 
        @Override
        public void onBackPressed() {
-               app.setOptions();
+               app.checkForChangedOptions();
                super.onBackPressed();
        }
 
index f32fa7025cfd22412f2b4872a866753df60cc89f..a1ee0eb9b4fe73f70550730b9bc6dc2abc24d986 100644 (file)
@@ -1,6 +1,8 @@
 package de.cwde.freeshisen;
 
+import android.app.AlertDialog;
 import android.app.Application;
+import android.content.DialogInterface;
 import android.content.SharedPreferences;
 import android.preference.PreferenceManager;
 import android.util.Log;
@@ -18,32 +20,27 @@ public class ShisenSho extends Application {
        public boolean gravity=true;
        public boolean timeCounter=true;
 
-       public static void log(String msg) {
-               Log.w("ShisenSho", msg);
-       }
-
        public void newPlay() {
                board = new Board();
                board.buildRandomBoard(boardSize[0],boardSize[1],difficulty,gravity);
        }
 
        public void setSize(int s) {
+               size = s;
+
                switch (s) {
                case 1:
-                       size=1;
-                       boardSize[0]=6+2;
-                       boardSize[1]=8+2;
+                       boardSize[0] = 6 + 2;
+                       boardSize[1] = 8 + 2;
                        break;
                case 2:
-                       size=2;
-                       boardSize[0]=6+2;
-                       boardSize[1]=12+2;
+                       boardSize[0] = 6 + 2;
+                       boardSize[1] = 12 + 2;
                        break;
                case 3:
                default:
-                       size=3;
-                       boardSize[0]=6+2;
-                       boardSize[1]=16+2;
+                       boardSize[0] = 6 + 2;
+                       boardSize[1] = 16 + 2;
                        break;
                }
        }
@@ -73,48 +70,74 @@ public class ShisenSho extends Application {
        public void onCreate() {
                super.onCreate();
                PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
-               setOptions();
+               Log.d("ShisenSho", "starting up...");
+               loadOptions();
+       }
+
+       private void loadOptions() {
+               SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
+
+               // FIXME: handle NumberFormatException here?
+               setSize(Integer.parseInt(sp.getString("pref_size", "1")));
+               difficulty = Integer.parseInt(sp.getString("pref_diff", "1"));
+               gravity = sp.getBoolean("pref_grav", true);
+               timeCounter = sp.getBoolean("pref_time", true);
+               tilesetid = sp.getString("pref_tile", "");
        }
 
-       public void setOptions() {
-               SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
+       public void checkForChangedOptions() {
+               SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
 
                // FIXME: handle NumberFormatException here?
-               int size = Integer.parseInt(sharedPref.getString("pref_size", "1"));
-               int difficulty = Integer.parseInt(sharedPref.getString("pref_diff", "1"));
-               boolean gravity = sharedPref.getBoolean("pref_grav", true);
-               boolean timeCounter = sharedPref.getBoolean("pref_time", true);
-               String tilesetid = sharedPref.getString("pref_tile", "");
+               int size = Integer.parseInt(sp.getString("pref_size", "1"));
+               int difficulty = Integer.parseInt(sp.getString("pref_diff", "1"));
+               boolean gravity = sp.getBoolean("pref_grav", true);
+               boolean timeCounter = sp.getBoolean("pref_time", true);
+               String tilesetid = sp.getString("pref_tile", "");
 
                boolean needsReset = false;
 
                if (size != this.size) {
-                       setSize(size);
                        needsReset = true;
                }
 
                if (difficulty != this.difficulty) {
-                       this.difficulty = difficulty;
                        needsReset = true;
                }
 
                if (gravity != this.gravity) {
-                       this.gravity = gravity;
                        needsReset = true;
                }
 
-               if ((timeCounter != this.timeCounter) && (view != null)) {
-                       this.timeCounter = timeCounter;
-                       view.onTimeCounterActivate();
+               if (timeCounter != this.timeCounter) {
+                       needsReset = true;
                }
 
                if ((tilesetid != this.tilesetid) && (view != null)) {
+                       // tileset can be changed without a reset
                        this.tilesetid = tilesetid;
                        view.loadTileset();
                }
 
-               if (needsReset && (view != null)) {
-                       view.reset();
+               if (needsReset && (view != null) && (activity != null)) {
+                       new AlertDialog.Builder(this)
+                               .setTitle("Preferences changed!") // FIXME: hardcoded string
+                               .setCancelable(true)
+                               .setIcon(R.drawable.icon)
+                               .setPositiveButton(android.R.string.yes,
+                                       new DialogInterface.OnClickListener() {
+                                       public void onClick(DialogInterface dialog, int id) {
+                                               // User clicked OK button - reset game
+                                               ((ShisenSho) ((AlertDialog) dialog).getContext()).view.reset();
+                                       }
+                               })
+                               .setNegativeButton(android.R.string.no, null)
+                               .setMessage("Changes in Preferences will only have an effect if" +
+                                                       " a new game is started. Abort current game and start" +
+                                                       " a new one?").create() // FIXME: hardcoded string
+                               .show();
+               } else {
+                       Log.d("ShisenSho", "Preferences changed, but no view or activity online - huh?");
                }
 
        }
Impressum, Datenschutz