/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java
ViewVC logotype

Annotation of /android/TrainInfo/src/dk/thoerup/traininfo/provider/OfflineStationProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1553 - (hide annotations) (download)
Fri Jul 8 11:56:58 2011 UTC (12 years, 10 months ago) by torben
File size: 4892 byte(s)
searchbylocation allways keeps track of the 8 nearest stations.
searchByName now has to name helper fields in StationEntry to make the search faster (so we don't do the same string transformations over and over again)
1 torben 1545 package dk.thoerup.traininfo.provider;
2    
3 torben 1546 import java.io.EOFException;
4 torben 1545 import java.io.File;
5 torben 1546 import java.io.FileInputStream;
6     import java.io.FileOutputStream;
7     import java.io.ObjectInputStream;
8     import java.io.ObjectOutputStream;
9 torben 1545 import java.util.Collections;
10     import java.util.Comparator;
11 torben 1553 import java.util.LinkedList;
12 torben 1545
13     import org.simpleframework.xml.Serializer;
14     import org.simpleframework.xml.core.Persister;
15    
16     import android.content.Context;
17     import android.location.Location;
18     import android.util.Log;
19     import dk.thoerup.android.traininfo.common.StationBean;
20     import dk.thoerup.android.traininfo.common.StationEntry;
21     import dk.thoerup.genericjavautils.HttpUtil;
22     import dk.thoerup.traininfo.util.IntSet;
23     import dk.thoerup.traininfo.util.XmlUtil;
24    
25     public class OfflineStationProvider implements StationProvider {
26    
27     StationBean stations = new StationBean();
28    
29    
30     public boolean loadStations(Context context) throws Exception {
31 torben 1553 long start = System.currentTimeMillis();
32    
33 torben 1546 stations.entries.clear(); //TODO: remove
34    
35 torben 1545 File parent = context.getFilesDir();
36 torben 1546 File stationsFile = new File(parent, "stations.bin");
37 torben 1545
38     if (!stationsFile.exists())
39     return false;
40    
41 torben 1551 /*int size = (int) stationsFile.length();
42     byte data[] = new byte[size];
43 torben 1545
44     RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");
45     raf.readFully(data);
46    
47     Serializer serializer = new Persister();
48 torben 1546 stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/
49 torben 1545
50 torben 1546
51     try {
52     ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
53     Object o;
54 torben 1553 StationEntry e = null;
55 torben 1546 while ( (o=in.readObject()) != null ) {
56 torben 1553 e = (StationEntry) o;
57     e.updateSearch();
58     stations.entries.add( e );
59 torben 1546 }
60     in.close();
61     } catch (EOFException e) {
62     //do nothing;
63     }
64    
65 torben 1545 Log.e("OFFLINE", "loaded" + stations.entries.size());
66 torben 1553 logElapsedTime(start, "loadStations");
67 torben 1545
68     return true;
69     }
70    
71 torben 1546 public void downloadStations(Context context) throws Exception {
72 torben 1545 File parent = context.getFilesDir();
73 torben 1546 File stationsFile = new File(parent, "stations.bin");
74 torben 1545
75 torben 1547
76 torben 1545 byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
77 torben 1546 Serializer serializer = new Persister();
78     stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );
79 torben 1545
80 torben 1546
81     ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
82 torben 1545 Log.e("OFFLINE", "data size" + data.length);
83 torben 1546
84     for (StationEntry entry : stations.entries) {
85     out.writeObject(entry);
86     }
87    
88     out.close();
89 torben 1545 }
90    
91    
92    
93     @Override
94     public void purgeOldEntries() {
95     }
96    
97 torben 1553 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 torben 1545
110     @Override
111     public StationBean lookupStationsByLocation(Location location) {
112    
113 torben 1553 long start = System.currentTimeMillis();
114 torben 1545 Location tmpLoc = new Location("GPS");
115    
116 torben 1553 LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
117 torben 1545
118     for (StationEntry entry : stations.entries) {
119     tmpLoc.setLatitude(entry.getLatitude());
120     tmpLoc.setLongitude(entry.getLongitude());
121    
122 torben 1553 int distance = (int) location.distanceTo(tmpLoc);
123 torben 1545
124 torben 1553 if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
125     entry.setCalcdist(distance);
126    
127     if (entries.size() == 8)
128     entries.removeLast();
129    
130     entries.addLast(entry);
131    
132     Collections.sort( entries, distanceComparator);
133     }
134 torben 1545 }
135    
136 torben 1553 logElapsedTime(start, "location_stage1");
137 torben 1545
138 torben 1553 Collections.sort( entries, distanceComparator);
139 torben 1545
140     StationBean tmpStations = new StationBean();
141     for (int i = 0; i<8; i++) {
142     tmpStations.entries.add( entries.get(i) );
143     }
144    
145 torben 1553 logElapsedTime(start, "location");
146 torben 1545 return tmpStations;
147     }
148 torben 1553
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 torben 1545
155     @Override
156     public StationBean lookupStationsByName(String name) {
157    
158 torben 1553 long start = System.currentTimeMillis();
159    
160 torben 1545 name = name.toLowerCase();
161     StationBean tmpStations = new StationBean();
162     for (StationEntry entry : stations.entries) {
163 torben 1553 if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
164 torben 1545 tmpStations.entries.add(entry);
165     }
166     }
167 torben 1553 logElapsedTime(start, "name");
168 torben 1545 return tmpStations;
169     }
170    
171     @Override
172     public StationBean lookupStationsByIds(String ids) {
173     IntSet idset = new IntSet();
174     idset.fromString(ids);
175    
176     StationBean tmpStations = new StationBean();
177     for (StationEntry entry : stations.entries) {
178     if (idset.contains( entry.getId() ) ) {
179     tmpStations.entries.add(entry);
180     }
181     }
182    
183     return tmpStations;
184     }
185    
186     }

  ViewVC Help
Powered by ViewVC 1.1.20