package com.rhomobile.rhodes.geolocation;

import com.rhomobile.rhodes.Logger;
import com.rhomobile.rhodes.RhodesInstance;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;

public class GeoLocationImpl implements LocationListener {

	private static final String TAG = "GeoLocationImpl";
	private LocationManager locationManager;
	private double longitude = 0;
	private double latitude = 0;
	private boolean determined = false;

	public GeoLocationImpl() {

		Looper.prepare();

		setCurrentGpsLocation(null);
	}

	private void setCurrentGpsLocation(Location location) {
		Logger.T(TAG, "GeoLocationImpl.setCurrentGpsLocation");
		try {
			if (location == null) {
				locationManager = (LocationManager) RhodesInstance
						.getInstance().getSystemService(
								Context.LOCATION_SERVICE);
				locationManager.requestLocationUpdates(
						LocationManager.GPS_PROVIDER, 0, 0, this);
				location = locationManager
						.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			}
			if (location != null) {
				longitude = location.getLongitude();
				latitude = location.getLatitude();
				determined = true;
			} else {
				determined = false;
			}

			Logger.T(TAG, "gps enabled: " + new Boolean(locationManager.isProviderEnabled(
					LocationManager.GPS_PROVIDER)).toString());
			Logger.T(TAG, "determined: " + new Boolean(determined).toString());
			if (determined) {
				Logger.T(TAG, "longitude: " + new Double(longitude).toString());
				Logger.T(TAG, "latitude: " + new Double(latitude).toString());
			}
		} catch (Exception e) {
			determined = false;
			Logger.E(TAG, e.getMessage());
		}
	}

	public void onLocationChanged(Location location) {
		Logger.T(TAG, "onLocationChanged");
		setCurrentGpsLocation(location);
	}

	public void onProviderDisabled(String provider) {
		Logger.T(TAG, "onProviderDisabled");
		setCurrentGpsLocation(null);

	}

	public void onProviderEnabled(String provider) {
		Logger.T(TAG, "onProviderEnabled");
		setCurrentGpsLocation(null);
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		Logger.T(TAG, "onStatusChanged");
		setCurrentGpsLocation(null);
	}

	public synchronized double GetLatitude() {
		return latitude;
	}

	public synchronized double GetLongitude() {
		return longitude;
	}

	public synchronized boolean isKnownPosition() {
		return determined;
	}
}

@@ -433,16 +433,86 @@
 nImpl%22;%0A
+%09private static final String PROVIDER = LocationManager.GPS_PROVIDER;%0A
 %09private
@@ -545,16 +545,16 @@
 anager;%0A
-
 %09private
@@ -643,17 +643,18 @@
  false;%0A
+%09
 %0A
-
 %09public 
@@ -677,30 +677,8 @@
 ) %7B%0A
-%0A%09%09Looper.prepare();%0A%0A
 %09%09se
@@ -912,23 +912,16 @@
 Instance
-%0A%09%09%09%09%09%09
 .getInst
@@ -926,16 +926,22 @@
 stance()
+%0A%09%09%09%09%09
 .getSyst
@@ -954,17 +954,8 @@
 ice(
-%0A%09%09%09%09%09%09%09%09
 Cont
@@ -1024,55 +1024,52 @@
 tes(
-%0A%09%09%09%09%09%09LocationManager.GPS_PROVIDER, 0, 0, this
+PROVIDER, 0, 0, this, Looper.getMainLooper()
 );%0A%09
@@ -1101,15 +1101,8 @@
 ager
-%0A%09%09%09%09%09%09
 .get
@@ -1119,36 +1119,16 @@
 ocation(
-LocationManager.GPS_
 PROVIDER
@@ -1307,16 +1307,16 @@
 ;%0A%09%09%09%7D%0A%0A
+
 %09%09%09Logge
@@ -1392,34 +1392,8 @@
 led(
-%0A%09%09%09%09%09LocationManager.GPS_
 PROV

Fix GPS location updating for Android