--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/10 16:58:22 253 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2010/09/28 15:30:13 1143 @@ -1,116 +1,157 @@ package dk.thoerup.traininfo; -import java.util.ArrayList; import java.util.List; -import org.json.JSONArray; -import org.json.JSONObject; - 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.location.LocationProvider; import android.os.Bundle; -import android.os.Handler; import android.util.Log; -import dk.thoerup.traininfo.provider.ProviderFactory; -import dk.thoerup.traininfo.provider.StationProvider; -import dk.thoerup.traininfo.util.DownloadUtil; -public class StationLocator implements LocationListener{ - LocationManager locManager; - Context cntx; - Handler hndl; +public class LocationLookup implements LocationListener, GpsStatus.Listener { + + public enum LookupStates { + GOTLOCATION, + NOPROVIDER, + STARTED, + IDLE + } - List stationList = new ArrayList(); + private LocationManager locManager; + private Context cntx; + private Location lastKnownLocation = null; + private Location savedLocation = null; + private int satCount; + + private boolean hasGps; + + private LookupStates state; - Location savedLocation = null; + private long startTime; - public StationLocator(Context c, Handler h) { + public LocationLookup(Context c) { + state = LookupStates.IDLE; cntx = c; - hndl = h; } - public List getStations() { - return stationList; + + public boolean hasLocation() { + return savedLocation != null; + } + + public Location getLocation() + { + return savedLocation; + } + + public boolean hasGps() { + return hasGps; } - public void abortLocationListener() { - locManager.removeUpdates(this); + public int getSatCount() { + return satCount; + } + + public LookupStates getState() { + return state; + } + + public Location getLastKnownLocation() { + return lastKnownLocation; + } + + public long elapsedTime() { + long now = android.os.SystemClock.elapsedRealtime(); + + return now - startTime; } public void locateStations() { - //http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&lstkp=0&rsz=small&hl=en&source=gsc&gss=.com&sig=fadf0e8d483d0f70bea11d5905010a16&q=Train%20station&near=56.377424%2C9.656695&key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q&v=1.0&nocache=1249640467498 - + + state = LookupStates.STARTED; + + hasGps = false; locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); - /*//testcode - List provs = locManager.getAllProviders(); - for (String p : provs) { - Log.e("Provider", p); - } - provs = locManager.getProviders(true); - for (String p : provs) { - Log.e("ActiveProvider", p); - } - */ - Criteria c = new Criteria(); - c.setAccuracy(Criteria.ACCURACY_FINE); - String bestProv = locManager.getBestProvider(c, true); - Log.i("BestProvider", ""+bestProv); + satCount = 0; + startTime = android.os.SystemClock.elapsedRealtime(); - if (bestProv != null) { - savedLocation = locManager.getLastKnownLocation(bestProv); - locManager.requestLocationUpdates(bestProv, 0, 0, this); + List providers = locManager.getProviders(true); + + if (providers.size() > 0) { + for(String provider : providers) { + Log.i("Provider", ""+provider); + 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(TrainInfoList.NOPROVIDER); + //hndl.sendEmptyMessage(StationList.NOPROVIDER); + state = LookupStates.NOPROVIDER; + } } @Override - public void onLocationChanged(Location location) { + public void onLocationChanged(Location location) { Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); + - savedLocation = new Location(location); //save a copy + if (savedLocation == null || location.getAccuracy() < savedLocation.getAccuracy()) + savedLocation = new Location(location); //save a copy - if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix - return; + if (hasGps) { + if (!location.getProvider().equals("gps")) { + return; // at least give the gps a chance + } else if (location.getAccuracy() > 512) { + return; //if we have a gps provider lets wait for a more precise fix + } + + } + stopSearch(); + state = LookupStates.GOTLOCATION; - locManager.removeUpdates(this); - hndl.sendEmptyMessage(TrainInfoList.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) { - StationProvider prov = ProviderFactory.getStationProvider(); - prov.lookupStations(location); - this.stationList = prov.getStations(); - - if (stationList.size() > 0) - hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); - else - hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); + public void stopSearch() + { + if (state == LookupStates.STARTED) { + state = LookupStates.IDLE; + locManager.removeGpsStatusListener(this); + locManager.removeUpdates(this); + } } @Override public void onProviderDisabled(String provider) { - // TODO Auto-generated method stub - } @Override public void onProviderEnabled(String provider) { - // TODO Auto-generated method stub - } @@ -119,32 +160,19 @@ // 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); - - - 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"); + + @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 ++; + } + + satCount = count; + } + } }