Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.dep.unipiinformaticsapp;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.annotation.TargetApi;
- import android.content.Context;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Build;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class LoginActivity extends AppCompatActivity {
- public static final Pattern VALID_USERNAME_REGEX = Pattern.compile("^p[0-9]{5}$", Pattern.CASE_INSENSITIVE);
- public static final Pattern VALID_PASSWORD_REGEX = Pattern.compile("^![0-9]{2}[A-Z]{2}!$", Pattern.CASE_INSENSITIVE);
- private AutoCompleteTextView mUsernameView;
- private EditText mPasswordView;
- private View mProgressView;
- private View mLoginFormView;
- DatabaseClass myDb;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- mUsernameView = (AutoCompleteTextView) findViewById(R.id.username);
- mPasswordView = (EditText) findViewById(R.id.password);
- Button mUsernameSignInButton = (Button) findViewById(R.id.btn_login);
- mUsernameSignInButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- attemptLogin();
- }
- });
- mLoginFormView = findViewById(R.id.login_form);
- mProgressView = findViewById(R.id.login_progress);
- }
- // Attempts to sign in and presents any form errors (invalid username, missing fields, etc.)
- private void attemptLogin() {
- // Reset errors.
- mUsernameView.setError(null);
- mPasswordView.setError(null);
- // Store values at the time of the login attempt.
- String username = mUsernameView.getText().toString();
- String password = mPasswordView.getText().toString();
- boolean cancel = false;
- View focusView = null;
- // Check for a valid password, if the user entered one.
- if (TextUtils.isEmpty(password)) {
- mPasswordView.setError(getString(R.string.error_field_required));
- focusView = mPasswordView;
- cancel = true;
- } else if (!isPasswordValid(password)) {
- mPasswordView.setError("The password is incorrect.");
- focusView = mPasswordView;
- cancel = true;
- }
- // Check for a valid username.
- if (TextUtils.isEmpty(username)) {
- mUsernameView.setError(getString(R.string.error_field_required));
- focusView = mUsernameView;
- cancel = true;
- } else if (!isUsernameValid(username)) {
- mUsernameView.setError("The username is incorrect.");
- focusView = mUsernameView;
- cancel = true;
- }
- if (cancel) {
- focusView.requestFocus();
- } else {
- showProgress(true);
- Context context = LoginActivity.this.getApplicationContext();
- myDb = new DatabaseClass(context);
- myDb.open();
- myDb.insertEntry("p13053","!11AA!");
- String mpassword = myDb.getSinlgeEntry(username);
- if (!password.equals(mpassword)) {
- Toast.makeText(context, "Incorrect Password", Toast.LENGTH_LONG).show();
- mPasswordView.setError(getString(R.string.error_incorrect_password));
- mPasswordView.requestFocus();
- } else {
- Toast.makeText(context, "Welcome!", Toast.LENGTH_LONG).show();
- Intent mainpageIntent = new Intent(LoginActivity.this, NavigationActivity.class);
- LoginActivity.this.startActivity(mainpageIntent);
- }
- showProgress(false);
- /*else {
- myDb.insertEntry(mUsername, mPassword);
- Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
- Intent i = new Intent(SignUPActivity.this, HomeActivity.class);
- startActivity(i);
- finish();
- } */
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- myDb.close();
- }
- private boolean isUsernameValid(String username) {
- Matcher matcher = VALID_USERNAME_REGEX.matcher(username);
- return matcher.find();
- }
- private boolean isPasswordValid(String password) {
- Matcher matcher = VALID_PASSWORD_REGEX.matcher(password);
- return matcher.find();
- }
- //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);
- }
- }
- }
Add Comment
Please, Sign In to add comment