Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.vema.mobile.mvl.activities;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.annotation.TargetApi;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.design.widget.TextInputEditText;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.AppCompatCheckBox;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.inputmethod.EditorInfo;
- import android.widget.Button;
- import android.widget.CompoundButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.crashlytics.android.Crashlytics;
- import com.crashlytics.android.answers.Answers;
- import com.crashlytics.android.answers.ContentViewEvent;
- import cz.msebera.android.httpclient.Header;
- import cz.vema.mobile.mvl.BuildConfig;
- import cz.vema.mobile.mvl.R;
- import cz.vema.mobile.mvl.client.MVLRestClientFactory;
- import cz.vema.mobile.mvl.client.VmJswsResponseHandler;
- import cz.vema.mobile.mvl.security.AesUtils;
- import cz.vema.mobile.mvl.utils.Preferences;
- /**
- * A login screen that offers login via email/password.
- */
- public class LoginActivity extends AppCompatActivity {
- public static final String TAG = "MVL.LoginActivity";
- /**
- * Keep track of the login task to ensure we can cancel it if requested.
- */
- private Boolean mAuthTask = null;
- // UI references.
- private TextInputEditText mEmailView;
- private TextInputEditText mPinView;
- private View mProgressView;
- private View mLoginFormView;
- private AppCompatCheckBox mCheckboxView;
- private boolean doAutoLogin;
- boolean isSecured;
- VmJswsResponseHandler loginHandler;
- // Special case when autologin failed
- // Application does not have stored a plain PIN,
- // so we set a special string to catch this case in login submit
- private static String AUTOLOGIN_FAKE_PIN = "_AUTOLOGIN_";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.i(TAG, "onCreate()");
- setContentView(R.layout.activity_login);
- if (!BuildConfig.DEBUG) {
- Answers.getInstance().logContentView(new ContentViewEvent().putContentName("LoginScreen"));
- }
- Log.i(TAG, "onCreate() after Answers");
- //isSecured = LockScreenTest.doesDeviceHaveSecuritySetup(LoginActivity.this);
- isSecured = true;
- doAutoLogin = Preferences.getAutologin(this);
- if (!isSecured) {
- doAutoLogin = false;
- }
- Log.i(TAG, "onCreate() doAutoLogin = " + String.valueOf(doAutoLogin));
- // Set up the login form.
- mEmailView = (TextInputEditText) findViewById(R.id.email);
- mEmailView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
- if (id == R.id.email || id == EditorInfo.IME_NULL) {
- mPinView.requestFocus();
- return true;
- }
- return false;
- }
- });
- mPinView = (TextInputEditText) findViewById(R.id.password);
- // Submit login by Enter
- mPinView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
- @Override
- public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
- if (id == R.id.login || id == EditorInfo.IME_NULL) {
- attemptLogin();
- return true;
- }
- return false;
- }
- });
- Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
- if (mEmailSignInButton != null) {
- mEmailSignInButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- attemptLogin();
- }
- });
- }
- TextView mHomepageView = (TextView) findViewById(R.id.action_homepage);
- if (mHomepageView != null) {
- mHomepageView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_homepage)));
- startActivity(browserIntent);
- }
- });
- }
- mLoginFormView = findViewById(R.id.login_form);
- mProgressView = findViewById(R.id.login_progress);
- mCheckboxView = (AppCompatCheckBox) findViewById(R.id.checkbox_remember_login);
- if (mCheckboxView != null) {
- mCheckboxView.setChecked(doAutoLogin);
- mCheckboxView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- //boolean isSecured = LockScreenTest.doesDeviceHaveSecuritySetup(LoginActivity.this);
- if (isChecked) {
- if (!isSecured) {
- doAutoLogin = false;
- mCheckboxView.setChecked(false);
- Intent i = new Intent(LoginActivity.this, LockSettingsActivity.class);
- startActivityForResult(i, 1);
- } else {
- doAutoLogin = true;
- }
- } else {
- doAutoLogin = false;
- }
- }
- });
- }
- if (isSecured && doAutoLogin) {
- attemptAutoLogin();
- } else if (BuildConfig.DEBUG) {
- mEmailView.setText("jiri.kveton+mVL_8@gmail.com");
- //mEmailView.setText("uzivatel@vema.cz");
- mPinView.setText("1111");
- }
- }
- private void attemptAutoLogin() {
- String userName = Preferences.getUsername(this);
- String userPass = Preferences.getPass(this);
- String userPassLegacy = Preferences.getPassLegacy(this);
- String userKey = Preferences.getKey(this);
- String userKeyLegacy = Preferences.getKeyLegacy(this);
- mEmailView.setText(userName);
- mPinView.setText(AUTOLOGIN_FAKE_PIN);
- if (!userName.equalsIgnoreCase("") && (!userPass.equalsIgnoreCase("") || !userPassLegacy.equalsIgnoreCase(""))
- && (!userKey.equalsIgnoreCase("") || !userKeyLegacy.equalsIgnoreCase(""))) {
- attemptLogin(userName, userPassLegacy, userPass, userKeyLegacy, userKey);
- } else {
- // If error occurred, disable auto-login feature to prevent login loop
- Preferences.putAutologin(this, false);
- Log.e(TAG, "Failed to autolog. Username or password is empty.");
- }
- }
- /**
- * Manually submitted login
- */
- private void attemptLogin() {
- try {
- final String email = mEmailView.getText().toString();
- final String pin = mPinView.getText().toString();
- String password;
- String passwordLegacy;
- String key;
- String keyLegacy;
- if (pin.equals(AUTOLOGIN_FAKE_PIN)){
- password = Preferences.getPass(this);
- passwordLegacy = Preferences.getPassLegacy(this);
- key = Preferences.getKey(this);
- keyLegacy = Preferences.getKeyLegacy(this);
- }else {
- passwordLegacy = AesUtils.pin2pass_compat(pin);
- password = AesUtils.pin2pass(pin);
- keyLegacy = AesUtils.pin2key_compat(pin);
- key = AesUtils.pin2key(pin);
- }
- if (isSecured && doAutoLogin && !email.equals("") && !password.equals("") && !key.equals("")) {
- Preferences.putUsername(this, email);
- Preferences.putPass(this, password);
- Preferences.putPassLegacy(this, passwordLegacy);
- Preferences.putKey(this, key);
- Preferences.putKeyLegacy(this, keyLegacy);
- Preferences.putAutologin(LoginActivity.this, doAutoLogin);
- }
- attemptLogin(email, passwordLegacy, password, keyLegacy, key);
- } catch (Exception e) {
- Toast.makeText(this, "Neočekávaná chyba při přihlášení.", Toast.LENGTH_LONG).show();
- e.printStackTrace();
- Crashlytics.logException(e);
- }
- }
- private void attemptLogin(final String email, final String passwordLegacy, final String password, final String keyLegacy, final String key) {
- Log.i(TAG, "attemptLogin()");
- if (mAuthTask != null) {
- return;
- }
- // Reset errors.
- mEmailView.setError(null);
- mPinView.setError(null);
- boolean cancel = false;
- View focusView = null;
- // Check for a valid password, if the user entered one.
- if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
- mPinView.setError(getString(R.string.error_invalid_password));
- focusView = mPinView;
- cancel = true;
- }
- // Check for a valid email address.
- if (TextUtils.isEmpty(email)) {
- mEmailView.setError(getString(R.string.error_field_required));
- focusView = mEmailView;
- cancel = true;
- } else if (!isEmailValid(email)) {
- mEmailView.setError(getString(R.string.error_invalid_email));
- focusView = mEmailView;
- cancel = true;
- }
- if (cancel) {
- // There was an error; don't attempt login and focus the first
- // form field with an error.
- focusView.requestFocus();
- } else {
- // Show a progress spinner, and kick off a background task to
- // perform the user login attempt.
- showProgress(true);
- mAuthTask = true;
- loginHandler = new VmJswsResponseHandler() {
- @Override
- public void onFailure(int statusCode, Header[] headers, String responseString,
- Throwable throwable) {
- mAuthTask = null;
- showProgress(false);
- doAutoLogin = false;
- //Preferences.putAutologin(LoginActivity.this, false);
- Toast.makeText(LoginActivity.this, R.string.msg_login_failed,
- Toast.LENGTH_LONG).show();
- Log.i(TAG, "Login failed! + " + responseString);
- Preferences.putUsername(LoginActivity.this, "");
- Preferences.putPass(LoginActivity.this, "");
- Preferences.putPassLegacy(LoginActivity.this, "");
- Preferences.putKey(LoginActivity.this, "");
- Preferences.putKeyLegacy(LoginActivity.this, "");
- Preferences.putAutologin(LoginActivity.this, false);
- if (!BuildConfig.DEBUG) {
- // Zalogovani nepovedeno loginu
- Answers.getInstance().logContentView(new ContentViewEvent()
- .putContentName("LoginScreen")
- .putContentType("Failure")
- .putCustomAttribute("email", email)
- );
- }
- }
- @Override
- public void onSuccess(int statusCode, Header[] headers, String responseString) {
- mAuthTask = null;
- showProgress(false);
- Log.i(TAG, "Logged-in, starting MainActivity()");
- if (LoginActivity.this.mCheckboxView.isChecked()) {
- Preferences.putUsername(LoginActivity.this, email);
- Preferences.putPass(LoginActivity.this, password);
- Preferences.putPassLegacy(LoginActivity.this, passwordLegacy);
- Preferences.putKey(LoginActivity.this, key);
- Preferences.putKeyLegacy(LoginActivity.this, keyLegacy);
- Preferences.putAutologin(LoginActivity.this, true);
- }
- else {
- Preferences.putUsername(LoginActivity.this, "");
- Preferences.putPass(LoginActivity.this, "");
- Preferences.putPassLegacy(LoginActivity.this, "");
- Preferences.putKey(LoginActivity.this, "");
- Preferences.putKeyLegacy(LoginActivity.this, "");
- Preferences.putAutologin(LoginActivity.this, false);
- }
- if (!BuildConfig.DEBUG) {
- // Zalogovani povedeno loginu
- Answers.getInstance().logContentView(new ContentViewEvent()
- .putContentName("LoginScreen")
- .putContentType("Success")
- .putCustomAttribute("email", email)
- );
- }
- Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
- myIntent.putExtra(MainActivity.ARG_KEY, key);
- myIntent.putExtra(MainActivity.ARG_KEY_LEGACY, keyLegacy);
- myIntent.putExtra(MainActivity.ARG_EMAIL, email);
- LoginActivity.this.startActivity(myIntent);
- finish();
- }
- };
- final VmJswsResponseHandler firstAttemptHandler = new VmJswsResponseHandler() {
- @Override
- public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
- loginHandler.onFailure(statusCode, headers, responseString, throwable);
- }
- @Override
- public void onSuccess(int statusCode, Header[] headers, String responseString) {
- loginHandler.onSuccess(statusCode, headers, responseString);
- }
- };
- MVLRestClientFactory.getClient(LoginActivity.this).login(LoginActivity.this, email, password, new VmJswsResponseHandler() {
- @Override
- public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
- // If failed, next attempt to login with buggy password
- MVLRestClientFactory.getClient(LoginActivity.this).login(LoginActivity.this, email, passwordLegacy, firstAttemptHandler);
- }
- @Override
- public void onSuccess(int statusCode, Header[] headers, String responseString) {
- loginHandler.onSuccess(statusCode, headers, responseString);
- }
- });
- }
- }
- private boolean isEmailValid(String email) {
- return email.contains("@");
- }
- private boolean isPasswordValid(String password) {
- return password.length() >= 4;
- }
- /**
- * Shows the progress UI and hides the login form.
- */
- @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
- private void showProgress(final boolean show) {
- // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
- // for very easy animations. If available, use these APIs to fade-in
- // the progress spinner.
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
- 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);
- }
- });
- } else {
- // The ViewPropertyAnimator APIs are not available, so simply show
- // and hide the relevant UI components.
- mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
- mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement