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

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

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

revision 1005 by torben, Mon Aug 2 23:03:11 2010 UTC revision 1031 by torben, Wed Sep 8 08:45:18 2010 UTC
# Line 20  public class XmlStationProvider implemen Line 20  public class XmlStationProvider implemen
20                    
21          final static int CACHE_TIMEOUT = 300*1000;          final static int CACHE_TIMEOUT = 300*1000;
22    
23          List<StationBean> stations = new ArrayList<StationBean>();                //List<StationBean> stations = new ArrayList<StationBean>();    
24          AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT);          AndroidTimeoutCache<String, List<StationBean>> stationCache = new AndroidTimeoutCache<String, List<StationBean>>(CACHE_TIMEOUT);
25                    
         @Override  
         public List<StationBean> getStations() {  
                 return stations;  
         }  
26    
27            double roundToPlaces(double value, int places) {
28                    double pow = Math.pow(10, places);
29                    double temp = Math.round( value*pow );
30                    
31                    return temp / pow;
32            }
33    
34          @Override          @Override
35          public boolean lookupStations(Location location) {          public List<StationBean> lookupStations(Location location) {
36                  String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + (float)location.getLatitude() + "&longitude=" + (float)location.getLongitude();                  double lat = roundToPlaces(location.getLatitude(), 4);
37                    double lng = roundToPlaces(location.getLongitude(), 4);
38                    
39                    String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng;
40                  Log.i("url", url);                  Log.i("url", url);
41                  return lookupStationsWorker(url);                  return lookupStationsWorker(url);
42          }          }
43                    
44          @Override          @Override
45          public boolean lookupStationsByName(String name) {                        public List<StationBean> lookupStationsByName(String name) {            
46                                    
47          //      String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);          //      String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + Uri.encode(name);
48                  String url = "";                  String url = "";
# Line 54  public class XmlStationProvider implemen Line 59  public class XmlStationProvider implemen
59                    
60                    
61          @Override          @Override
62          public boolean lookupStationsByIds(String ids) {          public List<StationBean> lookupStationsByIds(String ids) {
63                  String url = "";                  String url = "";
64                  url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;                  url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
65                                    
# Line 62  public class XmlStationProvider implemen Line 67  public class XmlStationProvider implemen
67                  return lookupStationsWorker(url);                  return lookupStationsWorker(url);
68          }          }
69                    
70          public boolean lookupStationsWorker(String url) {          public List<StationBean> lookupStationsWorker(String url) {
71                  boolean result;                  
72                  List<StationBean> tmpStations = stationCache.get(url);                  List<StationBean> tmpStations = stationCache.get(url);
73                                    
74                  if (tmpStations != null) {                  if (tmpStations != null) {
75                          Log.i("lookupStations", "cache hit " + url);                          Log.i("lookupStations", "cache hit " + url);
                         stations = tmpStations;  
                         result = true;  
76                  } else {                  } else {
77                          result = fetchStations(url);                          tmpStations = fetchStations(url);
78                                                    
79                          if (result == true) {                          if (tmpStations != null) {
80                                  stationCache.put(url, stations);                                  stationCache.put(url, tmpStations);
81                          }                          }
82                  }                  }
83                                    
84                  return result;                  return tmpStations;
85          }          }
86    
87    
88          public boolean fetchStations(String url) {          public List<StationBean> fetchStations(String url) {
                 boolean success = false;  
89    
90                  try {                  try {
91                          stations.clear();                          List<StationBean> stations = new ArrayList<StationBean>();
92                                                    
93                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");                          String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
94                                                    
# Line 144  public class XmlStationProvider implemen Line 146  public class XmlStationProvider implemen
146                                  }                                  }
147                                  stations.add(station);                                                            stations.add(station);                          
148                          }                          }
149                          success = true;                          return stations;
150                                                    
151                                                    
152                  } catch (Exception e) {                  } catch (Exception e) {
153                          Log.e("XmlStationProvider", "lookupStations: ", e);                          Log.e("XmlStationProvider", "lookupStations: ", e);
154                            return null;
155                  }                  }
                 return success;  
156          }          }
157  }  }

Legend:
Removed from v.1005  
changed lines
  Added in v.1031

  ViewVC Help
Powered by ViewVC 1.1.20