@Override protected Boolean doInBackground(Void... params) { error = ""; if (register) { // register a new user try { IOActions.addUser(new User(email, password, name)); boolean result = IOActions.loginUser(email, password); Log.println(Log.DEBUG, "GTMovies", "REGISTER: returning '" + register + "'"); return result; } catch (DuplicateUserException e) { Log.e("GTMovies", e.getMessage()); error = "duplicate"; return false; } catch (IllegalUserException e) { Log.e("GTMovies", e.getMessage()); return false; } catch (NullUserException e) { Log.e("GTMovies", e.getMessage()); return false; } } else { // login like normal try { boolean result = IOActions.loginUser(email, password); Log.println(Log.DEBUG, "GTMovies", "REGISTER: returning '" + register + "'"); return result; } catch (NullUserException e) { Log.e("GTMovies", e.getMessage()); error = "nulluser"; return false; } } }
@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(); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* SET THIS IN ADVANCE IN CASE ACTIVITY IS DESTROYED! * @Matt 17/March/2016 */ setResult(-1); Log.println(Log.WARN, "GTMovies", "LOGIN ACTIVITY ONCREATE!"); if (IOActions.userSignedIn()) { Log.i("GTMovies", "user already logged in"); finish(); return; } setContentView(R.layout.activity_login); rootView = findViewById(R.id.login_root); // start welcome screen startActivityForResult(new Intent(this, WelcomeActivity.class), 1); // load existing users try { accounts = IOActions.getAccounts(); } catch (Exception e) { Log.e("GTMovies", "Exception: " + Log.getStackTraceString(e)); } // remove up button ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setHomeButtonEnabled(false); actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setDisplayShowHomeEnabled(false); } // Set up the login form. mEmailView = (TextView) findViewById(R.id.email); mNameView = (EditText) findViewById(R.id.name); mPassConfirmView = (EditText) findViewById(R.id.passwordConfirm); 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; } }); // buttons Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); mEmailSignInButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View view) { attemptLogin(); } }); Button mRegisterButton = (Button) findViewById(R.id.register_button); mRegisterButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View view) { onRegisterPressed(); } }); }
/** * 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(""); } }