Canceled dialog bug fixed. Don't allow to cancel the dialog.

This commit is contained in:
2013-06-24 01:55:36 +04:00
parent 0b18acab40
commit 61d22af59b
2 changed files with 88 additions and 76 deletions

View File

@@ -10,7 +10,7 @@ public class App extends Application
{ {
public static final String TAG = "Sokoban"; public static final String TAG = "Sokoban";
public static final String PREFS = "org.dyndns.vahagn.sokoban.prefs"; public static final String PREFS = "org.dyndns.vahagn.sokoban.prefs";
static final int MIN_LEVEL = 1; public static final int MIN_LEVEL = 1;
private static App mApp = null; private static App mApp = null;
protected PuzzleContainer pc; protected PuzzleContainer pc;
@@ -33,7 +33,7 @@ public class App extends Application
prefsEdit = prefs.edit(); prefsEdit = prefs.edit();
max_level = pc.getCount(); max_level = pc.getCount();
current_level = prefs.getInt("current_level", MIN_LEVEL); current_level = prefs.getInt("current_level", MIN_LEVEL);
achieved_level = prefs.getInt("achieved_level", MIN_LEVEL); achieved_level = prefs.getInt("achieved_level", MIN_LEVEL+10);
} }
// //
// This is a singleton object. // This is a singleton object.
@@ -53,6 +53,20 @@ public class App extends Application
{ {
return getPuzzle( getCurrentLevel() ); return getPuzzle( getCurrentLevel() );
} }
public Puzzle getPrevPuzzle()
{
if ( current_level != MIN_LEVEL )
return getPuzzle( getCurrentLevel()-1 );
else
return null;
}
public Puzzle getNextPuzzle()
{
if ( current_level != achieved_level )
return getPuzzle( getCurrentLevel()+1 );
else
return null;
}
// //
// Provide amount of puzzles. // Provide amount of puzzles.
// //
@@ -75,11 +89,15 @@ public class App extends Application
return current_level; return current_level;
} }
// //
// Set the current puzzle level. It deons't alter // Set the current puzzle level.
// //
public void setCurrentLevel( int l ) public void setCurrentLevel( int l )
{ {
if ( current_level != l ) if ( l > achieved_level )
l = MIN_LEVEL;
if ( l < MIN_LEVEL )
l = achieved_level;
if ( l != current_level )
{ {
current_level = l; current_level = l;
prefsEdit.putInt("current_level", current_level); prefsEdit.putInt("current_level", current_level);
@@ -89,22 +107,19 @@ public class App extends Application
} }
} }
// //
// Advances current level. It also reviews achived level if the // Set achieved level.
// new level is bigger than achieved level.
// //
public void advanceCurrentLevel() public void advanceAchivedLevel( int l )
{ {
int new_level = current_level+1; if ( l > max_level )
if ( new_level == max_level ) l = max_level;
new_level = MIN_LEVEL; if ( l > achieved_level )
if ( new_level > achieved_level )
{ {
achieved_level = new_level; achieved_level = l;
prefsEdit.putInt("achieved_level", achieved_level); prefsEdit.putInt("achieved_level", achieved_level);
// Don't call apply() here since the call below prefsEdit.apply();
// to setCurrentLevel() will do that. if ( !prefs.edit().commit() )
Log.d(TAG, "prefs.edit().commit() failed.");
} }
setCurrentLevel(new_level);
} }
} }

View File

@@ -2,9 +2,12 @@ package org.dyndns.vahagn.sokoban.play;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
@@ -29,7 +32,7 @@ import org.dyndns.vahagn.sokoban.R;
import org.dyndns.vahagn.sokoban.Puzzle; import org.dyndns.vahagn.sokoban.Puzzle;
import org.dyndns.vahagn.sokoban.menu.MainActivity; import org.dyndns.vahagn.sokoban.menu.MainActivity;
public class PlayActivity extends Activity public class PlayActivity extends FragmentActivity
implements PuzzleControl.PuzzleControlLister implements PuzzleControl.PuzzleControlLister
{ {
protected final static int RESET_ITEM = 1; protected final static int RESET_ITEM = 1;
@@ -55,7 +58,6 @@ public class PlayActivity extends Activity
puzzle_view = new PuzzleControl( this ); puzzle_view = new PuzzleControl( this );
setContentView( puzzle_view ); setContentView( puzzle_view );
puzzle_view.setPuzzleControlLister( this ); puzzle_view.setPuzzleControlLister( this );
registerForContextMenu(puzzle_view);
// //
// Create action bar. // Create action bar.
// //
@@ -67,34 +69,6 @@ public class PlayActivity extends Activity
// Load the puzzle. // Load the puzzle.
// //
loadCurrentPuzzle(); loadCurrentPuzzle();
if (savedInstanceState == null)
{
// We were just launched -- set up a new game
// mSnakeView.setMode(SnakeView.READY);
}
else
{
// We are being restored
// Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
// if (map != null) {
// mSnakeView.restoreState(map);
// } else {
// mSnakeView.setMode(SnakeView.PAUSE);
// }
}
//
//
//
// super.onCreate(savedInstanceState);
// setContentView(R.layout.menu);
//
// try {
// theApp().getPuzzleContainer().getPuzzle( 0 );
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} }
public void onSolved() public void onSolved()
@@ -102,14 +76,20 @@ public class PlayActivity extends Activity
// //
// Advance current level and achieved level. // Advance current level and achieved level.
// //
theApp().advanceCurrentLevel(); final int nextl = theApp().getCurrentLevel()+1;
theApp().advanceAchivedLevel(nextl);
theApp().setCurrentLevel(nextl);
// //
// Bring a dialog for user to choose to continue or stop. // Bring a dialog for user to choose to continue or stop.
// //
AlertDialog.Builder builder = new AlertDialog.Builder(this); DialogFragment df = new DialogFragment()
builder.setTitle(""); {
builder.setMessage( "Congratulations! You Won!" ); public Dialog onCreateDialog(Bundle savedInstanceState)
builder.setNegativeButton("Menu", {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Congratulations! You Won!");
builder.setMessage( "Would you like to try the next puzzle?" );
builder.setNegativeButton("Enough",
new DialogInterface.OnClickListener() new DialogInterface.OnClickListener()
{ {
public void onClick(DialogInterface d, int w) public void onClick(DialogInterface d, int w)
@@ -117,7 +97,7 @@ public class PlayActivity extends Activity
finish(); finish();
} }
}); });
builder.setPositiveButton("Next", builder.setPositiveButton("Please",
new DialogInterface.OnClickListener() new DialogInterface.OnClickListener()
{ {
public void onClick(DialogInterface d, int w) public void onClick(DialogInterface d, int w)
@@ -126,8 +106,16 @@ public class PlayActivity extends Activity
puzzle_view.invalidate(); puzzle_view.invalidate();
} }
}); });
AlertDialog dlg = builder.create(); // Create the AlertDialog object and return it
dlg.show(); Dialog dlg = builder.create();
dlg.setCancelable(false);
dlg.setCanceledOnTouchOutside(false);
return dlg;
}
};
df.setCancelable(false);
df.show(getSupportFragmentManager(), null);
//dlg.show();
} }
public void onLongPress() public void onLongPress()
@@ -163,7 +151,9 @@ public class PlayActivity extends Activity
puzzle_view.invalidate(); puzzle_view.invalidate();
return true; return true;
} }
//
// Go to previouse puzzle.
//
public boolean onPrev( View v ) public boolean onPrev( View v )
{ {
// //
@@ -178,7 +168,9 @@ public class PlayActivity extends Activity
} }
return true; return true;
} }
//
// Go to next puzzle.
//
public boolean onNext( View v ) public boolean onNext( View v )
{ {
// //
@@ -190,10 +182,13 @@ public class PlayActivity extends Activity
theApp().setCurrentLevel( theApp().getCurrentLevel()+1 ); theApp().setCurrentLevel( theApp().getCurrentLevel()+1 );
loadCurrentPuzzle(); loadCurrentPuzzle();
puzzle_view.invalidate(); puzzle_view.invalidate();
} }
return true; return true;
} }
//
// Undo last action.
//
public boolean onUndo( View v ) public boolean onUndo( View v )
{ {
if ( puzzle.isUndoable() ) if ( puzzle.isUndoable() )
@@ -210,11 +205,13 @@ public class PlayActivity extends Activity
{ {
puzzle = theApp().getCurrentPuzzle(); puzzle = theApp().getCurrentPuzzle();
puzzle_view.setPuzzle(puzzle); puzzle_view.setPuzzle(puzzle);
updateTitle();
}
public void updateTitle()
{
String title = "Level " + new Integer(theApp().getCurrentLevel()).toString(); String title = "Level " + new Integer(theApp().getCurrentLevel()).toString();
setTitle(title); setTitle(title);
} }
public void setTitle(CharSequence title) public void setTitle(CharSequence title)
{ {
super.setTitle(title); super.setTitle(title);