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) {
            // create a new dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder
                .setMessage("GPS is disabled. Do you want to enable it?")
                .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();
        }

        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());
    }
}

@@ -1392,735 +1392,79 @@
     
-// create a new dialog%0A            AlertDialog.Builder builder = new AlertDialog.Builder(this);%0A            builder%0A                .setMessage(%22GPS is disabled. Do you want to enable it?%22)%0A                .setCancelable(false)%0A                .setPositiveButton(%22Yes%22, new DialogInterface.OnClickListener() %7B%0A                    public void onClick(DialogInterface dialog, int which) %7B%0A                        // intent to open settings%0A                        Intent settings = new Intent(%0A                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);%0A                        startActivity(settings);%0A                    %7D%0A                %7D);%0A            AlertDialog alert = builder.create();%0A            alert.show(
+promptOpenLocationSettings(%22GPS is disabled. Do you want to enable it?%22
 );%0A 
@@ -2683,11 +2683,694 @@
 );%0A    %7D
+%0A%0A    private void promptOpenLocationSettings(String message) %7B%0A        AlertDialog.Builder builder = new AlertDialog.Builder(this);%0A        builder%0A            .setMessage(message)%0A            .setCancelable(false)%0A            .setPositiveButton(%22Yes%22, new DialogInterface.OnClickListener() %7B%0A                public void onClick(DialogInterface dialog, int which) %7B%0A                    // intent to open settings%0A                    Intent settings = new Intent(%0A                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);%0A                    startActivity(settings);%0A                %7D%0A            %7D);%0A        AlertDialog alert = builder.create();%0A        alert.show();%0A    %7D
 %0A%7D%0A

move prompt to open settings in a function