1 |
package dk.thoerup.traininfo.provider; |
2 |
|
3 |
|
4 |
import java.net.URLEncoder; |
5 |
|
6 |
import org.simpleframework.xml.Serializer; |
7 |
import org.simpleframework.xml.core.Persister; |
8 |
|
9 |
|
10 |
import android.location.Location; |
11 |
import android.util.Log; |
12 |
import dk.thoerup.android.traininfo.common.StationBean; |
13 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
14 |
import dk.thoerup.traininfo.util.DownloadUtil; |
15 |
import dk.thoerup.traininfo.util.XmlUtil; |
16 |
|
17 |
public class XmlStationProvider implements StationProvider { |
18 |
|
19 |
final static int CACHE_TIMEOUT = 300*1000; |
20 |
|
21 |
|
22 |
AndroidTimeoutCache<String, StationBean> stationCache = new AndroidTimeoutCache<String, StationBean>(CACHE_TIMEOUT); |
23 |
|
24 |
|
25 |
double roundToPlaces(double value, int places) { |
26 |
double pow = Math.pow(10, places); |
27 |
double temp = Math.round( value*pow ); |
28 |
|
29 |
return temp / pow; |
30 |
} |
31 |
|
32 |
@Override |
33 |
public StationBean lookupStations(Location location) { |
34 |
double lat = roundToPlaces(location.getLatitude(), 4); |
35 |
double lng = roundToPlaces(location.getLongitude(), 4); |
36 |
|
37 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng; |
38 |
Log.i("url", url); |
39 |
return lookupStationsWorker(url); |
40 |
} |
41 |
|
42 |
@Override |
43 |
public StationBean lookupStationsByName(String name) { |
44 |
|
45 |
try { |
46 |
name = URLEncoder.encode(name, "ISO8859-1"); |
47 |
} catch (Exception e) { |
48 |
Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best |
49 |
} |
50 |
|
51 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name; |
52 |
|
53 |
|
54 |
Log.i("url", url); |
55 |
return lookupStationsWorker(url); |
56 |
} |
57 |
|
58 |
|
59 |
@Override |
60 |
public StationBean lookupStationsByIds(String ids) { |
61 |
String url = ""; |
62 |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
63 |
|
64 |
Log.i("url", url); |
65 |
return lookupStationsWorker(url); |
66 |
} |
67 |
|
68 |
public StationBean lookupStationsWorker(String url) { |
69 |
|
70 |
StationBean tmpStations = stationCache.get(url); |
71 |
|
72 |
if (tmpStations != null) { |
73 |
Log.i("lookupStations", "cache hit " + url); |
74 |
} else { |
75 |
tmpStations = fetchStations(url); |
76 |
|
77 |
if (tmpStations != null) { |
78 |
stationCache.put(url, tmpStations); |
79 |
} |
80 |
} |
81 |
|
82 |
return tmpStations; |
83 |
} |
84 |
|
85 |
|
86 |
public StationBean fetchStations(String url) { |
87 |
|
88 |
try { |
89 |
|
90 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
91 |
|
92 |
Serializer serializer = new Persister(); |
93 |
|
94 |
StationBean stations = serializer.read(StationBean.class, xml); |
95 |
|
96 |
return stations; |
97 |
|
98 |
} catch (Exception e) { |
99 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
100 |
return null; |
101 |
} |
102 |
} |
103 |
|
104 |
@Override |
105 |
public void purgeOldEntries() { |
106 |
stationCache.purgeOldEntries(); |
107 |
} |
108 |
} |