4 |
import java.io.File; |
import java.io.File; |
5 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
6 |
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
7 |
|
import java.io.IOException; |
8 |
import java.io.ObjectInputStream; |
import java.io.ObjectInputStream; |
9 |
import java.io.ObjectOutputStream; |
import java.io.ObjectOutputStream; |
10 |
import java.util.ArrayList; |
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 |
stations.entries.clear(); //TODO: remove |
37 |
|
|
38 |
File parent = context.getFilesDir(); |
File parent = context.getFilesDir(); |
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); |
54 |
try { |
try { |
55 |
ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); |
ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); |
56 |
Object o; |
Object o; |
57 |
|
StationEntry e = null; |
58 |
while ( (o=in.readObject()) != null ) { |
while ( (o=in.readObject()) != null ) { |
59 |
stations.entries.add( (StationEntry) o); |
e = (StationEntry) o; |
60 |
|
e.updateSearch(); |
61 |
|
stations.entries.add( e ); |
62 |
} |
} |
63 |
in.close(); |
in.close(); |
64 |
} catch (EOFException e) { |
} catch (EOFException e) { |
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 |
} |
} |
75 |
File parent = context.getFilesDir(); |
File parent = context.getFilesDir(); |
76 |
File stationsFile = new File(parent, "stations.bin"); |
File stationsFile = new File(parent, "stations.bin"); |
77 |
|
|
78 |
|
|
79 |
byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000); |
byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000); |
80 |
Serializer serializer = new Persister(); |
Serializer serializer = new Persister(); |
81 |
stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") ); |
stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") ); |
85 |
Log.e("OFFLINE", "data size" + data.length); |
Log.e("OFFLINE", "data size" + data.length); |
86 |
|
|
87 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
88 |
|
entry.updateSearch(); //prepare name fields for byName search |
89 |
out.writeObject(entry); |
out.writeObject(entry); |
90 |
} |
} |
91 |
|
|
98 |
public void purgeOldEntries() { |
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 |
@Override |
115 |
public StationBean lookupStationsByLocation(Location location) { |
public StationBean lookupStationsByLocation(Location location) { |
116 |
|
|
117 |
|
statsByLocation(location); |
118 |
|
|
119 |
|
long start = System.currentTimeMillis(); |
120 |
Location tmpLoc = new Location("GPS"); |
Location tmpLoc = new Location("GPS"); |
121 |
|
|
122 |
ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ; |
LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ; |
123 |
|
|
124 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
125 |
tmpLoc.setLatitude(entry.getLatitude()); |
tmpLoc.setLatitude(entry.getLatitude()); |
126 |
tmpLoc.setLongitude(entry.getLongitude()); |
tmpLoc.setLongitude(entry.getLongitude()); |
127 |
|
|
128 |
entry.setCalcdist( (int) location.distanceTo(tmpLoc) ); |
int distance = (int) location.distanceTo(tmpLoc); |
|
entries.add(entry); |
|
129 |
|
|
130 |
} |
if (entries.size() <8 || entries.getLast().getCalcdist() > distance) { |
131 |
|
entry.setCalcdist(distance); |
|
Collections.sort( entries, new Comparator<StationEntry>() { |
|
|
@Override |
|
|
public int compare(StationEntry object1, StationEntry object2) { |
|
|
if (object1.getCalcdist() == object2.getCalcdist()) |
|
|
return 0; |
|
132 |
|
|
133 |
if (object1.getCalcdist() > object2.getCalcdist()) |
if (entries.size() == 8) |
134 |
return 1; |
entries.removeLast(); |
135 |
else |
|
136 |
return -1; |
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(); |
StationBean tmpStations = new StationBean(); |
147 |
for (int i = 0; i<8; i++) { |
for (int i = 0; i<8; i++) { |
148 |
tmpStations.entries.add( entries.get(i) ); |
tmpStations.entries.add( entries.get(i) ); |
149 |
} |
} |
150 |
|
|
151 |
|
logElapsedTime(start, "location"); |
152 |
return tmpStations; |
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 |
@Override |
162 |
public StationBean lookupStationsByName(String name) { |
public StationBean lookupStationsByName(String name) { |
163 |
|
|
164 |
|
long start = System.currentTimeMillis(); |
165 |
|
|
166 |
name = name.toLowerCase(); |
name = name.toLowerCase(); |
167 |
StationBean tmpStations = new StationBean(); |
StationBean tmpStations = new StationBean(); |
168 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
169 |
if (entry.getName().toLowerCase().startsWith(name) ) { |
if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) { |
170 |
tmpStations.entries.add(entry); |
tmpStations.entries.add(entry); |
171 |
} |
} |
172 |
} |
} |
173 |
|
logElapsedTime(start, "name"); |
174 |
return tmpStations; |
return tmpStations; |
175 |
} |
} |
176 |
|
|
177 |
@Override |
@Override |
178 |
public StationBean lookupStationsByIds(String ids) { |
public StationBean lookupStationsByIds(String ids) { |
179 |
|
statsByIds(ids); |
180 |
|
|
181 |
IntSet idset = new IntSet(); |
IntSet idset = new IntSet(); |
182 |
idset.fromString(ids); |
idset.fromString(ids); |
183 |
|
|
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 |
return tmpStations; |
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 |
} |
} |