Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. package com.example.ethantien.m4.Controller;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. import com.example.ethantien.m4.Model.Admin;
  12. import com.example.ethantien.m4.Model.Manager;
  13. import com.example.ethantien.m4.Model.Person;
  14. import com.example.ethantien.m4.Model.User;
  15. import com.example.ethantien.m4.Model.Worker;
  16. import com.example.ethantien.m4.Model.vars;
  17. import com.example.ethantien.m4.R;
  18. import com.google.firebase.database.DataSnapshot;
  19. import com.google.firebase.database.DatabaseError;
  20. import com.google.firebase.database.DatabaseReference;
  21. import com.google.firebase.database.FirebaseDatabase;
  22. import com.google.firebase.database.ValueEventListener;
  23.  
  24.  
  25. /**
  26. * Login Screen that the user can enter their credentials in to access their own account.
  27. * Has 2 textfields, username and password, and 2 buttons, Login and Cancel.
  28. */
  29. public class logScreen extends AppCompatActivity {
  30. private EditText username;
  31. private EditText password;
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_log_screen);
  37. login();
  38.  
  39.  
  40.  
  41.  
  42. Button cancel = (Button) findViewById(R.id.cancel);
  43.  
  44. /**
  45. * Button handler for the cancel button
  46. * @param view the button
  47. */
  48. cancel.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View view) {
  51. startActivity(new Intent(logScreen.this, MainActivity.class));
  52. finish();
  53. }
  54. });
  55. }
  56.  
  57. /**
  58. * takes the values from the textboxes and checks to see if a user exists with the given
  59. * username and password, and then enters the application with said credentials
  60. */
  61.  
  62. private void login() {
  63. username = (EditText) findViewById(R.id.user);
  64. password = (EditText) findViewById(R.id.pass);
  65.  
  66. Button enterKeys = (Button) findViewById(R.id.loginButton);
  67.  
  68. /**
  69. * Button handler for the login button
  70. * @param view the button
  71. */
  72. enterKeys.setOnClickListener(new View.OnClickListener() {
  73. @Override
  74. public void onClick(View view) {
  75. DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
  76. mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
  77. @Override
  78. public void onDataChange(DataSnapshot dataSnapshot) {
  79. String userName = username.getText().toString();
  80. String passw = password.getText().toString();
  81. if (userName.equals("") || passw.equals("")) {
  82. Toast.makeText(logScreen.this, "Please fill in entire form", Toast.LENGTH_LONG).show();
  83. } else {
  84. String str = "";
  85. if (dataSnapshot.child("Users").child(userName).getValue() != null) {
  86. str = "Users";
  87. } else if (dataSnapshot.child("Workers").child(userName).getValue() != null) {
  88. str = "Workers";
  89. } else if (dataSnapshot.child("Managers").child(userName).getValue() != null) {
  90. str = "Managers";
  91. } else if (dataSnapshot.child("Admins").child(userName).getValue() != null) {
  92. str = "Admins";
  93. }
  94. if (str.equals("")) {
  95. Toast.makeText(logScreen.this, "Invalid Username or Password", Toast.LENGTH_LONG).show();
  96. } else {
  97. if (dataSnapshot.child(str).child(userName).child("password").getValue().toString().equals(passw)) {
  98. Toast.makeText(logScreen.this, "Login Successful", Toast.LENGTH_LONG).show();
  99. switch (str) {
  100. case "Users":
  101. vars.getInstance().setCurrPerson(dataSnapshot.child("Users").child(userName).getValue(User.class));
  102. break;
  103. case "Workers":
  104. vars.getInstance().setCurrPerson(dataSnapshot.child("Workers").child(userName).getValue(Worker.class));
  105. break;
  106. case "Managers":
  107. vars.getInstance().setCurrPerson(dataSnapshot.child("Managers").child(userName).getValue(Manager.class));
  108. break;
  109. case "Admins":
  110. vars.getInstance().setCurrPerson(dataSnapshot.child("Admins").child(userName).getValue(Admin.class));
  111. break;
  112. }
  113. startActivity(new Intent(logScreen.this, startApplication.class));
  114. } else {
  115. Toast.makeText(logScreen.this, "Invalid Username or Password", Toast.LENGTH_LONG).show();
  116. }
  117. }
  118. }
  119. }
  120. @Override
  121. public void onCancelled(DatabaseError databaseError) {
  122. Toast.makeText(logScreen.this, "Database Error", Toast.LENGTH_LONG).show();
  123. }
  124. });
  125.  
  126.  
  127. }
  128. });
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement