/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1551 by torben, Thu Jul 7 20:45:17 2011 UTC revision 1556 by torben, Fri Jul 8 12:37:16 2011 UTC
# Line 6  import java.io.FileInputStream; Line 6  import java.io.FileInputStream;
6  import java.io.FileOutputStream;  import java.io.FileOutputStream;
7  import java.io.ObjectInputStream;  import java.io.ObjectInputStream;
8  import java.io.ObjectOutputStream;  import java.io.ObjectOutputStream;
 import java.util.ArrayList;  
9  import java.util.Collections;  import java.util.Collections;
10  import java.util.Comparator;  import java.util.Comparator;
11    import java.util.LinkedList;
12    
13  import org.simpleframework.xml.Serializer;  import org.simpleframework.xml.Serializer;
14  import org.simpleframework.xml.core.Persister;  import org.simpleframework.xml.core.Persister;
# Line 28  public class OfflineStationProvider impl Line 28  public class OfflineStationProvider impl
28    
29    
30          public boolean loadStations(Context context) throws Exception {          public boolean loadStations(Context context) throws Exception {
31                    long start = System.currentTimeMillis();
32                    
33                  stations.entries.clear(); //TODO: remove                  stations.entries.clear(); //TODO: remove
34                                    
35                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
# Line 49  public class OfflineStationProvider impl Line 51  public class OfflineStationProvider impl
51                  try {                  try {
52                          ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );                          ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
53                          Object o;                          Object o;
54                            StationEntry e = null;
55                          while ( (o=in.readObject()) != null ) {                          while ( (o=in.readObject()) != null ) {
56                                  stations.entries.add( (StationEntry) o);                                  e = (StationEntry) o;
57                                    e.updateSearch();
58                                    stations.entries.add( e );
59                          }                          }
60                          in.close();                          in.close();
61                  } catch (EOFException e) {                  } catch (EOFException e) {
# Line 58  public class OfflineStationProvider impl Line 63  public class OfflineStationProvider impl
63                  }                  }
64                                    
65                  Log.e("OFFLINE", "loaded" + stations.entries.size());                  Log.e("OFFLINE", "loaded" + stations.entries.size());
66                    logElapsedTime(start, "loadStations");
67                                    
68                  return true;                  return true;
69          }                }      
# Line 76  public class OfflineStationProvider impl Line 82  public class OfflineStationProvider impl
82                  Log.e("OFFLINE", "data size" + data.length);                  Log.e("OFFLINE", "data size" + data.length);
83                                    
84                  for (StationEntry entry : stations.entries) {                  for (StationEntry entry : stations.entries) {
85                            entry.updateSearch(); //prepare name fields for byName search
86                          out.writeObject(entry);                          out.writeObject(entry);
87                  }                  }
88                                    
# Line 88  public class OfflineStationProvider impl Line 95  public class OfflineStationProvider impl
95          public void purgeOldEntries() {          public void purgeOldEntries() {
96          }          }
97    
98            Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() {
99                    @Override
100                    public int compare(StationEntry object1, StationEntry object2) {
101                            if (object1.getCalcdist() == object2.getCalcdist())                                    
102                                    return 0;
103                            
104                            if (object1.getCalcdist() > object2.getCalcdist())
105                                    return 1;
106                            else
107                                    return -1;
108                    }              
109            };
110                    
111          @Override          @Override
112          public StationBean lookupStationsByLocation(Location location) {          public StationBean lookupStationsByLocation(Location location) {
113                                    
114                    long start = System.currentTimeMillis();
115                  Location tmpLoc = new Location("GPS");                  Location tmpLoc = new Location("GPS");
116                                    
117                  ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ;                  LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
118                                    
119                  for (StationEntry entry : stations.entries) {                  for (StationEntry entry : stations.entries) {
120                          tmpLoc.setLatitude(entry.getLatitude());                          tmpLoc.setLatitude(entry.getLatitude());
121                          tmpLoc.setLongitude(entry.getLongitude());                          tmpLoc.setLongitude(entry.getLongitude());
122                                                    
123                          entry.setCalcdist( (int) location.distanceTo(tmpLoc) );                          int distance = (int) location.distanceTo(tmpLoc);
                         entries.add(entry);  
124                                                    
125                  }                          if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
126                                                    entry.setCalcdist(distance);
                 Collections.sort( entries, new Comparator<StationEntry>() {  
                         @Override  
                         public int compare(StationEntry object1, StationEntry object2) {  
                                 if (object1.getCalcdist() == object2.getCalcdist())                                      
                                         return 0;  
127                                                                    
128                                  if (object1.getCalcdist() > object2.getCalcdist())                                  if (entries.size() == 8)
129                                          return 1;                                          entries.removeLast();
130                                  else                                  
131                                          return -1;                                  entries.addLast(entry);
132                          }                                  
133                                                            Collections.sort( entries, distanceComparator);
134                  });                          }                      
135                    }
136                                    
137                    logElapsedTime(start, "location_stage1");
138                                    
139                    Collections.sort( entries, distanceComparator);
140                                    
141                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
142                  for (int i = 0; i<8; i++) {                  for (int i = 0; i<8; i++) {
143                          tmpStations.entries.add( entries.get(i) );                          tmpStations.entries.add( entries.get(i) );
144                  }                  }
145                                    
146                    logElapsedTime(start, "location");
147                  return tmpStations;                  return tmpStations;
148          }          }
149            
150            private void logElapsedTime(long start, String method) {
151                    long now = System.currentTimeMillis();
152                    
153                    Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) );
154            }
155    
156          @Override          @Override
157          public StationBean lookupStationsByName(String name) {          public StationBean lookupStationsByName(String name) {
158                                    
159                    long start = System.currentTimeMillis();
160                    
161                  name = name.toLowerCase();                  name = name.toLowerCase();
162                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
163                  for (StationEntry entry : stations.entries) {                  for (StationEntry entry : stations.entries) {
164                          if (entry.getName().toLowerCase().startsWith(name) ) {                          if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
165                                  tmpStations.entries.add(entry);                                  tmpStations.entries.add(entry);
166                          }                          }
167                  }                  }
168                                                                    logElapsedTime(start, "name");                          
169                  return tmpStations;                  return tmpStations;
170          }          }
171    
# Line 155  public class OfflineStationProvider impl Line 181  public class OfflineStationProvider impl
181                          }                          }
182                  }                  }
183                                    
                   
                   
184                  return tmpStations;                  return tmpStations;
185          }          }
186    

Legend:
Removed from v.1551  
changed lines
  Added in v.1556

  ViewVC Help
Powered by ViewVC 1.1.20