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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1160 - (hide annotations) (download)
Mon Oct 4 08:42:12 2010 UTC (13 years, 7 months ago) by torben
File size: 2585 byte(s)
Make cache cleanup code a little more elegant
1 torben 294 package dk.thoerup.traininfo.provider;
2    
3 torben 391
4 torben 383 import java.net.URLEncoder;
5 torben 294
6 torben 1066 import org.simpleframework.xml.Serializer;
7     import org.simpleframework.xml.core.Persister;
8 torben 294
9 torben 1077
10 torben 294 import android.location.Location;
11     import android.util.Log;
12 torben 1066 import dk.thoerup.android.traininfo.common.StationBean;
13 torben 1004 import dk.thoerup.traininfo.util.AndroidTimeoutCache;
14 torben 294 import dk.thoerup.traininfo.util.DownloadUtil;
15 torben 352 import dk.thoerup.traininfo.util.XmlUtil;
16 torben 294
17     public class XmlStationProvider implements StationProvider {
18 torben 1004
19     final static int CACHE_TIMEOUT = 300*1000;
20 torben 294
21 torben 1079
22 torben 1066 AndroidTimeoutCache<String, StationBean> stationCache = new AndroidTimeoutCache<String, StationBean>(CACHE_TIMEOUT);
23 torben 294
24    
25 torben 1031 double roundToPlaces(double value, int places) {
26     double pow = Math.pow(10, places);
27     double temp = Math.round( value*pow );
28    
29     return temp / pow;
30     }
31 torben 381
32 torben 294 @Override
33 torben 1066 public StationBean lookupStations(Location location) {
34 torben 1031 double lat = roundToPlaces(location.getLatitude(), 4);
35     double lng = roundToPlaces(location.getLongitude(), 4);
36    
37     String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng;
38 torben 294 Log.i("url", url);
39 torben 381 return lookupStationsWorker(url);
40     }
41    
42     @Override
43 torben 1066 public StationBean lookupStationsByName(String name) {
44 torben 383
45     try {
46 torben 1077 name = URLEncoder.encode(name, "ISO8859-1");
47 torben 383 } catch (Exception e) {
48 torben 1077 Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best
49 torben 383 }
50    
51 torben 1077 String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name;
52    
53    
54 torben 381 Log.i("url", url);
55     return lookupStationsWorker(url);
56     }
57    
58    
59 torben 433 @Override
60 torben 1066 public StationBean lookupStationsByIds(String ids) {
61 torben 433 String url = "";
62     url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids;
63    
64     Log.i("url", url);
65     return lookupStationsWorker(url);
66     }
67 torben 1004
68 torben 1066 public StationBean lookupStationsWorker(String url) {
69 torben 1006
70 torben 1066 StationBean tmpStations = stationCache.get(url);
71 torben 1004
72     if (tmpStations != null) {
73     Log.i("lookupStations", "cache hit " + url);
74     } else {
75 torben 1006 tmpStations = fetchStations(url);
76 torben 1004
77 torben 1006 if (tmpStations != null) {
78     stationCache.put(url, tmpStations);
79 torben 1004 }
80     }
81    
82 torben 1006 return tmpStations;
83 torben 1004 }
84 torben 433
85    
86 torben 1066 public StationBean fetchStations(String url) {
87 torben 381
88 torben 294 try {
89    
90 torben 302 String xml = DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
91 torben 294
92 torben 1066 Serializer serializer = new Persister();
93    
94     StationBean stations = serializer.read(StationBean.class, xml);
95 torben 294
96 torben 1006 return stations;
97 torben 294
98     } catch (Exception e) {
99     Log.e("XmlStationProvider", "lookupStations: ", e);
100 torben 1006 return null;
101 torben 294 }
102     }
103 torben 1160
104     @Override
105     public void purgeOldEntries() {
106     stationCache.purgeOldEntries();
107     }
108 torben 294 }

  ViewVC Help
Powered by ViewVC 1.1.20