--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/09 11:21:30 241 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2011/05/04 20:25:15 1446 @@ -1,150 +1,150 @@ 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.content.SharedPreferences; +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.preference.PreferenceManager; import android.util.Log; -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 + } - ArrayList stationList = new ArrayList(); - List safeStationList = java.util.Collections.unmodifiableList(stationList); + private LocationManager locManager; + private Context cntx; + private Location savedLocation = null; + private int satCount; + + private boolean hasGps; - Location savedLocation = null; + private LookupStates state; + + private long startTime; - public StationLocator(Context c, Handler h) { + public LocationLookup(Context c) { + state = LookupStates.IDLE; cntx = c; - hndl = h; } - public List getStations() { - return safeStationList; + + public boolean hasLocation() { + return savedLocation != null; + } + + public Location getLocation() + { + return savedLocation; + } + + public boolean hasGps() { + return hasGps; + } + + public int getSatCount() { + return satCount; + } + + public LookupStates getState() { + return state; } - public void abortLocationListener() { - locManager.removeUpdates(this); + + 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.e("BestProvider", bestProv); - - - if (bestProv != null) { - savedLocation = locManager.getLastKnownLocation(bestProv); - locManager.requestLocationUpdates(bestProv, 0, 0, this); - } else { + satCount = 0; + + startTime = android.os.SystemClock.elapsedRealtime(); + + 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; + } + } + + 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(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); - } - public void findNearestStations(Location location) { - //ToDo: to be nice put the api key into the request - String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=" + location.getLatitude() + "%2C" + location.getLongitude() + "&v=1.0"; - //String urlSource = "http://www.google.com/uds/GlocalSearch?callback=google.search.LocalSearch.RawCompletion&context=1&q=Train%20station&near=56.2%2C9.0&v=1.0"; - - try { - String data = DownloadUtil.getContent(urlSource, 30000, "UTF-8"); - StringBuilder builder = new StringBuilder(data); - - while (builder.charAt(0) != '{') - builder.deleteCharAt(0); - while (builder.charAt(builder.length()-1) != '}') - builder.deleteCharAt(builder.length()-1); - - JSONObject json = new JSONObject(builder.toString()); - // now have some fun with the results... - JSONArray res = json.getJSONArray("results"); - - Location tmpLocation = new Location("gps"); - stationList.clear(); - - for (int i=0; i