--- android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2010/09/28 14:58:45 1142 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2010/09/28 15:30:13 1143 @@ -9,26 +9,34 @@ 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, 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 lastKnownLocation = null; + private Location savedLocation = null; + private int satCount; + + private boolean hasGps; - boolean isSearching = false; - boolean hasGps; + private LookupStates state; + + private long startTime; - public LocationLookup(Context c, Handler h) { + public LocationLookup(Context c) { + state = LookupStates.IDLE; cntx = c; - hndl = h; } @@ -41,16 +49,37 @@ return savedLocation; } + public boolean hasGps() { + return hasGps; + } + + 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() { - isSearching = true; + state = LookupStates.STARTED; hasGps = false; locManager = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); + satCount = 0; + + startTime = android.os.SystemClock.elapsedRealtime(); List providers = locManager.getProviders(true); @@ -71,15 +100,12 @@ } else { // 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,7 +121,8 @@ } stopSearch(); - hndl.sendEmptyMessage(StationList.LookupStates.GOTLOCATION.ordinal()); + state = LookupStates.GOTLOCATION; + } private void saveLastKnownLocation(Location loc) { @@ -110,8 +137,8 @@ public void stopSearch() { - if (isSearching) { - isSearching = false; + if (state == LookupStates.STARTED) { + state = LookupStates.IDLE; locManager.removeGpsStatusListener(this); locManager.removeUpdates(this); } @@ -142,12 +169,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; } }