--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/09 11:48:36 242 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/28 06:34:42 285 @@ -1,21 +1,17 @@ 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.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.util.DownloadUtil; +import dk.thoerup.traininfo.provider.ProviderFactory; +import dk.thoerup.traininfo.provider.StationProvider; public class StationLocator implements LocationListener{ @@ -23,24 +19,27 @@ Context cntx; Handler hndl; - ArrayList stationList = new ArrayList(); - List safeStationList = java.util.Collections.unmodifiableList(stationList); - - + StationProvider provider; Location savedLocation = null; public StationLocator(Context c, Handler h) { cntx = c; hndl = h; + + provider = ProviderFactory.getStationProvider(); } public List getStations() { - return safeStationList; + return provider.getStations(); } public void abortLocationListener() { locManager.removeUpdates(this); } + + public boolean hasLocation() { + return savedLocation != null; + } 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 @@ -63,7 +62,6 @@ if (bestProv != null) { - savedLocation = locManager.getLastKnownLocation(bestProv); locManager.requestLocationUpdates(bestProv, 0, 0, this); } else { // message that no suitable provider was found @@ -88,49 +86,12 @@ } 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 0) hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); - - } catch (Exception e) { - Log.e("Location","Location",e); + else hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); - } - } @@ -154,14 +115,34 @@ } + public static void injectMockLocation(Context cntx) { - Location loc = new Location("gps2"); - loc.setLatitude(56.378084); - loc.setLongitude(9.659815); + 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); + + Location hillerod = new Location("gps"); + hillerod.setLatitude(55.929177); + hillerod.setLongitude(12.308095); 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", loc); - + lm.setTestProviderLocation("gps2", hillerod); + } + + public static void removeMockLocation(Context cntx) { + LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); + if (lm.getProvider("gps2") != null) + lm.removeTestProvider("gps2"); } }