1 |
package dk.thoerup.traininfo.provider; |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
import java.io.EOFException; |
|
3 |
import java.io.File; |
import java.io.File; |
4 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
5 |
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
6 |
|
import java.io.IOException; |
7 |
import java.io.ObjectInputStream; |
import java.io.ObjectInputStream; |
8 |
import java.io.ObjectOutputStream; |
import java.io.ObjectOutputStream; |
9 |
import java.util.ArrayList; |
import java.net.URLEncoder; |
10 |
import java.util.Collections; |
import java.util.Collections; |
11 |
import java.util.Comparator; |
import java.util.Comparator; |
12 |
|
import java.util.LinkedList; |
13 |
|
|
14 |
import org.simpleframework.xml.Serializer; |
import org.simpleframework.xml.Serializer; |
15 |
import org.simpleframework.xml.core.Persister; |
import org.simpleframework.xml.core.Persister; |
20 |
import dk.thoerup.android.traininfo.common.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
21 |
import dk.thoerup.android.traininfo.common.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
22 |
import dk.thoerup.genericjavautils.HttpUtil; |
import dk.thoerup.genericjavautils.HttpUtil; |
23 |
|
import dk.thoerup.traininfo.util.DownloadUtil; |
24 |
import dk.thoerup.traininfo.util.IntSet; |
import dk.thoerup.traininfo.util.IntSet; |
25 |
import dk.thoerup.traininfo.util.XmlUtil; |
import dk.thoerup.traininfo.util.XmlUtil; |
26 |
|
|
28 |
|
|
29 |
StationBean stations = new StationBean(); |
StationBean stations = new StationBean(); |
30 |
|
|
31 |
|
public boolean hasStations() { |
32 |
|
return (stations != null && stations.entries.size() > 0); |
33 |
|
} |
34 |
|
|
35 |
|
|
36 |
public boolean loadStations(Context context) throws Exception { |
public boolean loadStations(Context context) throws Exception { |
37 |
|
long start = System.currentTimeMillis(); |
38 |
|
|
39 |
stations.entries.clear(); //TODO: remove |
stations.entries.clear(); //TODO: remove |
40 |
|
|
41 |
File parent = context.getFilesDir(); |
File parent = context.getFilesDir(); |
44 |
if (!stationsFile.exists()) |
if (!stationsFile.exists()) |
45 |
return false; |
return false; |
46 |
|
|
47 |
int size = (int) stationsFile.length(); |
/*int size = (int) stationsFile.length(); |
48 |
/*byte data[] = new byte[size]; |
byte data[] = new byte[size]; |
49 |
|
|
50 |
RandomAccessFile raf = new RandomAccessFile(stationsFile, "r"); |
RandomAccessFile raf = new RandomAccessFile(stationsFile, "r"); |
51 |
raf.readFully(data); |
raf.readFully(data); |
54 |
stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/ |
stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/ |
55 |
|
|
56 |
|
|
57 |
try { |
|
58 |
ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); |
ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); |
59 |
Object o; |
|
60 |
while ( (o=in.readObject()) != null ) { |
int length = in.readInt(); // first field is the length |
61 |
stations.entries.add( (StationEntry) o); |
|
62 |
} |
for (int i=0; i<length; i++) { |
63 |
in.close(); |
StationEntry entry = (StationEntry) in.readObject(); |
64 |
} catch (EOFException e) { |
updateSearchStrings(entry); |
65 |
//do nothing; |
stations.entries.add( entry ); |
66 |
} |
} |
67 |
|
|
68 |
|
in.close(); |
69 |
|
|
70 |
|
|
71 |
Log.e("OFFLINE", "loaded" + stations.entries.size()); |
Log.e("OFFLINE", "loaded" + stations.entries.size()); |
72 |
|
logElapsedTime(start, "loadStations"); |
73 |
|
|
74 |
return true; |
return true; |
75 |
} |
} |
76 |
|
|
77 |
|
public void updateSearchStrings(StationEntry entry) { |
78 |
|
entry.nameLower = entry.getName().toLowerCase(); |
79 |
|
entry.nameInternational = entry.nameLower.replace("æ", "ae").replace("ø", "oe").replace("å", "aa"); |
80 |
|
} |
81 |
|
|
82 |
public void downloadStations(Context context) throws Exception { |
public void downloadStations(Context context) throws Exception { |
83 |
File parent = context.getFilesDir(); |
File parent = context.getFilesDir(); |
92 |
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) ); |
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) ); |
93 |
Log.e("OFFLINE", "data size" + data.length); |
Log.e("OFFLINE", "data size" + data.length); |
94 |
|
|
95 |
|
out.writeInt( stations.entries.size() ); //start with writing the length of the dataset |
96 |
|
|
97 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
98 |
|
updateSearchStrings( entry ); //prepare name fields for byName search |
99 |
out.writeObject(entry); |
out.writeObject(entry); |
100 |
} |
} |
101 |
|
|
108 |
public void purgeOldEntries() { |
public void purgeOldEntries() { |
109 |
} |
} |
110 |
|
|
111 |
|
Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() { |
112 |
|
@Override |
113 |
|
public int compare(StationEntry object1, StationEntry object2) { |
114 |
|
if (object1.getCalcdist() == object2.getCalcdist()) |
115 |
|
return 0; |
116 |
|
|
117 |
|
if (object1.getCalcdist() > object2.getCalcdist()) |
118 |
|
return 1; |
119 |
|
else |
120 |
|
return -1; |
121 |
|
} |
122 |
|
}; |
123 |
|
|
124 |
@Override |
@Override |
125 |
public StationBean lookupStationsByLocation(Location location) { |
public StationBean lookupStationsByLocation(Location location) { |
126 |
|
|
127 |
|
statsByLocation(location); |
128 |
|
|
129 |
|
long start = System.currentTimeMillis(); |
130 |
Location tmpLoc = new Location("GPS"); |
Location tmpLoc = new Location("GPS"); |
131 |
|
|
132 |
ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ; |
LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ; |
133 |
|
|
134 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
135 |
tmpLoc.setLatitude(entry.getLatitude()); |
tmpLoc.setLatitude(entry.getLatitude()); |
136 |
tmpLoc.setLongitude(entry.getLongitude()); |
tmpLoc.setLongitude(entry.getLongitude()); |
137 |
|
|
138 |
entry.setCalcdist( (int) location.distanceTo(tmpLoc) ); |
int distance = (int) location.distanceTo(tmpLoc); |
|
entries.add(entry); |
|
139 |
|
|
140 |
} |
if (entries.size() <8 || entries.getLast().getCalcdist() > distance) { |
141 |
|
entry.setCalcdist(distance); |
|
Collections.sort( entries, new Comparator<StationEntry>() { |
|
|
@Override |
|
|
public int compare(StationEntry object1, StationEntry object2) { |
|
|
if (object1.getCalcdist() == object2.getCalcdist()) |
|
|
return 0; |
|
142 |
|
|
143 |
if (object1.getCalcdist() > object2.getCalcdist()) |
if (entries.size() == 8) |
144 |
return 1; |
entries.removeLast(); |
145 |
else |
|
146 |
return -1; |
entries.addLast(entry); |
147 |
} |
|
148 |
|
Collections.sort( entries, distanceComparator); |
149 |
}); |
} |
150 |
|
} |
151 |
|
|
152 |
|
logElapsedTime(start, "location_stage1"); |
153 |
|
|
154 |
|
Collections.sort( entries, distanceComparator); |
155 |
|
|
156 |
StationBean tmpStations = new StationBean(); |
StationBean tmpStations = new StationBean(); |
157 |
for (int i = 0; i<8; i++) { |
for (int i = 0; i<8 && i<entries.size(); i++) { |
158 |
tmpStations.entries.add( entries.get(i) ); |
tmpStations.entries.add( entries.get(i) ); |
159 |
} |
} |
160 |
|
|
161 |
|
logElapsedTime(start, "location"); |
162 |
return tmpStations; |
return tmpStations; |
163 |
} |
} |
164 |
|
|
165 |
|
private void logElapsedTime(long start, String method) { |
166 |
|
long now = System.currentTimeMillis(); |
167 |
|
|
168 |
|
Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) ); |
169 |
|
} |
170 |
|
|
171 |
@Override |
@Override |
172 |
public StationBean lookupStationsByName(String name) { |
public StationBean lookupStationsByName(String name) { |
173 |
|
|
174 |
|
long start = System.currentTimeMillis(); |
175 |
|
|
176 |
name = name.toLowerCase(); |
name = name.toLowerCase(); |
177 |
StationBean tmpStations = new StationBean(); |
StationBean tmpStations = new StationBean(); |
178 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
179 |
if (entry.getName().toLowerCase().startsWith(name) ) { |
if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) { |
180 |
tmpStations.entries.add(entry); |
tmpStations.entries.add(entry); |
181 |
} |
} |
182 |
} |
} |
183 |
|
logElapsedTime(start, "name"); |
184 |
return tmpStations; |
return tmpStations; |
185 |
} |
} |
186 |
|
|
187 |
@Override |
@Override |
188 |
public StationBean lookupStationsByIds(String ids) { |
public StationBean lookupStationsByIds(String ids) { |
189 |
|
statsByIds(ids); |
190 |
|
|
191 |
IntSet idset = new IntSet(); |
IntSet idset = new IntSet(); |
192 |
idset.fromString(ids); |
idset.fromString(ids); |
193 |
|
|
198 |
} |
} |
199 |
} |
} |
200 |
|
|
201 |
|
return tmpStations; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
private void statsByLocation(Location location) { |
206 |
|
double lat = XmlStationProvider.roundToPlaces(location.getLatitude(), 4); |
207 |
|
double lng = XmlStationProvider.roundToPlaces(location.getLongitude(), 4); |
208 |
|
|
209 |
|
final String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng + "&dummy=1"; |
210 |
|
Log.i("url", url); |
211 |
|
urlSender(url); |
212 |
|
} |
213 |
|
|
214 |
|
private void statsByName(String name) { |
215 |
|
|
216 |
|
try { |
217 |
|
name = URLEncoder.encode(name, "ISO8859-1"); |
218 |
|
} catch (Exception e) { |
219 |
|
Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best |
220 |
|
} |
221 |
|
|
222 |
return tmpStations; |
String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name + "&dummy=1"; |
223 |
|
Log.i("url", url); |
224 |
|
urlSender(url); |
225 |
} |
} |
226 |
|
|
227 |
|
private void statsByIds(String ids) { |
228 |
|
final String url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids + "&dummy=1"; |
229 |
|
Log.i("url", url); |
230 |
|
urlSender(url); |
231 |
|
} |
232 |
|
|
233 |
|
private void urlSender(final String url) { |
234 |
|
Thread t = new Thread(new Runnable() { |
235 |
|
|
236 |
|
@Override |
237 |
|
public void run() { |
238 |
|
try { |
239 |
|
DownloadUtil.getContentString(url, 15000, "ISO-8859-1"); |
240 |
|
} catch (IOException e) { |
241 |
|
Log.e("TrainInfo", "stats failed"); |
242 |
|
} |
243 |
|
} |
244 |
|
}); |
245 |
|
t.start(); |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
} |
} |