/[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 1572 - (hide annotations) (download)
Sat Jul 9 09:45:40 2011 UTC (12 years, 10 months ago) by torben
File size: 6717 byte(s)
Make sure we actually have a stationlist

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

  ViewVC Help
Powered by ViewVC 1.1.20