Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LoginActivity with persistant AsyncTasks
package com.vokal.petcheck;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.vokal.petcheck.constants.Constants;
import com.vokal.petcheck.walk.User;
import java.util.ArrayList;
public class LoginActivity extends Activity
{
private static final String TAG = "LoginActivity";
private static final int SECURITY_ACTIVITY = 0;
private Button mLogin;
private Button mForgotPassword;
private EditText mUsername;
private EditText mPassword;
private ProgressDialog mDialog;
private LoginTask mLoginTask;
private ForgotPasswordLaunchTask mForgotTask;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login_activity);
ArrayList<Object> retained = (ArrayList<Object>) getLastNonConfigurationInstance();
if (retained != null) {
mLoginTask = (LoginTask) retained.get(0);
mForgotTask = (ForgotPasswordLaunchTask) retained.get(1);
}
if (mLoginTask != null) {
if (!mLoginTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
mLoginTask.setContext(this);
mDialog = ProgressDialog.show(LoginActivity.this,
"Loading", "Logging In");
}
}
if (mForgotTask != null) {
if (!mForgotTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
mForgotTask.setContext(this);
mDialog = ProgressDialog.show(LoginActivity.this,
"Loading", "Retrieving Security Question");
}
}
mLogin = (Button) findViewById(R.id.login_submit);
mForgotPassword = (Button) findViewById(R.id.login_forgot);
mUsername = (EditText) findViewById(R.id.login_username);
mPassword = (EditText) findViewById(R.id.login_password);
mLogin.setOnClickListener(mLoginListener);
mForgotPassword.setOnClickListener(mLoginListener);
}
@Override
public void onPause() {
super.onPause();
if (mDialog != null) {
mDialog.dismiss();
}
}
@Override
public void onStop() {
super.onStop();
if (mDialog != null) {
mDialog.dismiss();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mDialog != null) {
mDialog.dismiss();
}
}
@Override
public Object onRetainNonConfigurationInstance() {
ArrayList<Object> retainedObjects = new ArrayList<Object>();
retainedObjects.add(mLoginTask);
retainedObjects.add(mForgotTask);
return retainedObjects;
}
public void onActivityResult(int aRequestCode, int aResultCode, Intent aIntent) {
if (aRequestCode == SECURITY_ACTIVITY) {
if (aResultCode == RESULT_OK) {
launchWelcomeScreen();
}
}
}
public void checkLogin() {
String username = (mUsername.getText()).toString();
String password = (mPassword.getText()).toString();
boolean usernameEmpty = username.equals("");
boolean passwordEmpty = password.equals("");
if(!(usernameEmpty) &&!(passwordEmpty)) {
mLoginTask = new LoginTask();
mLoginTask.setContext(this);
mLoginTask.execute(username, password);
} else if (usernameEmpty && passwordEmpty) {
Toast.makeText(LoginActivity.this,
R.string.login_both_empty, Toast.LENGTH_SHORT).show();
} else if (usernameEmpty) {
Toast.makeText(LoginActivity.this,
R.string.login_username_empty, Toast.LENGTH_SHORT).show();
} else if (passwordEmpty) {
Toast.makeText(LoginActivity.this,
R.string.login_password_empty, Toast.LENGTH_SHORT).show();
}
}
public void forgotPressed() {
final EditText usernameField = new EditText(LoginActivity.this);
usernameField.setHint("username");
new AlertDialog.Builder(LoginActivity.this)
.setTitle("Enter Username")
.setView(usernameField)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface aDialog, int aWhich) {
String username = usernameField.getText().toString();
if (!username.equals("")) {
mForgotTask = new ForgotPasswordLaunchTask();
mForgotTask.setContext(LoginActivity.this);
mForgotTask.execute(username);
}
}
})
.setNegativeButton("Cancel", null)
.show();
}
public View.OnClickListener mLoginListener = new View.OnClickListener() {
public void onClick(View aView) {
switch(aView.getId()) {
case R.id.login_submit:
checkLogin();
break;
case R.id.login_forgot:
forgotPressed();
break;
default:
break;
}
}
};
public void launchWelcomeScreen() {
mUsername.setText("");
mPassword.setText("");
Intent i = new Intent();
i.setClass(LoginActivity.this, WelcomeActivity.class);
startActivity(i);
}
public class LoginTask extends AsyncTask<String, Void, Boolean> {
private LoginActivity mContext;
public void setContext(LoginActivity aContext) {
mContext = aContext;
}
public void onPreExecute() {
LoginActivity.this.mDialog = ProgressDialog.show(LoginActivity.this,
"Loading", "Logging In");
}
public Boolean doInBackground(String... aParams) {
boolean result = false;
if (!isCancelled()) {
result = User.getLogin(aParams[0], aParams[1]);
}
return result;
}
public void onPostExecute(Boolean aResult) {
if (!isCancelled()) {
mContext.clearDialog();
if (aResult == true) {
launchWelcomeScreen();
} else {
getWrongLoginDialog(LoginActivity.this).show();
}
}
}
}
public class ForgotPasswordLaunchTask extends AsyncTask<String, Void, String> {
private String mUsername;
private LoginActivity mContext;
public void setContext(LoginActivity aContext) {
mContext = aContext;
}
public void onPreExecute() {
mDialog = ProgressDialog.show(LoginActivity.this,
"Loading", "Retrieving Security Question");
}
public String doInBackground(String... aParams) {
String result = "";
if (!isCancelled()) {
mUsername = aParams[0];
result = User.getSecurityQuestion(aParams[0]);
}
return result;
}
public void onPostExecute(String aResult) {
if (!isCancelled()) {
mContext.clearDialog();
if (aResult != null && !aResult.equals("")) {
Intent i = new Intent();
i.setClass(LoginActivity.this, SecurityQuestionActivity.class);
i.putExtra(Constants.PARAM_USERNAME, mUsername);
i.putExtra(Constants.SECURITY_QUESTION, aResult);
startActivityForResult(i, SECURITY_ACTIVITY);
} else {
getWrongLoginDialog(LoginActivity.this).show();
}
}
}
}
public void clearDialog() {
Log.d(TAG, "Clearing Dialog");
mDialog.dismiss();
}
private AlertDialog.Builder getWrongLoginDialog(Context aContext) {
return new AlertDialog.Builder(aContext)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(aContext.getString(R.string.wrong_password))
.setMessage(aContext.getString(R.string.wrong_password_dialog))
.setPositiveButton("OK", null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.