Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.dep.androidapp;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.annotation.TargetApi;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.support.annotation.NonNull;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.app.LoaderManager.LoaderCallbacks;
- import android.content.CursorLoader;
- import android.content.Loader;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.text.TextUtils;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.inputmethod.EditorInfo;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import static android.Manifest.permission.READ_CONTACTS;
- // A login screen that offers login via username/password.
- 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);
- // A dummy authentication store containing known user names and passwords.
- // TODO: remove after connecting to a real authentication system.
- private static final String[] DUMMY_CREDENTIALS = new String[]{
- "p13053:!11AA!", "p13025:!22BB!"
- };
- // Keep track of the login task to ensure we can cancel it if requested.
- private UserLoginTask mAuthTask = null;
- private AutoCompleteTextView mUsernameView;
- private EditText mPasswordView;
- private View mProgressView;
- private View mLoginFormView;
- @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 or register the account specified by the login form. If there are form
- errors (invalid username, missing fields, etc.), the errors are presented */
- private void attemptLogin() {
- if (mAuthTask != null) {
- return;
- }
- // 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) { // There was an error; don't attempt login and focus the 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 = new UserLoginTask(username, password);
- mAuthTask.execute((Void) null);
- }
- }
- 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);
- }
- }
- public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
- private final String mUsername;
- private final String mPassword;
- UserLoginTask(String username, String password) {
- mUsername = username;
- mPassword = password;
- }
- @Override
- protected Boolean doInBackground(Void... params) {
- // TODO: attempt authentication against a network service.
- try { // Simulate network access.
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- return false;
- }
- for (String credential : DUMMY_CREDENTIALS) {
- String[] pieces = credential.split(":");
- if (pieces[0].equals(mUsername)) {
- // Account exists, return true if the password matches.
- return pieces[1].equals(mPassword);
- }
- }
- return true;
- }
- @Override
- protected void onPostExecute(final Boolean success) {
- mAuthTask = null;
- showProgress(false);
- if (success) {
- //finish();
- Intent mainpageIntent = new Intent(LoginActivity.this, NavigationActivity.class);
- LoginActivity.this.startActivity(mainpageIntent);
- } else {
- mPasswordView.setError(getString(R.string.error_incorrect_password));
- mPasswordView.requestFocus();
- }
- }
- @Override
- protected void onCancelled() {
- mAuthTask = null;
- showProgress(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement