@Override protected void onPostExecute(final Boolean success) { mAuthTask = null; // Check if no view has focus: if (success) { verified = true; if (getParent() == null) { setResult(1); } else { getParent().setResult(1); } Snackbar.make( findViewById(R.id.login_root), "'" + CurrentState.getUser().getUsername() + "' signed in.", Snackbar.LENGTH_LONG) .show(); // We are done. Go back to MainActivity, after a set delay. TimerTask task = new TimerTask() { @Override public void run() { // NavUtils.navigateUpFromSameTask(LoginActivity.this); finish(); } }; Timer t = new Timer(); t.schedule(task, 1000); } else { if (null != attempts.get(email) && attempts.get(email) >= 2) { Snackbar.make(rootView, "ACCOUNT '" + email + "' LOCKED!", Snackbar.LENGTH_SHORT).show(); IOActions.getUserByUsername(email).setPermission(0); } if (IOActions.getUserByUsername(email) != null) { if (attempts.get(email) != null) { attempts.put(email, attempts.get(email) + 1); Snackbar.make( rootView, (3 - attempts.get(email)) + " attempts remaining for " + email, Snackbar.LENGTH_SHORT) .show(); } else { attempts.put(email, 1); Snackbar.make(rootView, "2 attempts remaining for " + email, Snackbar.LENGTH_SHORT) .show(); Log.i("GTMovies", error); } } Log.i("GTMovies", error); if (error.equals("duplicate")) { mEmailView.setError("User already exists"); mEmailView.requestFocus(); } else if (error.equals("nulluser")) { mEmailView.setError("User not found"); mEmailView.requestFocus(); } else { mPasswordView.setError(getString(R.string.error_incorrect_password)); mPasswordView.requestFocus(); } } }
/** * 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 and no actual login * attempt is made. */ private void attemptLogin() { // hide keyboard if (rootView != null) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0); } // Reset errors. mEmailView.setError(null); mPasswordView.setError(null); mPassConfirmView.setError(null); mNameView.setError(null); // Store values at the time of the login attempt. email = mEmailView.getText().toString(); password = mPasswordView.getText().toString(); passwordCheck = mPassConfirmView.getText().toString(); name = mNameView.getText().toString(); // check if should cancel if (mAuthTask != null) { return; } else if (null != attempts.get(email) && attempts.get(email) >= 3) { Snackbar.make(rootView, "ACCOUNT '" + email + "' LOCKED!", Snackbar.LENGTH_SHORT).show(); return; } else if (null != IOActions.getUserByUsername(email) && IOActions.getUserByUsername(email).getPermission() == -1) { Snackbar.make(rootView, "ACCOUNT '" + email + "' IS BANNED", Snackbar.LENGTH_SHORT).show(); return; } boolean cancel = false; View focusView = null; // ALWAYS CHECK THESE FIELDS if (TextUtils.isEmpty(email)) { // username empty mEmailView.setError(getString(R.string.error_field_required)); focusView = mEmailView; cancel = true; } else if (TextUtils.isEmpty(password)) { // password empty mPasswordView.setError(getString(R.string.error_field_required)); focusView = mPasswordView; cancel = true; } else if (email.length() <= 3) { // username length mEmailView.setError(getString(R.string.error_invalid_email)); focusView = mEmailView; cancel = true; } else if (password.length() <= 3) { // password length mPasswordView.setError(getString(R.string.error_invalid_password)); focusView = mPasswordView; cancel = true; } // REGISTERING? if (findViewById(R.id.pwRegister).getVisibility() == (View.VISIBLE)) { if (!password.equals(passwordCheck)) { // password confirm mPassConfirmView.setError("Passwords do not match."); focusView = mPassConfirmView; cancel = true; } else if (TextUtils.isEmpty(name)) { // name entry mNameView.setError("Please enter your name."); focusView = mNameView; cancel = true; } } else { register = false; } if (cancel) { // cancel and focus on bad field focusView.requestFocus(); } else { // sign in or register mAuthTask = new UserLoginTask(); mAuthTask.execute((Void) null); mPasswordView.setText(""); mPassConfirmView.setText(""); } }