17 |
public class XmlTimetableProvider implements TimetableProvider { |
public class XmlTimetableProvider implements TimetableProvider { |
18 |
|
|
19 |
final static int CACHE_TIMEOUT = 60*1000; |
final static int CACHE_TIMEOUT = 60*1000; |
|
|
|
|
List<TimetableBean> timetables; |
|
20 |
|
|
21 |
AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT); |
AndroidTimeoutCache<String,List<TimetableBean>> departureCache = new AndroidTimeoutCache<String,List<TimetableBean>>(CACHE_TIMEOUT); |
|
|
|
|
@Override |
|
|
public List<TimetableBean> getTimetable(String type, String trainID) { |
|
|
String key = type + "-" + trainID; |
|
|
List<TimetableBean> list = departureCache.get(key); |
|
|
|
|
|
if (list == null) { |
|
|
list = new ArrayList<TimetableBean>(); |
|
|
} |
|
|
|
|
|
return list; |
|
|
} |
|
22 |
|
|
23 |
@Override |
@Override |
24 |
public boolean lookupTimetable(String type, String trainID) { |
public List<TimetableBean> lookupTimetable(String type, String trainID) { |
|
boolean success; |
|
25 |
|
|
26 |
String trainNumber = extractTrainNumber(trainID); |
String trainNumber = extractTrainNumber(trainID); |
27 |
|
|
28 |
String key = type + "-" + trainID; |
String key = type + "-" + trainID; |
29 |
timetables = departureCache.get(key); |
List<TimetableBean> timetables = departureCache.get(key); |
30 |
|
|
31 |
if (timetables == null) { |
if (timetables == null) { |
32 |
success = lookupTimetableWorker(type, trainNumber); |
timetables = lookupTimetableWorker(type, trainNumber); |
33 |
|
|
34 |
if (success) { |
if (timetables != null) { |
35 |
departureCache.put(key, timetables); |
departureCache.put(key, timetables); |
36 |
} |
} |
37 |
|
|
38 |
} else { |
} else { |
39 |
Log.i("XmlTimetableProvider", "cache hit !!!"); |
Log.i("XmlTimetableProvider", "cache hit !!!"); |
|
success = true; |
|
40 |
} |
} |
41 |
|
|
42 |
return success; |
return timetables; |
43 |
} |
} |
44 |
|
|
45 |
|
|
46 |
public boolean lookupTimetableWorker(String type, String trainNumber) { |
public List<TimetableBean> lookupTimetableWorker(String type, String trainNumber) { |
47 |
boolean success = false; |
|
48 |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type; |
String url = XmlUtil.SERVICE_BASE + "/TimetableServlet?train=" + trainNumber + "&type=" + type; |
49 |
Log.i("url", url); |
Log.i("url", url); |
50 |
try { |
try { |
51 |
timetables = new ArrayList<TimetableBean>(); |
List<TimetableBean> timetables = new ArrayList<TimetableBean>(); |
52 |
|
|
53 |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
54 |
|
|
101 |
} |
} |
102 |
timetables.add(timetable); |
timetables.add(timetable); |
103 |
} |
} |
104 |
success = true; |
return timetables; |
105 |
|
|
106 |
|
|
107 |
} catch (Exception e) { |
} catch (Exception e) { |
108 |
Log.e("XmlStationProvider", "lookupStations: ", e); |
Log.e("XmlStationProvider", "lookupStations: ", e); |
109 |
} |
} |
110 |
|
|
111 |
return success; |
return null; |
|
|
|
112 |
} |
} |
113 |
|
|
114 |
private String extractTrainNumber (String trainID) { |
private String extractTrainNumber (String trainID) { |