SHARE
TWEET

Untitled

a guest Sep 25th, 2016 61 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_STUDENT_USERNAME_REGEX = Pattern.compile("^p[0-9]{5}$", Pattern.CASE_INSENSITIVE);
  24.     public static final Pattern VALID_FACULTY_USERNAME_REGEX = Pattern.compile("^a[0-9]{5}$", Pattern.CASE_INSENSITIVE);
  25.     public static final Pattern VALID_PASSWORD_REGEX = Pattern.compile("^![0-9]{2}[A-Z]{2}!$", Pattern.CASE_INSENSITIVE);
  26.     private AutoCompleteTextView mUsernameView;
  27.     private EditText mPasswordView;
  28.     private View mProgressView;
  29.     private View mLoginFormView;
  30.     private Context context;
  31.     private DatabaseClass myDb;
  32.     private int type;
  33.  
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.activity_login);
  38.         mUsernameView = (AutoCompleteTextView) findViewById(R.id.username);
  39.         mPasswordView = (EditText) findViewById(R.id.password);
  40.  
  41.         Button mUsernameSignInButton = (Button) findViewById(R.id.btn_login);
  42.         mUsernameSignInButton.setOnClickListener(new OnClickListener() {
  43.             @Override
  44.             public void onClick(View view) {
  45.                 attemptLogin();
  46.             }
  47.         });
  48.  
  49.         mLoginFormView = findViewById(R.id.login_form);
  50.         mProgressView = findViewById(R.id.login_progress);
  51.  
  52.         context = LoginActivity.this.getApplicationContext();
  53.         myDb = new DatabaseClass(context);
  54.         myDb.open();
  55.         myDb.insertEntry("p13053","!11AA!");
  56.         myDb.insertEntry("p13025","!11AA!");
  57.         myDb.insertEntry("p13114","!11AA!");
  58.     }
  59.  
  60.     private void attemptLogin() {
  61.  
  62.         // Reset errors.
  63.         mUsernameView.setError(null);
  64.         mPasswordView.setError(null);
  65.  
  66.         // Store values at the time of the login attempt.
  67.         String username = mUsernameView.getText().toString();
  68.         String password = mPasswordView.getText().toString();
  69.  
  70.         boolean cancel = false;
  71.         View focusView= null;
  72.  
  73.         // Check for a valid password, if the user entered one.
  74.         if (TextUtils.isEmpty(password)) {
  75.             mPasswordView.setError(getString(R.string.error_field_required));
  76.             focusView = mPasswordView;
  77.             cancel = true;
  78.         } else if (!isPasswordValid(password)) {
  79.             mPasswordView.setError("Incorrect Password");
  80.             focusView = mPasswordView;
  81.             cancel = true;
  82.         }
  83.  
  84.         // Check for a valid username.
  85.         if (TextUtils.isEmpty(username)) {
  86.             mUsernameView.setError(getString(R.string.error_field_required));
  87.             focusView = mUsernameView;
  88.             cancel = true;
  89.         } else if (username.charAt(0)!='p' && username.charAt(0)!='a'){ //invalid
  90.             mUsernameView.setError("Incorrect Username");
  91.             focusView = mUsernameView;
  92.             cancel = true;
  93.         } else if (username.charAt(0)=='p' && !isStudentUsernameValid(username)){ // student
  94.             mUsernameView.setError("Incorrect Student Username");
  95.             focusView = mUsernameView;
  96.             cancel = true;
  97.         } else if (username.charAt(0)=='a' && !isFacultyUsernameValid(username)){ // faculty
  98.             mUsernameView.setError("Incorrect Faculty Username");
  99.             focusView = mUsernameView;
  100.             cancel = true;
  101.         }
  102.  
  103.         showProgress(true);
  104.  
  105.         if (cancel) {
  106.             focusView.requestFocus();
  107.         } else {
  108.             String mpassword = myDb.getSinlgeEntry(username);
  109.             if (!password.equals(mpassword)) {
  110.                 Toast.makeText(context, "Incorrect Password", Toast.LENGTH_LONG).show();
  111.                 if (username.charAt(0)=='p')
  112.                     type = 0;
  113.                 else
  114.                     type = 1;
  115.                 mPasswordView.setError(getString(R.string.error_incorrect_password));
  116.                 mPasswordView.requestFocus();
  117.             } else {
  118.                 Toast.makeText(context, "Login Successful", Toast.LENGTH_LONG).show();
  119.  
  120.                 Intent mainpageIntent = new Intent(LoginActivity.this, NavigationActivity.class);
  121.                 LoginActivity.this.startActivity(mainpageIntent);
  122.             }
  123.         }
  124.  
  125.         showProgress(false);
  126.     }
  127.  
  128.     public int getType(){
  129.         return type;
  130.     }
  131.  
  132.     @Override
  133.     protected void onDestroy() {
  134.         super.onDestroy();
  135.         myDb.close();
  136.     }
  137.  
  138.     private boolean isStudentUsernameValid(String username) {
  139.         Matcher matcher = VALID_STUDENT_USERNAME_REGEX.matcher(username);
  140.         return matcher.find();
  141.     }
  142.  
  143.     private boolean isFacultyUsernameValid(String username) {
  144.         Matcher matcher = VALID_STUDENT_USERNAME_REGEX.matcher(username);
  145.         return matcher.find();
  146.     }
  147.  
  148.     private boolean isPasswordValid(String password) {
  149.         Matcher matcher = VALID_PASSWORD_REGEX.matcher(password);
  150.         return matcher.find();
  151.     }
  152.  
  153.     //Shows the progress UI and hides the login form.
  154.     @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  155.     private void showProgress(final boolean show) {
  156.         // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  157.         // for very easy animations. If available, use these APIs to fade-in
  158.         // the progress spinner.
  159.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
  160.             int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
  161.  
  162.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  163.             mLoginFormView.animate().setDuration(shortAnimTime).alpha(
  164.                     show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
  165.                 @Override
  166.                 public void onAnimationEnd(Animator animation) {
  167.                     mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  168.                 }
  169.             });
  170.  
  171.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  172.             mProgressView.animate().setDuration(shortAnimTime).alpha(
  173.                     show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
  174.                 @Override
  175.                 public void onAnimationEnd(Animator animation) {
  176.                     mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  177.                 }
  178.             });
  179.         } else {
  180.             // The ViewPropertyAnimator APIs are not available, so simply show
  181.             // and hide the relevant UI components.
  182.             mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  183.             mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  184.         }
  185.     }
  186.  
  187. }
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