1 |
torben |
1545 |
package dk.thoerup.traininfo.provider; |
2 |
|
|
|
3 |
|
|
import java.io.File; |
4 |
|
|
import java.io.IOException; |
5 |
|
|
import java.io.RandomAccessFile; |
6 |
|
|
import java.util.ArrayList; |
7 |
|
|
import java.util.Collections; |
8 |
|
|
import java.util.Comparator; |
9 |
|
|
|
10 |
|
|
import org.simpleframework.xml.Serializer; |
11 |
|
|
import org.simpleframework.xml.core.Persister; |
12 |
|
|
|
13 |
|
|
import android.content.Context; |
14 |
|
|
import android.location.Location; |
15 |
|
|
import android.util.Log; |
16 |
|
|
import dk.thoerup.android.traininfo.common.StationBean; |
17 |
|
|
import dk.thoerup.android.traininfo.common.StationEntry; |
18 |
|
|
import dk.thoerup.genericjavautils.HttpUtil; |
19 |
|
|
import dk.thoerup.traininfo.util.IntSet; |
20 |
|
|
import dk.thoerup.traininfo.util.XmlUtil; |
21 |
|
|
|
22 |
|
|
public class OfflineStationProvider implements StationProvider { |
23 |
|
|
|
24 |
|
|
StationBean stations = new StationBean(); |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
public boolean loadStations(Context context) throws Exception { |
28 |
|
|
File parent = context.getFilesDir(); |
29 |
|
|
File stationsFile = new File(parent, "stations.xml"); |
30 |
|
|
|
31 |
|
|
if (!stationsFile.exists()) |
32 |
|
|
return false; |
33 |
|
|
|
34 |
|
|
int size = (int) stationsFile.length(); |
35 |
|
|
byte data[] = new byte[size]; |
36 |
|
|
|
37 |
|
|
RandomAccessFile raf = new RandomAccessFile(stationsFile, "r"); |
38 |
|
|
raf.readFully(data); |
39 |
|
|
|
40 |
|
|
Serializer serializer = new Persister(); |
41 |
|
|
stations = serializer.read(StationBean.class, new String(data, "ISO-8859-1") ); |
42 |
|
|
|
43 |
|
|
Log.e("OFFLINE", "loaded" + stations.entries.size()); |
44 |
|
|
|
45 |
|
|
return true; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
public void downloadStations(Context context) throws IOException { |
49 |
|
|
File parent = context.getFilesDir(); |
50 |
|
|
File stationsFile = new File(parent, "stations.xml"); |
51 |
|
|
|
52 |
|
|
byte data[] = HttpUtil.getContent(XmlUtil.SERVICE_BASE + "/LocateStations?dump=1", 5000); |
53 |
|
|
|
54 |
|
|
RandomAccessFile raf = new RandomAccessFile(stationsFile, "rw"); |
55 |
|
|
Log.e("OFFLINE", "data size" + data.length); |
56 |
|
|
raf.setLength(0); //truncate |
57 |
|
|
raf.write(data); |
58 |
|
|
raf.close(); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
@Override |
64 |
|
|
public void purgeOldEntries() { |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
|
|
@Override |
69 |
|
|
public StationBean lookupStationsByLocation(Location location) { |
70 |
|
|
|
71 |
|
|
Location tmpLoc = new Location("GPS"); |
72 |
|
|
|
73 |
|
|
ArrayList<StationEntry> entries = new ArrayList<StationEntry>() ; |
74 |
|
|
|
75 |
|
|
for (StationEntry entry : stations.entries) { |
76 |
|
|
tmpLoc.setLatitude(entry.getLatitude()); |
77 |
|
|
tmpLoc.setLongitude(entry.getLongitude()); |
78 |
|
|
|
79 |
|
|
entry.setCalcdist( (int) location.distanceTo(tmpLoc) ); |
80 |
|
|
entries.add(entry); |
81 |
|
|
|
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
Collections.sort( entries, new Comparator<StationEntry>() { |
85 |
|
|
@Override |
86 |
|
|
public int compare(StationEntry object1, StationEntry object2) { |
87 |
|
|
if (object1.getCalcdist() == object2.getCalcdist()) |
88 |
|
|
return 0; |
89 |
|
|
|
90 |
|
|
if (object1.getCalcdist() > object2.getCalcdist()) |
91 |
|
|
return 1; |
92 |
|
|
else |
93 |
|
|
return -1; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
}); |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
StationBean tmpStations = new StationBean(); |
101 |
|
|
for (int i = 0; i<8; i++) { |
102 |
|
|
tmpStations.entries.add( entries.get(i) ); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
return tmpStations; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
@Override |
109 |
|
|
public StationBean lookupStationsByName(String name) { |
110 |
|
|
|
111 |
|
|
name = name.toLowerCase(); |
112 |
|
|
StationBean tmpStations = new StationBean(); |
113 |
|
|
for (StationEntry entry : stations.entries) { |
114 |
|
|
if (entry.getName().toLowerCase().startsWith(name) ) { |
115 |
|
|
tmpStations.entries.add(entry); |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
return tmpStations; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
@Override |
123 |
|
|
public StationBean lookupStationsByIds(String ids) { |
124 |
|
|
IntSet idset = new IntSet(); |
125 |
|
|
idset.fromString(ids); |
126 |
|
|
|
127 |
|
|
StationBean tmpStations = new StationBean(); |
128 |
|
|
for (StationEntry entry : stations.entries) { |
129 |
|
|
if (idset.contains( entry.getId() ) ) { |
130 |
|
|
tmpStations.entries.add(entry); |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
return tmpStations; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
} |