1 |
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 |
public void setContinue(boolean cont) { |
143 |
doContinue = cont; |
144 |
} |
145 |
|
146 |
public void stop() { |
147 |
doContinue = false; |
148 |
} |
149 |
|
150 |
public void setImei( long imei) { |
151 |
this.imei = imei; |
152 |
} |
153 |
|
154 |
public void setCurrentLocation(Location location) { |
155 |
this.currentLocation = location; |
156 |
} |
157 |
|
158 |
|
159 |
|
160 |
public void setDroidsLocatedListener(DroidsLocatedListener listener) { |
161 |
this.listener = listener; |
162 |
} |
163 |
|
164 |
@Override |
165 |
public void run() { |
166 |
|
167 |
Date d; |
168 |
while (doContinue) { |
169 |
d = new Date(); |
170 |
|
171 |
if ( (d.getTime()-lastListUpdate) > 5000 && currentLocation != null) |
172 |
{ |
173 |
Log.i("RadarView", "updating droidlist"); |
174 |
|
175 |
//todo set handle and group |
176 |
locateDroids(imei, "-debug", "-debug", currentLocation); |
177 |
lastListUpdate = d.getTime(); |
178 |
|
179 |
if (listener != null) |
180 |
listener.onDroidsLocated(); |
181 |
} |
182 |
try { |
183 |
Thread.sleep(100); |
184 |
} catch (InterruptedException e ) {} |
185 |
} |
186 |
Log.i("DroidLocator", "locator thread exiting"); |
187 |
} |
188 |
|
189 |
} |