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

  ViewVC Help
Powered by ViewVC 1.1.20