Guest User

Untitled

a guest
Nov 21st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. new Handler().postDelayed(
  2. new Runnable() {
  3. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  4. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  5. public void run() {
  6. // On complete call either onLoginSuccess or onLoginFailed
  7. onLoginSuccess();
  8. //openMyc();
  9. progressDialog.dismiss();
  10. }
  11. }, 3000);
  12. }
  13.  
  14. public void startActivity() {
  15. Intent intent = new Intent(this, MainActivity.class);
  16. }
  17.  
  18. @Override
  19. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  20. if ((requestCode != REQUEST_SIGNUP)) {
  21. if ((resultCode != RESULT_OK)) {
  22.  
  23. // TODO: Implement successful signup logic here
  24. // By default we just finish the Activity and log them in automatically
  25. this.openMyc();
  26. }
  27. }
  28. }
  29.  
  30. public class LoginActivity extends AppCompatActivity {
  31. private static final String TAG = "LoginActivity";
  32. private static final int REQUEST_SIGNUP = 0;
  33. private FirebaseAuth mAuth;
  34. @BindView(R.id.input_email) EditText _emailText;
  35. @BindView(R.id.input_password) EditText _passwordText;
  36. @BindView(R.id.btn_login) Button _loginButton;
  37. @BindView(R.id.link_signup) TextView _signupLink;
  38. //@BindView(R.id.btn_logout) Button _logoutButton;
  39.  
  40. FirebaseAuth.AuthStateListener listener;
  41.  
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_login);
  45. ButterKnife.bind(this);
  46.  
  47. mAuth = FirebaseAuth.getInstance();
  48. listener = new FirebaseAuth.AuthStateListener() {
  49. @Override
  50. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  51. FirebaseUser user = mAuth.getCurrentUser();
  52. if (user == null){
  53. //No Ha ingeresado
  54. }else {
  55. openMyc();
  56. Toast.makeText(getApplicationContext(), "Right and logged", Toast.LENGTH_LONG).show();
  57. //YA ingreso
  58. }
  59. }
  60. };
  61.  
  62. _loginButton.setOnClickListener(new View.OnClickListener() {
  63.  
  64. @Override
  65. public void onClick(View v) {
  66. login();
  67. }
  68. });
  69.  
  70. /**_logoutButton.setOnClickListener(new View.OnClickListener() {
  71.  
  72. @Override
  73. public void onClick(View v) {
  74. login();
  75. }
  76. });**/
  77.  
  78. _signupLink.setOnClickListener(new View.OnClickListener() {
  79.  
  80. @Override
  81. public void onClick(View v) {
  82. // Start the Signup activity
  83. Intent intent = new Intent(getApplicationContext(), SignupActivity.class);
  84. startActivityForResult(intent, REQUEST_SIGNUP);
  85. finish();
  86. overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
  87. }
  88. });
  89.  
  90.  
  91. }
  92.  
  93. public void login() {
  94.  
  95. Log.d(TAG, "Login");
  96.  
  97. if (!validate()) {
  98. onLoginFailed();
  99. return;
  100. }
  101.  
  102. _loginButton.setEnabled(false);
  103.  
  104. final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
  105. R.style.AppTheme_Dark_Dialog);
  106. progressDialog.setIndeterminate(true);
  107. progressDialog.setMessage("Authenticating...");
  108. progressDialog.show();
  109.  
  110. String email = _emailText.getText().toString();
  111. String password = _passwordText.getText().toString();
  112.  
  113. // TODO: Implement your own authentication logic here.
  114. if (!email.isEmpty() && !password.isEmpty()){
  115. mAuth.signInWithEmailAndPassword(email,password) .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  116. @Override
  117. public void onComplete(@NonNull Task<AuthResult> task) {
  118. if (task.isSuccessful()){
  119. Toast.makeText(getApplicationContext(), "Correct login :)", Toast.LENGTH_LONG).show();
  120. }else {
  121. Toast.makeText(getApplicationContext(), "Incorrect login :(", Toast.LENGTH_LONG).show();
  122.  
  123. }
  124. }
  125. });
  126. }
  127.  
  128. new android.os.Handler().postDelayed(
  129. new Runnable() {
  130. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  131. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  132. public void run() {
  133. // On complete call either onLoginSuccess or onLoginFailed
  134. onLoginSuccess();
  135. progressDialog.dismiss();
  136. }
  137. }, 3000);
  138. }
  139.  
  140. public void startActivity() {
  141. Intent intent = new Intent(this, MainActivity.class);
  142. }
  143.  
  144. @Override
  145. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  146. if ((requestCode != REQUEST_SIGNUP)) {
  147. if ((resultCode != RESULT_OK)) {
  148.  
  149. // TODO: Implement successful signup logic here
  150. // By default we just finish the Activity and log them in automatically
  151. this.openMyc();
  152. }
  153. }
  154. }
  155.  
  156. @Override
  157. public void onBackPressed() {
  158. // Disable going back to the MainActivity
  159. moveTaskToBack(true);
  160. }
  161.  
  162. public void onLoginSuccess() {
  163. _loginButton.setEnabled(true);
  164. finish();
  165. }
  166.  
  167. public void onLoginFailed() {
  168. Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
  169.  
  170. _loginButton.setEnabled(true);
  171. }
  172.  
  173. public boolean validate() {
  174. boolean valid = true;
  175.  
  176. String email = _emailText.getText().toString();
  177. String password = _passwordText.getText().toString();
  178.  
  179. if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
  180. _emailText.setError("enter a valid email address");
  181. valid = false;
  182. } else {
  183. _emailText.setError(null);
  184. }
  185.  
  186. if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
  187. _passwordText.setError("between 4 and 10 alphanumeric characters");
  188. valid = false;
  189. } else {
  190. _passwordText.setError(null);
  191. }
  192.  
  193. return valid;
  194. }
  195.  
  196. private void openMyc() {
  197. Intent i = new Intent(this,MyCount.class);
  198. startActivity(i);
  199. finish();
  200. }
  201. @Override
  202. protected void onStart() {
  203. super.onStart();
  204. mAuth.addAuthStateListener(listener);
  205. }
  206.  
  207. @Override
  208. protected void onStop() {
  209. super.onStop();
  210. if (listener != null){
  211. mAuth.removeAuthStateListener(listener);
  212. }
  213. }
Add Comment
Please, Sign In to add comment