/[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 1638 - (hide annotations) (download)
Sun Nov 27 19:44:28 2011 UTC (12 years, 6 months ago) by torben
File size: 7081 byte(s)
Add basic about screen (and add /bin/ to svn:ignore)
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 1628 StationBean tmpStations = new StationBean();
40 torben 1546
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 torben 1628 tmpStations.entries.add( entry );
66 torben 1546 }
67    
68 torben 1568 in.close();
69    
70 torben 1628 stations = tmpStations; // når indlæsningen er ok skifter vi over
71 torben 1568
72 torben 1628 Log.e("OFFLINE", "loaded" + tmpStations.entries.size());
73 torben 1553 logElapsedTime(start, "loadStations");
74 torben 1545
75     return true;
76 torben 1562 }
77 torben 1545
78 torben 1562 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 torben 1546 public void downloadStations(Context context) throws Exception {
84 torben 1545 File parent = context.getFilesDir();
85 torben 1546 File stationsFile = new File(parent, "stations.bin");
86 torben 1545
87 torben 1638 long start = System.currentTimeMillis();
88     byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
89     logElapsedTime(start, "download XML");
90 torben 1547
91 torben 1638
92     Serializer serializer = new Persister();
93    
94     start = System.currentTimeMillis();
95 torben 1627 StationBean tmpStations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );
96 torben 1638 logElapsedTime(start, "parse XML");
97 torben 1545
98 torben 1546
99     ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
100 torben 1545 Log.e("OFFLINE", "data size" + data.length);
101 torben 1546
102 torben 1627 out.writeInt( tmpStations.entries.size() ); //start with writing the length of the dataset
103 torben 1568
104 torben 1627 for (StationEntry entry : tmpStations.entries) {
105 torben 1562 updateSearchStrings( entry ); //prepare name fields for byName search
106 torben 1546 out.writeObject(entry);
107     }
108    
109 torben 1627 out.close();
110    
111     stations = tmpStations; // når alt er ok skifter vi over til ny udgave
112 torben 1545 }
113    
114    
115    
116     @Override
117     public void purgeOldEntries() {
118     }
119    
120 torben 1553 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 torben 1545
133     @Override
134     public StationBean lookupStationsByLocation(Location location) {
135    
136 torben 1559 statsByLocation(location);
137    
138 torben 1553 long start = System.currentTimeMillis();
139 torben 1545 Location tmpLoc = new Location("GPS");
140    
141 torben 1553 LinkedList<StationEntry> entries = new LinkedList<StationEntry>() ;
142 torben 1545
143     for (StationEntry entry : stations.entries) {
144     tmpLoc.setLatitude(entry.getLatitude());
145     tmpLoc.setLongitude(entry.getLongitude());
146    
147 torben 1553 int distance = (int) location.distanceTo(tmpLoc);
148 torben 1545
149 torben 1553 if (entries.size() <8 || entries.getLast().getCalcdist() > distance) {
150     entry.setCalcdist(distance);
151    
152     if (entries.size() == 8)
153     entries.removeLast();
154    
155     entries.addLast(entry);
156    
157     Collections.sort( entries, distanceComparator);
158     }
159 torben 1545 }
160    
161 torben 1553 logElapsedTime(start, "location_stage1");
162 torben 1545
163 torben 1553 Collections.sort( entries, distanceComparator);
164 torben 1545
165     StationBean tmpStations = new StationBean();
166 torben 1595 for (int i = 0; i<8 && i<entries.size(); i++) {
167 torben 1545 tmpStations.entries.add( entries.get(i) );
168     }
169    
170 torben 1553 logElapsedTime(start, "location");
171 torben 1545 return tmpStations;
172     }
173 torben 1553
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 torben 1545
180     @Override
181     public StationBean lookupStationsByName(String name) {
182    
183 torben 1553 long start = System.currentTimeMillis();
184    
185 torben 1545 name = name.toLowerCase();
186     StationBean tmpStations = new StationBean();
187     for (StationEntry entry : stations.entries) {
188 torben 1553 if (entry.nameLower.startsWith(name) || entry.nameInternational.startsWith(name) ) {
189 torben 1545 tmpStations.entries.add(entry);
190     }
191     }
192 torben 1553 logElapsedTime(start, "name");
193 torben 1545 return tmpStations;
194     }
195    
196     @Override
197     public StationBean lookupStationsByIds(String ids) {
198 torben 1559 statsByIds(ids);
199    
200 torben 1545 IntSet idset = new IntSet();
201     idset.fromString(ids);
202    
203     StationBean tmpStations = new StationBean();
204     for (StationEntry entry : stations.entries) {
205     if (idset.contains( entry.getId() ) ) {
206     tmpStations.entries.add(entry);
207     }
208     }
209    
210     return tmpStations;
211     }
212 torben 1559
213 torben 1545
214 torben 1559 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     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 torben 1545 }

  ViewVC Help
Powered by ViewVC 1.1.20