Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. public class LoginActivity extends Activity {
  2. private static final String TAG = RegisterActivity.class.getSimpleName();
  3. private Button btnLogin;
  4. private Button btnLinkToRegister;
  5. private EditText inputEmail;
  6. private EditText inputPassword;
  7. private ProgressDialog pDialog;
  8. private SessionManager session;
  9. private SQLiteHandler db;
  10.  
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_login);
  15.  
  16. inputEmail = (EditText) findViewById(R.id.email);
  17. inputPassword = (EditText) findViewById(R.id.password);
  18. btnLogin = (Button) findViewById(R.id.btnLogin);
  19. btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
  20.  
  21. // Progress dialog
  22. pDialog = new ProgressDialog(this);
  23. pDialog.setCancelable(false);
  24.  
  25. // SQLite database handler
  26. db = new SQLiteHandler(getApplicationContext());
  27.  
  28. // Session manager
  29. session = new SessionManager(getApplicationContext());
  30.  
  31. // Check if user is already logged in or not
  32. if (session.isLoggedIn()) {
  33. // User is already logged in. Take him to main activity
  34. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  35. startActivity(intent);
  36. finish();
  37. }
  38.  
  39. // Login button Click Event
  40. btnLogin.setOnClickListener(new View.OnClickListener() {
  41.  
  42. public void onClick(View view) {
  43. String email = inputEmail.getText().toString().trim();
  44. String password = inputPassword.getText().toString().trim();
  45.  
  46. // Check for empty data in the form
  47. if (!email.isEmpty() && !password.isEmpty()) {
  48. // login user
  49. checkLogin(email, password);
  50. } else {
  51. // Prompt user to enter credentials
  52. Toast.makeText(getApplicationContext(),
  53. "Please enter the credentials!", Toast.LENGTH_LONG)
  54. .show();
  55. }
  56. }
  57.  
  58. });
  59.  
  60. // Link to Register Screen
  61. btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
  62.  
  63. public void onClick(View view) {
  64. Intent i = new Intent(getApplicationContext(),
  65. RegisterActivity.class);
  66. startActivity(i);
  67. finish();
  68. }
  69. });
  70.  
  71. }
  72.  
  73. /**
  74. * function to verify login details in mysql db
  75. * */
  76. private void checkLogin(final String email, final String password) {
  77. // Tag used to cancel the request
  78. String tag_string_req = "req_login";
  79.  
  80. pDialog.setMessage("Logging in ...");
  81. showDialog();
  82.  
  83. StringRequest strReq = new StringRequest(Method.POST,
  84. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  85.  
  86. @Override
  87. public void onResponse(String response) {
  88. Log.d(TAG, "Login Response: " + response.toString());
  89. hideDialog();
  90.  
  91. try {
  92. JSONObject jObj = new JSONObject(response);
  93. boolean error = jObj.getBoolean("error");
  94.  
  95. // Check for error node in json
  96. if (!error) {
  97. // user successfully logged in
  98. // Create login session
  99. session.setLogin(true);
  100.  
  101. // Now store the user in SQLite
  102. String uid = jObj.getString("uid");
  103.  
  104. JSONObject user = jObj.getJSONObject("user");
  105. String name = user.getString("name");
  106. String email = user.getString("email");
  107. String created_at = user
  108. .getString("created_at");
  109.  
  110. // Inserting row in users table
  111. db.addUser(name, email, uid, created_at);
  112.  
  113. // Launch main activity
  114. Intent intent = new Intent(LoginActivity.this,
  115. MainActivity.class);
  116. startActivity(intent);
  117. finish();
  118. } else {
  119. // Error in login. Get the error message
  120. String errorMsg = jObj.getString("error_msg");
  121. Toast.makeText(getApplicationContext(),
  122. errorMsg, Toast.LENGTH_LONG).show();
  123. }
  124. } catch (JSONException e) {
  125. // JSON error
  126. e.printStackTrace();
  127. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  128. }
  129.  
  130. }
  131. }, new Response.ErrorListener() {
  132.  
  133. @Override
  134. public void onErrorResponse(VolleyError error) {
  135. Log.e(TAG, "Login Error: " + error.getMessage());
  136. Toast.makeText(getApplicationContext(),
  137. error.getMessage(), Toast.LENGTH_LONG).show();
  138. hideDialog();
  139. }
  140. }) {
  141.  
  142. @Override
  143. protected Map<String, String> getParams() {
  144. // Posting parameters to login url
  145. Map<String, String> params = new HashMap<String, String>();
  146. params.put("email", email);
  147. params.put("password", password);
  148.  
  149. return params;
  150. }
  151.  
  152. };
  153.  
  154. // Adding request to request queue
  155. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  156. }
  157.  
  158. private void showDialog() {
  159. if (!pDialog.isShowing())
  160. pDialog.show();
  161. }
  162.  
  163. private void hideDialog() {
  164. if (pDialog.isShowing())
  165. pDialog.dismiss();
  166. }
  167. }
  168.  
  169. protected void onCreate(Bundle savedInstanceState) {
  170. super.onCreate(savedInstanceState);
  171. setContentView(R.layout.activity_main);
  172.  
  173. txtName = (TextView) findViewById(R.id.name);
  174. txtEmail = (TextView) findViewById(R.id.email);
  175. btnLogout = (Button) findViewById(R.id.btnLogout);
  176.  
  177. // SqLite database handler
  178. db = new SQLiteHandler(getApplicationContext());
  179.  
  180. // session manager
  181. session = new SessionManager(getApplicationContext());
  182.  
  183. if (!session.isLoggedIn()) {
  184. logoutUser();
  185. }
  186.  
  187. // Fetching user details from sqlite
  188. HashMap<String, String> user = db.getUserDetails();
  189.  
  190. String name = user.get("name");
  191. String email = user.get("email");
  192.  
  193. // Displaying the user details on the screen
  194. txtName.setText(name);
  195. txtEmail.setText(email);
  196.  
  197. // Logout button click event
  198. btnLogout.setOnClickListener(new View.OnClickListener() {
  199.  
  200. @Override
  201. public void onClick(View v) {
  202. logoutUser();
  203. }
  204. });
  205. }
  206.  
  207. /**
  208. * Logging out the user. Will set isLoggedIn flag to false in shared
  209. * preferences Clears the user data from sqlite users table
  210. * */
  211. private void logoutUser() {
  212. session.setLogin(false);
  213.  
  214. db.deleteUsers();
  215.  
  216. // Launching the login activity
  217. Intent intent = new Intent(MainActivity.this, LoginActivity.class);
  218. startActivity(intent);
  219. finish();
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement