/[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 1562 by torben, Fri Jul 8 16:26:09 2011 UTC revision 1792 by torben, Wed Apr 11 10:43:06 2012 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
 import java.io.EOFException;  
3  import java.io.File;  import java.io.File;
4  import java.io.FileInputStream;  import java.io.FileInputStream;
5  import java.io.FileOutputStream;  import java.io.FileOutputStream;
# Line 29  public class OfflineStationProvider impl Line 28  public class OfflineStationProvider impl
28                    
29          StationBean stations = new StationBean();          StationBean stations = new StationBean();
30    
31            boolean statsByNameSend = false;
32    
33            public boolean hasStations() {
34                    return (stations != null && stations.entries.size() > 0);
35            }
36            
37    
38          public boolean loadStations(Context context) throws Exception {          public boolean loadStations(Context context) throws Exception {
39                  long start = System.currentTimeMillis();                  long start = System.currentTimeMillis();
40                                    
41                  stations.entries.clear(); //TODO: remove                  StationBean tmpStations = new StationBean();
42                                    
43                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
44                  File stationsFile = new File(parent, "stations.bin");                  File stationsFile = new File(parent, "stations.bin");
# Line 51  public class OfflineStationProvider impl Line 56  public class OfflineStationProvider impl
56                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );*/                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );*/
57                                    
58                                    
59                  try {  
60                          ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );                  ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
61                          Object o;  
62                          StationEntry e = null;                  int length = in.readInt(); // first field is the length
63                          while ( (o=in.readObject()) != null ) {                  
64                                  e = (StationEntry) o;                  for (int i=0; i<length; i++) {                          
65                                  updateSearchStrings(e);                          StationEntry entry = (StationEntry) in.readObject();
66                                  stations.entries.add( e );                          updateSearchStrings(entry);
67                          }                          tmpStations.entries.add( entry );
                         in.close();  
                 } catch (EOFException e) {  
                         //do nothing;  
68                  }                  }
69                                    
70                  Log.e("OFFLINE", "loaded" + stations.entries.size());                  in.close();
71    
72                    stations = tmpStations; // når indlæsningen er ok skifter vi over
73                    
74                    Log.e("OFFLINE", "loaded" + tmpStations.entries.size());
75                  logElapsedTime(start, "loadStations");                  logElapsedTime(start, "loadStations");
76                                    
77                  return true;                  return true;
# Line 80  public class OfflineStationProvider impl Line 86  public class OfflineStationProvider impl
86                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
87                  File stationsFile = new File(parent, "stations.bin");                  File stationsFile = new File(parent, "stations.bin");
88    
89                                    long start = System.currentTimeMillis();
90                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
91                  Serializer serializer = new Persister();                  logElapsedTime(start, "download XML");
92                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );                  
93                    
94                    Serializer serializer = new Persister();                
95                    
96                    start = System.currentTimeMillis();
97                    StationBean tmpStations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );
98                    logElapsedTime(start, "parse XML");
99                                    
100                                    
101                  ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );                  ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
102                  Log.e("OFFLINE", "data size" + data.length);                  Log.e("OFFLINE", "data size" + data.length);
103                                    
104                  for (StationEntry entry : stations.entries) {                  out.writeInt( tmpStations.entries.size() ); //start with writing the length of the dataset
105                    
106                    for (StationEntry entry : tmpStations.entries) {
107                          updateSearchStrings( entry ); //prepare name fields for byName search                          updateSearchStrings( entry ); //prepare name fields for byName search
108                          out.writeObject(entry);                          out.writeObject(entry);
109                  }                  }
110                                    
111                  out.close();                              out.close();
112                    
113                    stations = tmpStations; // når alt er ok skifter vi over til ny udgave
114          }          }
115                    
116                    
# Line 149  public class OfflineStationProvider impl Line 165  public class OfflineStationProvider impl
165                  Collections.sort( entries, distanceComparator);                  Collections.sort( entries, distanceComparator);
166                                    
167                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
168                  for (int i = 0; i<8; i++) {                  for (int i = 0; i<8 && i<entries.size(); i++) {
169                          tmpStations.entries.add( entries.get(i) );                          tmpStations.entries.add( entries.get(i) );
170                  }                  }
171                                    
# Line 165  public class OfflineStationProvider impl Line 181  public class OfflineStationProvider impl
181    
182          @Override          @Override
183          public StationBean lookupStationsByName(String name) {          public StationBean lookupStationsByName(String name) {
184            
185                    statsByName(name);      
186                                    
187                  long start = System.currentTimeMillis();                  long start = System.currentTimeMillis();
188                                    
# Line 174  public class OfflineStationProvider impl Line 192  public class OfflineStationProvider impl
192                          if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {                          if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
193                                  tmpStations.entries.add(entry);                                  tmpStations.entries.add(entry);
194                          }                          }
195                            
196                            for(String alias : entry.getAliases()) {
197                                    if (alias.startsWith(name)) {
198                                            tmpStations.entries.add(entry);
199                                    }
200                            }
201                  }                  }
202                  logElapsedTime(start, "name");                                            logElapsedTime(start, "name");                          
203                  return tmpStations;                  return tmpStations;
# Line 207  public class OfflineStationProvider impl Line 231  public class OfflineStationProvider impl
231          }          }
232                    
233          private void statsByName(String name) {          private void statsByName(String name) {
234                    if (statsByNameSend == true) {
235                            return;
236                    }
237                    statsByNameSend = true;
238                                    
239                  try {                  try {
240                          name = URLEncoder.encode(name, "ISO8859-1");                              name = URLEncoder.encode(name, "ISO8859-1");    

Legend:
Removed from v.1562  
changed lines
  Added in v.1792

  ViewVC Help
Powered by ViewVC 1.1.20