--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/08 19:02:20 237 +++ android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/10 17:01:02 254 @@ -1,15 +1,7 @@ package dk.thoerup.traininfo; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -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; @@ -18,6 +10,8 @@ import android.os.Bundle; import android.os.Handler; import android.util.Log; +import dk.thoerup.traininfo.provider.ProviderFactory; +import dk.thoerup.traininfo.provider.StationProvider; public class StationLocator implements LocationListener{ @@ -25,16 +19,18 @@ 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() { @@ -58,9 +54,11 @@ Criteria c = new Criteria(); c.setAccuracy(Criteria.ACCURACY_FINE); String bestProv = locManager.getBestProvider(c, true); - Log.e("BestProvider", bestProv); + Log.i("BestProvider", ""+bestProv); + if (bestProv != null) { + savedLocation = locManager.getLastKnownLocation(bestProv); locManager.requestLocationUpdates(bestProv, 0, 0, this); } else { // message that no suitable provider was found @@ -71,64 +69,26 @@ 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 (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix return; locManager.removeUpdates(this); hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); + } + + public void findNearestStations() { + findNearestStations(savedLocation); + } + + public void findNearestStations(Location location) { + provider.lookupStations(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 { - Log.e("Url", urlSource); - URL url = new URL(urlSource); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - - String line; - StringBuilder builder = new StringBuilder(); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()), 8192); - while((line = reader.readLine()) != null) { - builder.append(line); - } - - 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); - } - } @@ -151,4 +111,32 @@ // TODO Auto-generated method stub } + + + public static void injectMockLocation(Context cntx) { + 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); + + + 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", kbh); + } + + public static void removeMockLocation(Context cntx) { + LocationManager lm = (LocationManager) cntx.getSystemService(Context.LOCATION_SERVICE); + if (lm.getProvider("gps2") != null) + lm.removeTestProvider("gps2"); + } }