--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java 2009/09/30 09:14:27 365 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlTimetableProvider.java 2010/06/11 18:46:46 840 @@ -4,31 +4,67 @@ import java.util.List; import org.w3c.dom.Document; -import org.w3c.dom.NamedNodeMap; + import org.w3c.dom.Node; import org.w3c.dom.NodeList; import android.util.Log; import dk.thoerup.traininfo.TimetableBean; +import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; import dk.thoerup.traininfo.util.XmlUtil; public class XmlTimetableProvider implements TimetableProvider { - List timetables = new ArrayList(); + final static int CACHE_TIMEOUT = 60*1000; + + List timetables; + AndroidTimeoutCache> departureCache = new AndroidTimeoutCache>(CACHE_TIMEOUT); + @Override - public List getTimetable() { - return timetables; + public List getTimetable(String type, String trainID) { + String key = type + "-" + trainID; + List list = departureCache.get(key); + + if (list == null) { + list = new ArrayList(); + } + + return list; } @Override - public boolean lookupTimetable(String trainID) { + public boolean lookupTimetable(String type, String trainID) { + boolean success; + + String trainNumber = extractTrainNumber(trainID); + + String key = type + "-" + trainID; + timetables = departureCache.get(key); + + if (timetables == null) { + success = lookupTimetableWorker(type, trainNumber); + + if (success) { + departureCache.put(key, timetables); + } + + } else { + Log.i("XmlTimetableProvider", "cache hit !!!"); + success = true; + } + + return success; + } + + + public boolean lookupTimetableWorker(String type, String trainNumber) { boolean success = false; - String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainID.replace(" ", "%20") ; + String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type; Log.i("url", url); try { - timetables.clear(); + timetables = new ArrayList(); String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); @@ -72,6 +108,9 @@ if (nodeName.equals("departure")) timetable.setDeparture( content ); + if (nodeName.equals("stationid")) + timetable.setStationId( Integer.parseInt(content)); + /*if (nodeName.equals("current")) timetable.setCurrent( Boolean.parseBoolean(content) );*/ @@ -84,8 +123,19 @@ } catch (Exception e) { Log.e("XmlStationProvider", "lookupStations: ", e); } + return success; } + + private String extractTrainNumber (String trainID) { + + String parts[] = trainID.split(" "); + if (parts.length == 2) { + return parts[1]; + } else { + return parts[0]; + } + } }