2 |
|
|
3 |
import java.io.StringReader; |
import java.io.StringReader; |
4 |
import java.util.ArrayList; |
import java.util.ArrayList; |
|
import java.util.Collections; |
|
|
import java.util.HashMap; |
|
5 |
import java.util.List; |
import java.util.List; |
6 |
|
|
7 |
import javax.xml.parsers.SAXParser; |
import javax.xml.parsers.SAXParser; |
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; |
import dk.thoerup.traininfo.util.XmlUtil; |
21 |
|
|
22 |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider { |
23 |
|
|
24 |
final static long CACHE_TIMEOUT = 60*1000; |
final static int CACHE_TIMEOUT = 60*1000; |
25 |
|
|
26 |
class CacheEntry { |
class CacheEntry { |
27 |
public long timestamp; |
public long timestamp; |
28 |
public List<DepartureBean> departures; |
public List<DepartureBean> departures; |
29 |
} |
} |
30 |
|
|
31 |
HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>(); |
|
32 |
ArrayList<DepartureBean> departures; |
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 boolean lookupDepartures(int stationID) { |
public boolean lookupDepartures(int stationID) { |
|
CacheEntry entry = departureCache.get(stationID); |
|
42 |
boolean success; |
boolean success; |
43 |
|
|
44 |
|
departures = departureCache.get(stationID); |
45 |
|
|
46 |
long now = android.os.SystemClock.elapsedRealtime(); |
if (departures == null) { |
|
if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { |
|
|
|
|
47 |
success = lookupDeparturesWorker(stationID); |
success = lookupDeparturesWorker(stationID); |
48 |
|
|
49 |
if (success) { |
if (success) { |
50 |
entry = new CacheEntry(); |
departureCache.put(stationID, departures); |
|
entry.timestamp = android.os.SystemClock.elapsedRealtime(); |
|
|
entry.departures = departures; |
|
|
|
|
|
departureCache.put(stationID, entry); |
|
51 |
} |
} |
52 |
|
|
53 |
} else { |
} else { |
54 |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
55 |
success = true; |
success = true; |
64 |
try |
try |
65 |
{ |
{ |
66 |
|
|
67 |
String url = XmlUtil.SERVICE_BASE + "/TrainInfoService/DepartureServlet?format=xml&station=" + stationID; |
String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID; |
68 |
Log.i("xmlurl",url); |
Log.i("xmlurl",url); |
69 |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
70 |
|
|
87 |
|
|
88 |
@Override |
@Override |
89 |
public List<DepartureBean> getDepartures(int station) { |
public List<DepartureBean> getDepartures(int station) { |
90 |
CacheEntry entry = departureCache.get(station); |
List<DepartureBean> list = departureCache.get(station); |
91 |
|
|
92 |
if (entry != null) { |
if (list == null) { |
93 |
return entry.departures; |
list = new ArrayList<DepartureBean>(); |
94 |
} else { |
} |
|
return new ArrayList<DepartureBean>(); |
|
|
} |
|
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 |