5 |
import java.util.ArrayList; |
import java.util.ArrayList; |
6 |
import java.util.List; |
import java.util.List; |
7 |
|
|
8 |
|
import org.simpleframework.xml.Serializer; |
9 |
|
import org.simpleframework.xml.core.Persister; |
10 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
11 |
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
12 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
13 |
|
|
14 |
import android.location.Location; |
import android.location.Location; |
15 |
import android.util.Log; |
import android.util.Log; |
16 |
import dk.thoerup.traininfo.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
17 |
|
import dk.thoerup.android.traininfo.common.TimetableBean; |
18 |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
19 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
20 |
import dk.thoerup.traininfo.util.XmlUtil; |
import dk.thoerup.traininfo.util.XmlUtil; |
23 |
|
|
24 |
final static int CACHE_TIMEOUT = 300*1000; |
final static int CACHE_TIMEOUT = 300*1000; |
25 |
|
|
26 |
List<StationBean> stations = new ArrayList<StationBean>(); |
//List<StationBean> stations = new ArrayList<StationBean>(); |
27 |
AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT); |
AndroidTimeoutCache<String, StationBean> stationCache = new AndroidTimeoutCache<String, StationBean>(CACHE_TIMEOUT); |
28 |
|
|
|
@Override |
|
|
public List<StationBean> getStations() { |
|
|
return stations; |
|
|
} |
|
29 |
|
|
30 |
|
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 |
|
|
37 |
@Override |
@Override |
38 |
public boolean lookupStations(Location location) { |
public StationBean lookupStations(Location location) { |
39 |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude(); |
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 |
Log.i("url", url); |
Log.i("url", url); |
44 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
45 |
} |
} |
46 |
|
|
47 |
@Override |
@Override |
48 |
public boolean lookupStationsByName(String name) { |
public StationBean lookupStationsByName(String name) { |
49 |
|
|
50 |
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
// String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name); |
51 |
String url = ""; |
String url = ""; |
62 |
|
|
63 |
|
|
64 |
@Override |
@Override |
65 |
public boolean lookupStationsByIds(String ids) { |
public StationBean lookupStationsByIds(String ids) { |
66 |
String url = ""; |
String url = ""; |
67 |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids; |
68 |
|
|
70 |
return lookupStationsWorker(url); |
return lookupStationsWorker(url); |
71 |
} |
} |
72 |
|
|
73 |
public boolean lookupStationsWorker(String url) { |
public StationBean lookupStationsWorker(String url) { |
74 |
boolean result; |
|
75 |
List<StationBean> tmpStations = stationCache.get(url); |
StationBean tmpStations = stationCache.get(url); |
76 |
|
|
77 |
if (tmpStations != null) { |
if (tmpStations != null) { |
78 |
Log.i("lookupStations", "cache hit " + url); |
Log.i("lookupStations", "cache hit " + url); |
|
result = true; |
|
79 |
} else { |
} else { |
80 |
result = fetchStations(url); |
tmpStations = fetchStations(url); |
81 |
|
|
82 |
if (result == true) { |
if (tmpStations != null) { |
83 |
stationCache.put(url, stations); |
stationCache.put(url, tmpStations); |
84 |
} |
} |
85 |
} |
} |
86 |
|
|
87 |
return result; |
return tmpStations; |
88 |
} |
} |
89 |
|
|
90 |
|
|
91 |
public boolean fetchStations(String url) { |
public StationBean fetchStations(String url) { |
|
boolean success = false; |
|
92 |
|
|
93 |
try { |
try { |
|
stations.clear(); |
|
94 |
|
|
95 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
96 |
|
|
97 |
|
Serializer serializer = new Persister(); |
|
Document doc = XmlUtil.parseXML(xml); |
|
|
Node rootNode = doc.getDocumentElement(); // stations |
|
|
NodeList stationList = rootNode.getChildNodes(); |
|
|
|
|
|
|
|
|
for (int i=0; i<stationList.getLength(); i++) { |
|
|
Node stationNode = stationList.item(i); |
|
|
|
|
|
if (! stationNode.getNodeName().equals("station")) |
|
|
continue; |
|
|
|
|
|
StationBean station = new StationBean(); |
|
|
|
|
|
NodeList stationElements = stationNode.getChildNodes(); |
|
|
for (int j=0; j<stationElements.getLength(); j++) { |
|
|
Node current = stationElements.item(j); |
|
|
|
|
|
String content = null; |
|
|
if (current.getFirstChild() != null) |
|
|
content = current.getFirstChild().getNodeValue(); //get the textNode - then get the text node's value |
|
|
|
|
|
String nodeName = current.getNodeName(); |
|
|
|
|
|
if (nodeName.equals("id")) |
|
|
station.setId( Integer.parseInt(content)); |
|
|
|
|
|
if (nodeName.equals("name")) |
|
|
station.setName(content); |
|
|
|
|
|
if (nodeName.equals("latitude")) |
|
|
station.setLatitude( Double.parseDouble(content) ); |
|
|
|
|
|
if (nodeName.equals("longitude")) |
|
|
station.setLongitude( Double.parseDouble(content) ); |
|
|
|
|
|
if (nodeName.equals("calcdist")) |
|
|
station.setDistance( (int) Double.parseDouble(content) ); |
|
|
|
|
|
if (nodeName.equals("address")) |
|
|
station.setAddress( content ); |
|
|
|
|
|
if (nodeName.equals("regional")) |
|
|
station.setRegional( Boolean.parseBoolean(content) ); |
|
|
|
|
|
if (nodeName.equals("strain")) |
|
|
station.setSTrain( Boolean.parseBoolean(content) ); |
|
|
|
|
|
if (nodeName.equals("metro")) |
|
|
station.setMetro( Boolean.parseBoolean(content) ); |
|
98 |
|
|
99 |
} |
StationBean stations = serializer.read(StationBean.class, xml); |
100 |
stations.add(station); |
|
101 |
} |
|
102 |
success = true; |
return stations; |
103 |
|
|
104 |
|
|
105 |
} catch (Exception e) { |
} catch (Exception e) { |
106 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
107 |
|
return null; |
108 |
} |
} |
|
return success; |
|
109 |
} |
} |
110 |
} |
} |