diff --git a/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java b/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java index ccd3fbe3..ba653470 100644 --- a/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java @@ -42,7 +42,8 @@ public class AddNewAccountActivity extends BaseActivity { private enum Protocol {HTTPS, HTTP} @Override - protected int getLayoutResourceId(){ + protected int getLayoutResourceId() { + return R.layout.activity_add_new_account; } @@ -62,12 +63,13 @@ public class AddNewAccountActivity extends BaseActivity { initCloseListener(); viewBinding.close.setOnClickListener(onClickListener); - ArrayAdapter adapterProtocols = new ArrayAdapter<>(AddNewAccountActivity.this, R.layout.spinner_item, AddNewAccountActivity.Protocol.values()); + ArrayAdapter adapterProtocols = new ArrayAdapter<>(AddNewAccountActivity.this, R.layout.spinner_item, + AddNewAccountActivity.Protocol.values()); adapterProtocols.setDropDownViewResource(R.layout.spinner_dropdown_item); viewBinding.protocolSpinner.setAdapter(adapterProtocols); - viewBinding.addNewAccount.setOnClickListener(login -> { + viewBinding.addNewAccount.setOnClickListener(login -> { boolean connToInternet = AppUtil.hasNetworkConnection(appCtx); @@ -80,7 +82,7 @@ public class AddNewAccountActivity extends BaseActivity { processLogin(); } - }); + }); } @@ -137,21 +139,16 @@ public class AddNewAccountActivity extends BaseActivity { if(responseVersion.code() == 200) { GiteaVersion version = responseVersion.body(); - Version giteaVersion; assert version != null; - - tinyDB.putString("giteaVersion", version.getVersion()); - - try { - giteaVersion = new Version(version.getVersion()); - } - catch(Exception e) { - + if(!Version.valid(version.getVersion())) { Toasty.error(ctx, getResources().getString(R.string.versionUnknown)); return; } + tinyDB.putString("giteaVersion", version.getVersion()); + Version giteaVersion = new Version(version.getVersion()); + if(giteaVersion.less(getString(R.string.versionLow))) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader)) @@ -161,7 +158,6 @@ public class AddNewAccountActivity extends BaseActivity { alertDialogBuilder.setNegativeButton(getString(R.string.cancelButton), (dialog, which) -> { dialog.dismiss(); - //enableProcessButton(); }); alertDialogBuilder.setPositiveButton(getString(R.string.textContinue), (dialog, which) -> { @@ -241,12 +237,12 @@ public class AddNewAccountActivity extends BaseActivity { case 401: - Toasty.error(ctx,getResources().getString(R.string.unauthorizedApiError)); + Toasty.error(ctx, getResources().getString(R.string.unauthorizedApiError)); break; default: - Toasty.error(ctx,getResources().getString(R.string.genericApiStatusError) + response.code()); + Toasty.error(ctx, getResources().getString(R.string.genericApiStatusError) + response.code()); } } @@ -255,7 +251,7 @@ public class AddNewAccountActivity extends BaseActivity { public void onFailure(@NonNull Call call, @NonNull Throwable t) { Log.e("onFailure", t.toString()); - Toasty.error(ctx,getResources().getString(R.string.genericError)); + Toasty.error(ctx, getResources().getString(R.string.genericError)); } }); diff --git a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java index 1e61c4cf..f55d67e8 100644 --- a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java @@ -276,22 +276,17 @@ public class LoginActivity extends BaseActivity { if(responseVersion.code() == 200) { GiteaVersion version = responseVersion.body(); - Version gitea_version; assert version != null; - - tinyDB.putString("giteaVersion", version.getVersion()); - - try { - gitea_version = new Version(version.getVersion()); - } - catch(Exception e) { - + if(!Version.valid(version.getVersion())) { Toasty.error(ctx, getResources().getString(R.string.versionUnknown)); enableProcessButton(); return; } + tinyDB.putString("giteaVersion", version.getVersion()); + Version gitea_version = new Version(version.getVersion()); + if(gitea_version.less(getString(R.string.versionLow))) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx).setTitle(getString(R.string.versionAlertDialogHeader)) @@ -572,7 +567,8 @@ public class LoginActivity extends BaseActivity { if(checkAccount == 0) { - accountId = userAccountsApi.insertNewAccount(accountName, instanceUrl, userDetails.getUsername(), newToken.getSha1(), ""); + accountId = userAccountsApi + .insertNewAccount(accountName, instanceUrl, userDetails.getUsername(), newToken.getSha1(), ""); tinyDB.putInt("currentActiveAccountId", (int) accountId); } else { diff --git a/app/src/main/java/org/mian/gitnex/helpers/Version.java b/app/src/main/java/org/mian/gitnex/helpers/Version.java index cc62bd69..50bc6c4c 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/Version.java +++ b/app/src/main/java/org/mian/gitnex/helpers/Version.java @@ -24,6 +24,21 @@ public class Version { } + /** + * valid return true if string is a valid version + * + * @param value + * @return + */ + public static boolean valid(String value) { + + if(value == null) { + return false; + } + final Pattern pattern_valid = Pattern.compile("^[v,V]?(\\d+)+(\\.(\\d+))*([_,\\-,+][\\w,\\d,_,\\-,+]*)?$"); + return pattern_valid.matcher(value).find(); + } + /** * init parse and store values for other functions of an Version instance * it use the raw variable as base @@ -32,10 +47,9 @@ public class Version { */ private void init() { - final Pattern pattern_valid = Pattern.compile("^[v,V]?(\\d+)+(\\.(\\d+))*([_,\\-,+][\\w,\\d,_,\\-,+]*)?$"); final Pattern pattern_number_dot_number = Pattern.compile("^\\d+(\\.(\\d)+)*"); - if(!pattern_valid.matcher(raw).find()) { + if(!valid(raw)) { throw new IllegalArgumentException("Invalid version format"); } diff --git a/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java b/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java index 33c7e955..4d60e594 100644 --- a/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java +++ b/app/src/test/java/org/mian/gitnex/helpers/VersionTest.java @@ -119,4 +119,25 @@ public class VersionTest { } + @Test + public void valid() { + + assertTrue(Version.valid("1.12")); + assertTrue(Version.valid("1.12.0+dev-211-g316db0fe7")); + assertTrue(Version.valid("v2")); + assertTrue(Version.valid("1.12.1-wowowow")); + assertTrue(Version.valid("0.2.4")); + assertTrue(Version.valid("1")); + assertTrue(Version.valid("1.12.0")); + + assertFalse(Version.valid("fdsa21.22.-")); + assertFalse(Version.valid("weo2.2.2")); + assertFalse(Version.valid("")); + assertFalse(Version.valid(" ")); + assertFalse(Version.valid("\t")); + assertFalse(Version.valid("abc")); + assertFalse(Version.valid("version1")); + assertFalse(Version.valid(null)); + } + }