package com.supinfo.jva.geocar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class Home extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String username = preferences.getString("username", "");
        Toast.makeText(this, username, Toast.LENGTH_SHORT).show();

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (isGPSEnabled){
            // Test if the GPS is enabled
            Toast.makeText(this, R.string.okGPS, Toast.LENGTH_SHORT).show(); // TODO : Supprimer la ligne Au moment de la livraison du projet (toast de test)
        }
        else{
            showGPSDisabledAlertToUser();
        }
    }

    private void showGPSDisabledAlertToUser(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage(R.string.errorGPS)
                .setCancelable(false)
                .setPositiveButton(R.string.enableGPS,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // If we want to active the GPS, we send the user to the activity
                                Intent callGPSSettingIntent = new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(callGPSSettingIntent);
                            }
                        });

        alertDialogBuilder.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int id){
                        dialog.cancel();
                    }
                });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.about:
                Intent intent = new Intent(this, About.class);
                startActivity(intent);
                break;
            case R.id.quit_app:
                finish();
                System.exit(0);
                break;
            default:
                Log.e("Other", "Other stuffs");
                break;
        }

        return super.onOptionsItemSelected(item);
    }

}

@@ -447,53 +447,221 @@
 t;%0A%0A
-public class Home extends ActionBarActivity %7B
+import com.supinfo.jva.external_class.APIRequest;%0A%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Apublic class Home extends ActionBarActivity %7B%0A%0A    private APIRequest requestStuff = new APIRequest();
 %0A%0A  
@@ -806,24 +806,57 @@
 out.home);%0A%0A
+        final Home that = this;%0A%0A
         Shar
@@ -1013,66 +1013,1119 @@
     
-Toast.makeText(this, username, Toast.LENGTH_SHORT).show();
+String password = preferences.getString(%22password%22, %22%22);%0A        if(username == %22%22 && password == %22%22) %7B%0A            Toast.makeText(this, R.string.errorSession, Toast.LENGTH_SHORT).show();%0A        %7D%0A        else %7B%0A            String response = requestStuff.requestAPI(that, %22getCarPosition%22, username, password);%0A            JSONObject json = null;%0A            Boolean success = false;%0A            try %7B%0A                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                try %7B%0A                    JSONObject position = (JSONObject) json.get(%22position%22);%0A                    Toast.makeText(this, position + %22%22, Toast.LENGTH_SHORT).show();%0A                %7D catch (JSONException e) %7B%0A                    Toast.makeText(that, R.string.error_car_position, Toast.LENGTH_SHORT).show();%0A                %7D%0A            %7D%0A            else %7B%0A                Toast.makeText(that, R.string.error_id, Toast.LENGTH_SHORT).show();%0A            %7D%0A        %7D
 %0A%0A  

Return the position of the car (ref #6)