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 |
|
|
27 |
class CacheEntry { |
AndroidTimeoutCache<String,List<DepartureBean>> departureCache = new AndroidTimeoutCache<String,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, boolean arrival) { |
|
CacheEntry entry = departureCache.get(stationID); |
|
37 |
boolean success; |
boolean success; |
38 |
|
|
39 |
|
String key = "" + stationID + ":" + arrival; |
40 |
|
|
41 |
|
departures = departureCache.get(key); |
42 |
|
|
43 |
long now = android.os.SystemClock.elapsedRealtime(); |
if (departures == null) { |
44 |
if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) { |
success = lookupDeparturesWorker(stationID, arrival); |
45 |
|
|
46 |
success = lookupDeparturesWorker(stationID); |
if (success) { |
47 |
|
departureCache.put(key, departures); |
|
if (success) { |
|
|
entry = new CacheEntry(); |
|
|
entry.timestamp = android.os.SystemClock.elapsedRealtime(); |
|
|
entry.departures = departures; |
|
|
|
|
|
departureCache.put(stationID, entry); |
|
48 |
} |
} |
49 |
|
|
50 |
} else { |
} else { |
51 |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
Log.i("XmlDepartureProvider", "cache hit !!!"); |
52 |
success = true; |
success = true; |
55 |
return success; |
return success; |
56 |
} |
} |
57 |
|
|
58 |
private boolean lookupDeparturesWorker(int stationID) { |
private boolean lookupDeparturesWorker(int stationID, boolean arrival) { |
59 |
boolean success = false; |
boolean success = false; |
60 |
departures = new ArrayList<DepartureBean>(); |
departures = new ArrayList<DepartureBean>(); |
61 |
try |
try |
62 |
{ |
{ |
63 |
|
int iArrival = arrival ? 1 : 0; |
64 |
String url = XmlUtil.SERVICE_BASE + "/TrainInfoService/DepartureServlet?format=xml&station=" + stationID; |
String url = XmlUtil.SERVICE_BASE + "/DepartureServlet?format=xml&station=" + stationID + "&arrival=" + iArrival; |
65 |
Log.i("xmlurl",url); |
Log.i("xmlurl",url); |
66 |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
String doc = DownloadUtil.getContentString(url, 45000, "ISO-8859-1"); |
67 |
|
|
83 |
} |
} |
84 |
|
|
85 |
@Override |
@Override |
86 |
public List<DepartureBean> getDepartures(int station) { |
public List<DepartureBean> getDepartures(int station,boolean arrival) { |
|
CacheEntry entry = departureCache.get(station); |
|
87 |
|
|
88 |
if (entry != null) { |
String key = "" + station + ":" + arrival; |
|
return entry.departures; |
|
|
} else { |
|
|
return new ArrayList<DepartureBean>(); |
|
|
} |
|
89 |
|
|
90 |
|
List<DepartureBean> list = departureCache.get(key); |
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 |
134 |
tempDeparture.setStatus(builder.toString().trim()); |
tempDeparture.setStatus(builder.toString().trim()); |
135 |
} else if (name.equals("note")) { |
} else if (name.equals("note")) { |
136 |
tempDeparture.setNote(builder.toString().trim()); |
tempDeparture.setNote(builder.toString().trim()); |
137 |
} |
} else if (name.equals("type")) { |
138 |
|
tempDeparture.setType(builder.toString().trim()); |
139 |
|
} |
140 |
} |
} |
141 |
} |
} |