4 |
import java.util.List; |
import java.util.List; |
5 |
|
|
6 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
7 |
import org.w3c.dom.NamedNodeMap; |
|
8 |
import org.w3c.dom.Node; |
import org.w3c.dom.Node; |
9 |
import org.w3c.dom.NodeList; |
import org.w3c.dom.NodeList; |
10 |
|
|
11 |
import android.util.Log; |
import android.util.Log; |
12 |
import dk.thoerup.traininfo.TimetableBean; |
import dk.thoerup.traininfo.TimetableBean; |
13 |
|
import dk.thoerup.traininfo.util.AndroidTimeoutCache; |
14 |
import dk.thoerup.traininfo.util.DownloadUtil; |
import dk.thoerup.traininfo.util.DownloadUtil; |
15 |
import dk.thoerup.traininfo.util.XmlUtil; |
import dk.thoerup.traininfo.util.XmlUtil; |
16 |
|
|
17 |
public class XmlTimetableProvider implements TimetableProvider { |
public class XmlTimetableProvider implements TimetableProvider { |
18 |
|
|
19 |
List<TimetableBean> timetables = new ArrayList<TimetableBean>(); |
final static int CACHE_TIMEOUT = 60*1000; |
20 |
|
|
21 |
|
List<TimetableBean> timetables; |
22 |
|
|
23 |
|
AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT); |
24 |
|
|
25 |
@Override |
@Override |
26 |
public List<TimetableBean> getTimetable() { |
public List<TimetableBean> getTimetable(String type, String trainID) { |
27 |
return timetables; |
String key = type + "-" + trainID; |
28 |
|
List<TimetableBean> list = departureCache.get(key); |
29 |
|
|
30 |
|
if (list == null) { |
31 |
|
list = new ArrayList<TimetableBean>(); |
32 |
|
} |
33 |
|
|
34 |
|
return list; |
35 |
} |
} |
36 |
|
|
37 |
@Override |
@Override |
38 |
public boolean lookupTimetable(String trainID) { |
public boolean lookupTimetable(String type, String trainID) { |
39 |
|
boolean success; |
40 |
|
|
41 |
|
String trainNumber = extractTrainNumber(trainID); |
42 |
|
|
43 |
|
String key = type + "-" + trainID; |
44 |
|
timetables = departureCache.get(key); |
45 |
|
|
46 |
|
if (timetables == null) { |
47 |
|
success = lookupTimetableWorker(type, trainNumber); |
48 |
|
|
49 |
|
if (success) { |
50 |
|
departureCache.put(key, timetables); |
51 |
|
} |
52 |
|
|
53 |
|
} else { |
54 |
|
Log.i("XmlTimetableProvider", "cache hit !!!"); |
55 |
|
success = true; |
56 |
|
} |
57 |
|
|
58 |
|
return success; |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
|
public boolean lookupTimetableWorker(String type, String trainNumber) { |
63 |
boolean success = false; |
boolean success = false; |
64 |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainID.replace(" ", "%20") ; |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type; |
65 |
Log.i("url", url); |
Log.i("url", url); |
66 |
try { |
try { |
67 |
timetables.clear(); |
timetables = new ArrayList<TimetableBean>(); |
68 |
|
|
69 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
70 |
|
|
108 |
if (nodeName.equals("departure")) |
if (nodeName.equals("departure")) |
109 |
timetable.setDeparture( content ); |
timetable.setDeparture( content ); |
110 |
|
|
111 |
|
if (nodeName.equals("stationid")) |
112 |
|
timetable.setStationId( Integer.parseInt(content)); |
113 |
|
|
114 |
/*if (nodeName.equals("current")) |
/*if (nodeName.equals("current")) |
115 |
timetable.setCurrent( Boolean.parseBoolean(content) );*/ |
timetable.setCurrent( Boolean.parseBoolean(content) );*/ |
116 |
|
|
123 |
} catch (Exception e) { |
} catch (Exception e) { |
124 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
125 |
} |
} |
126 |
|
|
127 |
return success; |
return success; |
128 |
|
|
129 |
} |
} |
130 |
|
|
131 |
|
private String extractTrainNumber (String trainID) { |
132 |
|
|
133 |
|
String parts[] = trainID.split(" "); |
134 |
|
if (parts.length == 2) { |
135 |
|
return parts[1]; |
136 |
|
} else { |
137 |
|
return parts[0]; |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
} |
} |