--- android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2010/09/28 14:58:45 1142 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2011/05/04 20:25:15 1446 @@ -1,34 +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.os.Message; +import android.preference.PreferenceManager; import android.util.Log; 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; } @@ -41,45 +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(); - 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); - } + boolean hasProvider = false; + + 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); + + 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.LookupStates.NOPROVIDER.ordinal()); + 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()); @@ -95,23 +123,15 @@ } stopSearch(); - hndl.sendEmptyMessage(StationList.LookupStates.GOTLOCATION.ordinal()); + 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); } @@ -142,12 +162,9 @@ 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); + satCount = count; } }