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