Implement functionality of 'import' button in CreateSudokuActivity
This commit is contained in:
parent
1bf2c9ec7f
commit
07552a1089
2 changed files with 68 additions and 6 deletions
|
@ -42,6 +42,7 @@ import org.secuso.privacyfriendlysudoku.controller.qqwing.QQWing;
|
||||||
import org.secuso.privacyfriendlysudoku.game.GameDifficulty;
|
import org.secuso.privacyfriendlysudoku.game.GameDifficulty;
|
||||||
import org.secuso.privacyfriendlysudoku.game.GameType;
|
import org.secuso.privacyfriendlysudoku.game.GameType;
|
||||||
import org.secuso.privacyfriendlysudoku.ui.listener.IFinalizeDialogFragmentListener;
|
import org.secuso.privacyfriendlysudoku.ui.listener.IFinalizeDialogFragmentListener;
|
||||||
|
import org.secuso.privacyfriendlysudoku.ui.listener.IImportDialogFragmentListener;
|
||||||
import org.secuso.privacyfriendlysudoku.ui.view.CreateSudokuSpecialButtonLayout;
|
import org.secuso.privacyfriendlysudoku.ui.view.CreateSudokuSpecialButtonLayout;
|
||||||
import org.secuso.privacyfriendlysudoku.ui.view.R;
|
import org.secuso.privacyfriendlysudoku.ui.view.R;
|
||||||
import org.secuso.privacyfriendlysudoku.ui.view.SudokuFieldLayout;
|
import org.secuso.privacyfriendlysudoku.ui.view.SudokuFieldLayout;
|
||||||
|
@ -52,9 +53,10 @@ import org.secuso.privacyfriendlysudoku.ui.view.SudokuKeyboardLayout;
|
||||||
* IFinalizeDialogFragementListener. It is used to create custom sudokus, which are passed to the
|
* IFinalizeDialogFragementListener. It is used to create custom sudokus, which are passed to the
|
||||||
* GameActivity afterwards.
|
* GameActivity afterwards.
|
||||||
*/
|
*/
|
||||||
public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialogFragmentListener {
|
public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialogFragmentListener, IImportDialogFragmentListener {
|
||||||
|
|
||||||
GameController gameController;
|
GameController gameController;
|
||||||
|
SharedPreferences sharedPref;
|
||||||
SudokuFieldLayout layout;
|
SudokuFieldLayout layout;
|
||||||
SudokuKeyboardLayout keyboard;
|
SudokuKeyboardLayout keyboard;
|
||||||
TextView viewName ;
|
TextView viewName ;
|
||||||
|
@ -63,7 +65,7 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
|
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
|
|
||||||
if(sharedPref.getBoolean("pref_keep_screen_on", true)) {
|
if(sharedPref.getBoolean("pref_keep_screen_on", true)) {
|
||||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||||
|
@ -80,6 +82,12 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
|
||||||
gameType, new int [boardSize], new int [boardSize], new boolean [boardSize][sectionSize]);
|
gameType, new int [boardSize], new int [boardSize], new boolean [boardSize][sectionSize]);
|
||||||
gameController.loadLevel(container);
|
gameController.loadLevel(container);
|
||||||
|
|
||||||
|
setUpLayout();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setUpLayout() {
|
||||||
|
|
||||||
setContentView(R.layout.activity_create_sudoku);
|
setContentView(R.layout.activity_create_sudoku);
|
||||||
layout = (SudokuFieldLayout)findViewById(R.id.sudokuLayout);
|
layout = (SudokuFieldLayout)findViewById(R.id.sudokuLayout);
|
||||||
layout.setSettingsAndGame(sharedPref, gameController);
|
layout.setSettingsAndGame(sharedPref, gameController);
|
||||||
|
@ -96,13 +104,13 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
|
||||||
keyboard.setKeyBoard(gameController.getSize(), p.x,layout.getHeight()-p.y, orientation);
|
keyboard.setKeyBoard(gameController.getSize(), p.x,layout.getHeight()-p.y, orientation);
|
||||||
|
|
||||||
specialButtonLayout = (CreateSudokuSpecialButtonLayout) findViewById(R.id.createSudokuLayout);
|
specialButtonLayout = (CreateSudokuSpecialButtonLayout) findViewById(R.id.createSudokuLayout);
|
||||||
specialButtonLayout.setButtons(p.x, gameController, keyboard, getFragmentManager(), orientation, CreateSudokuActivity.this, this);
|
specialButtonLayout.setButtons(p.x, gameController, keyboard, getFragmentManager(), orientation,
|
||||||
|
CreateSudokuActivity.this, this, this);
|
||||||
|
|
||||||
viewName = (TextView) findViewById(R.id.gameModeText);
|
viewName = (TextView) findViewById(R.id.gameModeText);
|
||||||
viewName.setText(getString(gameController.getGameType().getStringResID()));
|
viewName.setText(getString(gameController.getGameType().getStringResID()));
|
||||||
|
|
||||||
gameController.notifyHighlightChangedListeners();
|
gameController.notifyHighlightChangedListeners();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -186,6 +194,53 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public void onImportDialogPositiveClick(String input) {
|
||||||
|
String inputSudoku;
|
||||||
|
|
||||||
|
// a valid input needs to contain exactly one of these prefixes
|
||||||
|
String prefix1 = GameActivity.URL_SCHEME_WITHOUT_HOST + "://";
|
||||||
|
String prefix2 = GameActivity.URL_SCHEME_WITH_HOST + "://" + GameActivity.URL_HOST + "/";
|
||||||
|
|
||||||
|
/*
|
||||||
|
remove the present prefix, or, if the input contains neither of the prefixes, notify the user
|
||||||
|
that their input is not valid
|
||||||
|
*/
|
||||||
|
if (input.contains(prefix1)) {
|
||||||
|
inputSudoku = input.replace(prefix1, "");
|
||||||
|
} else if (input.contains(prefix2)) {
|
||||||
|
inputSudoku = input.replace(prefix2, "");
|
||||||
|
} else {
|
||||||
|
Toast.makeText(CreateSudokuActivity.this,
|
||||||
|
this.getString(R.string.menu_import_wrong_format_custom_sudoku) + " " + prefix1 + ", " + prefix2, Toast.LENGTH_LONG).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean validSize = Math.sqrt(inputSudoku.length()) == gameController.getSize();
|
||||||
|
|
||||||
|
if (!validSize) {
|
||||||
|
Toast.makeText(CreateSudokuActivity.this, R.string.failed_to_verify_custom_sudoku_toast, Toast.LENGTH_LONG).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//check whether or not the sudoku is valid and has a unique solution
|
||||||
|
boolean solvable = verify(gameController.getGameType(), inputSudoku);
|
||||||
|
|
||||||
|
// if the encoded sudoku is solvable, sent the code directly to the GameActivity; if not, notify the user
|
||||||
|
if (solvable) {
|
||||||
|
Toast.makeText(CreateSudokuActivity.this, R.string.finished_verifying_custom_sudoku_toast, Toast.LENGTH_LONG).show();
|
||||||
|
int boardSize = gameController.getGameType().getSize() * gameController.getGameType().getSize();
|
||||||
|
GameInfoContainer container = new GameInfoContainer(0, GameDifficulty.Unspecified,
|
||||||
|
gameController.getGameType(), new int [boardSize], new int [boardSize],
|
||||||
|
new boolean [boardSize][gameController.getGameType().getSize()]);
|
||||||
|
container.parseSetValues(inputSudoku);
|
||||||
|
|
||||||
|
gameController.loadLevel(container);
|
||||||
|
setUpLayout();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Toast.makeText(CreateSudokuActivity.this, R.string.failed_to_verify_custom_sudoku_toast, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the onDialogNegativeClick() method of the IFinalizeDialogFragmentListener
|
* Implements the onDialogNegativeClick() method of the IFinalizeDialogFragmentListener
|
||||||
|
|
|
@ -43,8 +43,10 @@ import androidx.core.content.ContextCompat;
|
||||||
import org.secuso.privacyfriendlysudoku.controller.GameController;
|
import org.secuso.privacyfriendlysudoku.controller.GameController;
|
||||||
import org.secuso.privacyfriendlysudoku.game.listener.IHighlightChangedListener;
|
import org.secuso.privacyfriendlysudoku.game.listener.IHighlightChangedListener;
|
||||||
import org.secuso.privacyfriendlysudoku.ui.GameActivity;
|
import org.secuso.privacyfriendlysudoku.ui.GameActivity;
|
||||||
|
import org.secuso.privacyfriendlysudoku.ui.MainActivity;
|
||||||
import org.secuso.privacyfriendlysudoku.ui.listener.IFinalizeDialogFragmentListener;
|
import org.secuso.privacyfriendlysudoku.ui.listener.IFinalizeDialogFragmentListener;
|
||||||
import org.secuso.privacyfriendlysudoku.ui.listener.IHintDialogFragmentListener;
|
import org.secuso.privacyfriendlysudoku.ui.listener.IHintDialogFragmentListener;
|
||||||
|
import org.secuso.privacyfriendlysudoku.ui.listener.IImportDialogFragmentListener;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
@ -54,6 +56,7 @@ import static org.secuso.privacyfriendlysudoku.ui.view.CreateSudokuButtonType.ge
|
||||||
public class CreateSudokuSpecialButtonLayout extends LinearLayout implements IHighlightChangedListener {
|
public class CreateSudokuSpecialButtonLayout extends LinearLayout implements IHighlightChangedListener {
|
||||||
|
|
||||||
IFinalizeDialogFragmentListener finalizeDialogFragmentListener;
|
IFinalizeDialogFragmentListener finalizeDialogFragmentListener;
|
||||||
|
IImportDialogFragmentListener importDialogFragmentListener;
|
||||||
CreateSudokuSpecialButton[] fixedButtons;
|
CreateSudokuSpecialButton[] fixedButtons;
|
||||||
public int fixedButtonsCount = getSpecialButtons().size();
|
public int fixedButtonsCount = getSpecialButtons().size();
|
||||||
GameController gameController;
|
GameController gameController;
|
||||||
|
@ -77,6 +80,8 @@ public class CreateSudokuSpecialButtonLayout extends LinearLayout implements IHi
|
||||||
gameController.deleteSelectedCellsValue();
|
gameController.deleteSelectedCellsValue();
|
||||||
break;
|
break;
|
||||||
case Import:
|
case Import:
|
||||||
|
MainActivity.ImportBoardDialog impDialog = new MainActivity.ImportBoardDialog();
|
||||||
|
impDialog.show(fragmentManager, "ImportDialogFragment");
|
||||||
break;
|
break;
|
||||||
case Do:
|
case Do:
|
||||||
gameController.ReDo();
|
gameController.ReDo();
|
||||||
|
@ -108,12 +113,14 @@ public class CreateSudokuSpecialButtonLayout extends LinearLayout implements IHi
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setButtons(int width, GameController gc, SudokuKeyboardLayout key, FragmentManager fm,
|
public void setButtons(int width, GameController gc, SudokuKeyboardLayout key, FragmentManager fm,
|
||||||
int orientation, Context cxt, IFinalizeDialogFragmentListener finalizeListener) {
|
int orientation, Context cxt, IFinalizeDialogFragmentListener finalizeListener,
|
||||||
|
IImportDialogFragmentListener importListener) {
|
||||||
fragmentManager = fm;
|
fragmentManager = fm;
|
||||||
keyboard=key;
|
keyboard=key;
|
||||||
gameController = gc;
|
gameController = gc;
|
||||||
context = cxt;
|
context = cxt;
|
||||||
finalizeDialogFragmentListener = finalizeListener;
|
finalizeDialogFragmentListener = finalizeListener;
|
||||||
|
importDialogFragmentListener = importListener;
|
||||||
if(gameController != null) {
|
if(gameController != null) {
|
||||||
gameController.registerHighlightChangedListener(this);
|
gameController.registerHighlightChangedListener(this);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue