package com.incidentlocator.client;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import android.content.Context;
import android.content.Intent;
import android.content.DialogInterface;

import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;

import android.provider.Settings;
import android.util.Log;
import java.text.DecimalFormat;


public class IncidentLocator extends Activity
{
    private static final String TAG = "IncidentLocator";

    protected LocationManager locationManager;
    protected final LocationListener locationListener = new GetLocationListener();

    private static Location location;

    private EditText coordinatesBox;

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

        coordinatesBox = (EditText) findViewById(R.id.show_message);

        locationManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    public void onStart() {
        super.onStart();

        // check if GPS is enabled
        boolean isGpsEnabled =
            locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (! isGpsEnabled) {
            promptOpenLocationSettings("GPS is disabled. Do you want to enable it?");
        }

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 0, 0, locationListener);
   }

    @Override
    public void onStop() {
        super.onStop();
        locationManager.removeUpdates(locationListener);
    }

    public class GetLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            location = loc;
            Log.d(TAG, "[GPS]" + location.toString());
        }

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }

    public void getLocation(View view) {
        DecimalFormat df = new DecimalFormat(".000000");
        df.setRoundingMode(java.math.RoundingMode.DOWN);
        StringBuilder sb = new StringBuilder(512);
        sb
            .append("Lat: ")
            .append(df.format(location.getLatitude()))
            .append("Lng: ")
            .append(df.format(location.getLongitude()))
            .append("\n");
        coordinatesBox.append(sb.toString());
    }

    private void promptOpenLocationSettings(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
            .setMessage(message)
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // intent to open settings
                    Intent settings = new Intent(
                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(settings);
                }
            });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

@@ -2256,24 +2256,176 @@
 iew view) %7B%0A
+        StringBuilder sb = new StringBuilder(512);%0A        if (location == null) %7B%0A            sb.append(%22quering for data...%5Cn%22);%0A        %7D else %7B%0A    
         Deci
@@ -2465,24 +2465,28 @@
 %22.000000%22);%0A
+    
         df.s
@@ -2542,62 +2542,21 @@
     
-StringBuilder sb = new StringBuilder(512);
+    sb
 %0A      
-  sb%0A
     
@@ -2553,34 +2553,32 @@
                 
-  
 .append(%22Lat: %22)
@@ -2574,24 +2574,28 @@
 nd(%22Lat: %22)%0A
+    
             
@@ -2641,32 +2641,36 @@
 )))%0A            
+    
 .append(%22Lng: %22)
@@ -2666,24 +2666,28 @@
 nd(%22Lng: %22)%0A
+    
             
@@ -2734,32 +2734,36 @@
 )))%0A            
+    
 .append(%22%5Cn%22);%0A 
@@ -2757,24 +2757,34 @@
 pend(%22%5Cn%22);%0A
+        %7D%0A
         coor

handle null location