Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.16 KB | None | 0 0
  1. package cz.vema.mobile.mvl.activities;
  2.  
  3. import android.animation.Animator;
  4. import android.animation.AnimatorListenerAdapter;
  5. import android.annotation.TargetApi;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.support.design.widget.TextInputEditText;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.support.v7.widget.AppCompatCheckBox;
  13. import android.text.TextUtils;
  14. import android.util.Log;
  15. import android.view.KeyEvent;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.view.inputmethod.EditorInfo;
  19. import android.widget.Button;
  20. import android.widget.CompoundButton;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23.  
  24. import com.crashlytics.android.Crashlytics;
  25. import com.crashlytics.android.answers.Answers;
  26. import com.crashlytics.android.answers.ContentViewEvent;
  27.  
  28. import cz.msebera.android.httpclient.Header;
  29. import cz.vema.mobile.mvl.BuildConfig;
  30. import cz.vema.mobile.mvl.R;
  31. import cz.vema.mobile.mvl.client.MVLRestClientFactory;
  32. import cz.vema.mobile.mvl.client.VmJswsResponseHandler;
  33. import cz.vema.mobile.mvl.security.AesUtils;
  34. import cz.vema.mobile.mvl.utils.Preferences;
  35.  
  36. /**
  37.  * A login screen that offers login via email/password.
  38.  */
  39. public class LoginActivity extends AppCompatActivity {
  40.  
  41.     public static final String TAG = "MVL.LoginActivity";
  42.  
  43.     /**
  44.      * Keep track of the login task to ensure we can cancel it if requested.
  45.      */
  46.     private Boolean mAuthTask = null;
  47.  
  48.     // UI references.
  49.     private TextInputEditText mEmailView;
  50.     private TextInputEditText mPinView;
  51.     private View mProgressView;
  52.     private View mLoginFormView;
  53.     private AppCompatCheckBox mCheckboxView;
  54.     private boolean doAutoLogin;
  55.     boolean isSecured;
  56.     VmJswsResponseHandler loginHandler;
  57.  
  58.     // Special case when autologin failed
  59.     // Application does not have stored a plain PIN,
  60.     // so we set a special string to catch this case in login submit
  61.     private static String AUTOLOGIN_FAKE_PIN = "_AUTOLOGIN_";
  62.  
  63.     @Override
  64.     protected void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.         Log.i(TAG, "onCreate()");
  67.         setContentView(R.layout.activity_login);
  68.         if (!BuildConfig.DEBUG) {
  69.             Answers.getInstance().logContentView(new ContentViewEvent().putContentName("LoginScreen"));
  70.         }
  71.         Log.i(TAG, "onCreate() after Answers");
  72.  
  73.         //isSecured = LockScreenTest.doesDeviceHaveSecuritySetup(LoginActivity.this);
  74.         isSecured = true;
  75.         doAutoLogin = Preferences.getAutologin(this);
  76.  
  77.         if (!isSecured) {
  78.             doAutoLogin = false;
  79.         }
  80.         Log.i(TAG, "onCreate() doAutoLogin = " + String.valueOf(doAutoLogin));
  81.  
  82.         // Set up the login form.
  83.         mEmailView = (TextInputEditText) findViewById(R.id.email);
  84.         mEmailView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  85.             @Override
  86.             public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
  87.                 if (id == R.id.email || id == EditorInfo.IME_NULL) {
  88.                     mPinView.requestFocus();
  89.                     return true;
  90.                 }
  91.                 return false;
  92.             }
  93.         });
  94.  
  95.         mPinView = (TextInputEditText) findViewById(R.id.password);
  96.         // Submit login by Enter
  97.         mPinView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  98.             @Override
  99.             public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
  100.                 if (id == R.id.login || id == EditorInfo.IME_NULL) {
  101.                     attemptLogin();
  102.                     return true;
  103.                 }
  104.                 return false;
  105.             }
  106.         });
  107.  
  108.         Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
  109.         if (mEmailSignInButton != null) {
  110.             mEmailSignInButton.setOnClickListener(new OnClickListener() {
  111.                 @Override
  112.                 public void onClick(View view) {
  113.                     attemptLogin();
  114.                 }
  115.             });
  116.         }
  117.  
  118.         TextView mHomepageView = (TextView) findViewById(R.id.action_homepage);
  119.         if (mHomepageView != null) {
  120.             mHomepageView.setOnClickListener(new OnClickListener() {
  121.                 @Override
  122.                 public void onClick(View v) {
  123.                     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.url_homepage)));
  124.                     startActivity(browserIntent);
  125.                 }
  126.             });
  127.         }
  128.  
  129.         mLoginFormView = findViewById(R.id.login_form);
  130.         mProgressView = findViewById(R.id.login_progress);
  131.         mCheckboxView = (AppCompatCheckBox) findViewById(R.id.checkbox_remember_login);
  132.  
  133.         if (mCheckboxView != null) {
  134.             mCheckboxView.setChecked(doAutoLogin);
  135.             mCheckboxView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  136.                 @Override
  137.                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  138.                     //boolean isSecured = LockScreenTest.doesDeviceHaveSecuritySetup(LoginActivity.this);
  139.                     if (isChecked) {
  140.                         if (!isSecured) {
  141.                             doAutoLogin = false;
  142.                             mCheckboxView.setChecked(false);
  143.                             Intent i = new Intent(LoginActivity.this, LockSettingsActivity.class);
  144.                             startActivityForResult(i, 1);
  145.                         } else {
  146.                             doAutoLogin = true;
  147.                         }
  148.                     } else {
  149.                         doAutoLogin = false;
  150.                     }
  151.                 }
  152.             });
  153.         }
  154.  
  155.         if (isSecured && doAutoLogin) {
  156.             attemptAutoLogin();
  157.         } else if (BuildConfig.DEBUG) {
  158.             mEmailView.setText("jiri.kveton+mVL_8@gmail.com");
  159.             //mEmailView.setText("uzivatel@vema.cz");
  160.             mPinView.setText("1111");
  161.         }
  162.     }
  163.  
  164.     private void attemptAutoLogin() {
  165.         String userName = Preferences.getUsername(this);
  166.         String userPass = Preferences.getPass(this);
  167.         String userPassLegacy = Preferences.getPassLegacy(this);
  168.         String userKey = Preferences.getKey(this);
  169.         String userKeyLegacy = Preferences.getKeyLegacy(this);
  170.  
  171.         mEmailView.setText(userName);
  172.         mPinView.setText(AUTOLOGIN_FAKE_PIN);
  173.  
  174.         if (!userName.equalsIgnoreCase("") && (!userPass.equalsIgnoreCase("") || !userPassLegacy.equalsIgnoreCase(""))
  175.                 && (!userKey.equalsIgnoreCase("") || !userKeyLegacy.equalsIgnoreCase(""))) {
  176.             attemptLogin(userName, userPassLegacy, userPass, userKeyLegacy, userKey);
  177.         } else {
  178.             // If error occurred, disable auto-login feature to prevent login loop
  179.             Preferences.putAutologin(this, false);
  180.             Log.e(TAG, "Failed to autolog. Username or password is empty.");
  181.         }
  182.     }
  183.  
  184.     /**
  185.      * Manually submitted login
  186.      */
  187.     private void attemptLogin() {
  188.         try {
  189.             final String email = mEmailView.getText().toString();
  190.             final String pin = mPinView.getText().toString();
  191.             String password;
  192.             String passwordLegacy;
  193.             String key;
  194.             String keyLegacy;
  195.  
  196.  
  197.             if (pin.equals(AUTOLOGIN_FAKE_PIN)){
  198.                 password = Preferences.getPass(this);
  199.                 passwordLegacy = Preferences.getPassLegacy(this);
  200.                 key = Preferences.getKey(this);
  201.                 keyLegacy = Preferences.getKeyLegacy(this);
  202.             }else {
  203.                 passwordLegacy = AesUtils.pin2pass_compat(pin);
  204.                 password = AesUtils.pin2pass(pin);
  205.                 keyLegacy = AesUtils.pin2key_compat(pin);
  206.                 key = AesUtils.pin2key(pin);
  207.             }
  208.             if (isSecured && doAutoLogin && !email.equals("") && !password.equals("") && !key.equals("")) {
  209.                 Preferences.putUsername(this, email);
  210.                 Preferences.putPass(this, password);
  211.                 Preferences.putPassLegacy(this, passwordLegacy);
  212.                 Preferences.putKey(this, key);
  213.                 Preferences.putKeyLegacy(this, keyLegacy);
  214.                 Preferences.putAutologin(LoginActivity.this, doAutoLogin);
  215.             }
  216.  
  217.             attemptLogin(email, passwordLegacy, password, keyLegacy, key);
  218.  
  219.         } catch (Exception e) {
  220.             Toast.makeText(this, "Neočekávaná chyba při přihlášení.", Toast.LENGTH_LONG).show();
  221.             e.printStackTrace();
  222.             Crashlytics.logException(e);
  223.         }
  224.     }
  225.  
  226.     private void attemptLogin(final String email, final String passwordLegacy, final String password, final String keyLegacy, final String key) {
  227.         Log.i(TAG, "attemptLogin()");
  228.         if (mAuthTask != null) {
  229.             return;
  230.         }
  231.  
  232.         // Reset errors.
  233.         mEmailView.setError(null);
  234.         mPinView.setError(null);
  235.  
  236.         boolean cancel = false;
  237.         View focusView = null;
  238.  
  239.         // Check for a valid password, if the user entered one.
  240.         if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
  241.             mPinView.setError(getString(R.string.error_invalid_password));
  242.             focusView = mPinView;
  243.             cancel = true;
  244.         }
  245.  
  246.         // Check for a valid email address.
  247.         if (TextUtils.isEmpty(email)) {
  248.             mEmailView.setError(getString(R.string.error_field_required));
  249.             focusView = mEmailView;
  250.             cancel = true;
  251.         } else if (!isEmailValid(email)) {
  252.             mEmailView.setError(getString(R.string.error_invalid_email));
  253.             focusView = mEmailView;
  254.             cancel = true;
  255.         }
  256.  
  257.         if (cancel) {
  258.             // There was an error; don't attempt login and focus the first
  259.             // form field with an error.
  260.             focusView.requestFocus();
  261.         } else {
  262.             // Show a progress spinner, and kick off a background task to
  263.             // perform the user login attempt.
  264.             showProgress(true);
  265.             mAuthTask = true;
  266.  
  267.  
  268.  
  269.             loginHandler = new VmJswsResponseHandler() {
  270.                 @Override
  271.                 public void onFailure(int statusCode, Header[] headers, String responseString,
  272.                                       Throwable throwable) {
  273.                     mAuthTask = null;
  274.                     showProgress(false);
  275.                     doAutoLogin = false;
  276.                     //Preferences.putAutologin(LoginActivity.this, false);
  277.                     Toast.makeText(LoginActivity.this, R.string.msg_login_failed,
  278.                             Toast.LENGTH_LONG).show();
  279.  
  280.                     Log.i(TAG, "Login failed! + " + responseString);
  281.  
  282.                     Preferences.putUsername(LoginActivity.this, "");
  283.                     Preferences.putPass(LoginActivity.this, "");
  284.                     Preferences.putPassLegacy(LoginActivity.this, "");
  285.                     Preferences.putKey(LoginActivity.this, "");
  286.                     Preferences.putKeyLegacy(LoginActivity.this, "");
  287.                     Preferences.putAutologin(LoginActivity.this, false);
  288.  
  289.                     if (!BuildConfig.DEBUG) {
  290.                         // Zalogovani nepovedeno loginu
  291.                         Answers.getInstance().logContentView(new ContentViewEvent()
  292.                                 .putContentName("LoginScreen")
  293.                                 .putContentType("Failure")
  294.                                 .putCustomAttribute("email", email)
  295.                         );
  296.                     }
  297.                 }
  298.  
  299.                 @Override
  300.                 public void onSuccess(int statusCode, Header[] headers, String responseString) {
  301.                     mAuthTask = null;
  302.                     showProgress(false);
  303.                     Log.i(TAG, "Logged-in, starting MainActivity()");
  304.  
  305.                     if (LoginActivity.this.mCheckboxView.isChecked()) {
  306.                         Preferences.putUsername(LoginActivity.this, email);
  307.                         Preferences.putPass(LoginActivity.this, password);
  308.                         Preferences.putPassLegacy(LoginActivity.this, passwordLegacy);
  309.                         Preferences.putKey(LoginActivity.this, key);
  310.                         Preferences.putKeyLegacy(LoginActivity.this, keyLegacy);
  311.                         Preferences.putAutologin(LoginActivity.this, true);
  312.                     }
  313.                     else {
  314.                         Preferences.putUsername(LoginActivity.this, "");
  315.                         Preferences.putPass(LoginActivity.this, "");
  316.                         Preferences.putPassLegacy(LoginActivity.this, "");
  317.                         Preferences.putKey(LoginActivity.this, "");
  318.                         Preferences.putKeyLegacy(LoginActivity.this, "");
  319.                         Preferences.putAutologin(LoginActivity.this, false);
  320.                     }
  321.  
  322.                     if (!BuildConfig.DEBUG) {
  323.                         // Zalogovani povedeno loginu
  324.                         Answers.getInstance().logContentView(new ContentViewEvent()
  325.                                 .putContentName("LoginScreen")
  326.                                 .putContentType("Success")
  327.                                 .putCustomAttribute("email", email)
  328.                         );
  329.                     }
  330.  
  331.                     Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
  332.                     myIntent.putExtra(MainActivity.ARG_KEY, key);
  333.                     myIntent.putExtra(MainActivity.ARG_KEY_LEGACY, keyLegacy);
  334.                     myIntent.putExtra(MainActivity.ARG_EMAIL, email);
  335.                     LoginActivity.this.startActivity(myIntent);
  336.                     finish();
  337.                 }
  338.             };
  339.  
  340.             final VmJswsResponseHandler firstAttemptHandler = new VmJswsResponseHandler() {
  341.                 @Override
  342.                 public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
  343.                     loginHandler.onFailure(statusCode, headers, responseString, throwable);
  344.                 }
  345.  
  346.                 @Override
  347.                 public void onSuccess(int statusCode, Header[] headers, String responseString) {
  348.                     loginHandler.onSuccess(statusCode, headers, responseString);
  349.                 }
  350.             };
  351.  
  352.             MVLRestClientFactory.getClient(LoginActivity.this).login(LoginActivity.this, email, password, new VmJswsResponseHandler() {
  353.                 @Override
  354.                 public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
  355.                     // If failed, next attempt to login with buggy password
  356.                     MVLRestClientFactory.getClient(LoginActivity.this).login(LoginActivity.this, email, passwordLegacy, firstAttemptHandler);
  357.                 }
  358.  
  359.                 @Override
  360.                 public void onSuccess(int statusCode, Header[] headers, String responseString) {
  361.                     loginHandler.onSuccess(statusCode, headers, responseString);
  362.                 }
  363.             });
  364.         }
  365.     }
  366.  
  367.     private boolean isEmailValid(String email) {
  368.         return email.contains("@");
  369.     }
  370.  
  371.     private boolean isPasswordValid(String password) {
  372.         return password.length() >= 4;
  373.     }
  374.  
  375.     /**
  376.      * Shows the progress UI and hides the login form.
  377.      */
  378.     @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  379.     private void showProgress(final boolean show) {
  380.         // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  381.         // for very easy animations. If available, use these APIs to fade-in
  382.         // the progress spinner.
  383.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
  384.             int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
  385.  
  386.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  387.             mLoginFormView.animate().setDuration(shortAnimTime).alpha(
  388.                     show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
  389.                 @Override
  390.                 public void onAnimationEnd(Animator animation) {
  391.                     mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  392.                 }
  393.             });
  394.  
  395.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  396.             mProgressView.animate().setDuration(shortAnimTime).alpha(
  397.                     show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
  398.                 @Override
  399.                 public void onAnimationEnd(Animator animation) {
  400.                     mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  401.                 }
  402.             });
  403.         } else {
  404.             // The ViewPropertyAnimator APIs are not available, so simply show
  405.             // and hide the relevant UI components.
  406.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  407.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  408.         }
  409.     }
  410.  
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement