/** * 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() { // Reset errors. mEmailView.setError(null); mPasswordView.setError(null); // Store values at the time of the login attempt. String email = mEmailView.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) && !isPasswordValid(password)) { mPasswordView.setError(getString(R.string.error_invalid_password)); focusView = mPasswordView; cancel = true; } // Check for a valid email address. if (TextUtils.isEmpty(email)) { mEmailView.setError(getString(R.string.error_field_required)); focusView = mEmailView; cancel = true; } else if (!isEmailValid(email)) { mEmailView.setError(getString(R.string.error_invalid_email)); focusView = mEmailView; cancel = true; } if (cancel) { // There was an error; don't attempt login and focus the first // form 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); AuthenUser authenUser = new AuthenUser(email); authenUser.setAuthenticateInfo(password); try { LoginService.getInstance().login(authenUser); finish(); } catch (InvalidUserInfoException e) { mEmailView.setError("Invalid username or password"); showProgress(false); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (LoginService.getInstance().validateCurrentSession() || FacebookHelper.getInstance().login() != null) { finish(); return; } FacebookHelper.getInstance().logout(); setContentView(R.layout.activity_login); // Set up the login form. mEmailView = (AutoCompleteTextView) findViewById(R.id.email); populateAutoComplete(); 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; } }); Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); mEmailSignInButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View view) { attemptLogin(); } }); mLoginFormView = findViewById(R.id.login_form); mProgressView = findViewById(R.id.login_progress); callbackManager = CallbackManager.Factory.create(); LoginButton loginButton = (LoginButton) findViewById(R.id.facebook_signin_button); loginButton.registerCallback( callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { ProfileTracker profileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged( Profile oldProfile, Profile currentProfile) { this.stopTracking(); Profile.setCurrentProfile(currentProfile); FacebookHelper.getInstance().login(); finish(); } }; if (Profile.getCurrentProfile() != null) { FacebookHelper.getInstance().login(); finish(); } else { showProgress(true); profileTracker.startTracking(); } } @Override public void onCancel() { Log.i("FB", "Cancel"); } @Override public void onError(FacebookException error) { // Toast.makeText(getApplicationContext(), "Error : " + error.toString(), // Toast.LENGTH_LONG).show(); Log.e("FB", "Error " + error.toString()); } }); }