--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2010/01/26 20:28:13 551 +++ 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 lookupStationsByName(String name) { + public List lookupStationsByName(String name) { // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); String url = ""; @@ -51,20 +59,36 @@ @Override - public boolean lookupStationsByIds(String ids) { + 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 boolean lookupStationsWorker(String url) { - boolean success = false; + public List fetchStations(String url) { try { - stations.clear(); + List stations = new ArrayList(); String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); @@ -104,9 +128,6 @@ 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) ); @@ -125,12 +146,12 @@ } stations.add(station); } - success = true; + return stations; } catch (Exception e) { Log.e("XmlStationProvider", "lookupStations: ", e); + return null; } - return success; } }