--- android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java 2011/07/08 11:54:56 1552 +++ android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java 2011/07/08 11:56:58 1553 @@ -6,9 +6,9 @@ import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedList; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; @@ -28,6 +28,8 @@ public boolean loadStations(Context context) throws Exception { + long start = System.currentTimeMillis(); + stations.entries.clear(); //TODO: remove File parent = context.getFilesDir(); @@ -49,8 +51,11 @@ try { ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) ); Object o; + StationEntry e = null; while ( (o=in.readObject()) != null ) { - stations.entries.add( (StationEntry) o); + e = (StationEntry) o; + e.updateSearch(); + stations.entries.add( e ); } in.close(); } catch (EOFException e) { @@ -58,6 +63,7 @@ } Log.e("OFFLINE", "loaded" + stations.entries.size()); + logElapsedTime(start, "loadStations"); return true; } @@ -88,58 +94,77 @@ public void purgeOldEntries() { } + Comparator 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) { + 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++) { tmpStations.entries.add( entries.get(i) ); } + logElapsedTime(start, "location"); return tmpStations; } + + private void logElapsedTime(long start, String method) { + long now = System.currentTimeMillis(); + + Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) ); + } @Override public StationBean lookupStationsByName(String name) { + long start = System.currentTimeMillis(); + name = name.toLowerCase(); StationBean tmpStations = new StationBean(); for (StationEntry entry : stations.entries) { - if (entry.getName().toLowerCase().startsWith(name) ) { + if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) { tmpStations.entries.add(entry); } } - + logElapsedTime(start, "name"); return tmpStations; } @@ -155,8 +180,6 @@ } } - - return tmpStations; }