--- android/TrainInfo/src/dk/thoerup/traininfo/StationLocator.java 2009/08/08 19:02:20 237 +++ android/TrainInfo/src/dk/thoerup/traininfo/LocationLookup.java 2009/10/27 13:40:15 474 @@ -1,17 +1,8 @@ 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; import android.location.LocationListener; import android.location.LocationManager; @@ -19,130 +10,93 @@ import android.os.Handler; import android.util.Log; -public class StationLocator implements LocationListener{ + +public class LocationLookup implements LocationListener{ LocationManager locManager; Context cntx; Handler hndl; - ArrayList stationList = new ArrayList(); - List safeStationList = java.util.Collections.unmodifiableList(stationList); + Location savedLocation = null; + + boolean isSearching = false; + boolean hasGps; - public StationLocator(Context c, Handler h) { + public LocationLookup(Context c, Handler h) { cntx = c; hndl = h; } - public List getStations() { - return safeStationList; + + public boolean hasLocation() { + return savedLocation != null; } - public void abortLocationListener() { - locManager.removeUpdates(this); + public Location getLocation() + { + return savedLocation; } 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 - + + isSearching = true; + + 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) { - locManager.requestLocationUpdates(bestProv, 0, 0, this); + List providers = locManager.getProviders(true); + + if (providers.size() > 0) { + for(String provider : providers) { + Log.i("Provider", ""+provider); + if (provider.equalsIgnoreCase("gps")) + hasGps = true; + locManager.requestLocationUpdates(provider, 0, 0, this); + } } else { // message that no suitable provider was found - hndl.sendEmptyMessage(TrainInfoList.NOPROVIDER); + hndl.sendEmptyMessage(StationList.NOPROVIDER); } } @Override public void onLocationChanged(Location location) { - Log.i("Location", "Got location fix " + location.getLatitude() + ", " + location.getLongitude() + " accuracy=" + location.getAccuracy() + " provider=" +location.getProvider()); - - if (location.getProvider().equals("gps") && location.getAccuracy() > 100) //if we have a gps provider lets wait for a more precise fix + if (isSearching == false) return; - - locManager.removeUpdates(this); - hndl.sendEmptyMessage(TrainInfoList.GOTLOCATION); - //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 256) { + return; //if we have a gps provider lets wait for a more precise fix } - hndl.sendEmptyMessage(TrainInfoList.GOTSTATIONLIST); - } catch (Exception e) { - Log.e("Location","Location",e); - hndl.sendEmptyMessage(TrainInfoList.LOOKUPSTATIONFAILED); } - + stopSearch(); + hndl.sendEmptyMessage(StationList.GOTLOCATION); + } + + public void stopSearch() + { + if (isSearching) { + isSearching = false; + locManager.removeUpdates(this); + } } @Override public void onProviderDisabled(String provider) { - // TODO Auto-generated method stub - } @Override public void onProviderEnabled(String provider) { - // TODO Auto-generated method stub - }