/[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 1628 by torben, Fri Nov 25 10:50:44 2011 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            public boolean hasStations() {
32                    return (stations != null && stations.entries.size() > 0);
33            }
34            
35    
36          public boolean loadStations(Context context) throws Exception {          public boolean loadStations(Context context) throws Exception {
37                  long start = System.currentTimeMillis();                  long start = System.currentTimeMillis();
38                                    
39                  stations.entries.clear(); //TODO: remove                  StationBean tmpStations = new StationBean();
40                                    
41                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
42                  File stationsFile = new File(parent, "stations.bin");                  File stationsFile = new File(parent, "stations.bin");
# Line 51  public class OfflineStationProvider impl Line 54  public class OfflineStationProvider impl
54                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );*/                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );*/
55                                    
56                                    
57                  try {  
58                          ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );                  ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
59                          Object o;  
60                          StationEntry e = null;                  int length = in.readInt(); // first field is the length
61                          while ( (o=in.readObject()) != null ) {                  
62                                  e = (StationEntry) o;                  for (int i=0; i<length; i++) {                          
63                                  updateSearchStrings(e);                          StationEntry entry = (StationEntry) in.readObject();
64                                  stations.entries.add( e );                          updateSearchStrings(entry);
65                          }                          tmpStations.entries.add( entry );
                         in.close();  
                 } catch (EOFException e) {  
                         //do nothing;  
66                  }                  }
67                                    
68                  Log.e("OFFLINE", "loaded" + stations.entries.size());                  in.close();
69    
70                    stations = tmpStations; // når indlæsningen er ok skifter vi over
71                    
72                    Log.e("OFFLINE", "loaded" + tmpStations.entries.size());
73                  logElapsedTime(start, "loadStations");                  logElapsedTime(start, "loadStations");
74                                    
75                  return true;                  return true;
# Line 83  public class OfflineStationProvider impl Line 87  public class OfflineStationProvider impl
87                                    
88                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
89                  Serializer serializer = new Persister();                  Serializer serializer = new Persister();
90                  stations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );                  StationBean tmpStations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );
91                                    
92                                    
93                  ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );                  ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
94                  Log.e("OFFLINE", "data size" + data.length);                  Log.e("OFFLINE", "data size" + data.length);
95                                    
96                  for (StationEntry entry : stations.entries) {                  out.writeInt( tmpStations.entries.size() ); //start with writing the length of the dataset
97                    
98                    for (StationEntry entry : tmpStations.entries) {
99                          updateSearchStrings( entry ); //prepare name fields for byName search                          updateSearchStrings( entry ); //prepare name fields for byName search
100                          out.writeObject(entry);                          out.writeObject(entry);
101                  }                  }
102                                    
103                  out.close();                              out.close();
104                    
105                    stations = tmpStations; // når alt er ok skifter vi over til ny udgave
106          }          }
107                    
108                    
# Line 149  public class OfflineStationProvider impl Line 157  public class OfflineStationProvider impl
157                  Collections.sort( entries, distanceComparator);                  Collections.sort( entries, distanceComparator);
158                                    
159                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
160                  for (int i = 0; i<8; i++) {                  for (int i = 0; i<8 && i<entries.size(); i++) {
161                          tmpStations.entries.add( entries.get(i) );                          tmpStations.entries.add( entries.get(i) );
162                  }                  }
163                                    

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

  ViewVC Help
Powered by ViewVC 1.1.20