/[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 1559 - (show annotations) (download)
Fri Jul 8 13:58:21 2011 UTC (12 years, 10 months ago) by torben
File size: 6327 byte(s)
add background statistics package senders
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 e.updateSearch();
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 downloadStations(Context context) throws Exception {
75 File parent = context.getFilesDir();
76 File stationsFile = new File(parent, "stations.bin");
77
78
79 byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
80 Serializer serializer = new Persister();
81 stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );
82
83
84 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
85 Log.e("OFFLINE", "data size" + data.length);
86
87 for (StationEntry entry : stations.entries) {
88 entry.updateSearch(); //prepare name fields for byName search
89 out.writeObject(entry);
90 }
91
92 out.close();
93 }
94
95
96
97 @Override
98 public void purgeOldEntries() {
99 }
100
101 Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() {
102 @Override
103 public int compare(StationEntry object1, StationEntry object2) {
104 if (object1.getCalcdist() == object2.getCalcdist())
105 return 0;
106
107 if (object1.getCalcdist() > object2.getCalcdist())
108 return 1;
109 else
110 return -1;
111 }
112 };
113
114 @Override
115 public StationBean lookupStationsByLocation(Location location) {
116
117 statsByLocation(location);
118
119 long start = System.currentTimeMillis();
120 Location tmpLoc = new Location("GPS");
121
122 LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
123
124 for (StationEntry entry : stations.entries) {
125 tmpLoc.setLatitude(entry.getLatitude());
126 tmpLoc.setLongitude(entry.getLongitude());
127
128 int distance = (int) location.distanceTo(tmpLoc);
129
130 if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
131 entry.setCalcdist(distance);
132
133 if (entries.size() == 8)
134 entries.removeLast();
135
136 entries.addLast(entry);
137
138 Collections.sort( entries, distanceComparator);
139 }
140 }
141
142 logElapsedTime(start, "location_stage1");
143
144 Collections.sort( entries, distanceComparator);
145
146 StationBean tmpStations = new StationBean();
147 for (int i = 0; i<8; i++) {
148 tmpStations.entries.add( entries.get(i) );
149 }
150
151 logElapsedTime(start, "location");
152 return tmpStations;
153 }
154
155 private void logElapsedTime(long start, String method) {
156 long now = System.currentTimeMillis();
157
158 Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) );
159 }
160
161 @Override
162 public StationBean lookupStationsByName(String name) {
163
164 long start = System.currentTimeMillis();
165
166 name = name.toLowerCase();
167 StationBean tmpStations = new StationBean();
168 for (StationEntry entry : stations.entries) {
169 if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
170 tmpStations.entries.add(entry);
171 }
172 }
173 logElapsedTime(start, "name");
174 return tmpStations;
175 }
176
177 @Override
178 public StationBean lookupStationsByIds(String ids) {
179 statsByIds(ids);
180
181 IntSet idset = new IntSet();
182 idset.fromString(ids);
183
184 StationBean tmpStations = new StationBean();
185 for (StationEntry entry : stations.entries) {
186 if (idset.contains( entry.getId() ) ) {
187 tmpStations.entries.add(entry);
188 }
189 }
190
191 return tmpStations;
192 }
193
194
195 private void statsByLocation(Location location) {
196 double lat = XmlStationProvider.roundToPlaces(location.getLatitude(), 4);
197 double lng = XmlStationProvider.roundToPlaces(location.getLongitude(), 4);
198
199 final String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng + "&dummy=1";
200 Log.i("url", url);
201 urlSender(url);
202 }
203
204 private void statsByName(String name) {
205
206 try {
207 name = URLEncoder.encode(name, "ISO8859-1");
208 } catch (Exception e) {
209 Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best
210 }
211
212 String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name + "&dummy=1";
213 Log.i("url", url);
214 urlSender(url);
215 }
216
217 private void statsByIds(String ids) {
218 final String url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids + "&dummy=1";
219 Log.i("url", url);
220 urlSender(url);
221 }
222
223 private void urlSender(final String url) {
224 Thread t = new Thread(new Runnable() {
225
226 @Override
227 public void run() {
228 try {
229 DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
230 } catch (IOException e) {
231 Log.e("TrainInfo", "stats failed");
232 }
233 }
234 });
235 t.start();
236 }
237
238
239 }

  ViewVC Help
Powered by ViewVC 1.1.20