15 |
|
|
16 |
import android.util.Log; |
import android.util.Log; |
17 |
import dk.thoerup.traininfo.DepartureBean; |
import dk.thoerup.traininfo.DepartureBean; |
18 |
|
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; |
21 |
|
|
22 |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
23 |
|
|
24 |
ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>(); |
final static int CACHE_TIMEOUT = 60*1000; |
25 |
|
|
26 |
|
class CacheEntry { |
27 |
|
public long timestamp; |
28 |
|
public List<DepartureBean> departures; |
29 |
|
} |
30 |
|
|
31 |
|
|
32 |
|
AndroidTimeoutCache<Integer,List<DepartureBean>> departureCache = new AndroidTimeoutCache<Integer,List<DepartureBean>>(CACHE_TIMEOUT); |
33 |
|
|
34 |
|
List<DepartureBean> departures; |
35 |
|
|
36 |
|
|
37 |
DepartureBean tempDeparture; |
DepartureBean tempDeparture; |
38 |
StringBuilder builder = new StringBuilder(512); |
StringBuilder builder = new StringBuilder(512); |
39 |
|
|
40 |
@Override |
@Override |
41 |
public void lookupDepartures(String station) { |
public boolean lookupDepartures(int stationID) { |
42 |
departures.clear(); |
boolean success; |
43 |
|
|
44 |
|
departures = departureCache.get(stationID); |
45 |
|
|
46 |
|
if (departures == null) { |
47 |
|
success = lookupDeparturesWorker(stationID); |
48 |
|
|
49 |
|
if (success) { |
50 |
|
departureCache.put(stationID, departures); |
51 |
|
} |
52 |
|
|
53 |
|
} else { |
54 |
|
Log.i("XmlDepartureProvider", "cache hit !!!"); |
55 |
|
success = true; |
56 |
|
} |
57 |
|
|
58 |
|
return success; |
59 |
|
} |
60 |
|
|
61 |
|
private boolean lookupDeparturesWorker(int stationID) { |
62 |
|
boolean success = false; |
63 |
|
departures = new ArrayList<DepartureBean>(); |
64 |
try |
try |
65 |
{ String url = "http://t-hoerup.dk/tog/xml_display.php?stationname="+station; |
{ |
66 |
String doc = DownloadUtil.getContent(url, 45000, "ISO-8859-1"); |
|
67 |
|
String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID; |
68 |
|
Log.i("xmlurl",url); |
69 |
|
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
70 |
|
|
71 |
InputSource source = new InputSource( new StringReader(doc)); |
InputSource source = new InputSource( new StringReader(doc)); |
72 |
|
|
76 |
|
|
77 |
xr.setContentHandler(this); |
xr.setContentHandler(this); |
78 |
xr.setErrorHandler(this); |
xr.setErrorHandler(this); |
|
xr.setDTDHandler(this); |
|
79 |
xr.parse(source); |
xr.parse(source); |
80 |
|
success = true; |
81 |
|
|
82 |
} catch (Exception e) { |
} catch (Exception e) { |
83 |
Log.e("XmlDepartureProvider", "looupFunction", e); |
Log.e("XmlDepartureProvider", "looupFunction", e); |
84 |
} |
} |
85 |
|
return success; |
86 |
} |
} |
87 |
|
|
88 |
@Override |
@Override |
89 |
public List<DepartureBean> getDepartures() { |
public List<DepartureBean> getDepartures(int station) { |
90 |
return departures; |
List<DepartureBean> list = departureCache.get(station); |
91 |
|
|
92 |
|
if (list == null) { |
93 |
|
list = new ArrayList<DepartureBean>(); |
94 |
|
} |
95 |
|
|
96 |
|
return list; |
97 |
} |
} |
98 |
|
|
99 |
// this can be called several times fore the same text-node if there are many chardata / lines |
// this can be called several times fore the same text-node if there are many chardata / lines |