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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1546 - (show annotations) (download)
Wed Jul 6 21:20:49 2011 UTC (12 years, 10 months ago) by torben
File size: 4047 byte(s)
Binary serialisation is way faster
1 package dk.thoerup.traininfo.provider;
2
3 import java.io.EOFException;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.Comparator;
12
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 stations.entries.clear(); //TODO: remove
32
33 File parent = context.getFilesDir();
34 File stationsFile = new File(parent, "stations.bin");
35
36 if (!stationsFile.exists())
37 return false;
38
39 int size = (int) stationsFile.length();
40 /*byte data[] = new byte[size];
41
42 RandomAccessFile raf = new RandomAccessFile(stationsFile, "r");
43 raf.readFully(data);
44
45 Serializer serializer = new Persister();
46 stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );*/
47
48
49 try {
50 ObjectInputStream in = new ObjectInputStream( new FileInputStream(stationsFile) );
51 Object o;
52 while ( (o=in.readObject()) != null ) {
53 stations.entries.add( (StationEntry) o);
54 }
55 in.close();
56 } catch (EOFException e) {
57 //do nothing;
58 }
59
60 Log.e("OFFLINE", "loaded" + stations.entries.size());
61
62 return true;
63 }
64
65 public void downloadStations(Context context) throws Exception {
66 File parent = context.getFilesDir();
67 File stationsFile = new File(parent, "stations.bin");
68
69 byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000);
70 Serializer serializer = new Persister();
71 stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") );
72
73
74 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) );
75 Log.e("OFFLINE", "data size" + data.length);
76
77 for (StationEntry entry : stations.entries) {
78 out.writeObject(entry);
79 }
80
81 out.close();
82 }
83
84
85
86 @Override
87 public void purgeOldEntries() {
88 }
89
90
91 @Override
92 public StationBean lookupStationsByLocation(Location location) {
93
94 Location tmpLoc = new Location("GPS");
95
96 ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ;
97
98 for (StationEntry entry : stations.entries) {
99 tmpLoc.setLatitude(entry.getLatitude());
100 tmpLoc.setLongitude(entry.getLongitude());
101
102 entry.setCalcdist( (int) location.distanceTo(tmpLoc) );
103 entries.add(entry);
104
105 }
106
107 Collections.sort( entries, new Comparator<StationEntry>() {
108 @Override
109 public int compare(StationEntry object1, StationEntry object2) {
110 if (object1.getCalcdist() == object2.getCalcdist())
111 return 0;
112
113 if (object1.getCalcdist() > object2.getCalcdist())
114 return 1;
115 else
116 return -1;
117 }
118
119 });
120
121
122
123 StationBean tmpStations = new StationBean();
124 for (int i = 0; i<8; i++) {
125 tmpStations.entries.add( entries.get(i) );
126 }
127
128 return tmpStations;
129 }
130
131 @Override
132 public StationBean lookupStationsByName(String name) {
133
134 name = name.toLowerCase();
135 StationBean tmpStations = new StationBean();
136 for (StationEntry entry : stations.entries) {
137 if (entry.getName().toLowerCase().startsWith(name) ) {
138 tmpStations.entries.add(entry);
139 }
140 }
141
142 return tmpStations;
143 }
144
145 @Override
146 public StationBean lookupStationsByIds(String ids) {
147 IntSet idset = new IntSet();
148 idset.fromString(ids);
149
150 StationBean tmpStations = new StationBean();
151 for (StationEntry entry : stations.entries) {
152 if (idset.contains( entry.getId() ) ) {
153 tmpStations.entries.add(entry);
154 }
155 }
156
157
158
159 return tmpStations;
160 }
161
162 }

  ViewVC Help
Powered by ViewVC 1.1.20