--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2009/09/29 19:06:34 357 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java 2010/05/03 11:19:18 699 @@ -2,8 +2,6 @@ import java.io.StringReader; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import javax.xml.parsers.SAXParser; @@ -17,42 +15,36 @@ 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 { - final static long CACHE_TIMEOUT = 60*1000; + final static int CACHE_TIMEOUT = 60*1000; + - class CacheEntry { - public long timestamp; - public List departures; - } + AndroidTimeoutCache> departureCache = new AndroidTimeoutCache>(CACHE_TIMEOUT); - HashMap departureCache = new HashMap(); - ArrayList departures; + List departures; DepartureBean tempDeparture; StringBuilder builder = new StringBuilder(512); @Override - public boolean lookupDepartures(int stationID) { - CacheEntry entry = departureCache.get(stationID); + public boolean lookupDepartures(int stationID) { boolean success; + + departures = departureCache.get(stationID); - long now = android.os.SystemClock.elapsedRealtime(); - if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { - + if (departures == null) { success = lookupDeparturesWorker(stationID); - if (success) { - entry = new CacheEntry(); - entry.timestamp = android.os.SystemClock.elapsedRealtime(); - entry.departures = departures; - - departureCache.put(stationID, entry); + if (success) { + departureCache.put(stationID, departures); } + } else { Log.i("XmlDepartureProvider", "cache hit !!!"); success = true; @@ -67,7 +59,7 @@ try { - String url = XmlUtil.SERVICE_BASE + "/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"); @@ -90,14 +82,13 @@ @Override public List getDepartures(int station) { - CacheEntry entry = departureCache.get(station); + List list = departureCache.get(station); - if (entry != null) { - return entry.departures; - } else { - return new ArrayList(); - } + 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 @@ -138,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()); + } } }