2 |
|
|
3 |
import java.io.StringReader; |
import java.io.StringReader; |
4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
5 |
|
import java.util.Collections; |
6 |
|
import java.util.HashMap; |
7 |
import java.util.List; |
import java.util.List; |
8 |
|
|
9 |
import javax.xml.parsers.SAXParser; |
import javax.xml.parsers.SAXParser; |
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 long CACHE_TIMEOUT = 60*1000; |
25 |
|
|
26 |
|
class CacheEntry { |
27 |
|
public long timestamp; |
28 |
|
public List<DepartureBean> departures; |
29 |
|
} |
30 |
|
|
31 |
|
HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>(); |
32 |
|
ArrayList<DepartureBean> departures; |
33 |
|
|
34 |
|
|
35 |
DepartureBean tempDeparture; |
DepartureBean tempDeparture; |
36 |
StringBuilder builder = new StringBuilder(512); |
StringBuilder builder = new StringBuilder(512); |
37 |
|
|
38 |
@Override |
@Override |
39 |
public void lookupDepartures(int stationID) { |
public boolean lookupDepartures(int stationID) { |
40 |
departures.clear(); |
CacheEntry entry = departureCache.get(stationID); |
41 |
|
boolean success; |
42 |
|
|
43 |
|
long now = android.os.SystemClock.elapsedRealtime(); |
44 |
|
if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { |
45 |
|
|
46 |
|
success = lookupDeparturesWorker(stationID); |
47 |
|
|
48 |
|
if (success) { |
49 |
|
entry = new CacheEntry(); |
50 |
|
entry.timestamp = android.os.SystemClock.elapsedRealtime(); |
51 |
|
entry.departures = departures; |
52 |
|
|
53 |
|
departureCache.put(stationID, entry); |
54 |
|
} |
55 |
|
} else { |
56 |
|
Log.i("XmlDepartureProvider", "cache hit !!!"); |
57 |
|
success = true; |
58 |
|
} |
59 |
|
|
60 |
|
return success; |
61 |
|
} |
62 |
|
|
63 |
|
private boolean lookupDeparturesWorker(int stationID) { |
64 |
|
boolean success = false; |
65 |
|
departures = new ArrayList<DepartureBean>(); |
66 |
try |
try |
67 |
{ |
{ |
68 |
//String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode; |
//String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode; |
78 |
|
|
79 |
xr.setContentHandler(this); |
xr.setContentHandler(this); |
80 |
xr.setErrorHandler(this); |
xr.setErrorHandler(this); |
|
xr.setDTDHandler(this); |
|
81 |
xr.parse(source); |
xr.parse(source); |
82 |
|
success = true; |
83 |
|
|
84 |
} catch (Exception e) { |
} catch (Exception e) { |
85 |
Log.e("XmlDepartureProvider", "looupFunction", e); |
Log.e("XmlDepartureProvider", "looupFunction", e); |
86 |
} |
} |
87 |
|
return success; |
88 |
} |
} |
89 |
|
|
90 |
@Override |
@Override |
91 |
public List<DepartureBean> getDepartures() { |
public List<DepartureBean> getDepartures(int station) { |
92 |
return departures; |
CacheEntry entry = departureCache.get(station); |
93 |
|
|
94 |
|
if (entry != null) { |
95 |
|
return entry.departures; |
96 |
|
} else { |
97 |
|
return new ArrayList<DepartureBean>(); |
98 |
|
} |
99 |
|
|
100 |
} |
} |
101 |
|
|
102 |
// 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 |