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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1797 - (show annotations) (download)
Mon Apr 16 14:19:48 2012 UTC (12 years, 1 month ago) by torben
File size: 7498 byte(s)
Only use aliases if it wasn't null (prevent NPE)

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

  ViewVC Help
Powered by ViewVC 1.1.20