/[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 1638 by torben, Sun Nov 27 19:44:28 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.provider;  package dk.thoerup.traininfo.provider;
2    
3  import java.io.File;  import java.io.File;
4    import java.io.FileInputStream;
5    import java.io.FileOutputStream;
6  import java.io.IOException;  import java.io.IOException;
7  import java.io.RandomAccessFile;  import java.io.ObjectInputStream;
8  import java.util.ArrayList;  import java.io.ObjectOutputStream;
9    import java.net.URLEncoder;
10  import java.util.Collections;  import java.util.Collections;
11  import java.util.Comparator;  import java.util.Comparator;
12    import java.util.LinkedList;
13    
14  import org.simpleframework.xml.Serializer;  import org.simpleframework.xml.Serializer;
15  import org.simpleframework.xml.core.Persister;  import org.simpleframework.xml.core.Persister;
# Line 16  import android.util.Log; Line 20  import android.util.Log;
20  import dk.thoerup.android.traininfo.common.StationBean;  import dk.thoerup.android.traininfo.common.StationBean;
21  import dk.thoerup.android.traininfo.common.StationEntry;  import dk.thoerup.android.traininfo.common.StationEntry;
22  import dk.thoerup.genericjavautils.HttpUtil;  import dk.thoerup.genericjavautils.HttpUtil;
23    import dk.thoerup.traininfo.util.DownloadUtil;
24  import dk.thoerup.traininfo.util.IntSet;  import dk.thoerup.traininfo.util.IntSet;
25  import dk.thoerup.traininfo.util.XmlUtil;  import dk.thoerup.traininfo.util.XmlUtil;
26    
# Line 23  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();
38                    
39                    StationBean tmpStations = new StationBean();
40                    
41                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
42                  File stationsFile = new File(parent, "stations.xml");                  File stationsFile = new File(parent, "stations.bin");
43                                    
44                  if (!stationsFile.exists())                  if (!stationsFile.exists())
45                          return false;                          return false;
46                                    
47                  int size = (int) stationsFile.length();                  /*int size = (int) stationsFile.length();
48                  byte data[] = new byte[size];                  byte data[] = new byte[size];
49                                    
50                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");
51                  raf.readFully(data);                  raf.readFully(data);
52                                    
53                  Serializer serializer = new Persister();                  Serializer serializer = new Persister();
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                  Log.e("OFFLINE", "loaded" + stations.entries.size());  
58                    ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
59    
60                    int length = in.readInt(); // first field is the length
61                    
62                    for (int i=0; i<length; i++) {                          
63                            StationEntry entry = (StationEntry) in.readObject();
64                            updateSearchStrings(entry);
65                            tmpStations.entries.add( entry );
66                    }
67                    
68                    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");
74                                    
75                  return true;                  return true;
76          }                }
77                    
78          public void downloadStations(Context context) throws IOException {          public void updateSearchStrings(StationEntry entry) {          
79                    entry.nameLower = entry.getName().toLowerCase();
80                    entry.nameInternational = entry.nameLower.replace("æ", "ae").replace("ø", "oe").replace("å", "aa");
81            }
82            
83            public void downloadStations(Context context) throws Exception {
84                  File parent = context.getFilesDir();                  File parent = context.getFilesDir();
85                  File stationsFile = new File(parent, "stations.xml");                  File stationsFile = new File(parent, "stations.bin");
86    
87                    long start = System.currentTimeMillis();
88                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);                  byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
89                    logElapsedTime(start, "download XML");
90                    
91                    
92                    Serializer serializer = new Persister();                
93                    
94                    start = System.currentTimeMillis();
95                    StationBean tmpStations = serializer.read(StationBean.class,  new String(data, "ISO-8859-1") );
96                    logElapsedTime(start, "parse XML");
97                                    
98                  RandomAccessFile raf = new RandomAccessFile(stationsFile, "rw");                  
99                    ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
100                  Log.e("OFFLINE", "data size" + data.length);                  Log.e("OFFLINE", "data size" + data.length);
101                  raf.setLength(0); //truncate                  
102                  raf.write(data);                  out.writeInt( tmpStations.entries.size() ); //start with writing the length of the dataset
103                  raf.close();                                              
104                    for (StationEntry entry : tmpStations.entries) {
105                            updateSearchStrings( entry ); //prepare name fields for byName search
106                            out.writeObject(entry);
107                    }
108                    
109                    out.close();
110                    
111                    stations = tmpStations; // når alt er ok skifter vi over til ny udgave
112          }          }
113                    
114                    
# Line 64  public class OfflineStationProvider impl Line 117  public class OfflineStationProvider impl
117          public void purgeOldEntries() {          public void purgeOldEntries() {
118          }          }
119    
120            Comparator<StationEntry> distanceComparator = new Comparator<StationEntry>() {
121                    @Override
122                    public int compare(StationEntry object1, StationEntry object2) {
123                            if (object1.getCalcdist() == object2.getCalcdist())                                    
124                                    return 0;
125                            
126                            if (object1.getCalcdist() > object2.getCalcdist())
127                                    return 1;
128                            else
129                                    return -1;
130                    }              
131            };
132                    
133          @Override          @Override
134          public StationBean lookupStationsByLocation(Location location) {          public StationBean lookupStationsByLocation(Location location) {
135                                    
136                    statsByLocation(location);
137                    
138                    long start = System.currentTimeMillis();
139                  Location tmpLoc = new Location("GPS");                  Location tmpLoc = new Location("GPS");
140                                    
141                  ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ;                  LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
142                                    
143                  for (StationEntry entry : stations.entries) {                  for (StationEntry entry : stations.entries) {
144                          tmpLoc.setLatitude(entry.getLatitude());                          tmpLoc.setLatitude(entry.getLatitude());
145                          tmpLoc.setLongitude(entry.getLongitude());                          tmpLoc.setLongitude(entry.getLongitude());
146                                                    
147                          entry.setCalcdist( (int) location.distanceTo(tmpLoc) );                          int distance = (int) location.distanceTo(tmpLoc);
                         entries.add(entry);  
148                                                    
149                  }                          if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
150                                                    entry.setCalcdist(distance);
                 Collections.sort( entries, new Comparator<StationEntry>() {  
                         @Override  
                         public int compare(StationEntry object1, StationEntry object2) {  
                                 if (object1.getCalcdist() == object2.getCalcdist())                                      
                                         return 0;  
151                                                                    
152                                  if (object1.getCalcdist() > object2.getCalcdist())                                  if (entries.size() == 8)
153                                          return 1;                                          entries.removeLast();
154                                  else                                  
155                                          return -1;                                  entries.addLast(entry);
156                          }                                  
157                                                            Collections.sort( entries, distanceComparator);
158                  });                          }                      
159                    }
160                                    
161                    logElapsedTime(start, "location_stage1");
162                                    
163                    Collections.sort( entries, distanceComparator);
164                                    
165                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
166                  for (int i = 0; i<8; i++) {                  for (int i = 0; i<8 && i<entries.size(); i++) {
167                          tmpStations.entries.add( entries.get(i) );                          tmpStations.entries.add( entries.get(i) );
168                  }                  }
169                                    
170                    logElapsedTime(start, "location");
171                  return tmpStations;                  return tmpStations;
172          }          }
173            
174            private void logElapsedTime(long start, String method) {
175                    long now = System.currentTimeMillis();
176                    
177                    Log.i("TrainInfo", "Search by " + method + " elapsed " + (now-start) );
178            }
179    
180          @Override          @Override
181          public StationBean lookupStationsByName(String name) {          public StationBean lookupStationsByName(String name) {
182                                    
183                    long start = System.currentTimeMillis();
184                    
185                  name = name.toLowerCase();                  name = name.toLowerCase();
186                  StationBean tmpStations = new StationBean();                  StationBean tmpStations = new StationBean();
187                  for (StationEntry entry : stations.entries) {                  for (StationEntry entry : stations.entries) {
188                          if (entry.getName().toLowerCase().startsWith(name) ) {                          if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
189                                  tmpStations.entries.add(entry);                                  tmpStations.entries.add(entry);
190                          }                          }
191                  }                  }
192                                                                    logElapsedTime(start, "name");                          
193                  return tmpStations;                  return tmpStations;
194          }          }
195    
196          @Override          @Override
197          public StationBean lookupStationsByIds(String ids) {          public StationBean lookupStationsByIds(String ids) {
198                    statsByIds(ids);
199                    
200                  IntSet idset = new IntSet();                  IntSet idset = new IntSet();
201                  idset.fromString(ids);                  idset.fromString(ids);
202                                    
# Line 131  public class OfflineStationProvider impl Line 207  public class OfflineStationProvider impl
207                          }                          }
208                  }                  }
209                                    
210                    return tmpStations;
211            }
212            
213    
214            private void statsByLocation(Location location) {
215                    double lat = XmlStationProvider.roundToPlaces(location.getLatitude(), 4);
216                    double lng = XmlStationProvider.roundToPlaces(location.getLongitude(), 4);
217                    
218                    final String url = XmlUtil.SERVICE_BASE + "/LocateStations?latitude=" + lat + "&longitude=" + lng + "&dummy=1";
219                    Log.i("url", url);
220                    urlSender(url);
221            }
222            
223            private void statsByName(String name) {
224                                    
225                    try {
226                            name = URLEncoder.encode(name, "ISO8859-1");    
227                    } catch (Exception e) {
228                            Log.e("lookupStations", "Encoding failed", e);//if encoding fails use original and hope for the best
229                    }
230                                    
231                  return tmpStations;                  String url = XmlUtil.SERVICE_BASE + "/LocateStations?name=" + name + "&dummy=1";
232                    Log.i("url", url);
233                    urlSender(url);
234            }
235            
236            private void statsByIds(String ids) {  
237                    final String url = XmlUtil.SERVICE_BASE + "/LocateStations?list=" + ids + "&dummy=1";
238                    Log.i("url", url);
239                    urlSender(url);
240          }          }
241            
242            private void urlSender(final String url) {
243                    Thread t = new Thread(new Runnable() {
244    
245                            @Override
246                            public void run() {
247                                    try {
248                                            DownloadUtil.getContentString(url, 15000, "ISO-8859-1");
249                                    } catch (IOException e) {
250                                            Log.e("TrainInfo", "stats failed");
251                                    }                              
252                            }                      
253                    });
254                    t.start();
255            }
256            
257    
258  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20