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,
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);
startActivity(intent);
finish();
@ -205,23 +209,30 @@ public class CreateSudokuActivity extends BaseActivity implements IFinalizeDialo
}
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
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 none of the valid prefixes, notify the user
that their input is not valid */
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;
}
/*
remove the present prefix, or, if the input contains neither of the prefixes, notify the user
that their input is not valid
*/
if (input.startsWith(prefix1)) {
inputSudoku = input.replace(prefix1, "");
} else if (input.startsWith(prefix2)) {
inputSudoku = input.replace(prefix2, "");
} else {
String endOfRecord = (i == GameActivity.validUris.size() - 1) ? "" : ", ";
errorMessage.append(prefix);
errorMessage.append(endOfRecord);
}
// the inputSudoku variable being null means the input did not match any of the valid prefixes
if (inputSudoku == null) {
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;
}
@ -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 (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,
gameController.getGameType(), new int [boardSize], new int [boardSize],
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.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
@ -77,15 +78,15 @@ import org.secuso.privacyfriendlysudoku.ui.view.WinDialog;
import org.secuso.privacyfriendlysudoku.ui.view.databinding.DialogFragmentShareBoardBinding;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
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 String URL_SCHEME_WITH_HOST = "http";
public static final String URL_SCHEME_WITH_HOST2 = "https";
public static final String URL_HOST = "sudoku.secuso.org";
public static final List<Uri> validUris = Arrays.asList(Uri.parse("sudoku://"),
Uri.parse("https://sudoku.secuso.org"),
Uri.parse("http://sudoku.secuso.org"));
GameController gameController;
SudokuFieldLayout layout;
@ -152,7 +153,7 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
int loadLevelID = 0;
boolean loadLevel = false;
if(savedInstanceState == null) {
if(savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
@ -174,12 +175,19 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
if (data != null && !intentReceivedFromMainActivity) {
// extract encoded sudoku board from the URI
String input = "";
if (data.getScheme().equals(URL_SCHEME_WITHOUT_HOST)){
input = data.getHost();
} else if ((data.getScheme().equals(URL_SCHEME_WITH_HOST) || data.getScheme().equals(URL_SCHEME_WITH_HOST2))
&& data.getHost().equals(URL_HOST)){
input = data.getPath();
input =input.replace("/", "");
for (int i = 0; i < validUris.size(); i++) {
if (data.getScheme().equals(validUris.get(i).getScheme())) {
if (validUris.get(i).getHost().equals("")) {
input = data.getHost();
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
@ -458,8 +466,10 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
case R.id.menu_share:
// Create import link from current sudoku board
String codeForClipboard = URL_SCHEME_WITHOUT_HOST + "://" + gameController.getCodeOfField();
String codeForClipboard1 = URL_SCHEME_WITH_HOST + "://" + URL_HOST + "/" + gameController.getCodeOfField();
String scheme = GameActivity.validUris.size() > 0 ? GameActivity.validUris.get(0).getScheme()
+ "://" + 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
ShareBoardDialog shareDialog = new ShareBoardDialog();

View file

@ -41,7 +41,6 @@ import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
@ -434,23 +433,28 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
}*/
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
String prefix1 = GameActivity.URL_SCHEME_WITHOUT_HOST + "://";
String prefix2 = GameActivity.URL_SCHEME_WITH_HOST + "://" + GameActivity.URL_HOST + "/";
// a valid input needs to contain exactly one of the valid prefixes
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;
}
/*
remove the present prefix, or, if the input contains neither of the prefixes, notify the user
that their input is not valid
*/
if (input.startsWith(prefix1)) {
inputSudoku = input.replace(prefix1, "");
} else if (input.startsWith(prefix2)) {
inputSudoku = input.replace(prefix2, "");
} else {
String endOfRecord = i == GameActivity.validUris.size() - 1 ? "" : ", ";
errorMessage.append(prefix);
errorMessage.append(endOfRecord);
}
if (inputSudoku == null) {
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;
}
@ -470,8 +474,6 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
return;
}
Log.i("Default_" + size + "x" + size, "SudokuTag");
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
@ -481,7 +483,7 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
if (solvable) {
Toast.makeText(MainActivity.this, R.string.finished_verifying_custom_sudoku_toast, Toast.LENGTH_LONG).show();
final Intent intent = new Intent(this, GameActivity.class);
intent.setData(Uri.parse(prefix1 + inputSudoku));
intent.setData(Uri.parse(prefix + inputSudoku));
startActivity(intent);
finish();
} else {