/[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 318 by torben, Fri Sep 11 08:34:57 2009 UTC revision 319 by torben, Fri Sep 11 12:24: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;
5    import java.util.Collections;
6    import java.util.HashMap;
7  import java.util.List;  import java.util.List;
8    
9  import javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParser;
# Line 19  import dk.thoerup.traininfo.util.Downloa Line 21  import dk.thoerup.traininfo.util.Downloa
21    
22  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {  public class XmlDepartureProvider extends DefaultHandler implements DepartureProvider {
23    
24          ArrayList<DepartureBean> departures = new ArrayList<DepartureBean>();          final static long CACHE_TIMEOUT = 60*1000;
25                    
26            class CacheEntry {
27                    public long timestamp;
28                    public List<DepartureBean> departures;
29            }
30            
31            HashMap<Integer, CacheEntry> departureCache = new HashMap<Integer,CacheEntry>();        
32            ArrayList<DepartureBean> departures;
33                    
34                    
35          DepartureBean tempDeparture;          DepartureBean tempDeparture;
36          StringBuilder builder = new StringBuilder(512);          StringBuilder builder = new StringBuilder(512);
37                    
38          @Override          @Override
39          public void lookupDepartures(int stationID) {          public boolean lookupDepartures(int stationID) {
40                  departures.clear();                  CacheEntry entry = departureCache.get(stationID);
41                    boolean success;
42            
43                    long now = android.os.SystemClock.elapsedRealtime();
44                    if (entry == null || (entry.timestamp+CACHE_TIMEOUT) < now) {
45                            
46                            success = lookupDeparturesWorker(stationID);
47                            
48                            if (success) {
49                                    entry = new CacheEntry();
50                                    entry.timestamp = android.os.SystemClock.elapsedRealtime();
51                                    entry.departures = departures;
52                            
53                                    departureCache.put(stationID, entry);
54                            }
55                    } else {
56                            Log.i("XmlDepartureProvider", "cache hit !!!");
57                            success = true;
58                    }              
59                    
60                    return success;
61            }
62            
63            private boolean lookupDeparturesWorker(int stationID) {
64                    boolean success = false;
65                    departures = new ArrayList<DepartureBean>();
66                  try                  try
67                  {                        {      
68                          //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;                          //String url = "http://t-hoerup.dk/tog/xml_display.php?stationcode="+stationCode;
# Line 44  public class XmlDepartureProvider extend Line 79  public class XmlDepartureProvider extend
79                          xr.setContentHandler(this);                          xr.setContentHandler(this);
80                          xr.setErrorHandler(this);                          xr.setErrorHandler(this);
81                          xr.parse(source);                          xr.parse(source);
82                            success = true;
83                            
84                  } catch (Exception e) {                  } catch (Exception e) {
85                          Log.e("XmlDepartureProvider", "looupFunction", e);                          Log.e("XmlDepartureProvider", "looupFunction", e);
86                  }                  }
87                    return success;
88          }          }
89                    
90          @Override          @Override
91          public List<DepartureBean> getDepartures() {          public List<DepartureBean> getDepartures(int station) {
92                  return departures;                  CacheEntry entry = departureCache.get(station);
93                    
94                    if (entry != null) {                    
95                            return Collections.unmodifiableList(entry.departures);
96                    } else {
97                            return new ArrayList<DepartureBean>();
98                    }
99                    
100          }          }
101                    
102          // 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.318  
changed lines
  Added in v.319

  ViewVC Help
Powered by ViewVC 1.1.20