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 |
|
70 |
byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000); |
71 |
Serializer serializer = new Persister(); |
72 |
stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") ); |
73 |
|
74 |
|
75 |
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(stationsFile) ); |
76 |
Log.e("OFFLINE", "data size" + data.length); |
77 |
|
78 |
for (StationEntry entry : stations.entries) { |
79 |
out.writeObject(entry); |
80 |
} |
81 |
|
82 |
out.close(); |
83 |
} |
84 |
|
85 |
|
86 |
|
87 |
@Override |
88 |
public void purgeOldEntries() { |
89 |
} |
90 |
|
91 |
|
92 |
@Override |
93 |
public StationBean lookupStationsByLocation(Location location) { |
94 |
|
95 |
Location tmpLoc = new Location("GPS"); |
96 |
|
97 |
ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ; |
98 |
|
99 |
for (StationEntry entry : stations.entries) { |
100 |
tmpLoc.setLatitude(entry.getLatitude()); |
101 |
tmpLoc.setLongitude(entry.getLongitude()); |
102 |
|
103 |
entry.setCalcdist( (int) location.distanceTo(tmpLoc) ); |
104 |
entries.add(entry); |
105 |
|
106 |
} |
107 |
|
108 |
Collections.sort( entries, 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 |
|
122 |
|
123 |
|
124 |
StationBean tmpStations = new StationBean(); |
125 |
for (int i = 0; i<8; i++) { |
126 |
tmpStations.entries.add( entries.get(i) ); |
127 |
} |
128 |
|
129 |
return tmpStations; |
130 |
} |
131 |
|
132 |
@Override |
133 |
public StationBean lookupStationsByName(String name) { |
134 |
|
135 |
name = name.toLowerCase(); |
136 |
StationBean tmpStations = new StationBean(); |
137 |
for (StationEntry entry : stations.entries) { |
138 |
if (entry.getName().toLowerCase().startsWith(name) ) { |
139 |
tmpStations.entries.add(entry); |
140 |
} |
141 |
} |
142 |
|
143 |
return tmpStations; |
144 |
} |
145 |
|
146 |
@Override |
147 |
public StationBean lookupStationsByIds(String ids) { |
148 |
IntSet idset = new IntSet(); |
149 |
idset.fromString(ids); |
150 |
|
151 |
StationBean tmpStations = new StationBean(); |
152 |
for (StationEntry entry : stations.entries) { |
153 |
if (idset.contains( entry.getId() ) ) { |
154 |
tmpStations.entries.add(entry); |
155 |
} |
156 |
} |
157 |
|
158 |
|
159 |
|
160 |
return tmpStations; |
161 |
} |
162 |
|
163 |
} |