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; |
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 |
|
|
28 |
@Override |
@Override |
29 |
public boolean lookupStations(Location location) { |
public List<StationBean> lookupStations(Location location) { |
30 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude(); |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude(); |
31 |
Log.i("url", url); |
Log.i("url", url); |
32 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
33 |
} |
} |
34 |
|
|
35 |
@Override |
@Override |
36 |
public boolean lookupStationsByName(String name) { |
public List<StationBean> lookupStationsByName(String name) { |
37 |
|
|
38 |
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
39 |
String url = ""; |
String url = ""; |
50 |
|
|
51 |
|
|
52 |
@Override |
@Override |
53 |
public boolean lookupStationsByIds(String ids) { |
public List<StationBean> lookupStationsByIds(String ids) { |
54 |
String url = ""; |
String url = ""; |
55 |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
56 |
|
|
57 |
Log.i("url", url); |
Log.i("url", url); |
58 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
59 |
} |
} |
60 |
|
|
61 |
|
public List<StationBean> lookupStationsWorker(String url) { |
62 |
|
|
63 |
|
List<StationBean> tmpStations = stationCache.get(url); |
64 |
|
|
65 |
|
if (tmpStations != null) { |
66 |
|
Log.i("lookupStations", "cache hit " + url); |
67 |
|
} else { |
68 |
|
tmpStations = fetchStations(url); |
69 |
|
|
70 |
|
if (tmpStations != null) { |
71 |
|
stationCache.put(url, tmpStations); |
72 |
|
} |
73 |
|
} |
74 |
|
|
75 |
|
return tmpStations; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
public boolean lookupStationsWorker(String url) { |
public List<StationBean> fetchStations(String url) { |
|
boolean success = false; |
|
80 |
|
|
81 |
try { |
try { |
82 |
stations.clear(); |
List<StationBean> stations = new ArrayList<StationBean>(); |
83 |
|
|
84 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
85 |
|
|
119 |
if (nodeName.equals("longitude")) |
if (nodeName.equals("longitude")) |
120 |
station.setLongitude( Double.parseDouble(content) ); |
station.setLongitude( Double.parseDouble(content) ); |
121 |
|
|
|
if (nodeName.equals("stationcode")) |
|
|
station.setCode(content); |
|
|
|
|
122 |
if (nodeName.equals("calcdist")) |
if (nodeName.equals("calcdist")) |
123 |
station.setDistance( (int) Double.parseDouble(content) ); |
station.setDistance( (int) Double.parseDouble(content) ); |
124 |
|
|
137 |
} |
} |
138 |
stations.add(station); |
stations.add(station); |
139 |
} |
} |
140 |
success = true; |
return stations; |
141 |
|
|
142 |
|
|
143 |
} catch (Exception e) { |
} catch (Exception e) { |
144 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
145 |
|
return null; |
146 |
} |
} |
|
return success; |
|
147 |
} |
} |
148 |
} |
} |