--- android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2009/09/24 08:19:23 341 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2010/09/28 14:58:45 1142 @@ -3,21 +3,24 @@ import java.util.List; import android.content.Context; -import android.location.Criteria; +import android.location.GpsSatellite; +import android.location.GpsStatus; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; +import android.os.Message; import android.util.Log; -public class LocationLookup implements LocationListener{ +public class LocationLookup implements LocationListener, GpsStatus.Listener { LocationManager locManager; Context cntx; Handler hndl; + Location lastKnownLocation = null; Location savedLocation = null; boolean isSearching = false; @@ -37,6 +40,10 @@ { return savedLocation; } + + public Location getLastKnownLocation() { + return lastKnownLocation; + } public void locateStations() { @@ -50,13 +57,22 @@ if (providers.size() > 0) { for(String provider : providers) { Log.i("Provider", ""+provider); - if (provider.equalsIgnoreCase("gps")) - hasGps = true; + if (provider.equalsIgnoreCase("gps")) { + locManager.addGpsStatusListener(this); + hasGps = true; + } + locManager.requestLocationUpdates(provider, 0, 0, this); + Location tmpLastKnown = locManager.getLastKnownLocation(provider); + if (tmpLastKnown != null) { + saveLastKnownLocation(tmpLastKnown); + } } } else { // message that no suitable provider was found - hndl.sendEmptyMessage(StationList.NOPROVIDER); + //hndl.sendEmptyMessage(StationList.NOPROVIDER); + hndl.sendEmptyMessage(StationList.LookupStates.NOPROVIDER.ordinal()); + } } @Override @@ -73,19 +89,30 @@ if (hasGps) { if (!location.getProvider().equals("gps")) { return; // at least give the gps a chance - } else if (location.getAccuracy() > 256) { + } else if (location.getAccuracy() > 512) { return; //if we have a gps provider lets wait for a more precise fix } } stopSearch(); - hndl.sendEmptyMessage(StationList.GOTLOCATION); + hndl.sendEmptyMessage(StationList.LookupStates.GOTLOCATION.ordinal()); + } + + 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 stopSearch() { if (isSearching) { isSearching = false; + locManager.removeGpsStatusListener(this); locManager.removeUpdates(this); } } @@ -106,41 +133,22 @@ // 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); - - Location aarhus = new Location("gps"); //Aros - aarhus.setLatitude(56.153828); - aarhus.setLongitude(10.200369); - - + + @Override //GpsStatus.Listener + public void onGpsStatusChanged(int event) { + if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) { + int count = 0; + GpsStatus status = locManager.getGpsStatus(null); + for (GpsSatellite sat : status.getSatellites()) { + count ++; + } + + Message msg = new Message(); + msg.what = StationList.LookupStates.GPS_SAT_COUNT.ordinal(); + msg.arg1 = count; + hndl.sendMessage(msg); + } - 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", kbh); - } - - public static void removeMockLocation(Context cntx) { - LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); - if (lm.getProvider("gps2") != null) - lm.removeTestProvider("gps2"); } }