1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
import java.io.ByteArrayInputStream; |
|
4 |
import java.io.IOException; |
import java.net.URLEncoder; |
5 |
import java.util.ArrayList; |
import java.util.ArrayList; |
6 |
import java.util.List; |
import java.util.List; |
7 |
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
import javax.xml.parsers.ParserConfigurationException; |
|
|
|
|
8 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
9 |
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
10 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
|
import org.xml.sax.SAXException; |
|
11 |
|
|
12 |
import android.location.Location; |
import android.location.Location; |
13 |
import android.util.Log; |
import android.util.Log; |
14 |
import dk.thoerup.traininfo.StationBean; |
import dk.thoerup.traininfo.StationBean; |
15 |
|
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
16 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
17 |
|
import dk.thoerup.traininfo.util.XmlUtil; |
18 |
|
|
19 |
public class XmlStationProvider implements StationProvider { |
public class XmlStationProvider implements StationProvider { |
20 |
|
|
21 |
|
final static int CACHE_TIMEOUT = 300*1000; |
22 |
|
|
23 |
List<StationBean> stations = new ArrayList<StationBean>(); |
//List<StationBean> stations = new ArrayList<StationBean>(); |
24 |
|
AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT); |
25 |
|
|
26 |
|
|
27 |
|
double roundToPlaces(double value, int places) { |
28 |
|
double pow = Math.pow(10, places); |
29 |
|
double temp = Math.round( value*pow ); |
30 |
|
|
31 |
|
return temp / pow; |
32 |
|
} |
33 |
|
|
34 |
|
@Override |
35 |
|
public List<StationBean> lookupStations(Location location) { |
36 |
|
double lat = roundToPlaces(location.getLatitude(), 4); |
37 |
|
double lng = roundToPlaces(location.getLongitude(), 4); |
38 |
|
|
39 |
|
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng; |
40 |
|
Log.i("url", url); |
41 |
|
return lookupStationsWorker(url); |
42 |
|
} |
43 |
|
|
44 |
@Override |
@Override |
45 |
public List<StationBean> getStations() { |
public List<StationBean> lookupStationsByName(String name) { |
46 |
return stations; |
|
47 |
|
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
48 |
|
String url = ""; |
49 |
|
|
50 |
|
try { |
51 |
|
url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + URLEncoder.encode(name, "ISO8859-1"); |
52 |
|
} catch (Exception e) { |
53 |
|
Log.e("lookupStations", "Encoding failed", e); |
54 |
|
} |
55 |
|
|
56 |
|
Log.i("url", url); |
57 |
|
return lookupStationsWorker(url); |
58 |
} |
} |
59 |
|
|
60 |
|
|
61 |
@Override |
@Override |
62 |
public boolean lookupStations(Location location) { |
public List<StationBean> lookupStationsByIds(String ids) { |
63 |
boolean success = false; |
String url = ""; |
64 |
String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
65 |
|
|
66 |
Log.i("url", url); |
Log.i("url", url); |
67 |
|
return lookupStationsWorker(url); |
68 |
|
} |
69 |
|
|
70 |
|
public List<StationBean> lookupStationsWorker(String url) { |
71 |
|
|
72 |
|
List<StationBean> tmpStations = stationCache.get(url); |
73 |
|
|
74 |
|
if (tmpStations != null) { |
75 |
|
Log.i("lookupStations", "cache hit " + url); |
76 |
|
} else { |
77 |
|
tmpStations = fetchStations(url); |
78 |
|
|
79 |
|
if (tmpStations != null) { |
80 |
|
stationCache.put(url, tmpStations); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
|
return tmpStations; |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
public List<StationBean> fetchStations(String url) { |
89 |
|
|
90 |
try { |
try { |
91 |
stations.clear(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
92 |
|
|
93 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
94 |
|
|
95 |
|
|
96 |
Document doc = parseXML(xml); |
Document doc = XmlUtil.parseXML(xml); |
97 |
Node rootNode = doc.getDocumentElement(); // stations |
Node rootNode = doc.getDocumentElement(); // stations |
98 |
NodeList stationList = rootNode.getChildNodes(); |
NodeList stationList = rootNode.getChildNodes(); |
99 |
|
|
128 |
if (nodeName.equals("longitude")) |
if (nodeName.equals("longitude")) |
129 |
station.setLongitude( Double.parseDouble(content) ); |
station.setLongitude( Double.parseDouble(content) ); |
130 |
|
|
|
if (nodeName.equals("stationcode")) |
|
|
station.setCode(content); |
|
|
|
|
131 |
if (nodeName.equals("calcdist")) |
if (nodeName.equals("calcdist")) |
132 |
station.setDistance( (int) Double.parseDouble(content) ); |
station.setDistance( (int) Double.parseDouble(content) ); |
133 |
|
|
134 |
|
if (nodeName.equals("address")) |
135 |
|
station.setAddress( content ); |
136 |
|
|
137 |
|
if (nodeName.equals("regional")) |
138 |
|
station.setRegional( Boolean.parseBoolean(content) ); |
139 |
|
|
140 |
|
if (nodeName.equals("strain")) |
141 |
|
station.setSTrain( Boolean.parseBoolean(content) ); |
142 |
|
|
143 |
|
if (nodeName.equals("metro")) |
144 |
|
station.setMetro( Boolean.parseBoolean(content) ); |
145 |
|
|
146 |
} |
} |
147 |
stations.add(station); |
stations.add(station); |
148 |
} |
} |
149 |
success = true; |
return stations; |
150 |
|
|
151 |
|
|
152 |
} catch (Exception e) { |
} catch (Exception e) { |
153 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
154 |
|
return null; |
155 |
} |
} |
|
return success; |
|
|
} |
|
|
|
|
|
private Document parseXML(String str) throws SAXException, IOException, ParserConfigurationException |
|
|
{ |
|
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
|
|
return builder.parse( new ByteArrayInputStream(str.getBytes()) ); |
|
156 |
} |
} |
157 |
} |
} |