|
|
|
@ -1,356 +1,3 @@
|
|
|
|
|
/*
|
|
|
|
|
package com.uam.wmi.findmytutor.activity;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
import android.annotation.TargetApi;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.support.design.widget.Snackbar;
|
|
|
|
|
import android.support.design.widget.TextInputLayout;
|
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
|
|
|
|
|
import com.auth0.android.jwt.Claim;
|
|
|
|
|
import com.auth0.android.jwt.JWT;
|
|
|
|
|
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
|
|
|
|
|
import com.jakewharton.rxbinding.widget.RxTextView;
|
|
|
|
|
import com.uam.wmi.findmytutor.R;
|
|
|
|
|
import com.uam.wmi.findmytutor.model.JwtToken;
|
|
|
|
|
import com.uam.wmi.findmytutor.model.LdapUser;
|
|
|
|
|
import com.uam.wmi.findmytutor.model.User;
|
|
|
|
|
import com.uam.wmi.findmytutor.network.ApiClient;
|
|
|
|
|
import com.uam.wmi.findmytutor.service.LdapService;
|
|
|
|
|
import com.uam.wmi.findmytutor.service.UserService;
|
|
|
|
|
import com.uam.wmi.findmytutor.utils.LocaleHelper;
|
|
|
|
|
import com.uam.wmi.findmytutor.utils.PrefUtils;
|
|
|
|
|
import com.uam.wmi.findmytutor.utils.RestApiHelper;
|
|
|
|
|
|
|
|
|
|
import org.reactivestreams.Subscriber;
|
|
|
|
|
import org.reactivestreams.Subscription;
|
|
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
import okhttp3.ResponseBody;
|
|
|
|
|
import rx.functions.Func1;
|
|
|
|
|
|
|
|
|
|
public class
|
|
|
|
|
LoginActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
|
|
@BindView(R.id.email_til)
|
|
|
|
|
TextInputLayout emailInputLayout;
|
|
|
|
|
@BindView(R.id.password_til)
|
|
|
|
|
TextInputLayout passwordInputLayout;
|
|
|
|
|
@BindView(R.id.email)
|
|
|
|
|
EditText loginText;
|
|
|
|
|
@BindView(R.id.password)
|
|
|
|
|
EditText passwordText;
|
|
|
|
|
@BindView(R.id.email_login_form)
|
|
|
|
|
LinearLayout signInLinearLayout;
|
|
|
|
|
@BindView(R.id.sign_in_btn)
|
|
|
|
|
Button signInButton;
|
|
|
|
|
@BindView(R.id.login_progress)
|
|
|
|
|
View mProgressView;
|
|
|
|
|
@BindView(R.id.login_form)
|
|
|
|
|
View mLoginFormView;
|
|
|
|
|
|
|
|
|
|
private LdapService ldapService;
|
|
|
|
|
private UserService userService;
|
|
|
|
|
private CompositeDisposable disposable = new CompositeDisposable();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void attachBaseContext(Context base) {
|
|
|
|
|
super.attachBaseContext(LocaleHelper.onAttach(base));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
setContentView(R.layout.activity_login);
|
|
|
|
|
ButterKnife.bind(this);
|
|
|
|
|
|
|
|
|
|
rx.Observable<CharSequence> loginObservable = RxTextView.textChanges(loginText);
|
|
|
|
|
rx.Observable<CharSequence> passwordObservable = RxTextView.textChanges(passwordText);
|
|
|
|
|
*/
|
|
|
|
|
/* Observable<Boolean> combinedObservables = Observable
|
|
|
|
|
.combineLatest(loginObservable, passwordObservable,
|
|
|
|
|
(o1, o2) -> isValidEmail(o1) && isValidPassword(o2));*//*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Subscription emailSubscription = loginObservable
|
|
|
|
|
.doOnNext(charSequence -> hideEmailError())
|
|
|
|
|
.debounce(400, TimeUnit.MILLISECONDS)
|
|
|
|
|
.filter(new Func1<CharSequence, Boolean>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean call(CharSequence charSequence) {
|
|
|
|
|
return !TextUtils.isEmpty(charSequence);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread()) // UI Thread
|
|
|
|
|
.subscribe(new Subscriber<CharSequence>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Throwable e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onComplete() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSubscribe(Subscription s) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onNext(CharSequence charSequence) {
|
|
|
|
|
boolean isEmailValid = validateEmail(charSequence.toString());
|
|
|
|
|
if (!isEmailValid) {
|
|
|
|
|
showEmailError();
|
|
|
|
|
} else {
|
|
|
|
|
hideEmailError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ldapService = ApiClient.getClient(getApplicationContext())
|
|
|
|
|
.create(LdapService.class);
|
|
|
|
|
userService = ApiClient.getClient(getApplicationContext())
|
|
|
|
|
.create(UserService.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
passwordText.setOnEditorActionListener((textView, id, keyEvent) -> {
|
|
|
|
|
if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
|
|
|
|
|
attemptLogin();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Button mEmailSignInButton = findViewById(R.id.email_sign_in_button);
|
|
|
|
|
mEmailSignInButton.setOnClickListener(view -> attemptLogin());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void hideEmailError(){
|
|
|
|
|
loginText.setError(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// region Helper Methods
|
|
|
|
|
private void enableError(TextInputLayout textInputLayout) {
|
|
|
|
|
if (textInputLayout.getChildCount() == 2)
|
|
|
|
|
textInputLayout.getChildAt(1).setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void disableError(TextInputLayout textInputLayout) {
|
|
|
|
|
if (textInputLayout.getChildCount() == 2)
|
|
|
|
|
textInputLayout.getChildAt(1).setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void showEmailError(){
|
|
|
|
|
enableError(emailInputLayout);
|
|
|
|
|
// emailInputLayout.setErrorEnabled(true);
|
|
|
|
|
emailInputLayout.setError(getString(R.string.invalid_email));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void hideEmailError(){
|
|
|
|
|
disableError(emailInputLayout);
|
|
|
|
|
// emailInputLayout.setErrorEnabled(false);
|
|
|
|
|
emailInputLayout.setError(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showPasswordError(){
|
|
|
|
|
enableError(passwordInputLayout);
|
|
|
|
|
// passwordInputLayout.setErrorEnabled(true);
|
|
|
|
|
passwordInputLayout.setError(getString(R.string.invalid_password));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void hidePasswordError(){
|
|
|
|
|
disableError(passwordInputLayout);
|
|
|
|
|
// passwordInputLayout.setErrorEnabled(false);
|
|
|
|
|
passwordInputLayout.setError(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void enableSignIn(){
|
|
|
|
|
signInLinearLayout.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
|
|
|
|
|
signInButton.setEnabled(true);
|
|
|
|
|
signInButton.setTextColor(ContextCompat.getColor(getContext(), android.R.color.white));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void disableSignIn(){
|
|
|
|
|
signInLinearLayout.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.grey_400));
|
|
|
|
|
signInButton.setEnabled(false);
|
|
|
|
|
signInButton.setTextColor(ContextCompat.getColor(getContext(), R.color.grey_500));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void attemptLogin() {
|
|
|
|
|
// Reset errors.
|
|
|
|
|
|
|
|
|
|
passwordText.setError(null);
|
|
|
|
|
|
|
|
|
|
// Store values at the time of the login attempt.
|
|
|
|
|
String loginName = loginText.getText().toString();
|
|
|
|
|
String password = passwordText.getText().toString();
|
|
|
|
|
|
|
|
|
|
boolean cancel = false;
|
|
|
|
|
View focusView = null;
|
|
|
|
|
|
|
|
|
|
// Check for a valid email address.
|
|
|
|
|
if (TextUtils.isEmpty(loginName)) {
|
|
|
|
|
loginText.setError(getString(R.string.error_field_required));
|
|
|
|
|
focusView = loginText;
|
|
|
|
|
cancel = true;
|
|
|
|
|
} else if (!isEmailValid(loginName)) {
|
|
|
|
|
loginText.setError(getString(R.string.error_invalid_login_name));
|
|
|
|
|
focusView = loginText;
|
|
|
|
|
cancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for a valid password address.
|
|
|
|
|
if (TextUtils.isEmpty(password)) {
|
|
|
|
|
passwordText.setError(getString(R.string.error_field_required));
|
|
|
|
|
focusView = passwordText;
|
|
|
|
|
cancel = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cancel) {
|
|
|
|
|
focusView.requestFocus();
|
|
|
|
|
} else {
|
|
|
|
|
showProgress(true);
|
|
|
|
|
loginProcess(loginName, password);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isValidEmail(CharSequence value) {
|
|
|
|
|
Pattern pattern = Pattern.compile("\\s");
|
|
|
|
|
Matcher matcher = pattern.matcher(value);
|
|
|
|
|
return !matcher.find();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isValidPassword(CharSequence value) {
|
|
|
|
|
return value.length() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
|
|
|
|
|
private void showProgress(final boolean show) {
|
|
|
|
|
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
|
|
|
|
mLoginFormView.animate().setDuration(shortAnimTime).alpha(
|
|
|
|
|
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
|
|
|
|
mProgressView.animate().setDuration(shortAnimTime).alpha(
|
|
|
|
|
show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loginProcess(String email, String password) {
|
|
|
|
|
//ValidateUser user = new ValidateUser(email, password);
|
|
|
|
|
LdapUser fakeUser = new LdapUser(email, password,"wmi","tutor",email,"Fałszywy",email);
|
|
|
|
|
// disposable.add(ldapService.validate(user)
|
|
|
|
|
disposable.add(ldapService.fakeValidate(fakeUser)
|
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.subscribe(this::handleResponse, this::handleError));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void getUserProfile(String userId) {
|
|
|
|
|
|
|
|
|
|
disposable.add(userService.getUserById(userId)
|
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.subscribe(this::saveUserProfileToSharedPreferences, this::handleError));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showSnackBarMessage(String message) {
|
|
|
|
|
Snackbar.make(findViewById(R.id.login_form), message, Snackbar.LENGTH_LONG)
|
|
|
|
|
.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleResponse(JwtToken jwtToken) {
|
|
|
|
|
showProgress(false);
|
|
|
|
|
|
|
|
|
|
String token = jwtToken.getToken();
|
|
|
|
|
JWT jwt = new JWT(token);
|
|
|
|
|
Claim userId = jwt.getClaim("nameid");
|
|
|
|
|
Claim role = jwt.getClaim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
|
|
|
|
|
|
|
|
|
|
if (Objects.requireNonNull(role.asString()).equals("Student")) {
|
|
|
|
|
PrefUtils.storeIsTutor(getApplicationContext(), false);
|
|
|
|
|
} else {
|
|
|
|
|
PrefUtils.storeIsTutor(getApplicationContext(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PrefUtils.storeIsLoggedIn(getApplicationContext(), true);
|
|
|
|
|
PrefUtils.storeApiKey(getApplicationContext(), token);
|
|
|
|
|
PrefUtils.storeUserId(getApplicationContext(), userId.asString());
|
|
|
|
|
|
|
|
|
|
getUserProfile(userId.asString());
|
|
|
|
|
|
|
|
|
|
Intent data = new Intent();
|
|
|
|
|
String txt = "Main Activity";
|
|
|
|
|
data.setData(Uri.parse(txt));
|
|
|
|
|
setResult(RESULT_OK, data);
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handleError(Throwable error) {
|
|
|
|
|
showProgress(false);
|
|
|
|
|
|
|
|
|
|
if (error instanceof HttpException) {
|
|
|
|
|
|
|
|
|
|
ResponseBody responseBody = ((HttpException) error).response().errorBody();
|
|
|
|
|
showSnackBarMessage(RestApiHelper.getErrorMessage(responseBody));
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
showSnackBarMessage("Network Error !");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveUserProfileToSharedPreferences(User user) {
|
|
|
|
|
PrefUtils.storeUserFirstName(getApplicationContext(), user.getFirstName());
|
|
|
|
|
PrefUtils.storeUserLastName(getApplicationContext(), user.getLastName());
|
|
|
|
|
PrefUtils.storeUserName(getApplicationContext(), user.getUserName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package com.uam.wmi.findmytutor.activity;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|