1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
import java.net.URI; |
|
4 |
import java.net.URLEncoder; |
import java.net.URLEncoder; |
5 |
import java.util.ArrayList; |
import java.util.ArrayList; |
6 |
import java.util.List; |
import java.util.List; |
10 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
11 |
|
|
12 |
import android.location.Location; |
import android.location.Location; |
|
import android.net.Uri; |
|
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; |
import dk.thoerup.traininfo.util.XmlUtil; |
18 |
|
|
19 |
public class XmlStationProvider implements StationProvider { |
public class XmlStationProvider implements StationProvider { |
|
|
|
|
List<StationBean> stations = new ArrayList<StationBean>(); |
|
20 |
|
|
21 |
|
final static int CACHE_TIMEOUT = 300*1000; |
22 |
|
|
23 |
|
//List<StationBean> stations = new ArrayList<StationBean>(); |
24 |
|
AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT); |
25 |
|
|
|
@Override |
|
|
public List<StationBean> getStations() { |
|
|
return stations; |
|
|
} |
|
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 |
@Override |
35 |
public boolean lookupStations(Location location) { |
public List<StationBean> lookupStations(Location location) { |
36 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + location.getLatitude() + "&longitude=" + location.getLongitude(); |
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); |
Log.i("url", url); |
41 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
42 |
} |
} |
43 |
|
|
44 |
@Override |
@Override |
45 |
public boolean lookupStations(String name) { |
public List<StationBean> lookupStationsByName(String name) { |
46 |
|
|
47 |
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
48 |
String url = ""; |
String url = ""; |
58 |
} |
} |
59 |
|
|
60 |
|
|
61 |
public boolean lookupStationsWorker(String url) { |
@Override |
62 |
boolean success = false; |
public List<StationBean> lookupStationsByIds(String ids) { |
63 |
|
String url = ""; |
64 |
|
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
65 |
|
|
66 |
|
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 |
|
|
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; |
|
156 |
} |
} |
157 |
} |
} |