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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1559 - (hide annotations) (download)
Fri Jul 8 13:58:21 2011 UTC (12 years, 10 months ago) by torben
File size: 6327 byte(s)
add background statistics package senders
1 torben 1545 package dk.thoerup.traininfo.provider;
2    
3 torben 1546 import java.io.EOFException;
4 torben 1545 import java.io.File;
5 torben 1546 import java.io.FileInputStream;
6     import java.io.FileOutputStream;
7 torben 1559 import java.io.IOException;
8 torben 1546 import java.io.ObjectInputStream;
9     import java.io.ObjectOutputStream;
10 torben 1559 import java.net.URLEncoder;
11 torben 1545 import java.util.Collections;
12     import java.util.Comparator;
13 torben 1553 import java.util.LinkedList;
14 torben 1545
15     import org.simpleframework.xml.Serializer;
16     import org.simpleframework.xml.core.Persister;
17    
18     import android.content.Context;
19     import android.location.Location;
20     import android.util.Log;
21     import dk.thoerup.android.traininfo.common.StationBean;
22     import dk.thoerup.android.traininfo.common.StationEntry;
23     import dk.thoerup.genericjavautils.HttpUtil;
24 torben 1559 import dk.thoerup.traininfo.util.DownloadUtil;
25 torben 1545 import dk.thoerup.traininfo.util.IntSet;
26     import dk.thoerup.traininfo.util.XmlUtil;
27    
28     public class OfflineStationProvider implements StationProvider {
29    
30     StationBean stations = new StationBean();
31    
32    
33     public boolean loadStations(Context context) throws Exception {
34 torben 1553 long start = System.currentTimeMillis();
35    
36 torben 1546 stations.entries.clear(); //TODO: remove
37    
38 torben 1545 File parent = context.getFilesDir();
39 torben 1546 File stationsFile = new File(parent, "stations.bin");
40 torben 1545
41     if (!stationsFile.exists())
42     return false;
43    
44 torben 1551 /*int size = (int) stationsFile.length();
45     byte data[] = new byte[size];
46 torben 1545
47     RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");
48     raf.readFully(data);
49    
50     Serializer serializer = new Persister();
51 torben 1546 stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/
52 torben 1545
53 torben 1546
54     try {
55     ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
56     Object o;
57 torben 1553 StationEntry e = null;
58 torben 1546 while ( (o=in.readObject()) != null ) {
59 torben 1553 e = (StationEntry) o;
60     e.updateSearch();
61     stations.entries.add( e );
62 torben 1546 }
63     in.close();
64     } catch (EOFException e) {
65     //do nothing;
66     }
67    
68 torben 1545 Log.e("OFFLINE", "loaded" + stations.entries.size());
69 torben 1553 logElapsedTime(start, "loadStations");
70 torben 1545
71     return true;
72     }
73    
74 torben 1546 public void downloadStations(Context context) throws Exception {
75 torben 1545 File parent = context.getFilesDir();
76 torben 1546 File stationsFile = new File(parent, "stations.bin");
77 torben 1545
78 torben 1547
79 torben 1545 byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
80 torben 1546 Serializer serializer = new Persister();
81     stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );
82 torben 1545
83 torben 1546
84     ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
85 torben 1545 Log.e("OFFLINE", "data size" + data.length);
86 torben 1546
87     for (StationEntry entry : stations.entries) {
88 torben 1556 entry.updateSearch(); //prepare name fields for byName search
89 torben 1546 out.writeObject(entry);
90     }
91    
92     out.close();
93 torben 1545 }
94    
95    
96    
97     @Override
98     public void purgeOldEntries() {
99     }
100    
101 torben 1553 Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() {
102     @Override
103     public int compare(StationEntry object1, StationEntry object2) {
104     if (object1.getCalcdist() == object2.getCalcdist())
105     return 0;
106    
107     if (object1.getCalcdist() > object2.getCalcdist())
108     return 1;
109     else
110     return -1;
111     }
112     };
113 torben 1545
114     @Override
115     public StationBean lookupStationsByLocation(Location location) {
116    
117 torben 1559 statsByLocation(location);
118    
119 torben 1553 long start = System.currentTimeMillis();
120 torben 1545 Location tmpLoc = new Location("GPS");
121    
122 torben 1553 LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
123 torben 1545
124     for (StationEntry entry : stations.entries) {
125     tmpLoc.setLatitude(entry.getLatitude());
126     tmpLoc.setLongitude(entry.getLongitude());
127    
128 torben 1553 int distance = (int) location.distanceTo(tmpLoc);
129 torben 1545
130 torben 1553 if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
131     entry.setCalcdist(distance);
132    
133     if (entries.size() == 8)
134     entries.removeLast();
135    
136     entries.addLast(entry);
137    
138     Collections.sort( entries, distanceComparator);
139     }
140 torben 1545 }
141    
142 torben 1553 logElapsedTime(start, "location_stage1");
143 torben 1545
144 torben 1553 Collections.sort( entries, distanceComparator);
145 torben 1545
146     StationBean tmpStations = new StationBean();
147     for (int i = 0; i<8; i++) {
148     tmpStations.entries.add( entries.get(i) );
149     }
150    
151 torben 1553 logElapsedTime(start, "location");
152 torben 1545 return tmpStations;
153     }
154 torben 1553
155     private void logElapsedTime(long start, String method) {
156     long now = System.currentTimeMillis();
157    
158     Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) );
159     }
160 torben 1545
161     @Override
162     public StationBean lookupStationsByName(String name) {
163    
164 torben 1553 long start = System.currentTimeMillis();
165    
166 torben 1545 name = name.toLowerCase();
167     StationBean tmpStations = new StationBean();
168     for (StationEntry entry : stations.entries) {
169 torben 1553 if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
170 torben 1545 tmpStations.entries.add(entry);
171     }
172     }
173 torben 1553 logElapsedTime(start, "name");
174 torben 1545 return tmpStations;
175     }
176    
177     @Override
178     public StationBean lookupStationsByIds(String ids) {
179 torben 1559 statsByIds(ids);
180    
181 torben 1545 IntSet idset = new IntSet();
182     idset.fromString(ids);
183    
184     StationBean tmpStations = new StationBean();
185     for (StationEntry entry : stations.entries) {
186     if (idset.contains( entry.getId() ) ) {
187     tmpStations.entries.add(entry);
188     }
189     }
190    
191     return tmpStations;
192     }
193 torben 1559
194 torben 1545
195 torben 1559 private void statsByLocation(Location location) {
196     double lat = XmlStationProvider.roundToPlaces(location.getLatitude(), 4);
197     double lng = XmlStationProvider.roundToPlaces(location.getLongitude(), 4);
198    
199     final String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng + "&dummy=1";
200     Log.i("url", url);
201     urlSender(url);
202     }
203    
204     private void statsByName(String name) {
205    
206     try {
207     name = URLEncoder.encode(name, "ISO8859-1");
208     } catch (Exception e) {
209     Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best
210     }
211    
212     String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name + "&dummy=1";
213     Log.i("url", url);
214     urlSender(url);
215     }
216    
217     private void statsByIds(String ids) {
218     final String url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids + "&dummy=1";
219     Log.i("url", url);
220     urlSender(url);
221     }
222    
223     private void urlSender(final String url) {
224     Thread t = new Thread(new Runnable() {
225    
226     @Override
227     public void run() {
228     try {
229     DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
230     } catch (IOException e) {
231     Log.e("TrainInfo", "stats failed");
232     }
233     }
234     });
235     t.start();
236     }
237    
238    
239 torben 1545 }

  ViewVC Help
Powered by ViewVC 1.1.20