Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- new Handler().postDelayed(
- new Runnable() {
- @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
- public void run() {
- // On complete call either onLoginSuccess or onLoginFailed
- onLoginSuccess();
- //openMyc();
- progressDialog.dismiss();
- }
- }, 3000);
- }
- public void startActivity() {
- Intent intent = new Intent(this, MainActivity.class);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if ((requestCode != REQUEST_SIGNUP)) {
- if ((resultCode != RESULT_OK)) {
- // TODO: Implement successful signup logic here
- // By default we just finish the Activity and log them in automatically
- this.openMyc();
- }
- }
- }
- public class LoginActivity extends AppCompatActivity {
- private static final String TAG = "LoginActivity";
- private static final int REQUEST_SIGNUP = 0;
- private FirebaseAuth mAuth;
- @BindView(R.id.input_email) EditText _emailText;
- @BindView(R.id.input_password) EditText _passwordText;
- @BindView(R.id.btn_login) Button _loginButton;
- @BindView(R.id.link_signup) TextView _signupLink;
- //@BindView(R.id.btn_logout) Button _logoutButton;
- FirebaseAuth.AuthStateListener listener;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- ButterKnife.bind(this);
- mAuth = FirebaseAuth.getInstance();
- listener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = mAuth.getCurrentUser();
- if (user == null){
- //No Ha ingeresado
- }else {
- openMyc();
- Toast.makeText(getApplicationContext(), "Right and logged", Toast.LENGTH_LONG).show();
- //YA ingreso
- }
- }
- };
- _loginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- login();
- }
- });
- /**_logoutButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- login();
- }
- });**/
- _signupLink.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Start the Signup activity
- Intent intent = new Intent(getApplicationContext(), SignupActivity.class);
- startActivityForResult(intent, REQUEST_SIGNUP);
- finish();
- overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
- }
- });
- }
- public void login() {
- Log.d(TAG, "Login");
- if (!validate()) {
- onLoginFailed();
- return;
- }
- _loginButton.setEnabled(false);
- final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
- R.style.AppTheme_Dark_Dialog);
- progressDialog.setIndeterminate(true);
- progressDialog.setMessage("Authenticating...");
- progressDialog.show();
- String email = _emailText.getText().toString();
- String password = _passwordText.getText().toString();
- // TODO: Implement your own authentication logic here.
- if (!email.isEmpty() && !password.isEmpty()){
- mAuth.signInWithEmailAndPassword(email,password) .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()){
- Toast.makeText(getApplicationContext(), "Correct login :)", Toast.LENGTH_LONG).show();
- }else {
- Toast.makeText(getApplicationContext(), "Incorrect login :(", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- new android.os.Handler().postDelayed(
- new Runnable() {
- @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
- public void run() {
- // On complete call either onLoginSuccess or onLoginFailed
- onLoginSuccess();
- progressDialog.dismiss();
- }
- }, 3000);
- }
- public void startActivity() {
- Intent intent = new Intent(this, MainActivity.class);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if ((requestCode != REQUEST_SIGNUP)) {
- if ((resultCode != RESULT_OK)) {
- // TODO: Implement successful signup logic here
- // By default we just finish the Activity and log them in automatically
- this.openMyc();
- }
- }
- }
- @Override
- public void onBackPressed() {
- // Disable going back to the MainActivity
- moveTaskToBack(true);
- }
- public void onLoginSuccess() {
- _loginButton.setEnabled(true);
- finish();
- }
- public void onLoginFailed() {
- Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
- _loginButton.setEnabled(true);
- }
- public boolean validate() {
- boolean valid = true;
- String email = _emailText.getText().toString();
- String password = _passwordText.getText().toString();
- if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
- _emailText.setError("enter a valid email address");
- valid = false;
- } else {
- _emailText.setError(null);
- }
- if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
- _passwordText.setError("between 4 and 10 alphanumeric characters");
- valid = false;
- } else {
- _passwordText.setError(null);
- }
- return valid;
- }
- private void openMyc() {
- Intent i = new Intent(this,MyCount.class);
- startActivity(i);
- finish();
- }
- @Override
- protected void onStart() {
- super.onStart();
- mAuth.addAuthStateListener(listener);
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (listener != null){
- mAuth.removeAuthStateListener(listener);
- }
- }
Add Comment
Please, Sign In to add comment