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.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; |
import java.util.LinkedList; |
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 |
|
|
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) ); |
ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); |
56 |
Object o; |
|
57 |
StationEntry e = null; |
int length = in.readInt(); // first field is the length |
58 |
while ( (o=in.readObject()) != null ) { |
|
59 |
e = (StationEntry) o; |
for (int i=0; i<length; i++) { |
60 |
e.updateSearch(); |
StationEntry entry = (StationEntry) in.readObject(); |
61 |
stations.entries.add( e ); |
updateSearchStrings(entry); |
62 |
} |
stations.entries.add( entry ); |
|
in.close(); |
|
|
} catch (EOFException e) { |
|
|
//do nothing; |
|
63 |
} |
} |
64 |
|
|
65 |
|
in.close(); |
66 |
|
|
67 |
|
|
68 |
Log.e("OFFLINE", "loaded" + stations.entries.size()); |
Log.e("OFFLINE", "loaded" + stations.entries.size()); |
69 |
logElapsedTime(start, "loadStations"); |
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 Exception { |
public void downloadStations(Context context) throws Exception { |
80 |
File parent = context.getFilesDir(); |
File parent = context.getFilesDir(); |
89 |
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) ); |
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) ); |
90 |
Log.e("OFFLINE", "data size" + data.length); |
Log.e("OFFLINE", "data size" + data.length); |
91 |
|
|
92 |
|
out.writeInt( stations.entries.size() ); //start with writing the length of the dataset |
93 |
|
|
94 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
95 |
|
updateSearchStrings( entry ); //prepare name fields for byName search |
96 |
out.writeObject(entry); |
out.writeObject(entry); |
97 |
} |
} |
98 |
|
|
121 |
@Override |
@Override |
122 |
public StationBean lookupStationsByLocation(Location location) { |
public StationBean lookupStationsByLocation(Location location) { |
123 |
|
|
124 |
|
statsByLocation(location); |
125 |
|
|
126 |
long start = System.currentTimeMillis(); |
long start = System.currentTimeMillis(); |
127 |
Location tmpLoc = new Location("GPS"); |
Location tmpLoc = new Location("GPS"); |
128 |
|
|
183 |
|
|
184 |
@Override |
@Override |
185 |
public StationBean lookupStationsByIds(String ids) { |
public StationBean lookupStationsByIds(String ids) { |
186 |
|
statsByIds(ids); |
187 |
|
|
188 |
IntSet idset = new IntSet(); |
IntSet idset = new IntSet(); |
189 |
idset.fromString(ids); |
idset.fromString(ids); |
190 |
|
|
197 |
|
|
198 |
return tmpStations; |
return tmpStations; |
199 |
} |
} |
200 |
|
|
201 |
|
|
202 |
|
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 |
} |
} |