tborg

LoginActivity.java

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