package com.neoranga55.cleanguitestarchitecture;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.neoranga55.cleanguitestarchitecture.support.util.CountingIdlingResourceListener;
import java.util.Random;
/**
* A login screen that offers login via email/password.
*/
public class LoginActivity extends AppCompatActivity {
/**
* Keep track of the login task to ensure we can cancel it if requested.
*/
private UserLoginTask mAuthTask = null;
// UI references.
private AutoCompleteTextView mNameView;
private EditText mPasswordView;
private View mProgressView;
private View mLoginFormView;
private static CountingIdlingResourceListener sIdlingNotificationListener;
public static void setIdlingNotificationListener(CountingIdlingResourceListener idlingNotificationListener) {
sIdlingNotificationListener = idlingNotificationListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form.
mNameView = (AutoCompleteTextView) findViewById(R.id.username);
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
final Button emailSignInButton = (Button) findViewById(R.id.login_button);
emailSignInButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
attemptLogin();
}
});
// Shown the login button with a nice animation
// only a 3 seconds after the application has started (app is idle in the meantime)
emailSignInButton.setAlpha(0);
emailSignInButton.setTranslationY(500);
emailSignInButton.animate()
.setStartDelay(1000)
.setInterpolator(new AccelerateDecelerateInterpolator())
.translationY(0)
.alpha(1)
.setDuration(2000)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (sIdlingNotificationListener != null) {
sIdlingNotificationListener.decrement(); // Resource is idle again
}
}
});
if (sIdlingNotificationListener != null) {
sIdlingNotificationListener.increment(); // Notify that our animation resource is busy
}
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 email, missing fields, etc.), the
* errors are presented and no actual login attempt is made.
*/
private void attemptLogin() {
if (mAuthTask != null) {
return;
}
// Show a progress spinner, while login
showProgress(true);
mAuthTask = new UserLoginTask(this, mNameView.getText().toString(),
mPasswordView.getText().toString());
mAuthTask.execute((Void) null);
}
/**
* 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);
}
}
/**
* Represents an asynchronous login/registration task used to authenticate
* the user.
*/
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
private final Activity mActivity;
private final String mUserName;
private final String mPassword;
UserLoginTask(Activity activity, String userName, String password) {
mActivity = activity;
mUserName = userName;
mPassword = password;
}
@Override
protected Boolean doInBackground(Void... params) {
try { // Simulate network access with random delay between 1 and 5 seconds
Thread.sleep((long)((new Random()).nextInt(4000) + 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);
Intent intent = new Intent(mActivity, WelcomeActivity.class);
Bundle extras = new Bundle();
extras.putString("USER", mUserName);
intent.putExtras(extras);
mActivity.startActivity(intent);
}
@Override
protected void onCancelled() {
mAuthTask = null;
showProgress(false);
}
}
}