--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/28 07:58:50 288 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2009/10/29 13:42:57 488 @@ -3,47 +3,49 @@ import java.util.List; import android.content.Context; -import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.util.Log; -import dk.thoerup.traininfo.provider.ProviderFactory; -import dk.thoerup.traininfo.provider.StationProvider; -public class StationLocator implements LocationListener{ + +public class LocationLookup implements LocationListener{ LocationManager locManager; Context cntx; Handler hndl; - StationProvider provider; + Location lastKnownLocation = null; Location savedLocation = null; + boolean isSearching = false; boolean hasGps; - public StationLocator(Context c, Handler h) { + public LocationLookup(Context c, Handler h) { cntx = c; hndl = h; - - provider = ProviderFactory.getStationProvider(); } - public List getStations() { - return provider.getStations(); + + public boolean hasLocation() { + return savedLocation != null; } - public void abortLocationListener() { - locManager.removeUpdates(this); + public Location getLocation() + { + return savedLocation; } - public boolean hasLocation() { - return savedLocation != null; + public Location getLastKnownLocation() { + return lastKnownLocation; } public void locateStations() { + + isSearching = true; + hasGps = false; locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); @@ -55,15 +57,21 @@ if (provider.equalsIgnoreCase("gps")) hasGps = true; locManager.requestLocationUpdates(provider, 0, 0, this); + + saveLastKnownLocation(locManager.getLastKnownLocation(provider)); } } else { // message that no suitable provider was found - hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); + hndl.sendEmptyMessage(StationList.NOPROVIDER); } } @Override public void onLocationChanged(Location location) { + if (isSearching == false) + return; + Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); + if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) savedLocation = new Location(location); //save a copy @@ -71,27 +79,31 @@ if (hasGps) { if (!location.getProvider().equals("gps")) { return; // at least give the gps a chance - } else if (location.getAccuracy() > 100) { + } else if (location.getAccuracy() > 256) { return; //if we have a gps provider lets wait for a more precise fix } } - - locManager.removeUpdates(this); - hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); + stopSearch(); + hndl.sendEmptyMessage(StationList.GOTLOCATION); } - public void findNearestStations() { - findNearestStations(savedLocation); + private void saveLastKnownLocation(Location loc) { + if (lastKnownLocation == null) { + lastKnownLocation = loc; + } else { + if (loc.getTime() > lastKnownLocation.getTime()) {//if loc is more recent than saved + lastKnownLocation = loc; + } + } } - public void findNearestStations(Location location) { - provider.lookupStations(location); - - if ( provider.getStations().size() > 0) - hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); - else - hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); + public void stopSearch() + { + if (isSearching) { + isSearching = false; + locManager.removeUpdates(this); + } } @@ -110,35 +122,4 @@ // TODO Auto-generated method stub } - - - public static void injectMockLocation(Context cntx) { - Location odder = new Location("gps2"); - odder.setLatitude(55.976632); - odder.setLongitude(10.16407); - - Location kbh = new Location("gps2"); //Christiansborg 55.675092,12.578573 - kbh.setLatitude(55.675092); - kbh.setLongitude(12.578573); - - Location bjbro = new Location("gps2"); - bjbro.setLatitude(56.380745); - bjbro.setLongitude(9.655609); - - Location hillerod = new Location("gps"); - hillerod.setLatitude(55.929177); - hillerod.setLongitude(12.308095); - - LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); - if (lm.getProvider("gps2") == null) - lm.addTestProvider("gps2", false, true, true, false, false, false, false, 0, Criteria.ACCURACY_FINE ); - lm.setTestProviderEnabled("gps2", true); - lm.setTestProviderLocation("gps2", hillerod); - } - - public static void removeMockLocation(Context cntx) { - LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); - if (lm.getProvider("gps2") != null) - lm.removeTestProvider("gps2"); - } }