/[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 1562 - (show 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 package dk.thoerup.traininfo.provider;
2
3 import java.io.EOFException;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.ObjectInputStream;
9 import java.io.ObjectOutputStream;
10 import java.net.URLEncoder;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.LinkedList;
14
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 import dk.thoerup.traininfo.util.DownloadUtil;
25 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 long start = System.currentTimeMillis();
35
36 stations.entries.clear(); //TODO: remove
37
38 File parent = context.getFilesDir();
39 File stationsFile = new File(parent, "stations.bin");
40
41 if (!stationsFile.exists())
42 return false;
43
44 /*int size = (int) stationsFile.length();
45 byte data[] = new byte[size];
46
47 RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");
48 raf.readFully(data);
49
50 Serializer serializer = new Persister();
51 stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/
52
53
54 try {
55 ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
56 Object o;
57 StationEntry e = null;
58 while ( (o=in.readObject()) != null ) {
59 e = (StationEntry) o;
60 updateSearchStrings(e);
61 stations.entries.add( e );
62 }
63 in.close();
64 } catch (EOFException e) {
65 //do nothing;
66 }
67
68 Log.e("OFFLINE", "loaded" + stations.entries.size());
69 logElapsedTime(start, "loadStations");
70
71 return true;
72 }
73
74 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 public void downloadStations(Context context) throws Exception {
80 File parent = context.getFilesDir();
81 File stationsFile = new File(parent, "stations.bin");
82
83
84 byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
85 Serializer serializer = new Persister();
86 stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );
87
88
89 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
90 Log.e("OFFLINE", "data size" + data.length);
91
92 for (StationEntry entry : stations.entries) {
93 updateSearchStrings( entry ); //prepare name fields for byName search
94 out.writeObject(entry);
95 }
96
97 out.close();
98 }
99
100
101
102 @Override
103 public void purgeOldEntries() {
104 }
105
106 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
119 @Override
120 public StationBean lookupStationsByLocation(Location location) {
121
122 statsByLocation(location);
123
124 long start = System.currentTimeMillis();
125 Location tmpLoc = new Location("GPS");
126
127 LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
128
129 for (StationEntry entry : stations.entries) {
130 tmpLoc.setLatitude(entry.getLatitude());
131 tmpLoc.setLongitude(entry.getLongitude());
132
133 int distance = (int) location.distanceTo(tmpLoc);
134
135 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 }
146
147 logElapsedTime(start, "location_stage1");
148
149 Collections.sort( entries, distanceComparator);
150
151 StationBean tmpStations = new StationBean();
152 for (int i = 0; i<8; i++) {
153 tmpStations.entries.add( entries.get(i) );
154 }
155
156 logElapsedTime(start, "location");
157 return tmpStations;
158 }
159
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
166 @Override
167 public StationBean lookupStationsByName(String name) {
168
169 long start = System.currentTimeMillis();
170
171 name = name.toLowerCase();
172 StationBean tmpStations = new StationBean();
173 for (StationEntry entry : stations.entries) {
174 if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
175 tmpStations.entries.add(entry);
176 }
177 }
178 logElapsedTime(start, "name");
179 return tmpStations;
180 }
181
182 @Override
183 public StationBean lookupStationsByIds(String ids) {
184 statsByIds(ids);
185
186 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
199
200 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 }

  ViewVC Help
Powered by ViewVC 1.1.20