/[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 1562 - (hide annotations) (download)
Fri Jul 8 16:26:09 2011 UTC (12 years, 10 months ago) by torben
File size: 6558 byte(s)
Flesh out all logic from TrainInfoCommon so it just contains simple beans
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 torben 1562 updateSearchStrings(e);
61 torben 1553 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 torben 1562 }
73 torben 1545
74 torben 1562 public void updateSearchStrings(StationEntry entry) {
75     entry.nameLower = entry.getName().toLowerCase();
76     entry.nameInternational = entry.nameLower.replace("æ", "ae").replace("ø", "oe").replace("å", "aa");
77     }
78    
79 torben 1546 public void downloadStations(Context context) throws Exception {
80 torben 1545 File parent = context.getFilesDir();
81 torben 1546 File stationsFile = new File(parent, "stations.bin");
82 torben 1545
83 torben 1547
84 torben 1545 byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
85 torben 1546 Serializer serializer = new Persister();
86     stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );
87 torben 1545
88 torben 1546
89     ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
90 torben 1545 Log.e("OFFLINE", "data size" + data.length);
91 torben 1546
92     for (StationEntry entry : stations.entries) {
93 torben 1562 updateSearchStrings( entry ); //prepare name fields for byName search
94 torben 1546 out.writeObject(entry);
95     }
96    
97     out.close();
98 torben 1545 }
99    
100    
101    
102     @Override
103     public void purgeOldEntries() {
104     }
105    
106 torben 1553 Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() {
107     @Override
108     public int compare(StationEntry object1, StationEntry object2) {
109     if (object1.getCalcdist() == object2.getCalcdist())
110     return 0;
111    
112     if (object1.getCalcdist() > object2.getCalcdist())
113     return 1;
114     else
115     return -1;
116     }
117     };
118 torben 1545
119     @Override
120     public StationBean lookupStationsByLocation(Location location) {
121    
122 torben 1559 statsByLocation(location);
123    
124 torben 1553 long start = System.currentTimeMillis();
125 torben 1545 Location tmpLoc = new Location("GPS");
126    
127 torben 1553 LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
128 torben 1545
129     for (StationEntry entry : stations.entries) {
130     tmpLoc.setLatitude(entry.getLatitude());
131     tmpLoc.setLongitude(entry.getLongitude());
132    
133 torben 1553 int distance = (int) location.distanceTo(tmpLoc);
134 torben 1545
135 torben 1553 if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
136     entry.setCalcdist(distance);
137    
138     if (entries.size() == 8)
139     entries.removeLast();
140    
141     entries.addLast(entry);
142    
143     Collections.sort( entries, distanceComparator);
144     }
145 torben 1545 }
146    
147 torben 1553 logElapsedTime(start, "location_stage1");
148 torben 1545
149 torben 1553 Collections.sort( entries, distanceComparator);
150 torben 1545
151     StationBean tmpStations = new StationBean();
152     for (int i = 0; i<8; i++) {
153     tmpStations.entries.add( entries.get(i) );
154     }
155    
156 torben 1553 logElapsedTime(start, "location");
157 torben 1545 return tmpStations;
158     }
159 torben 1553
160     private void logElapsedTime(long start, String method) {
161     long now = System.currentTimeMillis();
162    
163     Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) );
164     }
165 torben 1545
166     @Override
167     public StationBean lookupStationsByName(String name) {
168    
169 torben 1553 long start = System.currentTimeMillis();
170    
171 torben 1545 name = name.toLowerCase();
172     StationBean tmpStations = new StationBean();
173     for (StationEntry entry : stations.entries) {
174 torben 1553 if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
175 torben 1545 tmpStations.entries.add(entry);
176     }
177     }
178 torben 1553 logElapsedTime(start, "name");
179 torben 1545 return tmpStations;
180     }
181    
182     @Override
183     public StationBean lookupStationsByIds(String ids) {
184 torben 1559 statsByIds(ids);
185    
186 torben 1545 IntSet idset = new IntSet();
187     idset.fromString(ids);
188    
189     StationBean tmpStations = new StationBean();
190     for (StationEntry entry : stations.entries) {
191     if (idset.contains( entry.getId() ) ) {
192     tmpStations.entries.add(entry);
193     }
194     }
195    
196     return tmpStations;
197     }
198 torben 1559
199 torben 1545
200 torben 1559 private void statsByLocation(Location location) {
201     double lat = XmlStationProvider.roundToPlaces(location.getLatitude(), 4);
202     double lng = XmlStationProvider.roundToPlaces(location.getLongitude(), 4);
203    
204     final String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng + "&dummy=1";
205     Log.i("url", url);
206     urlSender(url);
207     }
208    
209     private void statsByName(String name) {
210    
211     try {
212     name = URLEncoder.encode(name, "ISO8859-1");
213     } catch (Exception e) {
214     Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best
215     }
216    
217     String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name + "&dummy=1";
218     Log.i("url", url);
219     urlSender(url);
220     }
221    
222     private void statsByIds(String ids) {
223     final String url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids + "&dummy=1";
224     Log.i("url", url);
225     urlSender(url);
226     }
227    
228     private void urlSender(final String url) {
229     Thread t = new Thread(new Runnable() {
230    
231     @Override
232     public void run() {
233     try {
234     DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
235     } catch (IOException e) {
236     Log.e("TrainInfo", "stats failed");
237     }
238     }
239     });
240     t.start();
241     }
242    
243    
244 torben 1545 }

  ViewVC Help
Powered by ViewVC 1.1.20