1 |
torben |
277 |
package dk.thoerup.droidradar; |
2 |
|
|
|
3 |
|
|
import java.io.ByteArrayOutputStream; |
4 |
|
|
import java.io.InputStream; |
5 |
|
|
import java.io.StringReader; |
6 |
|
|
import java.net.URL; |
7 |
|
|
import java.net.URLConnection; |
8 |
|
|
import java.util.ArrayList; |
9 |
|
|
import java.util.Date; |
10 |
|
|
import java.util.List; |
11 |
|
|
|
12 |
|
|
import javax.xml.parsers.SAXParser; |
13 |
|
|
import javax.xml.parsers.SAXParserFactory; |
14 |
|
|
|
15 |
|
|
import org.xml.sax.Attributes; |
16 |
|
|
import org.xml.sax.InputSource; |
17 |
|
|
import org.xml.sax.SAXException; |
18 |
|
|
import org.xml.sax.XMLReader; |
19 |
|
|
import org.xml.sax.helpers.DefaultHandler; |
20 |
|
|
|
21 |
|
|
import android.location.Location; |
22 |
|
|
import android.util.Log; |
23 |
|
|
|
24 |
|
|
public class DroidLocator extends DefaultHandler implements Runnable { |
25 |
|
|
|
26 |
|
|
static public interface DroidsLocatedListener { |
27 |
|
|
public void onDroidsLocated(); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
private final static String ENDPOINT = "http://app.t-hoerup.dk/DroidRadarService/LocationService"; |
31 |
|
|
|
32 |
|
|
private List<DroidBean> droids; |
33 |
|
|
DroidBean tmpDroid; |
34 |
|
|
StringBuilder builder = new StringBuilder(); |
35 |
|
|
|
36 |
|
|
private boolean doContinue; |
37 |
|
|
long lastListUpdate = 0; |
38 |
|
|
|
39 |
|
|
long imei; |
40 |
|
|
Location currentLocation; |
41 |
|
|
|
42 |
|
|
DroidsLocatedListener listener; |
43 |
|
|
|
44 |
|
|
public boolean locateDroids(long imei, String handle, String group, Location loc) { |
45 |
|
|
boolean success = false; |
46 |
|
|
|
47 |
|
|
droids = new ArrayList<DroidBean>(); |
48 |
|
|
|
49 |
|
|
try { |
50 |
|
|
URL url = new URL( generateUrl(imei, handle, group, loc)); |
51 |
|
|
URLConnection conn = url.openConnection(); |
52 |
|
|
conn.setConnectTimeout(20000); |
53 |
|
|
|
54 |
|
|
InputStream is = conn.getInputStream(); |
55 |
|
|
|
56 |
|
|
byte buf[] = new byte[1024]; |
57 |
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
58 |
|
|
int count; |
59 |
|
|
|
60 |
|
|
while ((count = is.read(buf)) != -1) { |
61 |
|
|
baos.write(buf,0,count); |
62 |
|
|
} |
63 |
|
|
is.close(); |
64 |
|
|
baos.close(); |
65 |
|
|
|
66 |
|
|
String xml = baos.toString(); |
67 |
|
|
|
68 |
|
|
InputSource source = new InputSource( new StringReader(xml)); |
69 |
|
|
SAXParserFactory spf = SAXParserFactory.newInstance(); |
70 |
|
|
SAXParser sp = spf.newSAXParser(); |
71 |
|
|
XMLReader xr = sp.getXMLReader(); |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
xr.setContentHandler(this); |
75 |
|
|
xr.setErrorHandler(this); |
76 |
|
|
xr.setDTDHandler(this); |
77 |
|
|
xr.parse(source); |
78 |
|
|
|
79 |
|
|
success = true; |
80 |
|
|
} catch (Exception e) { |
81 |
|
|
Log.e("DroidLocator","Error", e); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
return success; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
public List<DroidBean> getDroids() { |
89 |
|
|
return droids; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
private String generateUrl(long imei, String handle, String group, Location loc) { |
93 |
|
|
StringBuffer buf = new StringBuffer(); |
94 |
|
|
buf.append(ENDPOINT); |
95 |
|
|
buf.append("?imei=").append(imei); |
96 |
|
|
buf.append("&handle=").append(handle); |
97 |
|
|
buf.append("&group=").append(group); |
98 |
|
|
buf.append("&latitude=").append(loc.getLatitude()); |
99 |
|
|
buf.append("&longitude=").append(loc.getLongitude()); |
100 |
|
|
|
101 |
|
|
return buf.toString(); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
@Override |
105 |
|
|
public void characters (char ch[], int start, int length) |
106 |
|
|
{ |
107 |
|
|
for (int i= start; i<start+length; i++) |
108 |
|
|
builder.append(ch[i]); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
|
112 |
|
|
@Override |
113 |
|
|
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { |
114 |
|
|
if (localName.equalsIgnoreCase("droid")) |
115 |
|
|
tmpDroid = new DroidBean(); |
116 |
|
|
|
117 |
|
|
builder.setLength(0); //reset StringBuilder |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
@Override |
122 |
|
|
public void endElement (String uri, String name, String qName) throws SAXException |
123 |
|
|
{ |
124 |
|
|
String str = builder.toString(); |
125 |
|
|
|
126 |
|
|
if (name.equals("droid")) { |
127 |
|
|
droids.add(tmpDroid); |
128 |
|
|
} else if (name.equals("imei")) { |
129 |
|
|
tmpDroid.setImei( Long.parseLong(str) ); |
130 |
|
|
} else if (name.equals("handle")) { |
131 |
|
|
tmpDroid.setHandle(str); |
132 |
|
|
} else if (name.equals("group")) { |
133 |
|
|
tmpDroid.setGroup(str); |
134 |
|
|
} else if (name.equals("latitude")) { |
135 |
|
|
tmpDroid.setLatitude( Double.parseDouble( str )); |
136 |
|
|
} else if (name.equals("longitude")) { |
137 |
|
|
tmpDroid.setLongitude( Double.parseDouble(str)); |
138 |
|
|
} else if (name.equals("lastupdate")) { |
139 |
|
|
tmpDroid.setLastUpdate( Integer.parseInt(str)); |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
public void stop() { |
144 |
|
|
doContinue = false; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
public void setImei( long imei) { |
148 |
|
|
this.imei = imei; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
public void setCurrentLocation(Location location) { |
152 |
|
|
this.currentLocation = location; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
public void setDroidsLocatedListener(DroidsLocatedListener listener) { |
158 |
|
|
this.listener = listener; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
@Override |
162 |
|
|
public void run() { |
163 |
|
|
doContinue = true; |
164 |
|
|
Date d; |
165 |
|
|
while (doContinue) { |
166 |
|
|
d = new Date(); |
167 |
|
|
|
168 |
|
|
if ( (d.getTime()-lastListUpdate) > 5000 && currentLocation != null) |
169 |
|
|
{ |
170 |
|
|
Log.i("RadarView", "updating droidlist"); |
171 |
|
|
|
172 |
|
|
//todo set handle and group |
173 |
|
|
locateDroids(imei, "-debug", "-debug", currentLocation); |
174 |
|
|
lastListUpdate = d.getTime(); |
175 |
|
|
|
176 |
|
|
if (listener != null) |
177 |
|
|
listener.onDroidsLocated(); |
178 |
|
|
} |
179 |
|
|
try { |
180 |
|
|
Thread.sleep(100); |
181 |
|
|
} catch (InterruptedException e ) {} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
} |