Save valid url schemes in a list instead of saving a constant for each valid scheme + host

This commit is contained in:
uykek 2020-08-31 14:48:30 +02:00
parent a941f146f4
commit 4e012b024a
3 changed files with 70 additions and 48 deletions

View file

@ -195,7 +195,11 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
Since the GameActivity expects the links of imported sudokus to start with an url scheme, Since the GameActivity expects the links of imported sudokus to start with an url scheme,
add one to the start of the encoded board add one to the start of the encoded board
*/ */
intent.setData(Uri.parse(GameActivity.URL_SCHEME_WITHOUT_HOST + "://" + boardContent)); String scheme = GameActivity.validUris.size() > 0 ? GameActivity.validUris.get(0).getScheme()
+ "://" + GameActivity.validUris.get(0).getHost() : "";
if (!scheme.equals("") && !scheme.endsWith("/")) scheme = scheme + "/";
intent.setData(Uri.parse(scheme + boardContent));
intent.putExtra("isCustom", true); intent.putExtra("isCustom", true);
startActivity(intent); startActivity(intent);
finish(); finish();
@ -205,23 +209,30 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
} }
public void onImportDialogPositiveClick(String input) { public void onImportDialogPositiveClick(String input) {
String inputSudoku; String inputSudoku = null;
String prefix;
StringBuilder errorMessage = new StringBuilder();
// a valid input needs to contain exactly one of these prefixes /* remove the present prefix, or, if the input contains none of the valid prefixes, notify the user
String prefix1 = GameActivity.URL_SCHEME_WITHOUT_HOST + "://"; that their input is not valid */
String prefix2 = GameActivity.URL_SCHEME_WITH_HOST + "://" + GameActivity.URL_HOST + "/"; for (int i = 0; i < GameActivity.validUris.size(); i++) {
prefix = GameActivity.validUris.get(i).getHost().equals("") ?
GameActivity.validUris.get(i).getScheme() + "://" :
GameActivity.validUris.get(i).getScheme() + "://" + GameActivity.validUris.get(i).getHost() + "/";
if (input.startsWith(prefix)) {
inputSudoku = input.replace(prefix, "");
break;
}
/* String endOfRecord = (i == GameActivity.validUris.size() - 1) ? "" : ", ";
remove the present prefix, or, if the input contains neither of the prefixes, notify the user errorMessage.append(prefix);
that their input is not valid errorMessage.append(endOfRecord);
*/ }
if (input.startsWith(prefix1)) {
inputSudoku = input.replace(prefix1, ""); // the inputSudoku variable being null means the input did not match any of the valid prefixes
} else if (input.startsWith(prefix2)) { if (inputSudoku == null) {
inputSudoku = input.replace(prefix2, "");
} else {
Toast.makeText(CreateSudokuActivity.this, Toast.makeText(CreateSudokuActivity.this,
this.getString(R.string.menu_import_wrong_format_custom_sudoku) + " " + prefix1 + ", " + prefix2, Toast.LENGTH_LONG).show(); this.getString(R.string.menu_import_wrong_format_custom_sudoku) + " " + errorMessage.toString(), Toast.LENGTH_LONG).show();
return; return;
} }
@ -237,8 +248,7 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
// if the encoded sudoku is solvable, sent the code directly to the GameActivity; if not, notify the user // if the encoded sudoku is solvable, sent the code directly to the GameActivity; if not, notify the user
if (solvable) { 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();
int boardSize = gameController.getGameType().getSize() * gameController.getGameType().getSize();
GameInfoContainer container = new GameInfoContainer(0, GameDifficulty.Unspecified, GameInfoContainer container = new GameInfoContainer(0, GameDifficulty.Unspecified,
gameController.getGameType(), new int [boardSize], new int [boardSize], gameController.getGameType(), new int [boardSize], new int [boardSize],
new boolean [boardSize][gameController.getGameType().getSize()]); new boolean [boardSize][gameController.getGameType().getSize()]);

View file

@ -44,6 +44,7 @@ import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -77,15 +78,15 @@ import org.secuso.privacyfriendlysudoku.ui.view.WinDialog;
import org.secuso.privacyfriendlysudoku.ui.view.databinding.DialogFragmentShareBoardBinding; import org.secuso.privacyfriendlysudoku.ui.view.databinding.DialogFragmentShareBoardBinding;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
public class GameActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, IGameSolvedListener ,ITimerListener, IHintDialogFragmentListener, IResetDialogFragmentListener, IShareDialogFragmentListener { public class GameActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener, IGameSolvedListener ,ITimerListener, IHintDialogFragmentListener, IResetDialogFragmentListener, IShareDialogFragmentListener {
public static final String URL_SCHEME_WITHOUT_HOST = "sudoku"; public static final List<Uri> validUris = Arrays.asList(Uri.parse("sudoku://"),
public static final String URL_SCHEME_WITH_HOST = "http"; Uri.parse("https://sudoku.secuso.org"),
public static final String URL_SCHEME_WITH_HOST2 = "https"; Uri.parse("http://sudoku.secuso.org"));
public static final String URL_HOST = "sudoku.secuso.org";
GameController gameController; GameController gameController;
SudokuFieldLayout layout; SudokuFieldLayout layout;
@ -152,7 +153,7 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
int loadLevelID = 0; int loadLevelID = 0;
boolean loadLevel = false; boolean loadLevel = false;
if(savedInstanceState == null) { if(savedInstanceState == null) {
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
@ -174,12 +175,19 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
if (data != null && !intentReceivedFromMainActivity) { if (data != null && !intentReceivedFromMainActivity) {
// extract encoded sudoku board from the URI // extract encoded sudoku board from the URI
String input = ""; String input = "";
if (data.getScheme().equals(URL_SCHEME_WITHOUT_HOST)){
input = data.getHost(); for (int i = 0; i < validUris.size(); i++) {
} else if ((data.getScheme().equals(URL_SCHEME_WITH_HOST) || data.getScheme().equals(URL_SCHEME_WITH_HOST2)) if (data.getScheme().equals(validUris.get(i).getScheme())) {
&& data.getHost().equals(URL_HOST)){ if (validUris.get(i).getHost().equals("")) {
input = data.getPath(); input = data.getHost();
input =input.replace("/", ""); break;
}
else if (data.getHost().equals(validUris.get(i).getHost())) {
input = data.getPath();
input =input.replace("/", "");
break;
}
}
} }
// Save all of the information that can be extracted from the encoded board in a GameInfoContainer object // Save all of the information that can be extracted from the encoded board in a GameInfoContainer object
@ -458,8 +466,10 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
case R.id.menu_share: case R.id.menu_share:
// Create import link from current sudoku board // Create import link from current sudoku board
String codeForClipboard = URL_SCHEME_WITHOUT_HOST + "://" + gameController.getCodeOfField(); String scheme = GameActivity.validUris.size() > 0 ? GameActivity.validUris.get(0).getScheme()
String codeForClipboard1 = URL_SCHEME_WITH_HOST + "://" + URL_HOST + "/" + gameController.getCodeOfField(); + "://" + GameActivity.validUris.get(0).getHost() : "";
if (!scheme.equals("") && !scheme.endsWith("/")) scheme = scheme + "/";
String codeForClipboard = scheme + gameController.getCodeOfField();
// Create new ShareBoardDialog using the previously created links // Create new ShareBoardDialog using the previously created links
ShareBoardDialog shareDialog = new ShareBoardDialog(); ShareBoardDialog shareDialog = new ShareBoardDialog();

View file

@ -41,7 +41,6 @@ import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -434,23 +433,28 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
}*/ }*/
public void onImportDialogPositiveClick(String input) { public void onImportDialogPositiveClick(String input) {
String inputSudoku; String inputSudoku = null;
String prefix = "";
StringBuilder errorMessage = new StringBuilder();
// a valid input needs to contain exactly one of these prefixes // a valid input needs to contain exactly one of the valid prefixes
String prefix1 = GameActivity.URL_SCHEME_WITHOUT_HOST + "://"; for (int i = 0; i < GameActivity.validUris.size(); i++) {
String prefix2 = GameActivity.URL_SCHEME_WITH_HOST + "://" + GameActivity.URL_HOST + "/"; prefix = GameActivity.validUris.get(i).getHost().equals("") ?
GameActivity.validUris.get(i).getScheme() + "://" :
GameActivity.validUris.get(i).getScheme() + "://" + GameActivity.validUris.get(i).getHost() + "/";
if (input.startsWith(prefix)) {
inputSudoku = input.replace(prefix, "");
break;
}
/* String endOfRecord = i == GameActivity.validUris.size() - 1 ? "" : ", ";
remove the present prefix, or, if the input contains neither of the prefixes, notify the user errorMessage.append(prefix);
that their input is not valid errorMessage.append(endOfRecord);
*/ }
if (input.startsWith(prefix1)) {
inputSudoku = input.replace(prefix1, ""); if (inputSudoku == null) {
} else if (input.startsWith(prefix2)) {
inputSudoku = input.replace(prefix2, "");
} else {
Toast.makeText(MainActivity.this, Toast.makeText(MainActivity.this,
this.getString(R.string.menu_import_wrong_format_custom_sudoku) + " " + prefix1 + ", " + prefix2, Toast.LENGTH_LONG).show(); this.getString(R.string.menu_import_wrong_format_custom_sudoku) + " " + errorMessage.toString(), Toast.LENGTH_LONG).show();
return; return;
} }
@ -470,8 +474,6 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
return; return;
} }
Log.i("Default_" + size + "x" + size, "SudokuTag");
GameType gameType = Enum.valueOf(GameType.class, "Default_" + (int)size + "x" + (int)size); GameType gameType = Enum.valueOf(GameType.class, "Default_" + (int)size + "x" + (int)size);
//check whether or not the sudoku is valid and has a unique solution //check whether or not the sudoku is valid and has a unique solution
@ -481,7 +483,7 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
if (solvable) { if (solvable) {
Toast.makeText(MainActivity.this, R.string.finished_verifying_custom_sudoku_toast, Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, R.string.finished_verifying_custom_sudoku_toast, Toast.LENGTH_LONG).show();
final Intent intent = new Intent(this, GameActivity.class); final Intent intent = new Intent(this, GameActivity.class);
intent.setData(Uri.parse(prefix1 + inputSudoku)); intent.setData(Uri.parse(prefix + inputSudoku));
startActivity(intent); startActivity(intent);
finish(); finish();
} else { } else {