/[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 1627 - (hide annotations) (download)
Fri Nov 25 10:20:40 2011 UTC (12 years, 6 months ago) by torben
File size: 6833 byte(s)
Work on a tmpStations list while doing the download and only switch over when it's complete

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

  ViewVC Help
Powered by ViewVC 1.1.20