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; |
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 |
|
|
27 |
class CacheEntry { |
AndroidTimeoutCache<Integer,List<DepartureBean>> departureCache = new AndroidTimeoutCache<Integer,List<DepartureBean>>(CACHE_TIMEOUT); |
|
public long timestamp; |
|
|
public List<DepartureBean> departures; |
|
|
} |
|
28 |
|
|
29 |
HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>(); |
List<DepartureBean> departures; |
|
ArrayList<DepartureBean> departures; |
|
30 |
|
|
31 |
|
|
32 |
DepartureBean tempDeparture; |
DepartureBean tempDeparture; |
33 |
StringBuilder builder = new StringBuilder(512); |
StringBuilder builder = new StringBuilder(512); |
34 |
|
|
35 |
@Override |
@Override |
36 |
public boolean lookupDepartures(int stationID) { |
public boolean lookupDepartures(int stationID) { |
|
CacheEntry entry = departureCache.get(stationID); |
|
37 |
boolean success; |
boolean success; |
38 |
|
|
39 |
|
departures = departureCache.get(stationID); |
40 |
|
|
41 |
long now = android.os.SystemClock.elapsedRealtime(); |
if (departures == null) { |
|
if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { |
|
|
|
|
42 |
success = lookupDeparturesWorker(stationID); |
success = lookupDeparturesWorker(stationID); |
43 |
|
|
44 |
if (success) { |
if (success) { |
45 |
entry = new CacheEntry(); |
departureCache.put(stationID, departures); |
|
entry.timestamp = android.os.SystemClock.elapsedRealtime(); |
|
|
entry.departures = departures; |
|
|
|
|
|
departureCache.put(stationID, entry); |
|
46 |
} |
} |
47 |
|
|
48 |
} else { |
} else { |
49 |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
50 |
success = true; |
success = true; |
58 |
departures = new ArrayList<DepartureBean>(); |
departures = new ArrayList<DepartureBean>(); |
59 |
try |
try |
60 |
{ |
{ |
61 |
//String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode; |
|
62 |
String url = "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" + stationID; |
String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID; |
63 |
Log.i("xmlurl",url); |
Log.i("xmlurl",url); |
64 |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
65 |
|
|
82 |
|
|
83 |
@Override |
@Override |
84 |
public List<DepartureBean> getDepartures(int station) { |
public List<DepartureBean> getDepartures(int station) { |
85 |
CacheEntry entry = departureCache.get(station); |
List<DepartureBean> list = departureCache.get(station); |
86 |
|
|
87 |
if (entry != null) { |
if (list == null) { |
88 |
return entry.departures; |
list = new ArrayList<DepartureBean>(); |
89 |
} else { |
} |
|
return new ArrayList<DepartureBean>(); |
|
|
} |
|
90 |
|
|
91 |
|
return list; |
92 |
} |
} |
93 |
|
|
94 |
// 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 |