--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2009/09/10 19:09:09 310 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2010/05/03 11:19:18 699 @@ -15,23 +15,51 @@ import android.util.Log; import dk.thoerup.traininfo.DepartureBean; +import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; +import dk.thoerup.traininfo.util.XmlUtil; public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { - ArrayList departures = new ArrayList(); + final static int CACHE_TIMEOUT = 60*1000; + + + AndroidTimeoutCache> departureCache = new AndroidTimeoutCache>(CACHE_TIMEOUT); + List departures; + DepartureBean tempDeparture; StringBuilder builder = new StringBuilder(512); @Override - public void lookupDepartures(int stationID) { - departures.clear(); + public boolean lookupDepartures(int stationID) { + boolean success; + + departures = departureCache.get(stationID); + + if (departures == null) { + success = lookupDeparturesWorker(stationID); + + if (success) { + departureCache.put(stationID, departures); + } + + } else { + Log.i("XmlDepartureProvider", "cache hit !!!"); + success = true; + } + + return success; + } + + private boolean lookupDeparturesWorker(int stationID) { + boolean success = false; + departures = new ArrayList(); try { - //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode; - String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID; + + String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID; Log.i("xmlurl",url); String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); @@ -43,16 +71,24 @@ xr.setContentHandler(this); xr.setErrorHandler(this); - xr.setDTDHandler(this); xr.parse(source); + success = true; + } catch (Exception e) { Log.e("XmlDepartureProvider", "looupFunction", e); } + return success; } @Override - public List getDepartures() { - return departures; + public List getDepartures(int station) { + List list = departureCache.get(station); + + if (list == null) { + list = new ArrayList(); + } + + return list; } // this can be called several times fore the same text-node if there are many chardata / lines @@ -93,6 +129,8 @@ tempDeparture.setStatus(builder.toString().trim()); } else if (name.equals("note")) { tempDeparture.setNote(builder.toString().trim()); - } + } else if (name.equals("type")) { + tempDeparture.setType(builder.toString().trim()); + } } }