--- android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java 2011/07/07 20:45:17 1551 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java 2011/08/31 20:32:50 1595 @@ -1,14 +1,15 @@ package dk.thoerup.traininfo.provider; -import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import java.util.ArrayList; +import java.net.URLEncoder; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedList; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; @@ -19,6 +20,7 @@ import dk.thoerup.android.traininfo.common.StationBean; import dk.thoerup.android.traininfo.common.StationEntry; import dk.thoerup.genericjavautils.HttpUtil; +import dk.thoerup.traininfo.util.DownloadUtil; import dk.thoerup.traininfo.util.IntSet; import dk.thoerup.traininfo.util.XmlUtil; @@ -26,8 +28,14 @@ StationBean stations = new StationBean(); + public boolean hasStations() { + return (stations != null && stations.entries.size() > 0); + } + public boolean loadStations(Context context) throws Exception { + long start = System.currentTimeMillis(); + stations.entries.clear(); //TODO: remove File parent = context.getFilesDir(); @@ -46,21 +54,30 @@ stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/ - try { - ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); - Object o; - while ( (o=in.readObject()) != null ) { - stations.entries.add( (StationEntry) o); - } - in.close(); - } catch (EOFException e) { - //do nothing; + + ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); + + int length = in.readInt(); // first field is the length + + for (int i=0; i distanceComparator = new Comparator() { + @Override + public int compare(StationEntry object1, StationEntry object2) { + if (object1.getCalcdist() == object2.getCalcdist()) + return 0; + + if (object1.getCalcdist() > object2.getCalcdist()) + return 1; + else + return -1; + } + }; @Override public StationBean lookupStationsByLocation(Location location) { + statsByLocation(location); + + long start = System.currentTimeMillis(); Location tmpLoc = new Location("GPS"); - ArrayList entries = new ArrayList() ; + LinkedList entries = new LinkedList() ; for (StationEntry entry : stations.entries) { tmpLoc.setLatitude(entry.getLatitude()); tmpLoc.setLongitude(entry.getLongitude()); - entry.setCalcdist( (int) location.distanceTo(tmpLoc) ); - entries.add(entry); + int distance = (int) location.distanceTo(tmpLoc); - } - - Collections.sort( entries, new Comparator() { - @Override - public int compare(StationEntry object1, StationEntry object2) { - if (object1.getCalcdist() == object2.getCalcdist()) - return 0; + if (entries.size() <8 || entries.getLast().getCalcdist() > distance) { + entry.setCalcdist(distance); - if (object1.getCalcdist() > object2.getCalcdist()) - return 1; - else - return -1; - } - - }); + if (entries.size() == 8) + entries.removeLast(); + + entries.addLast(entry); + + Collections.sort( entries, distanceComparator); + } + } + logElapsedTime(start, "location_stage1"); + Collections.sort( entries, distanceComparator); StationBean tmpStations = new StationBean(); - for (int i = 0; i<8; i++) { + for (int i = 0; i<8 && i