daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Sep 22nd, 2016 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.dep.unipiinformaticsapp;
  2.  
  3. import android.animation.Animator;
  4. import android.animation.AnimatorListenerAdapter;
  5. import android.annotation.TargetApi;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.text.TextUtils;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.AutoCompleteTextView;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.Toast;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20.  
  21. public class LoginActivity extends AppCompatActivity {
  22.  
  23.     public static final Pattern VALID_USERNAME_REGEX = Pattern.compile("^p[0-9]{5}$", Pattern.CASE_INSENSITIVE);
  24.     public static final Pattern VALID_PASSWORD_REGEX = Pattern.compile("^![0-9]{2}[A-Z]{2}!$", Pattern.CASE_INSENSITIVE);
  25.     private AutoCompleteTextView mUsernameView;
  26.     private EditText mPasswordView;
  27.     private View mProgressView;
  28.     private View mLoginFormView;
  29.     DatabaseClass myDb;
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_login);
  35.         mUsernameView = (AutoCompleteTextView) findViewById(R.id.username);
  36.         mPasswordView = (EditText) findViewById(R.id.password);
  37.  
  38.         Button mUsernameSignInButton = (Button) findViewById(R.id.btn_login);
  39.         mUsernameSignInButton.setOnClickListener(new OnClickListener() {
  40.             @Override
  41.             public void onClick(View view) {
  42.                 attemptLogin();
  43.             }
  44.         });
  45.  
  46.         mLoginFormView = findViewById(R.id.login_form);
  47.         mProgressView = findViewById(R.id.login_progress);
  48.     }
  49.  
  50.     // Attempts to sign in and presents any form errors (invalid username, missing fields, etc.)
  51.     private void attemptLogin() {
  52.  
  53.         // Reset errors.
  54.         mUsernameView.setError(null);
  55.         mPasswordView.setError(null);
  56.  
  57.         // Store values at the time of the login attempt.
  58.         String username = mUsernameView.getText().toString();
  59.         String password = mPasswordView.getText().toString();
  60.  
  61.         boolean cancel = false;
  62.         View focusView = null;
  63.  
  64.         // Check for a valid password, if the user entered one.
  65.         if (TextUtils.isEmpty(password)) {
  66.             mPasswordView.setError(getString(R.string.error_field_required));
  67.             focusView = mPasswordView;
  68.             cancel = true;
  69.         } else if (!isPasswordValid(password)) {
  70.             mPasswordView.setError("The password is incorrect.");
  71.             focusView = mPasswordView;
  72.             cancel = true;
  73.         }
  74.  
  75.         // Check for a valid username.
  76.         if (TextUtils.isEmpty(username)) {
  77.             mUsernameView.setError(getString(R.string.error_field_required));
  78.             focusView = mUsernameView;
  79.             cancel = true;
  80.         } else if (!isUsernameValid(username)) {
  81.             mUsernameView.setError("The username is incorrect.");
  82.             focusView = mUsernameView;
  83.             cancel = true;
  84.         }
  85.  
  86.         if (cancel) {
  87.             focusView.requestFocus();
  88.         } else {
  89.             showProgress(true);
  90.             Context context = LoginActivity.this.getApplicationContext();
  91.             myDb = new DatabaseClass(context);
  92.             myDb.open();
  93.             myDb.insertEntry("p13053","!11AA!");
  94.  
  95.             String mpassword = myDb.getSinlgeEntry(username);
  96.             if (!password.equals(mpassword)) {
  97.                 Toast.makeText(context, "Incorrect Password", Toast.LENGTH_LONG).show();
  98.                 mPasswordView.setError(getString(R.string.error_incorrect_password));
  99.                 mPasswordView.requestFocus();
  100.             } else {
  101.                 Toast.makeText(context, "Welcome!", Toast.LENGTH_LONG).show();
  102.                 Intent mainpageIntent = new Intent(LoginActivity.this, NavigationActivity.class);
  103.                 LoginActivity.this.startActivity(mainpageIntent);
  104.             }
  105.             showProgress(false);
  106.  
  107.             /*else {
  108.                 myDb.insertEntry(mUsername, mPassword);
  109.                 Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
  110.                 Intent i = new Intent(SignUPActivity.this, HomeActivity.class);
  111.                 startActivity(i);
  112.                 finish();
  113.             }   */
  114.         }
  115.     }
  116.  
  117.     @Override
  118.     protected void onDestroy() {
  119.         super.onDestroy();
  120.         myDb.close();
  121.     }
  122.  
  123.     private boolean isUsernameValid(String username) {
  124.         Matcher matcher = VALID_USERNAME_REGEX.matcher(username);
  125.         return matcher.find();
  126.     }
  127.  
  128.     private boolean isPasswordValid(String password) {
  129.         Matcher matcher = VALID_PASSWORD_REGEX.matcher(password);
  130.         return matcher.find();
  131.     }
  132.  
  133.     //Shows the progress UI and hides the login form.
  134.     @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  135.     private void showProgress(final boolean show) {
  136.         // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  137.         // for very easy animations. If available, use these APIs to fade-in
  138.         // the progress spinner.
  139.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
  140.             int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
  141.  
  142.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  143.             mLoginFormView.animate().setDuration(shortAnimTime).alpha(
  144.                     show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
  145.                 @Override
  146.                 public void onAnimationEnd(Animator animation) {
  147.                     mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  148.                 }
  149.             });
  150.  
  151.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  152.             mProgressView.animate().setDuration(shortAnimTime).alpha(
  153.                     show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
  154.                 @Override
  155.                 public void onAnimationEnd(Animator animation) {
  156.                     mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  157.                 }
  158.             });
  159.         } else {
  160.             // The ViewPropertyAnimator APIs are not available, so simply show
  161.             // and hide the relevant UI components.
  162.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  163.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  164.         }
  165.     }
  166.  
  167. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top