package com.supinfo.jva.geocar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Login extends ActionBarActivity {

    private EditText usernameField = null;
    private EditText passwordField = null;
    private Button sendDataButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        usernameField = (EditText) findViewById(R.id.username);
        passwordField = (EditText) findViewById(R.id.password);
        sendDataButton = (Button) findViewById(R.id.sendButton);
        final Login that = this;

        sendDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameField.getText().toString();
                String password = passwordField.getText().toString();
                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(that, R.string.error_toast, Toast.LENGTH_SHORT).show();
                }
                else {
                    if (connectUser(username, password)) {
                        Intent home = new Intent(Login.this, Home.class);

                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(that);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString("username", username);
                        editor.putString("password", password);
                        editor.commit();

                        startActivity(home);
                    }
                    else {
                        Toast.makeText(that, R.string.error_id, Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public Boolean connectUser(String username, String password) {
        HttpClient httpclient = new DefaultHttpClient(); // Create a new HttpClient
        HttpPost httppost = new HttpPost("http://91.121.105.200/SUPTracking/"); // Create a post header

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("action", "login"));
            nameValuePairs.add(new BasicNameValuePair("login", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            String json_s = EntityUtils.toString(response.getEntity());
            JSONObject json = new JSONObject(json_s);
            return (Boolean) json.get("success");

        } catch (ClientProtocolException e) {
            Toast.makeText(this, R.string.error_connection, Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(this, R.string.error_connection, Toast.LENGTH_SHORT).show();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }
}

@@ -99,16 +99,42 @@
 rences;%0A
+import android.os.Bundle;%0A
 import a
@@ -155,16 +155,16 @@
 ctMode;%0A
-
 import a
@@ -253,59 +253,8 @@
 ty;%0A
-import android.os.Bundle;%0Aimport android.util.Log;%0A
 impo
@@ -848,24 +848,75 @@
 SONObject;%0A%0A
+import com.supinfo.jva.external_class.APIRequest;%0A%0A
 import java.
@@ -1161,16 +1161,73 @@
  null;%0A%0A
+    private APIRequest requestStuff = new APIRequest();%0A%0A
     @Ove
@@ -2096,43 +2096,437 @@
     
-if (connectUser(username, password)
+String response = requestStuff.requestAPI(that, %22login%22, username, password);%0A                    Boolean success = false;%0A                    try %7B%0A                        JSONObject json = new JSONObject(response);%0A                        success = (Boolean) json.get(%22success%22);%0A                    %7D catch (JSONException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A%0A                    if (success
 ) %7B%0A
@@ -3189,32 +3189,32 @@
 %7D%0A            %7D%0A
+
         %7D);%0A    
@@ -3218,1455 +3218,7 @@
    %7D
-%0A%0A    public Boolean connectUser(String username, String password) %7B%0A        HttpClient httpclient = new DefaultHttpClient(); // Create a new HttpClient%0A        HttpPost httppost = new HttpPost(%22http://91.121.105.200/SUPTracking/%22); // Create a post header%0A%0A        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();%0A        StrictMode.setThreadPolicy(policy);%0A%0A        try %7B%0A            List%3CNameValuePair%3E nameValuePairs = new ArrayList%3CNameValuePair%3E(2);%0A            nameValuePairs.add(new BasicNameValuePair(%22action%22, %22login%22));%0A            nameValuePairs.add(new BasicNameValuePair(%22login%22, username));%0A            nameValuePairs.add(new BasicNameValuePair(%22password%22, password));%0A            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));%0A%0A            // Execute HTTP Post Request%0A            HttpResponse response = httpclient.execute(httppost);%0A            String json_s = EntityUtils.toString(response.getEntity());%0A            JSONObject json = new JSONObject(json_s);%0A            return (Boolean) json.get(%22success%22);%0A%0A        %7D catch (ClientProtocolException e) %7B%0A            Toast.makeText(this, R.string.error_connection, Toast.LENGTH_SHORT).show();%0A        %7D catch (IOException e) %7B%0A            Toast.makeText(this, R.string.error_connection, Toast.LENGTH_SHORT).show();%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return false;%0A    %7D
 %0A%7D%0A

Update the login method