20 |
|
|
21 |
final static int CACHE_TIMEOUT = 300*1000; |
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); |
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 |
|
|
58 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
59 |
} |
} |
60 |
|
|
61 |
public boolean lookupStationsWorker(String url) { |
public List<StationBean> lookupStationsWorker(String url) { |
62 |
boolean result; |
|
63 |
List<StationBean> tmpStations = stationCache.get(url); |
List<StationBean> tmpStations = stationCache.get(url); |
64 |
|
|
65 |
if (tmpStations != null) { |
if (tmpStations != null) { |
66 |
Log.i("lookupStations", "cache hit " + url); |
Log.i("lookupStations", "cache hit " + url); |
|
stations = tmpStations; |
|
|
result = true; |
|
67 |
} else { |
} else { |
68 |
result = fetchStations(url); |
tmpStations = fetchStations(url); |
69 |
|
|
70 |
if (result == true) { |
if (tmpStations != null) { |
71 |
stationCache.put(url, stations); |
stationCache.put(url, tmpStations); |
72 |
} |
} |
73 |
} |
} |
74 |
|
|
75 |
return result; |
return tmpStations; |
76 |
} |
} |
77 |
|
|
78 |
|
|
79 |
public boolean fetchStations(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 |
|
|
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 |
} |
} |