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 { |
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 |
@Override |
@Override |
27 |
public List<StationBean> getStations() { |
public List<StationBean> getStations() { |
61 |
Log.i("url", url); |
Log.i("url", url); |
62 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
63 |
} |
} |
64 |
|
|
65 |
|
public boolean lookupStationsWorker(String url) { |
66 |
|
boolean result; |
67 |
|
List<StationBean> tmpStations = stationCache.get(url); |
68 |
|
|
69 |
|
if (tmpStations != null) { |
70 |
|
Log.i("lookupStations", "cache hit " + url); |
71 |
|
result = true; |
72 |
|
} else { |
73 |
|
result = fetchStations(url); |
74 |
|
|
75 |
|
if (result == true) { |
76 |
|
stationCache.put(url, stations); |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
return result; |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
public boolean lookupStationsWorker(String url) { |
public boolean fetchStations(String url) { |
85 |
boolean success = false; |
boolean success = false; |
86 |
|
|
87 |
try { |
try { |
125 |
if (nodeName.equals("longitude")) |
if (nodeName.equals("longitude")) |
126 |
station.setLongitude( Double.parseDouble(content) ); |
station.setLongitude( Double.parseDouble(content) ); |
127 |
|
|
|
if (nodeName.equals("stationcode")) |
|
|
station.setCode(content); |
|
|
|
|
128 |
if (nodeName.equals("calcdist")) |
if (nodeName.equals("calcdist")) |
129 |
station.setDistance( (int) Double.parseDouble(content) ); |
station.setDistance( (int) Double.parseDouble(content) ); |
130 |
|
|