Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2.  
  3. public static final String TAG = "activity_main";
  4. private static final String APP_PREFERENCES_PASSWORD = "Password";
  5. public static final String APP_PREFERENCES_USER = "User";
  6.  
  7. private FirebaseAuth mAuth;
  8. private FirebaseAuth.AuthStateListener mAuthListner;
  9.  
  10.  
  11. private TextView email;
  12. private TextView password;
  13. private CheckBox remember_me;
  14. private ProgressBar progressBar;
  15.  
  16.  
  17. SharedPreferences mSettings;
  18.  
  19. EditSharedPreferences editSharedPreferences = new EditSharedPreferences();
  20.  
  21. ChangeStateProgressBar changeStateProgressBar = new ChangeStateProgressBar();
  22.  
  23. CheckInternetConnection checkInternetConnection = new CheckInternetConnection();
  24.  
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(layout.activity_main);
  28.  
  29.  
  30. mSettings = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
  31.  
  32. mAuth = FirebaseAuth.getInstance(); // get instance of fireBase
  33.  
  34. if(mAuth.getCurrentUser()!=null){
  35. if(checkAdmin(mAuth.getCurrentUser().getEmail())){
  36. Intent intent = new Intent(MainActivity.this, admin_Activity.class);
  37. startActivity(intent);
  38. }else{
  39. Intent intent = new Intent(MainActivity.this, Categories.class);
  40. startActivity(intent);
  41. }
  42. }
  43.  
  44. progressBar = (ProgressBar)findViewById(id.progressBar);
  45. email = (TextView) findViewById(id.email);
  46. password = (TextView) findViewById(id.password);
  47. remember_me = (CheckBox)findViewById(id.Remember_Me);
  48.  
  49.  
  50. // check file APP_PREFERENCES. If it consist
  51. // APP_PREFERENCES_USER code need to set email text view
  52. if(mSettings.contains(APP_PREFERENCES_USER)){
  53. email.setText(mSettings.getString(APP_PREFERENCES_USER, ""));
  54. }
  55.  
  56. if(mSettings.contains(APP_PREFERENCES_PASSWORD)) {
  57. password.setText(mSettings.getString(APP_PREFERENCES_PASSWORD, ""));
  58. }
  59.  
  60. // if user connect or disconnect listener recieves
  61. // an answer of instance
  62. mAuthListner = new FirebaseAuth.AuthStateListener() {
  63. @Override
  64. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  65. FirebaseUser user = firebaseAuth.getCurrentUser();
  66. if (user != null) {
  67. Log.d(TAG, "Sign_in" + user.getUid());
  68. } else {
  69. Log.d(TAG, "Sign_out");
  70. }
  71. }
  72. };
  73.  
  74.  
  75. //find buttons
  76. findViewById(id.sign_in_Button).setOnClickListener(this);
  77. findViewById(id.registration_Button).setOnClickListener(this);
  78.  
  79. }
  80.  
  81.  
  82. public void onStart() {
  83. super.onStart();
  84. mAuth.addAuthStateListener(mAuthListner);
  85. }
  86.  
  87. public void onStop() {
  88. super.onStop();
  89.  
  90. //must to remove of listener when
  91. // app closed
  92. if (mAuthListner != null) {
  93. mAuth.removeAuthStateListener(mAuthListner);
  94. }
  95. }
  96.  
  97.  
  98. @Override
  99. public void onClick(View v) {
  100. if (v.getId() == id.sign_in_Button) {
  101.  
  102. //check email and password rows for filling
  103. // if any rows an empty we take an
  104. // toast error
  105. signIn();
  106. }
  107.  
  108. // if we press registration button
  109.  
  110. if (v.getId() == id.registration_Button) {
  111. // show progress bar
  112.  
  113. changeStateProgressBar.showProgressBar(progressBar);
  114.  
  115. //start registration activity
  116. Intent intent = new Intent(MainActivity.this, RegistrationActivity.class);
  117. startActivity(intent);
  118.  
  119. changeStateProgressBar.hideProgressBar(progressBar);
  120.  
  121. }
  122. }
  123.  
  124.  
  125.  
  126. private void signIn(){
  127. if ((!email.getText().toString().equals(null)
  128. && !email.getText().toString().equals(""))
  129. || (!password.getText().toString().equals(null)
  130. && !password.getText().toString().equals(""))) {
  131.  
  132. //start progress bar visibility, when we do
  133. // hard piece of code
  134. changeStateProgressBar.showProgressBar(progressBar);
  135.  
  136. // try to sign in with email and password
  137.  
  138. mAuth.signInWithEmailAndPassword(email.getText().toString(), password.getText().toString())
  139.  
  140. // need to add complete listener to track of successful doing
  141. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  142. @Override
  143. public void onComplete(@NonNull Task<AuthResult> task) {
  144.  
  145. //if task is successfull need to go to another intent
  146. if (task.isSuccessful()) {
  147. //delete from settings
  148. editSharedPreferences.deletePreferences(mSettings, APP_PREFERENCES_PASSWORD);
  149. editSharedPreferences.deletePreferences(mSettings,APP_PREFERENCES_USER);
  150.  
  151. if(remember_me.isChecked()) {
  152. // remember me
  153. editSharedPreferences.addPreferences(mSettings, APP_PREFERENCES_USER, email.getText().toString());
  154. editSharedPreferences.addPreferences(mSettings, APP_PREFERENCES_PASSWORD, password.getText().toString());
  155. }
  156. // check email for administrator
  157. // email, if administrator entered
  158. // start entent Administrator activity
  159. if (checkAdmin(email.getText().toString())) {
  160.  
  161. //start admin activity
  162. Intent intent = new Intent(MainActivity.this, admin_Activity.class);
  163. startActivity(intent);
  164.  
  165. // hide progress bar
  166. changeStateProgressBar.hideProgressBar(progressBar);
  167.  
  168. email.setText("");
  169. password.setText("");
  170.  
  171. } else{
  172.  
  173. // if not admin entered, start personal activity
  174. Log.d(TAG, "Succesful enter");
  175.  
  176. //start new activity
  177. Intent intent = new Intent(MainActivity.this, Categories.class);
  178. startActivity(intent);
  179.  
  180. // HIDE PROGRESS bar
  181. changeStateProgressBar.hideProgressBar(progressBar);
  182.  
  183. email.setText("");
  184. password.setText("");
  185. }
  186. }
  187. }
  188. })
  189.  
  190. // if in process of entering
  191. // we have an errors we must
  192. // send toast with code of error
  193. .addOnFailureListener(new OnFailureListener() {
  194. @Override
  195. public void onFailure(@NonNull Exception e) {
  196. // make toast message if there is anyobe exception
  197. Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
  198. password.setText("");
  199. }
  200. });
  201. }else{
  202. // make toast message if pass or email rows is empty
  203. Toast.makeText(MainActivity.this, "Password or email is empty", Toast.LENGTH_SHORT).show();
  204. }
  205. }
  206.  
  207. private boolean checkAdmin(String email){
  208. if(email.equals("admin@rea.ru")){
  209.  
  210. return true;
  211. }else{
  212.  
  213. return false;
  214. }
  215.  
  216. }
  217.  
  218. public class Categories extends AppCompatActivity implements View.OnClickListener
  219. {
  220. private EditSharedPreferences editSharedPreferences;
  221. private static final String APP_PREFERENCES_USER = "User";
  222. private static final String APP_PREFERENCES_PASSWORD = "Password";
  223. private static final String APP_PREFERENCES_DOESNOT_REMEMBER = "isChecked";
  224.  
  225. SharedPreferences mSettings;
  226.  
  227. Button problemsWithTeachers, signOut;
  228. private static final String TAG = "CATEGORIES";
  229. @Override
  230. protected void onCreate(Bundle savedInstanceState) {
  231. super.onCreate(savedInstanceState);
  232. setContentView(layout.activity_categories);
  233.  
  234. mSettings = PreferenceManager.getDefaultSharedPreferences(Categories.this);
  235.  
  236. problemsWithTeachers = (Button)findViewById(id.SignOutBut);
  237. signOut = (Button)findViewById(id.problemsWithTeachersBut);
  238. signOut.setOnClickListener(this);
  239. problemsWithTeachers.setOnClickListener(this);
  240.  
  241. }
  242.  
  243. @Override
  244. public void onClick(View v) {
  245. if(v.getId() == id.SignOutBut){
  246. logout();
  247.  
  248. }else if(v.getId() == id.problemsWithTeachersBut){
  249. signOut.setVisibility(View.GONE);
  250. }
  251.  
  252. }
  253.  
  254.  
  255. private void logout(){
  256. FirebaseAuth mAuth = FirebaseAuth.getInstance();
  257. mAuth.signOut();
  258. Intent intent = new Intent(Categories.this, MainActivity.class);
  259. startActivity(intent);
  260. }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement