1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
3 |
|
import java.io.EOFException; |
4 |
import java.io.File; |
import java.io.File; |
5 |
|
import java.io.FileInputStream; |
6 |
|
import java.io.FileOutputStream; |
7 |
import java.io.IOException; |
import java.io.IOException; |
8 |
import java.io.RandomAccessFile; |
import java.io.ObjectInputStream; |
9 |
import java.util.ArrayList; |
import java.io.ObjectOutputStream; |
10 |
|
import java.net.URLEncoder; |
11 |
import java.util.Collections; |
import java.util.Collections; |
12 |
import java.util.Comparator; |
import java.util.Comparator; |
13 |
|
import java.util.LinkedList; |
14 |
|
|
15 |
import org.simpleframework.xml.Serializer; |
import org.simpleframework.xml.Serializer; |
16 |
import org.simpleframework.xml.core.Persister; |
import org.simpleframework.xml.core.Persister; |
21 |
import dk.thoerup.android.traininfo.common.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
22 |
import dk.thoerup.android.traininfo.common.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
23 |
import dk.thoerup.genericjavautils.HttpUtil; |
import dk.thoerup.genericjavautils.HttpUtil; |
24 |
|
import dk.thoerup.traininfo.util.DownloadUtil; |
25 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
26 |
import dk.thoerup.traininfo.util.XmlUtil; |
import dk.thoerup.traininfo.util.XmlUtil; |
27 |
|
|
31 |
|
|
32 |
|
|
33 |
public boolean loadStations(Context context) throws Exception { |
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(); |
File parent = context.getFilesDir(); |
39 |
File stationsFile = new File(parent, "stations.xml"); |
File stationsFile = new File(parent, "stations.bin"); |
40 |
|
|
41 |
if (!stationsFile.exists()) |
if (!stationsFile.exists()) |
42 |
return false; |
return false; |
43 |
|
|
44 |
int size = (int) stationsFile.length(); |
/*int size = (int) stationsFile.length(); |
45 |
byte data[] = new byte[size]; |
byte data[] = new byte[size]; |
46 |
|
|
47 |
RandomAccessFile raf = new RandomAccessFile(stationsFile, "r"); |
RandomAccessFile raf = new RandomAccessFile(stationsFile, "r"); |
48 |
raf.readFully(data); |
raf.readFully(data); |
49 |
|
|
50 |
Serializer serializer = new Persister(); |
Serializer serializer = new Persister(); |
51 |
stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") ); |
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()); |
Log.e("OFFLINE", "loaded" + stations.entries.size()); |
69 |
|
logElapsedTime(start, "loadStations"); |
70 |
|
|
71 |
return true; |
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 IOException { |
public void downloadStations(Context context) throws Exception { |
80 |
File parent = context.getFilesDir(); |
File parent = context.getFilesDir(); |
81 |
File stationsFile = new File(parent, "stations.xml"); |
File stationsFile = new File(parent, "stations.bin"); |
82 |
|
|
83 |
|
|
84 |
byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000); |
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 |
RandomAccessFile raf = new RandomAccessFile(stationsFile, "rw"); |
|
89 |
|
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) ); |
90 |
Log.e("OFFLINE", "data size" + data.length); |
Log.e("OFFLINE", "data size" + data.length); |
91 |
raf.setLength(0); //truncate |
|
92 |
raf.write(data); |
for (StationEntry entry : stations.entries) { |
93 |
raf.close(); |
updateSearchStrings( entry ); //prepare name fields for byName search |
94 |
|
out.writeObject(entry); |
95 |
|
} |
96 |
|
|
97 |
|
out.close(); |
98 |
} |
} |
99 |
|
|
100 |
|
|
103 |
public void purgeOldEntries() { |
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 |
@Override |
120 |
public StationBean lookupStationsByLocation(Location location) { |
public StationBean lookupStationsByLocation(Location location) { |
121 |
|
|
122 |
|
statsByLocation(location); |
123 |
|
|
124 |
|
long start = System.currentTimeMillis(); |
125 |
Location tmpLoc = new Location("GPS"); |
Location tmpLoc = new Location("GPS"); |
126 |
|
|
127 |
ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ; |
LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ; |
128 |
|
|
129 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
130 |
tmpLoc.setLatitude(entry.getLatitude()); |
tmpLoc.setLatitude(entry.getLatitude()); |
131 |
tmpLoc.setLongitude(entry.getLongitude()); |
tmpLoc.setLongitude(entry.getLongitude()); |
132 |
|
|
133 |
entry.setCalcdist( (int) location.distanceTo(tmpLoc) ); |
int distance = (int) location.distanceTo(tmpLoc); |
|
entries.add(entry); |
|
134 |
|
|
135 |
} |
if (entries.size() <8 || entries.getLast().getCalcdist() > distance) { |
136 |
|
entry.setCalcdist(distance); |
|
Collections.sort( entries, new Comparator<StationEntry>() { |
|
|
@Override |
|
|
public int compare(StationEntry object1, StationEntry object2) { |
|
|
if (object1.getCalcdist() == object2.getCalcdist()) |
|
|
return 0; |
|
137 |
|
|
138 |
if (object1.getCalcdist() > object2.getCalcdist()) |
if (entries.size() == 8) |
139 |
return 1; |
entries.removeLast(); |
140 |
else |
|
141 |
return -1; |
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(); |
StationBean tmpStations = new StationBean(); |
152 |
for (int i = 0; i<8; i++) { |
for (int i = 0; i<8; i++) { |
153 |
tmpStations.entries.add( entries.get(i) ); |
tmpStations.entries.add( entries.get(i) ); |
154 |
} |
} |
155 |
|
|
156 |
|
logElapsedTime(start, "location"); |
157 |
return tmpStations; |
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 |
@Override |
167 |
public StationBean lookupStationsByName(String name) { |
public StationBean lookupStationsByName(String name) { |
168 |
|
|
169 |
|
long start = System.currentTimeMillis(); |
170 |
|
|
171 |
name = name.toLowerCase(); |
name = name.toLowerCase(); |
172 |
StationBean tmpStations = new StationBean(); |
StationBean tmpStations = new StationBean(); |
173 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
174 |
if (entry.getName().toLowerCase().startsWith(name) ) { |
if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) { |
175 |
tmpStations.entries.add(entry); |
tmpStations.entries.add(entry); |
176 |
} |
} |
177 |
} |
} |
178 |
|
logElapsedTime(start, "name"); |
179 |
return tmpStations; |
return tmpStations; |
180 |
} |
} |
181 |
|
|
182 |
@Override |
@Override |
183 |
public StationBean lookupStationsByIds(String ids) { |
public StationBean lookupStationsByIds(String ids) { |
184 |
|
statsByIds(ids); |
185 |
|
|
186 |
IntSet idset = new IntSet(); |
IntSet idset = new IntSet(); |
187 |
idset.fromString(ids); |
idset.fromString(ids); |
188 |
|
|
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 |
return tmpStations; |
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 |
} |
} |