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 |
|
|
57 |
StationEntry e = null; |
StationEntry e = null; |
58 |
while ( (o=in.readObject()) != null ) { |
while ( (o=in.readObject()) != null ) { |
59 |
e = (StationEntry) o; |
e = (StationEntry) o; |
60 |
e.updateSearch(); |
updateSearchStrings(e); |
61 |
stations.entries.add( e ); |
stations.entries.add( e ); |
62 |
} |
} |
63 |
in.close(); |
in.close(); |
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(); |
90 |
Log.e("OFFLINE", "data size" + data.length); |
Log.e("OFFLINE", "data size" + data.length); |
91 |
|
|
92 |
for (StationEntry entry : stations.entries) { |
for (StationEntry entry : stations.entries) { |
93 |
|
updateSearchStrings( entry ); //prepare name fields for byName search |
94 |
out.writeObject(entry); |
out.writeObject(entry); |
95 |
} |
} |
96 |
|
|
119 |
@Override |
@Override |
120 |
public StationBean lookupStationsByLocation(Location location) { |
public StationBean lookupStationsByLocation(Location location) { |
121 |
|
|
122 |
|
statsByLocation(location); |
123 |
|
|
124 |
long start = System.currentTimeMillis(); |
long start = System.currentTimeMillis(); |
125 |
Location tmpLoc = new Location("GPS"); |
Location tmpLoc = new Location("GPS"); |
126 |
|
|
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 |
|
|
195 |
|
|
196 |
return tmpStations; |
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 |
|
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 |
} |
} |