/[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 1545 by torben, Wed Jul 6 20:59:19 2011 UTC revision 1553 by torben, Fri Jul 8 11:56:58 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3    import java.io.EOFException;
4  import java.io.File;  import java.io.File;
5  import java.io.IOException;  import java.io.FileInputStream;
6  import java.io.RandomAccessFile;  import java.io.FileOutputStream;
7  import java.util.ArrayList;  import java.io.ObjectInputStream;
8    import java.io.ObjectOutputStream;
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 25  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
34                    
35                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
36                  File stationsFile = new File(parent, "stations.xml");                  File stationsFile = new File(parent, "stations.bin");
37                                    
38                  if (!stationsFile.exists())                  if (!stationsFile.exists())
39                          return false;                          return false;
40                                    
41                  int size = (int) stationsFile.length();                  /*int size = (int) stationsFile.length();
42                  byte data[] = new byte[size];                  byte data[] = new byte[size];
43                                    
44                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");
45                  raf.readFully(data);                  raf.readFully(data);
46                                    
47                  Serializer serializer = new Persister();                  Serializer serializer = new Persister();
48                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );*/
49                    
50                    
51                    try {
52                            ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
53                            Object o;
54                            StationEntry e = null;
55                            while ( (o=in.readObject()) != null ) {
56                                    e = (StationEntry) o;
57                                    e.updateSearch();
58                                    stations.entries.add( e );
59                            }
60                            in.close();
61                    } catch (EOFException e) {
62                            //do nothing;
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          }                }      
70                    
71          public void downloadStations(Context context) throws IOException {          public void downloadStations(Context context) throws Exception {
72                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
73                  File stationsFile = new File(parent, "stations.xml");                  File stationsFile = new File(parent, "stations.bin");
74    
75                    
76                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
77                    Serializer serializer = new Persister();
78                    stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );
79                                    
80                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "rw");                  
81                    ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
82                  Log.e("OFFLINE", "data size" + data.length);                  Log.e("OFFLINE", "data size" + data.length);
83                  raf.setLength(0); //truncate                  
84                  raf.write(data);                  for (StationEntry entry : stations.entries) {
85                  raf.close();                                                      out.writeObject(entry);
86                    }
87                    
88                    out.close();            
89          }          }
90                    
91                    
# Line 64  public class OfflineStationProvider impl Line 94  public class OfflineStationProvider impl
94          public void purgeOldEntries() {          public void purgeOldEntries() {
95          }          }
96    
97            Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() {
98                    @Override
99                    public int compare(StationEntry object1, StationEntry object2) {
100                            if (object1.getCalcdist() == object2.getCalcdist())                                    
101                                    return 0;
102                            
103                            if (object1.getCalcdist() > object2.getCalcdist())
104                                    return 1;
105                            else
106                                    return -1;
107                    }              
108            };
109                    
110          @Override          @Override
111          public StationBean lookupStationsByLocation(Location location) {          public StationBean lookupStationsByLocation(Location location) {
112                                    
113                    long start = System.currentTimeMillis();
114                  Location tmpLoc = new Location("GPS");                  Location tmpLoc = new Location("GPS");
115                                    
116                  ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ;                  LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
117                                    
118                  for (StationEntry entry : stations.entries) {                  for (StationEntry entry : stations.entries) {
119                          tmpLoc.setLatitude(entry.getLatitude());                          tmpLoc.setLatitude(entry.getLatitude());
120                          tmpLoc.setLongitude(entry.getLongitude());                          tmpLoc.setLongitude(entry.getLongitude());
121                                                    
122                          entry.setCalcdist( (int) location.distanceTo(tmpLoc) );                          int distance = (int) location.distanceTo(tmpLoc);
                         entries.add(entry);  
123                                                    
124                  }                          if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
125                                                    entry.setCalcdist(distance);
                 Collections.sort( entries, new Comparator<StationEntry>() {  
                         @Override  
                         public int compare(StationEntry object1, StationEntry object2) {  
                                 if (object1.getCalcdist() == object2.getCalcdist())                                      
                                         return 0;  
126                                                                    
127                                  if (object1.getCalcdist() > object2.getCalcdist())                                  if (entries.size() == 8)
128                                          return 1;                                          entries.removeLast();
129                                  else                                  
130                                          return -1;                                  entries.addLast(entry);
131                          }                                  
132                                                            Collections.sort( entries, distanceComparator);
133                  });                          }                      
134                    }
135                                    
136                    logElapsedTime(start, "location_stage1");
137                                    
138                    Collections.sort( entries, distanceComparator);
139                                    
140                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
141                  for (int i = 0; i<8; i++) {                  for (int i = 0; i<8; i++) {
142                          tmpStations.entries.add( entries.get(i) );                          tmpStations.entries.add( entries.get(i) );
143                  }                  }
144                                    
145                    logElapsedTime(start, "location");
146                  return tmpStations;                  return tmpStations;
147          }          }
148            
149            private void logElapsedTime(long start, String method) {
150                    long now = System.currentTimeMillis();
151                    
152                    Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) );
153            }
154    
155          @Override          @Override
156          public StationBean lookupStationsByName(String name) {          public StationBean lookupStationsByName(String name) {
157                                    
158                    long start = System.currentTimeMillis();
159                    
160                  name = name.toLowerCase();                  name = name.toLowerCase();
161                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
162                  for (StationEntry entry : stations.entries) {                  for (StationEntry entry : stations.entries) {
163                          if (entry.getName().toLowerCase().startsWith(name) ) {                          if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
164                                  tmpStations.entries.add(entry);                                  tmpStations.entries.add(entry);
165                          }                          }
166                  }                  }
167                                                                    logElapsedTime(start, "name");                          
168                  return tmpStations;                  return tmpStations;
169          }          }
170    
# Line 131  public class OfflineStationProvider impl Line 180  public class OfflineStationProvider impl
180                          }                          }
181                  }                  }
182                                    
                   
                   
183                  return tmpStations;                  return tmpStations;
184          }          }
185    

Legend:
Removed from v.1545  
changed lines
  Added in v.1553

  ViewVC Help
Powered by ViewVC 1.1.20