diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java index 202ce643f8..42e351525f 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoTracker.java @@ -1,16 +1,18 @@ package net.osmand.plus.osmo; +import net.osmand.Location; +import net.osmand.PlatformUtil; +import net.osmand.plus.OsmandSettings.OsmandPreference; +import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; + +import org.apache.commons.logging.Log; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.Collection; import java.util.Map; import java.util.concurrent.ConcurrentLinkedQueue; -import net.osmand.Location; -import net.osmand.plus.OsmandSettings.OsmandPreference; -import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; - -import org.json.JSONException; -import org.json.JSONObject; - public class OsMoTracker implements OsMoReactor { private ConcurrentLinkedQueue bufferOfLocations = new ConcurrentLinkedQueue(); private OsMoService service; @@ -22,70 +24,71 @@ public class OsMoTracker implements OsMoReactor { private String sessionURL; private Map trackingDevices = new java.util.concurrent.ConcurrentHashMap(); private OsmandPreference stateSendLocation; - + protected static final Log LOG = PlatformUtil.getLog(OsMoTracker.class); + public interface OsmoTrackerListener { - + public void locationChange(String trackerId, Location location); } - - public OsMoTracker(OsMoService service, OsmandPreference interval, - OsmandPreference stateSendLocation) { + + public OsMoTracker(OsMoService service, OsmandPreference interval, + OsmandPreference stateSendLocation) { this.service = service; this.pref = interval; this.stateSendLocation = stateSendLocation; service.registerReactor(this); } - + public String getSessionURL() { if (!isEnabledTracker() || sessionURL == null) { return null; } return OsMoService.TRACK_URL + sessionURL; } - + public boolean isEnabledTracker() { return stateSendLocation.get(); } - + public void enableTracker() { - if(!isEnabledTracker()) { + if (!isEnabledTracker()) { enableTrackerCmd(); } } - + public void enableTrackerCmd() { stateSendLocation.set(true); service.pushCommand("TRACKER_SESSION_OPEN"); } - + public void disableTracker() { - if(isEnabledTracker()) { + if (isEnabledTracker()) { stateSendLocation.set(false); service.pushCommand("TRACKER_SESSION_CLOSE"); } } - + public void startTrackingId(OsMoDevice d) { - service.pushCommand("LISTEN:"+d.getTrackerId()); + service.pushCommand("LISTEN:" + d.getTrackerId()); trackingDevices.put(d.getTrackerId(), d); } - + public void stopTrackingId(OsMoDevice d) { - service.pushCommand("UNLISTEN:"+d.getTrackerId()); + service.pushCommand("UNLISTEN:" + d.getTrackerId()); trackingDevices.remove(d.getTrackerId()); } @Override public String nextSendCommand(OsMoThread thread) { - if(!bufferOfLocations.isEmpty()){ + if (!bufferOfLocations.isEmpty()) { Location loc = bufferOfLocations.poll(); lastSendLocation = loc; - locationsSent ++; - if((System.currentTimeMillis() - loc.getTime()) > 2 * 60000 && loc.getTime() != 0) { - return "B|"+formatLocation(loc); + locationsSent++; + if ((System.currentTimeMillis() - loc.getTime()) > 2 * 60000 && loc.getTime() != 0) { + return "B|" + formatLocation(loc); } else { - return "T|"+formatLocation(loc); + return "T|" + formatLocation(loc); } } return null; @@ -93,37 +96,38 @@ public class OsMoTracker implements OsMoReactor { public static String formatLocation(Location loc) { StringBuilder cmd = new StringBuilder(); - cmd.append("L").append((float)loc.getLatitude()).append(":").append((float)loc.getLongitude()); - if(loc.hasAccuracy()) { - cmd.append("H").append((int)loc.getAccuracy()); + cmd.append("L").append((float) loc.getLatitude()).append(":").append((float) loc.getLongitude()); + if (loc.hasAccuracy()) { + cmd.append("H").append((int) loc.getAccuracy()); } - if(loc.hasAltitude()) { - cmd.append("A").append((int)loc.getAltitude()); + if (loc.hasAltitude()) { + cmd.append("A").append((int) loc.getAltitude()); } - if(loc.hasSpeed()) { - cmd.append("S").append((float)((int)(loc.getSpeed()*100))/100f); + if (loc.hasSpeed()) { + cmd.append("S").append((float) ((int) (loc.getSpeed() * 100)) / 100f); } - if(loc.hasBearing()) { - cmd.append("C").append((int)loc.getBearing()); + if (loc.hasBearing()) { + cmd.append("C").append((int) loc.getBearing()); } - if((System.currentTimeMillis() - loc.getTime()) > 30000 && loc.getTime() != 0) { + if ((System.currentTimeMillis() - loc.getTime()) > 30000 && loc.getTime() != 0) { cmd.append("T").append(loc.getTime()); } + LOG.debug("formatLocation cmd=" + cmd); return cmd.toString(); } - + public Location getLastSendLocation() { return lastSendLocation; } public void sendCoordinate(Location location) { - if(stateSendLocation.get() && location != null) { + if (stateSendLocation.get() && location != null) { long ltime = lastBufferLocation == null ? 0 : lastBufferLocation.getTime(); - - if (location.getTime() - ltime > pref.get()) { - if(lastBufferLocation != null && (!lastBufferLocation.hasSpeed() || lastBufferLocation.getSpeed() < 1) && - lastBufferLocation.distanceTo(location) < 20){ - if(lastBufferLocation != null && location.getTime() - ltime < 60000) { + + if (location.getTime() - ltime > pref.get()) { + if (lastBufferLocation != null && (!lastBufferLocation.hasSpeed() || lastBufferLocation.getSpeed() < 1) && + lastBufferLocation.distanceTo(location) < 20) { + if (lastBufferLocation != null && location.getTime() - ltime < 60000) { // ignores return; } @@ -133,11 +137,11 @@ public class OsMoTracker implements OsMoReactor { } } } - + public int getLocationsSent() { return locationsSent; } - + public int getBufferLocationsSize() { return bufferOfLocations.size(); } @@ -149,17 +153,17 @@ public class OsMoTracker implements OsMoReactor { l.setLongitude(lon); sendCoordinate(l); } - + @Override public boolean acceptCommand(String command, String tid, String data, JSONObject obj, OsMoThread thread) { - if(command.equals("LISTEN")) { + if (command.equals("LISTEN")) { return true; - } else if(command.equals("UNLISTEN")) { + } else if (command.equals("UNLISTEN")) { return true; - } else if(command.equals("TRACKER_SESSION_CLOSE")) { + } else if (command.equals("TRACKER_SESSION_CLOSE")) { return true; - } else if(command.equals("TRACKER_SESSION_OPEN")) { + } else if (command.equals("TRACKER_SESSION_OPEN")) { try { sessionURL = obj.getString("url"); } catch (JSONException e) { @@ -167,15 +171,15 @@ public class OsMoTracker implements OsMoReactor { e.printStackTrace(); } return true; - } else if(command.equals("LT")) { + } else if (command.equals("LT")) { double lat = 0; double lon = 0; double speed = 0; int k = 0; for (int i = 1; i <= data.length(); i++) { - boolean separator = i == data.length() || - !(Character.isDigit(data.charAt(i)) || - data.charAt(i) == ':' || data.charAt(i) == '.' || data.charAt(i) == '-'); + boolean separator = i == data.length() || + !(Character.isDigit(data.charAt(i)) || + data.charAt(i) == ':' || data.charAt(i) == '.' || data.charAt(i) == '-'); if (separator) { char ch = data.charAt(k); String vl = data.substring(k + 1, i); @@ -189,15 +193,15 @@ public class OsMoTracker implements OsMoReactor { k = i; } } - if(lat != 0 || lon != 0) { + if (lat != 0 || lon != 0) { Location loc = new Location("osmo"); loc.setTime(System.currentTimeMillis()); loc.setLatitude(lat); loc.setLongitude(lon); - if(speed > 0) { + if (speed > 0) { loc.setSpeed((float) speed); } - if(trackerListener != null) { + if (trackerListener != null) { trackerListener.locationChange(tid, loc); } } @@ -205,15 +209,15 @@ public class OsMoTracker implements OsMoReactor { } return false; } - + public void setTrackerListener(OsmoTrackerListener trackerListener) { this.trackerListener = trackerListener; } - + public OsmoTrackerListener getTrackerListener() { return trackerListener; } - + public Collection getTrackingDevices() { return trackingDevices.values(); @@ -221,7 +225,7 @@ public class OsMoTracker implements OsMoReactor { @Override public void onConnected() { - if(stateSendLocation.get()) { + if (stateSendLocation.get()) { enableTrackerCmd(); } }