--- android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2009/10/29 13:42:57 488 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2011/05/04 20:25:15 1446 @@ -1,31 +1,42 @@ package dk.thoerup.traininfo; -import java.util.List; import android.content.Context; +import android.content.SharedPreferences; +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.preference.PreferenceManager; import android.util.Log; -public class LocationLookup implements LocationListener{ +public class LocationLookup implements LocationListener, GpsStatus.Listener { + + public enum LookupStates { + GOTLOCATION, + NOPROVIDER, + STARTED, + IDLE + } - LocationManager locManager; - Context cntx; - Handler hndl; + private LocationManager locManager; + private Context cntx; - Location lastKnownLocation = null; - Location savedLocation = null; + private Location savedLocation = null; + private int satCount; + + private boolean hasGps; + + private LookupStates state; - boolean isSearching = false; - boolean hasGps; + private long startTime; - public LocationLookup(Context c, Handler h) { + public LocationLookup(Context c) { + state = LookupStates.IDLE; cntx = c; - hndl = h; } @@ -38,38 +49,65 @@ return savedLocation; } - public Location getLastKnownLocation() { - return lastKnownLocation; + public boolean hasGps() { + return hasGps; + } + + public int getSatCount() { + return satCount; + } + + public LookupStates getState() { + return state; + } + + + public long elapsedTime() { + long now = android.os.SystemClock.elapsedRealtime(); + + return now - startTime; } public void locateStations() { - isSearching = true; + state = LookupStates.STARTED; hasGps = false; locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); - - List providers = locManager.getProviders(true); + satCount = 0; + + startTime = android.os.SystemClock.elapsedRealtime(); + + boolean hasProvider = false; - if (providers.size() > 0) { - for(String provider : providers) { - Log.i("Provider", ""+provider); - if (provider.equalsIgnoreCase("gps")) - hasGps = true; - locManager.requestLocationUpdates(provider, 0, 0, this); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(cntx); + String networkPref = prefs.getString("location", "GPS"); //default value is gps + + if (networkPref.equals("GPS")) { + if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + locManager.addGpsStatusListener(this); + locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); - saveLastKnownLocation(locManager.getLastKnownLocation(provider)); + hasGps = true; + hasProvider = true; } - } else { + } + + if (locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { + locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); + hasProvider = true; + } + + + if (hasProvider == false) { // message that no suitable provider was found - hndl.sendEmptyMessage(StationList.NOPROVIDER); + //hndl.sendEmptyMessage(StationList.NOPROVIDER); + state = LookupStates.NOPROVIDER; + } } @Override - public void onLocationChanged(Location location) { - if (isSearching == false) - return; - + public void onLocationChanged(Location location) { Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); @@ -79,29 +117,22 @@ 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); + state = LookupStates.GOTLOCATION; + } - 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; + if (state == LookupStates.STARTED) { + state = LookupStates.IDLE; + locManager.removeGpsStatusListener(this); locManager.removeUpdates(this); } } @@ -122,4 +153,19 @@ // TODO Auto-generated method stub } + + + @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; + } + + } }