mirror of
https://codeberg.org/gitnex/GitNex.git
synced 2024-12-26 16:04:07 +08:00
Version Check before try to Login (#147)
This commit is contained in:
parent
d29b1901c0
commit
d14ecd9c68
@ -1,9 +1,9 @@
|
||||
package org.mian.gitnex.activities;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.GradientDrawable;
|
||||
import android.os.Bundle;
|
||||
@ -17,19 +17,23 @@ import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import com.tooltip.Tooltip;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.clients.RetrofitClient;
|
||||
import org.mian.gitnex.helpers.Toasty;
|
||||
import org.mian.gitnex.helpers.VersionCheck;
|
||||
import org.mian.gitnex.models.GiteaVersion;
|
||||
import org.mian.gitnex.models.UserTokens;
|
||||
import org.mian.gitnex.util.AppUtil;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.util.TinyDB;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.Headers;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
|
||||
@ -42,6 +46,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
||||
private Button login_button;
|
||||
private EditText instance_url, login_uid, login_passwd, otpCode;
|
||||
private Spinner protocolSpinner;
|
||||
final Context ctx = this;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -58,10 +63,10 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
||||
login_passwd = findViewById(R.id.login_passwd);
|
||||
otpCode = findViewById(R.id.otpCode);
|
||||
ImageView info_button = findViewById(R.id.info);
|
||||
final TextView viewTextGiteaVersion = findViewById(R.id.appVersion);
|
||||
final TextView viewTextAppVersion = findViewById(R.id.appVersion);
|
||||
protocolSpinner = findViewById(R.id.httpsSpinner);
|
||||
|
||||
viewTextGiteaVersion.setText(AppUtil.getAppVersion(getApplicationContext()));
|
||||
viewTextAppVersion.setText(AppUtil.getAppVersion(getApplicationContext()));
|
||||
|
||||
Resources res = getResources();
|
||||
String[] allProtocols = res.getStringArray(R.array.protocolValues);
|
||||
@ -254,7 +259,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
||||
|
||||
}
|
||||
|
||||
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP);
|
||||
versionCheck(instanceUrl, loginUid, loginPass, loginOTP);
|
||||
|
||||
}
|
||||
else {
|
||||
@ -265,6 +270,87 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
||||
|
||||
}
|
||||
|
||||
private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) {
|
||||
|
||||
final TinyDB tinyDb = new TinyDB(getApplicationContext());
|
||||
|
||||
Call<GiteaVersion> callVersion = RetrofitClient
|
||||
.getInstance(instanceUrl)
|
||||
.getApiInterface()
|
||||
.getGiteaVersion();
|
||||
|
||||
callVersion.enqueue(new Callback<GiteaVersion>() {
|
||||
|
||||
@Override
|
||||
public void onResponse(@NonNull final Call<GiteaVersion> callVersion, @NonNull retrofit2.Response<GiteaVersion> responseVersion) {
|
||||
|
||||
if (responseVersion.code() == 200) {
|
||||
|
||||
GiteaVersion version = responseVersion.body();
|
||||
assert version != null;
|
||||
|
||||
VersionCheck vt = VersionCheck.check(getString(R.string.versionLow), getString(R.string.versionHigh), version.getVersion());
|
||||
tinyDb.putString("giteaVersion", version.getVersion());
|
||||
|
||||
switch (vt) {
|
||||
case UNSUPPORTED_NEW:
|
||||
//Toasty.info(getApplicationContext(), getString(R.string.versionUnsupportedNew));
|
||||
case SUPPORTED_LATEST:
|
||||
case SUPPORTED_OLD:
|
||||
case DEVELOPMENT:
|
||||
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP);
|
||||
return;
|
||||
case UNSUPPORTED_OLD:
|
||||
|
||||
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx, R.style.confirmDialog);
|
||||
|
||||
alertDialogBuilder
|
||||
.setTitle(getString(R.string.versionAlertDialogHeader))
|
||||
.setMessage(getResources().getString(R.string.versionUnsupportedOld, version.getVersion()))
|
||||
.setCancelable(true)
|
||||
.setIcon(R.drawable.ic_warning)
|
||||
.setNegativeButton(getString(R.string.versionAlertDialogCopyNegative), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
enableProcessButton();
|
||||
}
|
||||
})
|
||||
.setPositiveButton(getString(R.string.versionAlertDialogCopyPositive), new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
|
||||
dialog.dismiss();
|
||||
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
AlertDialog alertDialog = alertDialogBuilder.create();
|
||||
|
||||
alertDialog.show();
|
||||
return;
|
||||
default: // UNKNOWN
|
||||
Toasty.info(getApplicationContext(), getString(R.string.versionUnknow));
|
||||
enableProcessButton();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<GiteaVersion> callVersion, Throwable t) {
|
||||
|
||||
Log.e("onFailure-version", t.toString());
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void letTheUserIn(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) {
|
||||
|
||||
final String credential = Credentials.basic(loginUid, loginPass);
|
||||
@ -290,7 +376,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
||||
|
||||
List<UserTokens> userTokens = response.body();
|
||||
final TinyDB tinyDb = new TinyDB(getApplicationContext());
|
||||
Headers responseHeaders = response.headers();
|
||||
//Headers responseHeaders = response.headers();
|
||||
|
||||
if (response.isSuccessful()) {
|
||||
|
||||
@ -332,7 +418,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<UserTokens> callCreateToken, @NonNull retrofit2.Response<UserTokens> responseCreate) {
|
||||
|
||||
if (responseCreate.isSuccessful()) {
|
||||
if(responseCreate.isSuccessful()) {
|
||||
|
||||
if(responseCreate.code() == 201) {
|
||||
|
||||
|
@ -6,27 +6,17 @@ import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.activities.CreditsActivity;
|
||||
import org.mian.gitnex.activities.MainActivity;
|
||||
import org.mian.gitnex.activities.SponsorsActivity;
|
||||
import org.mian.gitnex.clients.RetrofitClient;
|
||||
import org.mian.gitnex.helpers.Authorization;
|
||||
import org.mian.gitnex.models.GiteaVersion;
|
||||
import org.mian.gitnex.util.AppUtil;
|
||||
import org.mian.gitnex.util.TinyDB;
|
||||
import java.util.Objects;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
|
||||
/**
|
||||
* Author M M Arif
|
||||
@ -34,17 +24,12 @@ import retrofit2.Callback;
|
||||
|
||||
public class AboutFragment extends Fragment {
|
||||
|
||||
private TextView viewTextGiteaVersion;
|
||||
private ProgressBar mProgressBar;
|
||||
private LinearLayout pageContent;
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_about, container, false);
|
||||
((MainActivity) Objects.requireNonNull(getActivity())).setActionBarTitle(getResources().getString(R.string.pageTitleAbout));
|
||||
|
||||
TinyDB tinyDb = new TinyDB(getContext());
|
||||
String instanceUrl = tinyDb.getString("instanceUrl");
|
||||
|
||||
final TextView appVerBuild;
|
||||
final TextView donationLink;
|
||||
@ -55,13 +40,8 @@ public class AboutFragment extends Fragment {
|
||||
final TextView appWebsite;
|
||||
final TextView appRepo;
|
||||
|
||||
pageContent = v.findViewById(R.id.aboutFrame);
|
||||
pageContent.setVisibility(View.GONE);
|
||||
|
||||
mProgressBar = v.findViewById(R.id.progress_bar);
|
||||
|
||||
appVerBuild = v.findViewById(R.id.appVerBuild);
|
||||
viewTextGiteaVersion = v.findViewById(R.id.giteaVersion);
|
||||
TextView viewTextGiteaVersion = v.findViewById(R.id.giteaVersion);
|
||||
creditsButton = v.findViewById(R.id.creditsButton);
|
||||
donationLink = v.findViewById(R.id.donationLink);
|
||||
donationLinkPatreon = v.findViewById(R.id.donationLinkPatreon);
|
||||
@ -134,67 +114,10 @@ public class AboutFragment extends Fragment {
|
||||
}
|
||||
});
|
||||
|
||||
boolean connToInternet = AppUtil.haveNetworkConnection(getContext());
|
||||
|
||||
if(!connToInternet) {
|
||||
|
||||
mProgressBar.setVisibility(View.GONE);
|
||||
pageContent.setVisibility(View.VISIBLE);
|
||||
|
||||
} else {
|
||||
|
||||
giteaVer(instanceUrl);
|
||||
|
||||
}
|
||||
String commit = getResources().getString(R.string.commitPage) + tinyDb.getString("giteaVersion");
|
||||
viewTextGiteaVersion.setText(commit);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
private void giteaVer(String instanceUrl) {
|
||||
|
||||
TinyDB tinyDb = new TinyDB(getContext());
|
||||
final String loginUid = tinyDb.getString("loginUid");
|
||||
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
||||
|
||||
Call<GiteaVersion> call = RetrofitClient
|
||||
.getInstance(instanceUrl)
|
||||
.getApiInterface()
|
||||
.getGiteaVersion(Authorization.returnAuthentication(getContext(), loginUid, instanceToken));
|
||||
|
||||
call.enqueue(new Callback<GiteaVersion>() {
|
||||
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<GiteaVersion> call, @NonNull Response<GiteaVersion> response) {
|
||||
|
||||
if (response.isSuccessful()) {
|
||||
if (response.code() == 200) {
|
||||
|
||||
GiteaVersion version = response.body();
|
||||
assert version != null;
|
||||
String commit = getResources().getString(R.string.commitPage) + version.getVersion();
|
||||
viewTextGiteaVersion.setText(commit);
|
||||
mProgressBar.setVisibility(View.GONE);
|
||||
pageContent.setVisibility(View.VISIBLE);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
String commit = getResources().getString(R.string.commitPage);
|
||||
viewTextGiteaVersion.setText(commit);
|
||||
mProgressBar.setVisibility(View.GONE);
|
||||
pageContent.setVisibility(View.VISIBLE);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<GiteaVersion> call, @NonNull Throwable t) {
|
||||
Log.e("onFailure", t.toString());
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
135
app/src/main/java/org/mian/gitnex/helpers/VersionCheck.java
Normal file
135
app/src/main/java/org/mian/gitnex/helpers/VersionCheck.java
Normal file
@ -0,0 +1,135 @@
|
||||
package org.mian.gitnex.helpers;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Author 6543
|
||||
*/
|
||||
|
||||
public enum VersionCheck {
|
||||
|
||||
UNKNOWN,
|
||||
SUPPORTED_LATEST,
|
||||
SUPPORTED_OLD,
|
||||
DEVELOPMENT,
|
||||
UNSUPPORTED_OLD,
|
||||
UNSUPPORTED_NEW;
|
||||
|
||||
public static VersionCheck check(String min, String last, String value) {
|
||||
|
||||
final Pattern pattern_stable_release = Pattern.compile("^(\\d)\\.(\\d+)\\.(\\d+)$");
|
||||
final Pattern pattern_dev_release = Pattern.compile("^(\\d).(\\d+).(\\d+)(\\D)(.+)");
|
||||
Matcher match;
|
||||
|
||||
if (!pattern_stable_release.matcher(min).find() || !pattern_stable_release.matcher(last).find()) {
|
||||
throw new IllegalArgumentException("VersionCheck: wrong format for min or last version given");
|
||||
}
|
||||
|
||||
match = pattern_stable_release.matcher(value);
|
||||
if (match.find()) {
|
||||
|
||||
switch (correlate(min, last, match.group())){
|
||||
case 0:
|
||||
return UNSUPPORTED_OLD;
|
||||
case 1:
|
||||
return SUPPORTED_OLD;
|
||||
case 2:
|
||||
return SUPPORTED_LATEST;
|
||||
default:
|
||||
return UNSUPPORTED_NEW;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
match = pattern_dev_release.matcher(value);
|
||||
if (match.find()) {
|
||||
|
||||
match = Pattern.compile("^(\\d)\\.(\\d+)\\.(\\d+)").matcher(value);
|
||||
match.find();
|
||||
|
||||
if (correlate(min, last, match.group())>0) {
|
||||
return DEVELOPMENT;
|
||||
}
|
||||
else {
|
||||
return UNSUPPORTED_OLD;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return UNKNOWN;
|
||||
|
||||
}
|
||||
|
||||
//helper
|
||||
// 0 to less
|
||||
// 1 in range
|
||||
// 2 at the top
|
||||
// 3 above
|
||||
private static int correlate(String min, String last, String value){
|
||||
int min_check = compareVersion(value,min);
|
||||
int max_check = compareVersion(value,last);
|
||||
int range_check = compareVersion(min,last);
|
||||
|
||||
switch (range_check) {
|
||||
case 2:
|
||||
throw new IllegalArgumentException("Minimum Version higher than Last Version");
|
||||
case 1: //min == last
|
||||
switch (min_check) {
|
||||
case 0:
|
||||
return 0;
|
||||
case 1:
|
||||
return 2;
|
||||
default:
|
||||
return 3;
|
||||
}
|
||||
default:
|
||||
if (max_check >1) return 3;
|
||||
if (max_check == 1) return 2;
|
||||
if (min_check < 1) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description compare doted formatted Versions
|
||||
* @param A doted formatted Versions
|
||||
* @param B doted formatted Versions
|
||||
* @return 0|1|2
|
||||
* 0 = less
|
||||
* 1 = same
|
||||
* 2 = more
|
||||
*/
|
||||
public static int compareVersion(String A, String B) {
|
||||
//throw new IllegalArgumentException
|
||||
if((!A.matches("[0-9]+(\\.[0-9]+)*")) || (!B.matches("[0-9]+(\\.[0-9]+)*"))) throw new IllegalArgumentException("Invalid version format");
|
||||
|
||||
if (A.contains(".") || B.contains(".")) {
|
||||
// example 2 vs 1.3
|
||||
if (!(A.contains(".") && B.contains("."))) {
|
||||
if (A.contains(".")) {
|
||||
return compareVersion(A,B + ".0");
|
||||
}
|
||||
if (B.contains(".")) {
|
||||
return compareVersion(A + ".0",B);
|
||||
}
|
||||
}
|
||||
|
||||
//normal compare
|
||||
int a = Integer.parseInt(A.substring(0,A.indexOf(".")));
|
||||
int b = Integer.parseInt(B.substring(0,B.indexOf(".")));
|
||||
if (a < b) return 0;
|
||||
if (a == b) return compareVersion(A.substring(A.indexOf(".")+1),B.substring(B.indexOf(".")+1));
|
||||
return 2; //if (a > b)
|
||||
}
|
||||
else {
|
||||
int a = Integer.parseInt(A);
|
||||
int b = Integer.parseInt(B);
|
||||
if (a < b) return 0;
|
||||
if (a == b) return 1;
|
||||
return 2; //if (a > b)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -48,7 +48,7 @@ import retrofit2.http.Query;
|
||||
public interface ApiInterface {
|
||||
|
||||
@GET("version") // gitea version API
|
||||
Call<GiteaVersion> getGiteaVersion(@Header("Authorization") String token);
|
||||
Call<GiteaVersion> getGiteaVersion();
|
||||
|
||||
@GET("user") // username, full name, email
|
||||
Call<UserInfo> getUserInfo(@Header("Authorization") String token);
|
||||
|
@ -191,21 +191,4 @@
|
||||
|
||||
</ScrollView>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/progressBarLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_bar"
|
||||
style="@style/Base.Widget.AppCompat.ProgressBar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminate="true"
|
||||
android:visibility="visible"
|
||||
android:layout_centerInParent="true"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</RelativeLayout>
|
@ -20,6 +20,9 @@
|
||||
<string name="supportText" translatable="false">Support the App on Liberapay</string>
|
||||
<string name="supportTextPatreon" translatable="false">Become a Patreon</string>
|
||||
|
||||
<string name="versionLow" translatable="false">1.9.0</string>
|
||||
<string name="versionHigh" translatable="false">1.10.9</string>
|
||||
|
||||
<!-- menu items -->
|
||||
<string name="navMyRepos">My Repositories</string>
|
||||
<string name="navStarredRepos">Starred Repositories</string>
|
||||
@ -514,4 +517,14 @@
|
||||
<string name="unWatchRepositorySuccess">Repository removed from watch list</string>
|
||||
<string name="filesBreadcrumbRoot" translatable="false">Root</string>
|
||||
|
||||
<string name="versionUnsupportedOld">Unsupported old version(%1$s) of Gitea detected. Please update to latest stable version. If you continue, the app may not function properly.</string>
|
||||
<string name="versionSupportedOld">Old Gitea version detected, please update to latest stable version</string>
|
||||
<string name="versionUnsupportedNew">New Gitea version detected! Please UPDATE GitNex!</string>
|
||||
<string name="versionSupportedLatest">Gitea version is up to date</string>
|
||||
<string name="versionDevelopment">Gitea development version</string>
|
||||
<string name="versionUnknow">No Gitea detected!</string>
|
||||
<string name="versionAlertTitle">Version Alert</string>
|
||||
<string name="versionAlertDialogHeader">Unsupported Version of Gitea</string>
|
||||
<string name="versionAlertDialogCopyNegative">Cancel</string>
|
||||
<string name="versionAlertDialogCopyPositive">Continue</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user