Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. package com.mycompany.myapp;
  2. import android.content.*;
  3. import android.widget.*;
  4. import android.app.*;
  5. import java.security.*;
  6. import android.content.SharedPreferences;
  7. import android.content.Context;
  8.  
  9. public class MainLogin implements Logger
  10. {
  11. private String userName = null;
  12. private String password = null;
  13. private DBHelper db = null;
  14. private Context context;
  15. private SharedPreferences preferences;
  16. private SharedPreferences loggedInPreferences;
  17.  
  18. public void MainLoginSetter(String userName , String password , Context context){
  19. this.userName = userName;
  20. this.password = password;
  21. this.context = context;
  22. }
  23.  
  24. @Override
  25. public Boolean Login(){
  26. db = new DBHelper(context);
  27. preferences = context.getSharedPreferences(SignUpAndRegistrationActivity.SPREFERENCES, Context.MODE_PRIVATE);
  28. SharedPreferences.Editor editor = preferences.edit();
  29.  
  30.  
  31. if(this.userName.toString().equals(db.getUser(this.userName)) && this.password.toString().equals(db.getPassword(this.password))){
  32. editor.putBoolean("loggedIn", true);
  33. editor.putString("User",db.getRowUsers("UserName",this.userName));
  34. editor.commit();
  35. return true;
  36. }
  37. return false;
  38. }
  39.  
  40. @Override
  41. public Boolean Logout(){
  42. preferences = context.getSharedPreferences(SignUpAndRegistrationActivity.SPREFERENCES , Context.MODE_PRIVATE);
  43. SharedPreferences.Editor editor = preferences.edit();
  44. editor.clear();
  45. editor.commit();
  46. editor.putBoolean("loggedIn", false);
  47. return true;
  48. }
  49.  
  50. }
  51.  
  52. package com.mycompany.myapp;
  53.  
  54. import android.app.*;
  55. import android.os.*;
  56. import android.widget.*;
  57. import android.content.*;
  58. import android.view.View.*;
  59. import android.view.*;
  60. import java.security.*;
  61. import java.nio.charset.*;
  62. import android.util.*;
  63.  
  64. public class MainActivity extends Activity
  65. {
  66. private MainLogin mainLogin;
  67. private FacebookLogin fbLogin;
  68. private GoogleLogin googleLogin;
  69. private Intent intent;
  70. private Boolean LoggedIn = null;
  71. private Boolean Failure = false;
  72. private Integer loginAttempts = 3;
  73.  
  74. @Override
  75. protected void onCreate(Bundle savedInstanceState)
  76. {
  77. super.onCreate(savedInstanceState);
  78. setContentView(R.layout.main);
  79.  
  80. final EditText userName = (EditText) findViewById(R.id.credential_one);
  81. final EditText password = (EditText) findViewById(R.id.credential_two);
  82. final Button loginButton = (Button) findViewById(R.id.loginButton);
  83.  
  84. loginButton.setOnClickListener(new OnClickListener(){
  85. public void onClick(View view){
  86. LoggedIn = true;
  87.  
  88. if(userName.getText().toString().isEmpty() || password.getText().toString().isEmpty() && userName.getText().toString() == null && password.getText().toString() == null){
  89. Toast.makeText(MainActivity.this, "Please fill in credentials", Toast.LENGTH_LONG).show();
  90. intent = new Intent(MainActivity.this , Home.class);
  91. } else {
  92.  
  93. mainLogin = new MainLogin();
  94.  
  95. mainLogin.MainLoginSetter(userName.getText().toString(), password.getText().toString(), MainActivity.this);
  96. if(mainLogin.Login() == LoggedIn){
  97. intent = new Intent(MainActivity.this , Home.class);
  98. startActivity(intent);
  99. finish();
  100. } else if(mainLogin.Login() == Failure){
  101. loginAttempts--;
  102. }
  103. }
  104.  
  105. if(loginAttempts == 0){
  106. Toast.makeText(MainActivity.this, "Unsuccessful Login", Toast.LENGTH_LONG).show();
  107. }
  108. }
  109. });
  110.  
  111. final TextView signup = (TextView) findViewById(R.id.signupTextView);
  112.  
  113. signup.setOnClickListener(new OnClickListener(){
  114. public void onClick(View view){
  115. intent = new Intent(MainActivity.this, SignUpAndRegistrationActivity.class);
  116. startActivity(intent);
  117. }
  118. });
  119.  
  120. }
  121.  
  122. @Override
  123. protected void onResume(){
  124. SharedPreferences preferences = this.getSharedPreferences(SignUpAndRegistrationActivity.SPREFERENCES , Context.MODE_PRIVATE);
  125. boolean signedIn = preferences.getBoolean("loggedIn", false);
  126. if(signedIn == true){
  127. Intent intent = new Intent(MainActivity.this, Home.class);
  128. startActivity(intent);
  129. finish();
  130. } else if(signedIn == false){
  131. super.onResume();
  132. }
  133. super.onResume();
  134. }
  135.  
  136. @Override
  137. public void onBackPressed(){
  138. finish();
  139. super.onBackPressed();
  140. }
  141.  
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement