--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2009/10/03 10:55:43 391 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2010/09/08 08:45:18 1031 @@ -12,29 +12,37 @@ import android.location.Location; import android.util.Log; import dk.thoerup.traininfo.StationBean; +import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; import dk.thoerup.traininfo.util.XmlUtil; public class XmlStationProvider implements StationProvider { - - List stations = new ArrayList(); + final static int CACHE_TIMEOUT = 300*1000; + + //List stations = new ArrayList(); + AndroidTimeoutCache> stationCache = new AndroidTimeoutCache>(CACHE_TIMEOUT); - @Override - public List getStations() { - return stations; - } + double roundToPlaces(double value, int places) { + double pow = Math.pow(10, places); + double temp = Math.round( value*pow ); + + return temp / pow; + } @Override - public boolean lookupStations(Location location) { - String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); + public List lookupStations(Location location) { + double lat = roundToPlaces(location.getLatitude(), 4); + double lng = roundToPlaces(location.getLongitude(), 4); + + String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng; Log.i("url", url); return lookupStationsWorker(url); } @Override - public boolean lookupStations(String name) { + public List lookupStationsByName(String name) { // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); String url = ""; @@ -50,11 +58,37 @@ } - public boolean lookupStationsWorker(String url) { - boolean success = false; + @Override + public List lookupStationsByIds(String ids) { + String url = ""; + url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; + + Log.i("url", url); + return lookupStationsWorker(url); + } + + public List lookupStationsWorker(String url) { + + List tmpStations = stationCache.get(url); + + if (tmpStations != null) { + Log.i("lookupStations", "cache hit " + url); + } else { + tmpStations = fetchStations(url); + + if (tmpStations != null) { + stationCache.put(url, tmpStations); + } + } + + return tmpStations; + } + + + public List fetchStations(String url) { try { - stations.clear(); + List stations = new ArrayList(); String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); @@ -94,21 +128,30 @@ if (nodeName.equals("longitude")) station.setLongitude( Double.parseDouble(content) ); - if (nodeName.equals("stationcode")) - station.setCode(content); - if (nodeName.equals("calcdist")) station.setDistance( (int) Double.parseDouble(content) ); + + if (nodeName.equals("address")) + station.setAddress( content ); + + if (nodeName.equals("regional")) + station.setRegional( Boolean.parseBoolean(content) ); + + if (nodeName.equals("strain")) + station.setSTrain( Boolean.parseBoolean(content) ); + + if (nodeName.equals("metro")) + station.setMetro( Boolean.parseBoolean(content) ); } stations.add(station); } - success = true; + return stations; } catch (Exception e) { Log.e("XmlStationProvider", "lookupStations: ", e); + return null; } - return success; } }