Advertisement
Guest User

Untitled

a guest
Jul 20th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 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 enputEmail;
  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. enputEmail = (EditText) findViewById(R.id.numero_telefonico);
  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 = enputEmail.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 cognome = user.getString("cognome");
  107.  
  108. String email = user.getString("email");
  109. String email2 = user.getString("email2");
  110. String numero_appartamento = user.getString("numero_appartamento");
  111. String nome_edificio = user.getString("nome_edificio");
  112. String zona_metropolitana = user.getString("zona_metropolitana");
  113.  
  114.  
  115.  
  116. String created_at = user
  117. .getString("created_at");
  118.  
  119. // Inserting row in users table
  120. db.addUser(name,cognome, email,email2,numero_appartamento,nome_edificio,zona_metropolitana,uid, created_at);
  121.  
  122. // Launch main activity
  123. Intent intent = new Intent(LoginActivity.this,
  124. MainActivity.class);
  125. startActivity(intent);
  126. finish();
  127. } else {
  128. // Error in login. Get the error message
  129. String errorMsg = jObj.getString("error_msg");
  130. Toast.makeText(getApplicationContext(),
  131. errorMsg, Toast.LENGTH_LONG).show();
  132. }
  133. } catch (JSONException e) {
  134. // JSON error
  135. e.printStackTrace();
  136. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  137. }
  138.  
  139. }
  140. }, new Response.ErrorListener() {
  141.  
  142. @Override
  143. public void onErrorResponse(VolleyError error) {
  144. Log.e(TAG, "Login Error: " + error.getMessage());
  145. Toast.makeText(getApplicationContext(),
  146. error.getMessage(), Toast.LENGTH_LONG).show();
  147. hideDialog();
  148. }
  149. }) {
  150.  
  151. @Override
  152. protected Map<String, String> getParams() {
  153. // Posting parameters to login url
  154. Map<String, String> params = new HashMap<String, String>();
  155. params.put("email", email);
  156. params.put("password", password);
  157. System.out.println(params);
  158. return params;
  159. }
  160.  
  161. };
  162.  
  163. // Adding request to request queue
  164. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  165. }
  166.  
  167. private void showDialog() {
  168. if (!pDialog.isShowing())
  169. pDialog.show();
  170. }
  171.  
  172. private void hideDialog() {
  173. if (pDialog.isShowing())
  174. pDialog.dismiss();
  175. }
  176. }
  177.  
  178. public class MainActivity extends Activity {
  179.  
  180. private TextView txtName;
  181. private TextView txtEmail;
  182. private Button btnLogout;
  183.  
  184. private SQLiteHandler db;
  185. private SessionManager session;
  186.  
  187. @Override
  188. protected void onCreate(Bundle savedInstanceState) {
  189. super.onCreate(savedInstanceState);
  190. setContentView(R.layout.activity_main);
  191.  
  192. txtName = (TextView) findViewById(R.id.name);
  193. txtEmail = (TextView) findViewById(R.id.numero_telefonico);
  194. btnLogout = (Button) findViewById(R.id.btnLogout);
  195.  
  196. // SqLite database handler
  197. db = new SQLiteHandler(getApplicationContext());
  198.  
  199.  
  200. // session manager
  201. session = new SessionManager(getApplicationContext());
  202.  
  203. if (!session.isLoggedIn()) {
  204. logoutUser();
  205. }
  206.  
  207. // Fetching user details from SQLite
  208. HashMap<String, String> user = db.getUserDetails();
  209.  
  210. String name = user.get("name");
  211. //String email = user.get("email");
  212. String email = user.get("email");
  213.  
  214.  
  215. // Displaying the user details on the screen
  216. System.out.println(name+email);
  217.  
  218. txtName.setText(name);
  219. txtEmail.setText(email);
  220.  
  221. // Logout button click event
  222. btnLogout.setOnClickListener(new View.OnClickListener() {
  223.  
  224. @Override
  225. public void onClick(View v) {
  226. logoutUser();
  227. }
  228. });
  229. }
  230.  
  231. /**
  232. * Logging out the user. Will set isLoggedIn flag to false in shared
  233. * preferences Clears the user data from sqlite users table
  234. * */
  235. private void logoutUser() {
  236. session.setLogin(false);
  237.  
  238. db.deleteUsers();
  239.  
  240. // Launching the login activity
  241. Intent intent = new Intent(MainActivity.this, LoginActivity.class);
  242. startActivity(intent);
  243. finish();
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement