package dk.thoerup.droidradar; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.StringReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import android.location.Location; import android.util.Log; public class DroidLocator extends DefaultHandler implements Runnable { static public interface DroidsLocatedListener { public void onDroidsLocated(); } private final static String ENDPOINT = "http://app.t-hoerup.dk/DroidRadarService/LocationService"; private List droids; DroidBean tmpDroid; StringBuilder builder = new StringBuilder(); private boolean doContinue; long lastListUpdate = 0; long imei; Location currentLocation; DroidsLocatedListener listener; public boolean locateDroids(long imei, String handle, String group, Location loc) { boolean success = false; droids = new ArrayList(); try { URL url = new URL( generateUrl(imei, handle, group, loc)); URLConnection conn = url.openConnection(); conn.setConnectTimeout(20000); InputStream is = conn.getInputStream(); byte buf[] = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int count; while ((count = is.read(buf)) != -1) { baos.write(buf,0,count); } is.close(); baos.close(); String xml = baos.toString(); InputSource source = new InputSource( new StringReader(xml)); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(this); xr.setErrorHandler(this); xr.setDTDHandler(this); xr.parse(source); success = true; } catch (Exception e) { Log.e("DroidLocator","Error", e); } return success; } public List getDroids() { return droids; } private String generateUrl(long imei, String handle, String group, Location loc) { StringBuffer buf = new StringBuffer(); buf.append(ENDPOINT); buf.append("?imei=").append(imei); buf.append("&handle=").append(handle); buf.append("&group=").append(group); buf.append("&latitude=").append(loc.getLatitude()); buf.append("&longitude=").append(loc.getLongitude()); return buf.toString(); } @Override public void characters (char ch[], int start, int length) { for (int i= start; i 5000 && currentLocation != null) { Log.i("RadarView", "updating droidlist"); //todo set handle and group locateDroids(imei, "-debug", "-debug", currentLocation); lastListUpdate = d.getTime(); if (listener != null) listener.onDroidsLocated(); } try { Thread.sleep(100); } catch (InterruptedException e ) {} } } }