/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/provider/XmlDepartureProvider.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 320 by torben, Sat Sep 12 12:33:24 2009 UTC revision 390 by torben, Fri Oct 2 19:01:53 2009 UTC
# Line 2  package dk.thoerup.traininfo.provider; Line 2  package dk.thoerup.traininfo.provider;
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;
# Line 17  import org.xml.sax.helpers.DefaultHandle Line 15  import org.xml.sax.helpers.DefaultHandle
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          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;
# Line 65  public class XmlDepartureProvider extend Line 63  public class XmlDepartureProvider extend
63                  departures = new ArrayList<DepartureBean>();                  departures = new ArrayList<DepartureBean>();
64                  try                  try
65                  {                        {      
66                          //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;  
67                          String url = "http://app.t-hoerup.dk/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                                                    
# Line 89  public class XmlDepartureProvider extend Line 87  public class XmlDepartureProvider extend
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

Legend:
Removed from v.320  
changed lines
  Added in v.390

  ViewVC Help
Powered by ViewVC 1.1.20