1 |
torben |
294 |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
3 |
torben |
391 |
|
4 |
torben |
383 |
import java.net.URLEncoder; |
5 |
torben |
294 |
import java.util.ArrayList; |
6 |
|
|
import java.util.List; |
7 |
|
|
|
8 |
|
|
import org.w3c.dom.Document; |
9 |
|
|
import org.w3c.dom.Node; |
10 |
|
|
import org.w3c.dom.NodeList; |
11 |
|
|
|
12 |
|
|
import android.location.Location; |
13 |
|
|
import android.util.Log; |
14 |
|
|
import dk.thoerup.traininfo.StationBean; |
15 |
torben |
1004 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
16 |
torben |
294 |
import dk.thoerup.traininfo.util.DownloadUtil; |
17 |
torben |
352 |
import dk.thoerup.traininfo.util.XmlUtil; |
18 |
torben |
294 |
|
19 |
|
|
public class XmlStationProvider implements StationProvider { |
20 |
torben |
1004 |
|
21 |
|
|
final static int CACHE_TIMEOUT = 300*1000; |
22 |
torben |
294 |
|
23 |
torben |
1006 |
//List<StationBean> stations = new ArrayList<StationBean>(); |
24 |
torben |
1004 |
AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT); |
25 |
torben |
294 |
|
26 |
|
|
|
27 |
torben |
1031 |
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 |
torben |
381 |
|
34 |
torben |
294 |
@Override |
35 |
torben |
1006 |
public List<StationBean> lookupStations(Location location) { |
36 |
torben |
1031 |
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 |
torben |
294 |
Log.i("url", url); |
41 |
torben |
381 |
return lookupStationsWorker(url); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
@Override |
45 |
torben |
1006 |
public List<StationBean> lookupStationsByName(String name) { |
46 |
torben |
383 |
|
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 |
torben |
381 |
Log.i("url", url); |
57 |
|
|
return lookupStationsWorker(url); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
torben |
433 |
@Override |
62 |
torben |
1006 |
public List<StationBean> lookupStationsByIds(String ids) { |
63 |
torben |
433 |
String url = ""; |
64 |
|
|
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
65 |
|
|
|
66 |
|
|
Log.i("url", url); |
67 |
|
|
return lookupStationsWorker(url); |
68 |
|
|
} |
69 |
torben |
1004 |
|
70 |
torben |
1006 |
public List<StationBean> lookupStationsWorker(String url) { |
71 |
|
|
|
72 |
torben |
1004 |
List<StationBean> tmpStations = stationCache.get(url); |
73 |
|
|
|
74 |
|
|
if (tmpStations != null) { |
75 |
|
|
Log.i("lookupStations", "cache hit " + url); |
76 |
|
|
} else { |
77 |
torben |
1006 |
tmpStations = fetchStations(url); |
78 |
torben |
1004 |
|
79 |
torben |
1006 |
if (tmpStations != null) { |
80 |
|
|
stationCache.put(url, tmpStations); |
81 |
torben |
1004 |
} |
82 |
|
|
} |
83 |
|
|
|
84 |
torben |
1006 |
return tmpStations; |
85 |
torben |
1004 |
} |
86 |
torben |
433 |
|
87 |
|
|
|
88 |
torben |
1006 |
public List<StationBean> fetchStations(String url) { |
89 |
torben |
381 |
|
90 |
torben |
294 |
try { |
91 |
torben |
1006 |
List<StationBean> stations = new ArrayList<StationBean>(); |
92 |
torben |
294 |
|
93 |
torben |
302 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
94 |
torben |
294 |
|
95 |
|
|
|
96 |
torben |
352 |
Document doc = XmlUtil.parseXML(xml); |
97 |
torben |
294 |
Node rootNode = doc.getDocumentElement(); // stations |
98 |
|
|
NodeList stationList = rootNode.getChildNodes(); |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
for (int i=0; i<stationList.getLength(); i++) { |
102 |
|
|
Node stationNode = stationList.item(i); |
103 |
|
|
|
104 |
|
|
if (! stationNode.getNodeName().equals("station")) |
105 |
|
|
continue; |
106 |
|
|
|
107 |
|
|
StationBean station = new StationBean(); |
108 |
|
|
|
109 |
|
|
NodeList stationElements = stationNode.getChildNodes(); |
110 |
|
|
for (int j=0; j<stationElements.getLength(); j++) { |
111 |
|
|
Node current = stationElements.item(j); |
112 |
|
|
|
113 |
|
|
String content = null; |
114 |
|
|
if (current.getFirstChild() != null) |
115 |
|
|
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
116 |
|
|
|
117 |
|
|
String nodeName = current.getNodeName(); |
118 |
|
|
|
119 |
torben |
310 |
if (nodeName.equals("id")) |
120 |
|
|
station.setId( Integer.parseInt(content)); |
121 |
|
|
|
122 |
torben |
294 |
if (nodeName.equals("name")) |
123 |
|
|
station.setName(content); |
124 |
|
|
|
125 |
|
|
if (nodeName.equals("latitude")) |
126 |
|
|
station.setLatitude( Double.parseDouble(content) ); |
127 |
|
|
|
128 |
|
|
if (nodeName.equals("longitude")) |
129 |
torben |
575 |
station.setLongitude( Double.parseDouble(content) ); |
130 |
torben |
294 |
|
131 |
|
|
if (nodeName.equals("calcdist")) |
132 |
|
|
station.setDistance( (int) Double.parseDouble(content) ); |
133 |
torben |
493 |
|
134 |
|
|
if (nodeName.equals("address")) |
135 |
|
|
station.setAddress( content ); |
136 |
torben |
551 |
|
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 |
torben |
294 |
|
146 |
|
|
} |
147 |
|
|
stations.add(station); |
148 |
torben |
319 |
} |
149 |
torben |
1006 |
return stations; |
150 |
torben |
294 |
|
151 |
|
|
|
152 |
|
|
} catch (Exception e) { |
153 |
|
|
Log.e("XmlStationProvider", "lookupStations: ", e); |
154 |
torben |
1006 |
return null; |
155 |
torben |
294 |
} |
156 |
|
|
} |
157 |
|
|
} |