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 |
|
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=" + (float)location.getLatitude() + "&longitude=" + (float)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 lookupStationsByName(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 = ""; |
59 |
|
|
60 |
|
|
61 |
@Override |
@Override |
62 |
public boolean lookupStationsByIds(String ids) { |
public List<StationBean> lookupStationsByIds(String ids) { |
63 |
String url = ""; |
String url = ""; |
64 |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
65 |
|
|
67 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
68 |
} |
} |
69 |
|
|
70 |
public boolean lookupStationsWorker(String url) { |
public List<StationBean> lookupStationsWorker(String url) { |
71 |
boolean result; |
|
72 |
List<StationBean> tmpStations = stationCache.get(url); |
List<StationBean> tmpStations = stationCache.get(url); |
73 |
|
|
74 |
if (tmpStations != null) { |
if (tmpStations != null) { |
75 |
Log.i("lookupStations", "cache hit " + url); |
Log.i("lookupStations", "cache hit " + url); |
|
stations = tmpStations; |
|
|
result = true; |
|
76 |
} else { |
} else { |
77 |
result = fetchStations(url); |
tmpStations = fetchStations(url); |
78 |
|
|
79 |
if (result == true) { |
if (tmpStations != null) { |
80 |
stationCache.put(url, stations); |
stationCache.put(url, tmpStations); |
81 |
} |
} |
82 |
} |
} |
83 |
|
|
84 |
return result; |
return tmpStations; |
85 |
} |
} |
86 |
|
|
87 |
|
|
88 |
public boolean fetchStations(String url) { |
public List<StationBean> fetchStations(String url) { |
|
boolean success = false; |
|
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 |
|
|
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 |
} |
} |