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