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 5f640399..a8df33b4 100644 --- a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java @@ -48,678 +48,752 @@ import retrofit2.Callback; public class LoginActivity extends BaseActivity implements View.OnClickListener { - private Button loginButton; - private EditText instanceUrlET, loginUidET, loginPassword, otpCode, loginTokenCode; - private Spinner protocolSpinner; - private TextView otpInfo; - private RadioGroup loginMethod; - final Context ctx = this; - private String device_id = "token"; + private Button loginButton; + private EditText instanceUrlET, loginUidET, loginPassword, otpCode, loginTokenCode; + private Spinner protocolSpinner; + private TextView otpInfo; + private RadioGroup loginMethod; + final Context ctx = this; + private String device_id = "token"; - @Override - protected int getLayoutResourceId() { + @Override + protected int getLayoutResourceId(){ + return R.layout.activity_login; + } - return R.layout.activity_login; - } + @Override + public void onCreate(Bundle savedInstanceState) { - @Override - public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); - super.onCreate(savedInstanceState); + TinyDB tinyDb = new TinyDB(getApplicationContext()); + NetworkObserver networkMonitor = new NetworkObserver(this); - TinyDB tinyDb = new TinyDB(getApplicationContext()); - NetworkObserver networkMonitor = new NetworkObserver(this); + loginButton = findViewById(R.id.login_button); + instanceUrlET = findViewById(R.id.instance_url); + loginUidET = findViewById(R.id.login_uid); + loginPassword = findViewById(R.id.login_passwd); + otpCode = findViewById(R.id.otpCode); + otpInfo = findViewById(R.id.otpInfo); + ImageView info_button = findViewById(R.id.info); + final TextView viewTextAppVersion = findViewById(R.id.appVersion); + protocolSpinner = findViewById(R.id.httpsSpinner); + loginMethod = findViewById(R.id.loginMethod); + loginTokenCode = findViewById(R.id.loginTokenCode); - loginButton = findViewById(R.id.login_button); - instanceUrlET = findViewById(R.id.instance_url); - loginUidET = findViewById(R.id.login_uid); - loginPassword = findViewById(R.id.login_passwd); - otpCode = findViewById(R.id.otpCode); - otpInfo = findViewById(R.id.otpInfo); - ImageView info_button = findViewById(R.id.info); - final TextView viewTextAppVersion = findViewById(R.id.appVersion); - protocolSpinner = findViewById(R.id.httpsSpinner); - loginMethod = findViewById(R.id.loginMethod); - loginTokenCode = findViewById(R.id.loginTokenCode); + viewTextAppVersion.setText(AppUtil.getAppVersion(getApplicationContext())); - viewTextAppVersion.setText(AppUtil.getAppVersion(getApplicationContext())); + Resources res = getResources(); + String[] allProtocols = res.getStringArray(R.array.protocolValues); - Resources res = getResources(); - String[] allProtocols = res.getStringArray(R.array.protocolValues); + final ArrayAdapter adapterProtocols = new ArrayAdapter(Objects.requireNonNull(getApplicationContext()), + R.layout.spinner_item, allProtocols); - final ArrayAdapter adapterProtocols = new ArrayAdapter(Objects.requireNonNull(getApplicationContext()), R.layout.spinner_item, allProtocols); + adapterProtocols.setDropDownViewResource(R.layout.spinner_dropdown_item); + protocolSpinner.setAdapter(adapterProtocols); - adapterProtocols.setDropDownViewResource(R.layout.spinner_dropdown_item); - protocolSpinner.setAdapter(adapterProtocols); + protocolSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - protocolSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + public void onItemSelected(AdapterView parent, View view, int pos, long id) { - public void onItemSelected(AdapterView parent, View view, int pos, long id) { + String value = getResources().getStringArray(R.array.protocolValues)[pos]; + if(value.toLowerCase().equals("http")) { + Toasty.info(getApplicationContext(), getResources().getString(R.string.protocolError)); + } - String value = getResources().getStringArray(R.array.protocolValues)[pos]; - if(value.toLowerCase().equals("http")) { - Toasty.info(getApplicationContext(), getResources().getString(R.string.protocolError)); - } + } - } + public void onNothingSelected(AdapterView parent) { + } - public void onNothingSelected(AdapterView parent) { + }); - } - }); + info_button.setOnClickListener(infoListener); - info_button.setOnClickListener(infoListener); + loginMethod.setOnCheckedChangeListener((group, checkedId) -> { - loginMethod.setOnCheckedChangeListener((group, checkedId) -> { + if(checkedId == R.id.loginToken) { - if(checkedId == R.id.loginToken) { + loginUidET.setVisibility(View.GONE); + loginPassword.setVisibility(View.GONE); + otpCode.setVisibility(View.GONE); + otpInfo.setVisibility(View.GONE); + loginTokenCode.setVisibility(View.VISIBLE); - loginUidET.setVisibility(View.GONE); - loginPassword.setVisibility(View.GONE); - otpCode.setVisibility(View.GONE); - otpInfo.setVisibility(View.GONE); - loginTokenCode.setVisibility(View.VISIBLE); + } + else { - } - else { + loginUidET.setVisibility(View.VISIBLE); + loginPassword.setVisibility(View.VISIBLE); + otpCode.setVisibility(View.VISIBLE); + otpInfo.setVisibility(View.VISIBLE); + loginTokenCode.setVisibility(View.GONE); - loginUidET.setVisibility(View.VISIBLE); - loginPassword.setVisibility(View.VISIBLE); - otpCode.setVisibility(View.VISIBLE); - otpInfo.setVisibility(View.VISIBLE); - loginTokenCode.setVisibility(View.GONE); + } - } + }); - }); + networkMonitor.onInternetStateListener(isAvailable -> { - networkMonitor.onInternetStateListener(isAvailable -> { + if(isAvailable) { + enableProcessButton(); + } + else { + disableProcessButton(); + Toasty.info(getApplicationContext(), getResources().getString(R.string.checkNetConnection)); + } - if(isAvailable) { - enableProcessButton(); - } - else { - disableProcessButton(); - Toasty.info(getApplicationContext(), getResources().getString(R.string.checkNetConnection)); - } + }); - }); + //login_button.setOnClickListener(this); + if(!tinyDb.getString("instanceUrlRaw").isEmpty()) { + instanceUrlET.setText(tinyDb.getString("instanceUrlRaw")); + } + if(!tinyDb.getString("loginUid").isEmpty()) { + loginUidET.setText(tinyDb.getString("loginUid")); + } - //login_button.setOnClickListener(this); - if(!tinyDb.getString("instanceUrlRaw").isEmpty()) { - instanceUrlET.setText(tinyDb.getString("instanceUrlRaw")); - } - if(!tinyDb.getString("loginUid").isEmpty()) { - loginUidET.setText(tinyDb.getString("loginUid")); - } + if(tinyDb.getBoolean("loggedInMode")) { - if(tinyDb.getBoolean("loggedInMode")) { + startActivity(new Intent(LoginActivity.this, MainActivity.class)); + finish(); - startActivity(new Intent(LoginActivity.this, MainActivity.class)); - finish(); + } - } + loginButton.setOnClickListener(loginListener); - loginButton.setOnClickListener(loginListener); + if(!tinyDb.getString("uniqueAppId").isEmpty()) { + device_id = tinyDb.getString("uniqueAppId"); + } + else { + device_id = UUID.randomUUID().toString(); + tinyDb.putString("uniqueAppId", device_id); + } - if(!tinyDb.getString("uniqueAppId").isEmpty()) { - device_id = tinyDb.getString("uniqueAppId"); - } - else { - device_id = UUID.randomUUID().toString(); - tinyDb.putString("uniqueAppId", device_id); - } + } - } + @Override + public void onClick(View v) { - @Override - public void onClick(View v) { + if (v.getId() == R.id.login_button) { + login(); + } - if(v.getId() == R.id.login_button) { - login(); - } + } - } + private View.OnClickListener loginListener = new View.OnClickListener() { + public void onClick(View v) { - private View.OnClickListener loginListener = new View.OnClickListener() { + disableProcessButton(); + loginButton.setText(R.string.processingText); + login(); - public void onClick(View v) { + } + }; - disableProcessButton(); - loginButton.setText(R.string.processingText); - login(); + 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(); + } + }; - } - }; + @SuppressLint("ResourceAsColor") + private void login() { - private View.OnClickListener infoListener = new View.OnClickListener() { + TinyDB tinyDb = new TinyDB(getApplicationContext()); + AppUtil appUtil = new AppUtil(); + boolean connToInternet = AppUtil.haveNetworkConnection(LoginActivity.this); - public void onClick(View v) { + String instanceUrl = instanceUrlET.getText().toString().trim(); + String loginUid = loginUidET.getText().toString(); + String loginPass = loginPassword.getText().toString(); + String protocol = protocolSpinner.getSelectedItem().toString(); + String loginOTP_ = otpCode.getText().toString().trim(); + int loginMethodType = loginMethod.getCheckedRadioButtonId(); + String loginToken_ = loginTokenCode.getText().toString().trim(); - 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(); - } - }; + if(loginMethodType == R.id.loginUsernamePassword) { - @SuppressLint("ResourceAsColor") - private void login() { + if(instanceUrl.contains("@")) { - TinyDB tinyDb = new TinyDB(getApplicationContext()); - AppUtil appUtil = new AppUtil(); - boolean connToInternet = AppUtil.haveNetworkConnection(LoginActivity.this); + String[] urlForHttpAuth = instanceUrl.split("@"); - String instanceUrl = instanceUrlET.getText().toString().trim(); - String loginUid = loginUidET.getText().toString(); - String loginPass = loginPassword.getText().toString(); - String protocol = protocolSpinner.getSelectedItem().toString(); - String loginOTP_ = otpCode.getText().toString().trim(); - int loginMethodType = loginMethod.getCheckedRadioButtonId(); - String loginToken_ = loginTokenCode.getText().toString().trim(); + tinyDb.putString("basicAuthPassword", loginPass); + tinyDb.putBoolean("basicAuthFlag", true); - if(loginMethodType == R.id.loginUsernamePassword) { + instanceUrl = urlForHttpAuth[1]; + loginUid = urlForHttpAuth[0]; - if(instanceUrl.contains("@")) { + } - String[] urlForHttpAuth = instanceUrl.split("@"); + String instanceHost; + if(AppUtil.httpCheck(instanceUrl)) { - tinyDb.putString("basicAuthPassword", loginPass); - tinyDb.putBoolean("basicAuthFlag", true); + URI uri = null; + try { + uri = new URI(instanceUrl); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + assert uri != null; + instanceHost = uri.getHost(); - instanceUrl = urlForHttpAuth[1]; - loginUid = urlForHttpAuth[0]; + } + else { + instanceHost = instanceUrl; + } - } + String instanceUrlWithProtocol; + if(protocol.toLowerCase().equals("https")) { + instanceUrl = "https://" + instanceHost + "/api/v1/"; + instanceUrlWithProtocol = "https://" + instanceHost; + } + else { + instanceUrl = "http://" + instanceHost + "/api/v1/"; + instanceUrlWithProtocol = "http://" + instanceHost; + } - String instanceHost; - if(AppUtil.httpCheck(instanceUrl)) { + tinyDb.putString("instanceUrlRaw", instanceHost); + tinyDb.putString("loginUid", loginUid); + tinyDb.putString("instanceUrl", instanceUrl); + tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol); - URI uri = null; - try { - uri = new URI(instanceUrl); - } - catch(URISyntaxException e) { - e.printStackTrace(); - } - assert uri != null; - instanceHost = uri.getHost(); + if(connToInternet) { - } - else { - instanceHost = instanceUrl; - } + if(instanceUrlET.getText().toString().equals("")) { - String instanceUrlWithProtocol; - if(protocol.toLowerCase().equals("https")) { - instanceUrl = "https://" + instanceHost + "/api/v1/"; - instanceUrlWithProtocol = "https://" + instanceHost; - } - else { - instanceUrl = "http://" + instanceHost + "/api/v1/"; - instanceUrlWithProtocol = "http://" + instanceHost; - } + Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + return; - tinyDb.putString("instanceUrlRaw", instanceHost); - tinyDb.putString("loginUid", loginUid); - tinyDb.putString("instanceUrl", instanceUrl); - tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol); + } + if(loginUid.equals("")) { - if(connToInternet) { + Toasty.info(getApplicationContext(), getString(R.string.emptyFieldUsername)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + return; - if(instanceUrlET.getText().toString().equals("")) { + } + if(loginPassword.getText().toString().equals("")) { - Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - return; + Toasty.info(getApplicationContext(), getString(R.string.emptyFieldPassword)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + return; - } - if(loginUid.equals("")) { + } - Toasty.info(getApplicationContext(), getString(R.string.emptyFieldUsername)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - return; + int loginOTP = 0; + if(loginOTP_.length() == 6) { - } - if(loginPassword.getText().toString().equals("")) { + if(appUtil.checkIntegers(loginOTP_)) { - Toasty.info(getApplicationContext(), getString(R.string.emptyFieldPassword)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - return; + loginOTP = Integer.parseInt(loginOTP_); + } + else { - } + Toasty.info(getApplicationContext(), getString(R.string.loginOTPTypeError)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + return; - int loginOTP = 0; - if(loginOTP_.length() == 6) { + } - if(appUtil.checkIntegers(loginOTP_)) { + } - loginOTP = Integer.parseInt(loginOTP_); - } - else { + versionCheck(instanceUrl, loginUid, loginPass, loginOTP, loginToken_, 1); - Toasty.info(getApplicationContext(), getString(R.string.loginOTPTypeError)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - return; + } + else { - } + Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection)); - } + } - versionCheck(instanceUrl, loginUid, loginPass, loginOTP, loginToken_, 1); + } + else { - } - else { + String instanceHost; + if(AppUtil.httpCheck(instanceUrl)) { - Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection)); + URI uri = null; + try { + uri = new URI(instanceUrl); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + assert uri != null; + instanceHost = uri.getHost(); - } + } + else { + instanceHost = instanceUrl; + } - } - else { + String instanceUrlWithProtocol; + if(protocol.toLowerCase().equals("https")) { + instanceUrl = "https://" + instanceHost + "/api/v1/"; + instanceUrlWithProtocol = "https://" + instanceHost; + } + else { + instanceUrl = "http://" + instanceHost + "/api/v1/"; + instanceUrlWithProtocol = "http://" + instanceHost; + } - String instanceHost; - if(AppUtil.httpCheck(instanceUrl)) { + tinyDb.putString("instanceUrlRaw", instanceHost); + //tinyDb.putString("loginUid", loginUid); + tinyDb.putString("instanceUrl", instanceUrl); + tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol); - URI uri = null; - try { - uri = new URI(instanceUrl); - } - catch(URISyntaxException e) { - e.printStackTrace(); - } - assert uri != null; - instanceHost = uri.getHost(); + if(connToInternet) { - } - else { - instanceHost = instanceUrl; - } + if (instanceUrlET.getText().toString().equals("")) { - String instanceUrlWithProtocol; - if(protocol.toLowerCase().equals("https")) { - instanceUrl = "https://" + instanceHost + "/api/v1/"; - instanceUrlWithProtocol = "https://" + instanceHost; - } - else { - instanceUrl = "http://" + instanceHost + "/api/v1/"; - instanceUrlWithProtocol = "http://" + instanceHost; - } + Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + return; - tinyDb.putString("instanceUrlRaw", instanceHost); - //tinyDb.putString("loginUid", loginUid); - tinyDb.putString("instanceUrl", instanceUrl); - tinyDb.putString("instanceUrlWithProtocol", instanceUrlWithProtocol); + } + if (loginToken_.equals("")) { - if(connToInternet) { + Toasty.info(getApplicationContext(), getString(R.string.loginTokenError)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + return; - if(instanceUrlET.getText().toString().equals("")) { + } - Toasty.info(getApplicationContext(), getString(R.string.emptyFieldURL)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - return; + versionCheck(instanceUrl, loginUid, loginPass, 123, loginToken_, 2); + } + else { - } - if(loginToken_.equals("")) { + Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection)); - Toasty.info(getApplicationContext(), getString(R.string.loginTokenError)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - return; + } - } + } - versionCheck(instanceUrl, loginUid, loginPass, 123, loginToken_, 2); - } - else { + } - Toasty.info(getApplicationContext(), getString(R.string.checkNetConnection)); + private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP, final String loginToken_, final int loginType) { - } + Call callVersion = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getGiteaVersion(); - } + callVersion.enqueue(new Callback() { - } + @Override + public void onResponse(@NonNull final Call callVersion, @NonNull retrofit2.Response responseVersion) { - private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP, final String loginToken_, final int loginType) { + if (responseVersion.code() == 200) { - final TinyDB tinyDb = new TinyDB(getApplicationContext()); + GiteaVersion version = responseVersion.body(); + assert version != null; - Call callVersion = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getGiteaVersion(); + VersionCheck vt = VersionCheck.check(getString(R.string.versionLow), getString(R.string.versionHigh), version.getVersion()); - callVersion.enqueue(new Callback() { + switch (vt) { + case UNSUPPORTED_NEW: + //Toasty.info(getApplicationContext(), getString(R.string.versionUnsupportedNew)); + case SUPPORTED_LATEST: + case SUPPORTED_OLD: + case DEVELOPMENT: + login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_); + return; + case UNSUPPORTED_OLD: - @Override - public void onResponse(@NonNull final Call callVersion, @NonNull retrofit2.Response responseVersion) { + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx); - if(responseVersion.code() == 200) { + 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) -> { - GiteaVersion version = responseVersion.body(); - assert version != null; + dialog.dismiss(); + enableProcessButton(); - VersionCheck vt = VersionCheck.check(getString(R.string.versionLow), getString(R.string.versionHigh), version.getVersion()); + }) + .setPositiveButton(getString(R.string.textContinue), (dialog, which) -> { - switch(vt) { - case UNSUPPORTED_NEW: - //Toasty.info(getApplicationContext(), getString(R.string.versionUnsupportedNew)); - case SUPPORTED_LATEST: - case SUPPORTED_OLD: - case DEVELOPMENT: - login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_); - return; - case UNSUPPORTED_OLD: + dialog.dismiss(); + login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_); - 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() { + AlertDialog alertDialog = alertDialogBuilder.create(); - @Override - public void onClick(DialogInterface dialog, int which) { + alertDialog.show(); + return; + default: // UNKNOWN + Toasty.info(getApplicationContext(), getString(R.string.versionUnknow)); + enableProcessButton(); - dialog.dismiss(); - enableProcessButton(); - } - }).setPositiveButton(getString(R.string.textContinue), new DialogInterface.OnClickListener() { + } - @Override - public void onClick(DialogInterface dialog, int which) { + } + else if (responseVersion.code() == 403) { - dialog.dismiss(); - login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_); + login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_); - } - }); + } + } - AlertDialog alertDialog = alertDialogBuilder.create(); + private void login(int loginType, String instanceUrl, String loginUid, String loginPass, int loginOTP, String loginToken_) { - alertDialog.show(); - return; - default: // UNKNOWN - Toasty.info(getApplicationContext(), getString(R.string.versionUnknow)); - enableProcessButton(); + if (loginType == 1) { + letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP); + } + else if (loginType == 2) { // token + letTheUserInViaToken(instanceUrl, loginToken_); + } - } + } - } - else if(responseVersion.code() == 403) { - login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_); - } - } + @Override + public void onFailure(@NonNull Call callVersion, Throwable t) { - private void login(int loginType, String instanceUrl, String loginUid, String loginPass, int loginOTP, String loginToken_) { + Log.e("onFailure-version", t.toString()); - if(loginType == 1) { - letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP); - } - else if(loginType == 2) { // token - letTheUserInViaToken(instanceUrl, loginToken_); - } - } + } - @Override - public void onFailure(@NonNull Call callVersion, Throwable t) { + }); - 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)); - } + private void letTheUserInViaToken(String instanceUrl, final String loginToken_) { - enableProcessButton(); - loginButton.setText(R.string.btnLogin); + final TinyDB tinyDb = new TinyDB(getApplicationContext()); - } + Call call = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getUserInfo("token " + loginToken_); - }); + call.enqueue(new Callback() { - } + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - private void letTheUserInViaToken(String instanceUrl, final String loginToken_) { + UserInfo userDetails = response.body(); - final TinyDB tinyDb = new TinyDB(getApplicationContext()); + if (response.isSuccessful()) { - Call call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getUserInfo("token " + loginToken_); + if (response.code() == 200) { + + tinyDb.putBoolean("loggedInMode", true); + assert userDetails != null; + tinyDb.putString(userDetails.getLogin() + "-token", loginToken_); + tinyDb.putString("loginUid", userDetails.getLogin()); + tinyDb.putString("userLogin", userDetails.getUsername()); - call.enqueue(new Callback() { + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + startActivity(new Intent(LoginActivity.this, MainActivity.class)); + finish(); - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + } - UserInfo userDetails = response.body(); + } + else if(response.code() == 401) { - if(response.isSuccessful()) { + String toastError = getResources().getString(R.string.unauthorizedApiError); + Toasty.info(getApplicationContext(), toastError); - if(response.code() == 200) { + enableProcessButton(); + loginButton.setText(R.string.btnLogin); - tinyDb.putBoolean("loggedInMode", true); - assert userDetails != null; - tinyDb.putString(userDetails.getLogin() + "-token", loginToken_); - tinyDb.putString("loginUid", userDetails.getLogin()); + } + else { - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - startActivity(new Intent(LoginActivity.this, MainActivity.class)); - finish(); + String toastError = getResources().getString(R.string.genericApiStatusError) + response.code(); + Toasty.info(getApplicationContext(), toastError); - } + enableProcessButton(); + loginButton.setText(R.string.btnLogin); - } - else if(response.code() == 401) { + } - String toastError = getResources().getString(R.string.unauthorizedApiError); - Toasty.info(getApplicationContext(), toastError); + } - enableProcessButton(); - loginButton.setText(R.string.btnLogin); + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { - } - else { + Log.e("onFailure", t.toString()); + Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); - String toastError = getResources().getString(R.string.genericApiStatusError) + response.code(); - Toasty.info(getApplicationContext(), toastError); + } + }); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); + } - } + private void letTheUserIn(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) { - } + final String credential = Credentials.basic(loginUid, loginPass, StandardCharsets.UTF_8); - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Call> call; + if(loginOTP != 0) { - Log.e("onFailure", t.toString()); - Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - } - }); + call = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getUserTokensWithOTP(credential, loginOTP, loginUid); - } + } + else { - private void letTheUserIn(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP) { + call = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getUserTokens(credential, loginUid); - final String credential = Credentials.basic(loginUid, loginPass, StandardCharsets.UTF_8); + } - Call> call; - if(loginOTP != 0) { - call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getUserTokensWithOTP(credential, loginOTP, loginUid); - } - else { - call = RetrofitClient.getInstance(instanceUrl, getApplicationContext()).getApiInterface().getUserTokens(credential, loginUid); - } + call.enqueue(new Callback>() { - call.enqueue(new Callback>() { + @Override + public void onResponse(@NonNull Call> call, @NonNull retrofit2.Response> response) { - @Override - public void onResponse(@NonNull Call> call, @NonNull retrofit2.Response> response) { + List userTokens = response.body(); + final TinyDB tinyDb = new TinyDB(getApplicationContext()); + final AppUtil appUtil = new AppUtil(); + //Headers responseHeaders = response.headers(); - List userTokens = response.body(); - final TinyDB tinyDb = new TinyDB(getApplicationContext()); - final AppUtil appUtil = new AppUtil(); - //Headers responseHeaders = response.headers(); + if (response.isSuccessful()) { - if(response.isSuccessful()) { + if (response.code() == 200) { - if(response.code() == 200) { + boolean setTokenFlag = false; - boolean setTokenFlag = false; + assert userTokens != null; + if (userTokens.size() > 0) { - assert userTokens != null; - if(userTokens.size() > 0) { + if(userTokens.get(0).getToken_last_eight() != null) { - if(userTokens.get(0).getToken_last_eight() != null) { + for (int i = 0; i < userTokens.size(); i++) { + if (userTokens.get(i).getToken_last_eight().equals(tinyDb.getString(loginUid + "-token-last-eight"))) { + setTokenFlag = true; + break; + } + //Log.i("Tokens: ", userTokens.get(i).getToken_last_eight()); + } - for(int i = 0; i < userTokens.size(); i++) { - if(userTokens.get(i).getToken_last_eight().equals(tinyDb.getString(loginUid + "-token-last-eight"))) { - setTokenFlag = true; - break; - } - //Log.i("Tokens: ", userTokens.get(i).getToken_last_eight()); - } + } + else { - } - else { + for (int i = 0; i < userTokens.size(); i++) { + if (userTokens.get(i).getSha1().equals(tinyDb.getString(loginUid + "-token"))) { + setTokenFlag = true; + break; + } + //Log.i("Tokens: ", userTokens.get(i).getSha1()); + } - for(int i = 0; i < userTokens.size(); i++) { - if(userTokens.get(i).getSha1().equals(tinyDb.getString(loginUid + "-token"))) { - setTokenFlag = true; - break; - } - //Log.i("Tokens: ", userTokens.get(i).getSha1()); - } + } - } + } - } + if(tinyDb.getString(loginUid + "-token").isEmpty() || !setTokenFlag) { - if(tinyDb.getString(loginUid + "-token").isEmpty() || !setTokenFlag) { + UserTokens createUserToken = new UserTokens("gitnex-app-" + device_id); - UserTokens createUserToken = new UserTokens("gitnex-app-" + device_id); + Call callCreateToken; + if(loginOTP != 0) { - Call callCreateToken; - if(loginOTP != 0) { - 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() + .createNewTokenWithOTP(credential, loginOTP, loginUid, createUserToken); - callCreateToken.enqueue(new Callback() { + } + else { - @Override - public void onResponse(@NonNull Call callCreateToken, @NonNull retrofit2.Response responseCreate) { + callCreateToken = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .createNewToken(credential, loginUid, createUserToken); - if(responseCreate.isSuccessful()) { + } - if(responseCreate.code() == 201) { + callCreateToken.enqueue(new Callback() { - UserTokens newToken = responseCreate.body(); - assert newToken != null; - //Log.i("Tokens-NEW", "new:" + newToken.getSha1()); + @Override + public void onResponse(@NonNull Call callCreateToken, @NonNull retrofit2.Response responseCreate) { - if(!newToken.getSha1().equals("")) { + if(responseCreate.isSuccessful()) { - tinyDb.remove("loginPass"); - tinyDb.putBoolean("loggedInMode", true); - 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")); + if(responseCreate.code() == 201) { - startActivity(new Intent(LoginActivity.this, MainActivity.class)); - finish(); + UserTokens newToken = responseCreate.body(); + assert newToken != null; + //Log.i("Tokens-NEW", "new:" + newToken.getSha1()); - } + if (!newToken.getSha1().equals("")) { - } + Call call = RetrofitClient + .getInstance(instanceUrl, getApplicationContext()) + .getApiInterface() + .getUserInfo("token " + newToken.getSha1()); - } - else if(responseCreate.code() == 500) { + call.enqueue(new Callback() { - String toastError = getResources().getString(R.string.genericApiStatusError) + responseCreate.code(); - Toasty.info(getApplicationContext(), toastError); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - } + UserInfo userDetails = response.body(); - } + if (response.isSuccessful()) { - @Override - public void onFailure(@NonNull Call createUserToken, Throwable t) { + 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)); - }); - } - else { + startActivity(new Intent(LoginActivity.this, MainActivity.class)); + finish(); - //Log.i("Current Token", tinyDb.getString(loginUid + "-token")); - tinyDb.putBoolean("loggedInMode", true); - startActivity(new Intent(LoginActivity.this, MainActivity.class)); - finish(); + } - } + } + else if(response.code() == 401) { - } + String toastError = getResources().getString(R.string.unauthorizedApiError); + Toasty.info(getApplicationContext(), toastError); - } - else if(response.code() == 500) { + enableProcessButton(); + loginButton.setText(R.string.btnLogin); - String toastError = getResources().getString(R.string.genericApiStatusError) + response.code(); - Toasty.info(getApplicationContext(), toastError); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); + } + else { - } - else { + String toastError = getResources().getString(R.string.genericApiStatusError) + response.code(); + Toasty.info(getApplicationContext(), toastError); - String toastError = getResources().getString(R.string.genericApiStatusError) + response.code(); - //Log.i("error message else4", String.valueOf(response.code())); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); - Toasty.info(getApplicationContext(), toastError); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); + } - } + } - } + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { - @Override - public void onFailure(@NonNull Call> call, @NonNull Throwable t) { + Log.e("onFailure", t.toString()); + Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); - Log.e("onFailure-login", t.toString()); - Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson)); - enableProcessButton(); - loginButton.setText(R.string.btnLogin); - } - }); + } + }); - } + } - private void disableProcessButton() { + } - loginButton.setEnabled(false); - GradientDrawable shape = new GradientDrawable(); - shape.setCornerRadius(8); - shape.setColor(getResources().getColor(R.color.hintColor)); - loginButton.setBackground(shape); + } + else if(responseCreate.code() == 500) { - } + String toastError = getResources().getString(R.string.genericApiStatusError) + responseCreate.code(); + Toasty.info(getApplicationContext(), toastError); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); - private void enableProcessButton() { + } - loginButton.setEnabled(true); - GradientDrawable shape = new GradientDrawable(); - shape.setCornerRadius(8); - shape.setColor(getResources().getColor(R.color.btnBackground)); - loginButton.setBackground(shape); + } - } + @Override + public void onFailure(@NonNull Call createUserToken, @NonNull Throwable t) { + + Log.e("onFailure-token", t.toString()); + + } + + }); + } + else { + + tinyDb.putBoolean("loggedInMode", true); + startActivity(new Intent(LoginActivity.this, MainActivity.class)); + finish(); + + } + + } + + } + else if(response.code() == 500) { + + String toastError = getResources().getString(R.string.genericApiStatusError) + response.code(); + 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> call, @NonNull Throwable t) { + + Log.e("onFailure-login", t.toString()); + Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson)); + enableProcessButton(); + loginButton.setText(R.string.btnLogin); + + } + }); + + } + + private void disableProcessButton() { + + loginButton.setEnabled(false); + GradientDrawable shape = new GradientDrawable(); + shape.setCornerRadius( 8 ); + shape.setColor(getResources().getColor(R.color.hintColor)); + loginButton.setBackground(shape); + + } + + private void enableProcessButton() { + + loginButton.setEnabled(true); + GradientDrawable shape = new GradientDrawable(); + shape.setCornerRadius( 8 ); + shape.setColor(getResources().getColor(R.color.btnBackground)); + loginButton.setBackground(shape); + + } }