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