--- android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2009/09/10 19:09:09 310 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlStationProvider.java 2010/09/16 15:32:42 1066 @@ -1,102 +1,110 @@ package dk.thoerup.traininfo.provider; -import java.io.ByteArrayInputStream; -import java.io.IOException; + +import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; import android.location.Location; import android.util.Log; -import dk.thoerup.traininfo.StationBean; +import dk.thoerup.android.traininfo.common.StationBean; +import dk.thoerup.android.traininfo.common.TimetableBean; +import dk.thoerup.traininfo.util.AndroidTimeoutCache; import dk.thoerup.traininfo.util.DownloadUtil; +import dk.thoerup.traininfo.util.XmlUtil; public class XmlStationProvider implements StationProvider { + + final static int CACHE_TIMEOUT = 300*1000; - List stations = new ArrayList(); + //List stations = new ArrayList(); + AndroidTimeoutCache stationCache = new AndroidTimeoutCache(CACHE_TIMEOUT); + + double roundToPlaces(double value, int places) { + double pow = Math.pow(10, places); + double temp = Math.round( value*pow ); + + return temp / pow; + } + + @Override + public StationBean 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 List getStations() { - return stations; + public StationBean lookupStationsByName(String name) { + + // String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); + String url = ""; + + try { + url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1"); + } catch (Exception e) { + Log.e("lookupStations", "Encoding failed", e); + } + + Log.i("url", url); + return lookupStationsWorker(url); } - + + @Override - public void lookupStations(Location location) { + public StationBean lookupStationsByIds(String ids) { + String url = ""; + url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; - //String url = "http://pumba.t-hoerup.dk:8080/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); - String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); Log.i("url", url); + return lookupStationsWorker(url); + } + + public StationBean lookupStationsWorker(String url) { + + StationBean 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 StationBean fetchStations(String url) { + try { - stations.clear(); String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); + Serializer serializer = new Persister(); + + StationBean stations = serializer.read(StationBean.class, xml); + - Document doc = parseXML(xml); - Node rootNode = doc.getDocumentElement(); // stations - NodeList stationList = rootNode.getChildNodes(); - - - for (int i=0; i