/[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 1568 - (show annotations) (download)
Sat Jul 9 07:26:15 2011 UTC (12 years, 10 months ago) by torben
File size: 6649 byte(s)
Actually keep track of how many records there are in the client side cache
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
55 ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
56
57 int length = in.readInt(); // first field is the length
58
59 for (int i=0; i<length; i++) {
60 StationEntry entry = (StationEntry) in.readObject();
61 updateSearchStrings(entry);
62 stations.entries.add( entry );
63 }
64
65 in.close();
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 out.writeInt( stations.entries.size() ); //start with writing the length of the dataset
93
94 for (StationEntry entry : stations.entries) {
95 updateSearchStrings( entry ); //prepare name fields for byName search
96 out.writeObject(entry);
97 }
98
99 out.close();
100 }
101
102
103
104 @Override
105 public void purgeOldEntries() {
106 }
107
108 Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() {
109 @Override
110 public int compare(StationEntry object1, StationEntry object2) {
111 if (object1.getCalcdist() == object2.getCalcdist())
112 return 0;
113
114 if (object1.getCalcdist() > object2.getCalcdist())
115 return 1;
116 else
117 return -1;
118 }
119 };
120
121 @Override
122 public StationBean lookupStationsByLocation(Location location) {
123
124 statsByLocation(location);
125
126 long start = System.currentTimeMillis();
127 Location tmpLoc = new Location("GPS");
128
129 LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
130
131 for (StationEntry entry : stations.entries) {
132 tmpLoc.setLatitude(entry.getLatitude());
133 tmpLoc.setLongitude(entry.getLongitude());
134
135 int distance = (int) location.distanceTo(tmpLoc);
136
137 if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
138 entry.setCalcdist(distance);
139
140 if (entries.size() == 8)
141 entries.removeLast();
142
143 entries.addLast(entry);
144
145 Collections.sort( entries, distanceComparator);
146 }
147 }
148
149 logElapsedTime(start, "location_stage1");
150
151 Collections.sort( entries, distanceComparator);
152
153 StationBean tmpStations = new StationBean();
154 for (int i = 0; i<8; i++) {
155 tmpStations.entries.add( entries.get(i) );
156 }
157
158 logElapsedTime(start, "location");
159 return tmpStations;
160 }
161
162 private void logElapsedTime(long start, String method) {
163 long now = System.currentTimeMillis();
164
165 Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) );
166 }
167
168 @Override
169 public StationBean lookupStationsByName(String name) {
170
171 long start = System.currentTimeMillis();
172
173 name = name.toLowerCase();
174 StationBean tmpStations = new StationBean();
175 for (StationEntry entry : stations.entries) {
176 if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
177 tmpStations.entries.add(entry);
178 }
179 }
180 logElapsedTime(start, "name");
181 return tmpStations;
182 }
183
184 @Override
185 public StationBean lookupStationsByIds(String ids) {
186 statsByIds(ids);
187
188 IntSet idset = new IntSet();
189 idset.fromString(ids);
190
191 StationBean tmpStations = new StationBean();
192 for (StationEntry entry : stations.entries) {
193 if (idset.contains( entry.getId() ) ) {
194 tmpStations.entries.add(entry);
195 }
196 }
197
198 return tmpStations;
199 }
200
201
202 private void statsByLocation(Location location) {
203 double lat = XmlStationProvider.roundToPlaces(location.getLatitude(), 4);
204 double lng = XmlStationProvider.roundToPlaces(location.getLongitude(), 4);
205
206 final String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng + "&dummy=1";
207 Log.i("url", url);
208 urlSender(url);
209 }
210
211 private void statsByName(String name) {
212
213 try {
214 name = URLEncoder.encode(name, "ISO8859-1");
215 } catch (Exception e) {
216 Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best
217 }
218
219 String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name + "&dummy=1";
220 Log.i("url", url);
221 urlSender(url);
222 }
223
224 private void statsByIds(String ids) {
225 final String url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids + "&dummy=1";
226 Log.i("url", url);
227 urlSender(url);
228 }
229
230 private void urlSender(final String url) {
231 Thread t = new Thread(new Runnable() {
232
233 @Override
234 public void run() {
235 try {
236 DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
237 } catch (IOException e) {
238 Log.e("TrainInfo", "stats failed");
239 }
240 }
241 });
242 t.start();
243 }
244
245
246 }

  ViewVC Help
Powered by ViewVC 1.1.20