Commit c347b0fb authored by Andrey Filippov's avatar Andrey Filippov
Browse files

Requesting GPS if it was not running already

parent f689fb71
Loading
Loading
Loading
Loading
+51 −3
Original line number Diff line number Diff line
@@ -33,10 +33,15 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class SensorsUpdate implements SensorEventListener{
	private double longitude = Double.NaN;
	private double latitude = Double.NaN;;

    private static String  TAG = "SENSORS";

	private final Activity activity;
@@ -102,6 +107,16 @@ public class SensorsUpdate implements SensorEventListener{

	public void pause() {
		sensorManager.unregisterListener(this);

    	LocationManager lm = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
    	boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    	if (isGPSEnabled) {
    		lm.removeUpdates(locationListener);
            Log.d(TAG, "Removed GPS updates");

    	}


	}

    private void updateOrientationAngles() {
@@ -124,20 +139,53 @@ public class SensorsUpdate implements SensorEventListener{
    }

    public double [] getLocation() {
    	double longitude = Double.NaN;
    	double latitude = Double.NaN;;
//    	double longitude = Double.NaN;
//    	double latitude = Double.NaN;;

    	try {
	    	LocationManager lm = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
	    	boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
	    	Log.i(TAG,"Got LocationManager: "+lm.toString()+", isGPSEnabled = "+isGPSEnabled);
	    	Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	    	if (isGPSEnabled && (location == null)) {
                Log.d(TAG, "Requested GPS Updates");
	    		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
	    	}
	    	Log.i(TAG,"Got Location: "+location);
	    	longitude = location.getLongitude();
	    	latitude = location.getLatitude();
    	} catch (Exception e) {
    		Log.e(TAG,"Problem getting longitude/latitude");
    		Log.e(TAG,"Problem getting longitude/latitude: "+e.toString());
    	}
    	double [] thisLocation = {longitude, latitude};
    	return thisLocation;

    }
    // Should be unregistered somehow on pause?
    private final LocationListener locationListener = new LocationListener() {
        @Override
		public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}
    };

}