From 03f986eeb7aa5f6f8ca2e6856d98046993969cb5 Mon Sep 17 00:00:00 2001 From: gitknilch Date: Fri, 5 Apr 2013 15:17:43 +0200 Subject: [PATCH] Handle settings differently. Should fix issue #1. Change-Id: I93176b71d7050373a5b8e6f91fdaafecbd748cf5 Signed-off-by: gitknilch --- src/de/cwde/freeshisen/SettingsActivity.java | 2 +- src/de/cwde/freeshisen/ShisenSho.java | 81 +++++++++++++------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/de/cwde/freeshisen/SettingsActivity.java b/src/de/cwde/freeshisen/SettingsActivity.java index 643c6d3..26a63f9 100644 --- a/src/de/cwde/freeshisen/SettingsActivity.java +++ b/src/de/cwde/freeshisen/SettingsActivity.java @@ -33,7 +33,7 @@ implements OnSharedPreferenceChangeListener { @Override public void onBackPressed() { - app.setOptions(); + app.checkForChangedOptions(); super.onBackPressed(); } diff --git a/src/de/cwde/freeshisen/ShisenSho.java b/src/de/cwde/freeshisen/ShisenSho.java index f32fa70..a1ee0eb 100644 --- a/src/de/cwde/freeshisen/ShisenSho.java +++ b/src/de/cwde/freeshisen/ShisenSho.java @@ -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?"); } } -- 2.39.2