Merge remote-tracking branch 'remotes/main/master' into self-signed

# Conflicts:
#	app/src/main/java/org/mian/gitnex/activities/LoginActivity.java
This commit is contained in:
anonTree1417 2020-04-02 23:07:20 +02:00
commit 63865fefec
1 changed files with 548 additions and 474 deletions

View File

@ -58,7 +58,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
@Override
protected int getLayoutResourceId(){
return R.layout.activity_login;
}
@ -87,7 +86,8 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Resources res = getResources();
String[] allProtocols = res.getStringArray(R.array.protocolValues);
final ArrayAdapter<String> adapterProtocols = new ArrayAdapter<String>(Objects.requireNonNull(getApplicationContext()), R.layout.spinner_item, allProtocols);
final ArrayAdapter<String> adapterProtocols = new ArrayAdapter<String>(Objects.requireNonNull(getApplicationContext()),
R.layout.spinner_item, allProtocols);
adapterProtocols.setDropDownViewResource(R.layout.spinner_dropdown_item);
protocolSpinner.setAdapter(adapterProtocols);
@ -104,8 +104,8 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
info_button.setOnClickListener(infoListener);
@ -182,7 +182,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
private View.OnClickListener loginListener = new View.OnClickListener() {
public void onClick(View v) {
disableProcessButton();
@ -193,10 +192,17 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
};
private View.OnClickListener infoListener = new View.OnClickListener() {
public void onClick(View v) {
new Tooltip.Builder(v).setText(R.string.urlInfoTooltip).setTextColor(getResources().getColor(R.color.white)).setBackgroundColor(getResources().getColor(R.color.tooltipBackground)).setCancelable(true).setDismissOnClick(true).setPadding(30).setCornerRadius(R.dimen.tooltipCornor).setGravity(Gravity.BOTTOM).show();
new Tooltip.Builder(v)
.setText(R.string.urlInfoTooltip)
.setTextColor(getResources().getColor(R.color.white))
.setBackgroundColor(getResources().getColor(R.color.tooltipBackground))
.setCancelable(true)
.setDismissOnClick(true)
.setPadding(30)
.setCornerRadius(R.dimen.tooltipCornor)
.setGravity(Gravity.BOTTOM)
.show();
}
};
@ -235,8 +241,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
URI uri = null;
try {
uri = new URI(instanceUrl);
}
catch(URISyntaxException e) {
} catch (URISyntaxException e) {
e.printStackTrace();
}
assert uri != null;
@ -325,8 +330,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
URI uri = null;
try {
uri = new URI(instanceUrl);
}
catch(URISyntaxException e) {
} catch (URISyntaxException e) {
e.printStackTrace();
}
assert uri != null;
@ -385,9 +389,10 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP, final String loginToken_, final int loginType) {
final TinyDB tinyDb = new TinyDB(getApplicationContext());
Call<GiteaVersion> callVersion = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getGiteaVersion();
Call<GiteaVersion> callVersion = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getGiteaVersion();
callVersion.enqueue(new Callback<GiteaVersion>() {
@ -413,23 +418,22 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx);
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.cancelButton), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
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.cancelButton), (dialog, which) -> {
dialog.dismiss();
enableProcessButton();
}
}).setPositiveButton(getString(R.string.textContinue), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
})
.setPositiveButton(getString(R.string.textContinue), (dialog, which) -> {
dialog.dismiss();
login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
@ -444,7 +448,9 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else if (responseVersion.code() == 403) {
login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_);
}
}
@ -456,6 +462,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
else if (loginType == 2) { // token
letTheUserInViaToken(instanceUrl, loginToken_);
}
}
@Override
@ -463,16 +470,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Log.e("onFailure-version", t.toString());
if(t instanceof NoRouteToHostException) {
Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedUrl));
}
else {
Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError));
}
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
});
@ -483,7 +480,10 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
final TinyDB tinyDb = new TinyDB(getApplicationContext());
Call<UserInfo> call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getUserInfo("token " + loginToken_);
Call<UserInfo> call = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getUserInfo("token " + loginToken_);
call.enqueue(new Callback<UserInfo>() {
@ -500,6 +500,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
assert userDetails != null;
tinyDb.putString(userDetails.getLogin() + "-token", loginToken_);
tinyDb.putString("loginUid", userDetails.getLogin());
tinyDb.putString("userLogin", userDetails.getUsername());
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -537,6 +538,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
});
@ -548,10 +550,20 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Call<List<UserTokens>> call;
if(loginOTP != 0) {
call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getUserTokensWithOTP(credential, loginOTP, loginUid);
call = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getUserTokensWithOTP(credential, loginOTP, loginUid);
}
else {
call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getUserTokens(credential, loginUid);
call = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getUserTokens(credential, loginUid);
}
call.enqueue(new Callback<List<UserTokens>>() {
@ -604,10 +616,20 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Call<UserTokens> callCreateToken;
if(loginOTP != 0) {
callCreateToken = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().createNewTokenWithOTP(credential, loginOTP, loginUid, createUserToken);
callCreateToken = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.createNewTokenWithOTP(credential, loginOTP, loginUid, createUserToken);
}
else {
callCreateToken = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().createNewToken(credential, loginUid, createUserToken);
callCreateToken = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.createNewToken(credential, loginUid, createUserToken);
}
callCreateToken.enqueue(new Callback<UserTokens>() {
@ -625,11 +647,28 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
if (!newToken.getSha1().equals("")) {
Call<UserInfo> call = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getUserInfo("token " + newToken.getSha1());
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(@NonNull Call<UserInfo> call, @NonNull retrofit2.Response<UserInfo> response) {
UserInfo userDetails = response.body();
if (response.isSuccessful()) {
if (response.code() == 200) {
tinyDb.remove("loginPass");
tinyDb.putBoolean("loggedInMode", true);
assert userDetails != null;
tinyDb.putString("userLogin", userDetails.getUsername());
tinyDb.putString(loginUid + "-token", newToken.getSha1());
tinyDb.putString(loginUid + "-token-last-eight", appUtil.getLastCharactersOfWord(newToken.getSha1(), 8));
//Log.i("Tokens", "new:" + newToken.getSha1() + " old:" + tinyDb.getString(loginUid + "-token"));
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
@ -637,6 +676,41 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
}
else if(response.code() == 401) {
String toastError = getResources().getString(R.string.unauthorizedApiError);
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
}
@Override
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
});
}
}
}
else if(responseCreate.code() == 500) {
@ -651,7 +725,9 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
@Override
public void onFailure(@NonNull Call<UserTokens> createUserToken, Throwable t) {
public void onFailure(@NonNull Call<UserTokens> createUserToken, @NonNull Throwable t) {
Log.e("onFailure-token", t.toString());
}
@ -659,7 +735,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else {
//Log.i("Current Token", tinyDb.getString(loginUid + "-token"));
tinyDb.putBoolean("loggedInMode", true);
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
@ -680,8 +755,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
//Log.i("error message else4", String.valueOf(response.code()));
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -697,6 +770,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
});