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

Legend:
Removed from v.1547  
changed lines
  Added in v.1568

  ViewVC Help
Powered by ViewVC 1.1.20