diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java index 0563a47902..94b89a9d50 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java @@ -44,7 +44,7 @@ public class DownloadFileHelper { return e != null && e.getMessage().equals("Interrupted"); } - private InputStream getInputStreamToDownload(final URL url, final boolean forceWifi) throws IOException { + public InputStream getInputStreamToDownload(final URL url, final boolean forceWifi) throws IOException { InputStream cis = new InputStream() { byte[] buffer = new byte[BUFFER_SIZE]; int bufLen = 0; diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoControlDevice.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoControlDevice.java index bae8a5a135..eb0a179a40 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoControlDevice.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoControlDevice.java @@ -23,9 +23,12 @@ public class OsMoControlDevice implements OsMoReactor { private OsMoService service; private OsmandApplication app; private OsMoTracker tracker; + private OsMoPlugin plugin; - public OsMoControlDevice(OsmandApplication app, OsMoService service, OsMoTracker tracker) { + public OsMoControlDevice(OsmandApplication app, OsMoPlugin plugin, + OsMoService service, OsMoTracker tracker) { this.app = app; + this.plugin = plugin; this.service = service; this.tracker = tracker; service.registerReactor(this); @@ -96,6 +99,8 @@ public class OsMoControlDevice implements OsMoReactor { } } return true; + } else if(command.equals("GPX_GET")) { + plugin.getDownloadGpxTask().execute(obj); } return false; } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java index 6a34ce5f00..3477bc025a 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java @@ -29,11 +29,13 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { private static final String DELETED = "deleted"; private static final String GROUP_TRACKER_ID = "group_tracker_id"; private static final String LAST_ONLINE = "last_online"; + private static final String TRACK = "track"; private OsMoTracker tracker; private OsMoService service; private OsMoGroupsStorage storage; private OsMoGroupsUIListener uiListener; + private OsMoPlugin plugin; public interface OsMoGroupsUIListener { @@ -42,7 +44,8 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { public void deviceLocationChanged(OsMoDevice device); } - public OsMoGroups(OsMoService service, OsMoTracker tracker, OsmandSettings settings) { + public OsMoGroups(OsMoPlugin plugin, OsMoService service, OsMoTracker tracker, OsmandSettings settings) { + this.plugin = plugin; this.service = service; this.tracker = tracker; service.registerReactor(this); @@ -282,6 +285,9 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { delta.add(device); } } + if(obj.has(TRACK)){ + plugin.getDownloadGpxTask().execute(obj.getJSONObject(TRACK)); + } if(deleteUsers) { for(OsMoDevice s : toDelete.values()) { s.deleted = System.currentTimeMillis(); diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java index 12c56a1e25..08b64ab5bf 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java @@ -1,18 +1,27 @@ package net.osmand.plus.osmo; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import java.text.SimpleDateFormat; +import net.osmand.IndexConstants; import net.osmand.Location; import net.osmand.data.LatLon; import net.osmand.plus.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; +import net.osmand.plus.GPXUtilities; +import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.SettingsActivity; +import net.osmand.plus.download.DownloadFileHelper; import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; import net.osmand.plus.osmo.OsMoService.SessionInfo; import net.osmand.plus.views.MapInfoLayer; @@ -22,10 +31,15 @@ import net.osmand.plus.views.OsmandMapLayer.DrawSettings; import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.mapwidgets.BaseMapWidget; import net.osmand.plus.views.mapwidgets.TextInfoWidget; + +import org.json.JSONException; +import org.json.JSONObject; + import android.content.DialogInterface; import android.content.Intent; import android.graphics.Paint; import android.graphics.drawable.Drawable; +import android.os.AsyncTask; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceScreen; @@ -40,16 +54,17 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer private OsMoGroups groups; private BaseMapWidget osmoControl; private OsMoPositionLayer olayer; + protected MapActivity mapActivity; // 2014-05-27 23:11:40 public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public OsMoPlugin(final OsmandApplication app) { - service = new OsMoService(app); + service = new OsMoService(app, this); tracker = new OsMoTracker(service, app.getSettings().OSMO_SAVE_TRACK_INTERVAL, app.getSettings().OSMO_AUTO_SEND_LOCATIONS); - new OsMoControlDevice(app, service, tracker); - groups = new OsMoGroups(service, tracker, app.getSettings()); + new OsMoControlDevice(app, this, service, tracker); + groups = new OsMoGroups(this, service, tracker, app.getSettings()); this.app = app; ApplicationMode.regWidget("osmo_control", (ApplicationMode[])null); } @@ -80,6 +95,8 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer public String getName() { return app.getString(R.string.osmo_plugin_name); } + + @Override public void updateLayers(OsmandMapTileView mapView, MapActivity activity) { @@ -157,13 +174,15 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer @Override public void mapActivityPause(MapActivity activity) { groups.setUiListener(null); + mapActivity = activity; } @Override public void mapActivityResume(MapActivity activity) { - if(olayer != null) { + if (olayer != null) { groups.setUiListener(olayer); } + mapActivity = null; } /** @@ -305,6 +324,60 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer return service; } - + public AsyncTask getDownloadGpxTask() { + + return new AsyncTask (){ + + @Override + protected String doInBackground(JSONObject... params) { + final File fl = app.getAppPath(IndexConstants.GPX_INDEX_DIR+"/osmo"); + if(!fl.exists()) { + fl.mkdirs(); + } + String errors = ""; + for(JSONObject obj : params) { + try { + File f = new File(fl, obj.getString("name")); + long timestamp = obj.getLong("timestamp"); + boolean visible = obj.has("visible"); + if(!f.exists() || fl.lastModified() != timestamp) { + String url = obj.getString("url"); + DownloadFileHelper df = new DownloadFileHelper(app); + InputStream is = df.getInputStreamToDownload(new URL(url), false); + FileOutputStream fout = new FileOutputStream(f); + byte[] buf = new byte[1024]; + int k; + while((k = is.read(buf)) >= 0) { + fout.write(buf, 0, k); + } + fout.close(); + is.close(); + } + if(visible) { + GPXFile selectGPXFile = GPXUtilities.loadGPXFile(app, f); + app.setGpxFileToDisplay(selectGPXFile, app.getSettings().SHOW_CURRENT_GPX_TRACK.get()); + } + } catch (JSONException e) { + e.printStackTrace(); + errors += e.getMessage() +"\n"; + } catch (IOException e) { + e.printStackTrace(); + errors += e.getMessage() +"\n"; + } + } + return errors; + } + + @Override + protected void onPostExecute(String result) { + if(result.length() > 0) { + app.showToastMessage(app.getString(R.string.osmo_io_error)+ result); + } + } + + }; + + } + } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java index 3c4c649182..f320ee576a 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java @@ -37,7 +37,7 @@ import android.widget.Toast; * */ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider, OsMoGroupsUIListener { - + private DisplayMetrics dm; private final MapActivity map; private OsmandMapTileView view; @@ -232,7 +232,6 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye @Override public void deviceLocationChanged(final OsMoDevice device) { - boolean sameId = Algorithms.objectEquals(followTrackerId, device.trackerId); boolean sameDestId = Algorithms.objectEquals(followDestinationId, device.trackerId); Location l = device.getLastLocation(); if(sameDestId && l != null) { @@ -249,19 +248,30 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye targets.navigateToPoint(lt, true, -1); } } - if(sameId && !schedule && l != null) { - schedule = true; + + boolean sameId = Algorithms.objectEquals(followTrackerId, device.trackerId); + if(sameId && !schedule && l != null) { ContextMenuLayer cl = map.getMapLayers().getContextMenuLayer(); - final boolean sameObject = Algorithms.objectEquals(device, cl.getFirstSelectedObject()) && cl.isVisible(); + final boolean sameObject; + if(cl.getFirstSelectedObject() instanceof OsMoDevice && cl.isVisible()) { + sameObject = Algorithms.objectEquals(device.trackerId, ((OsMoDevice) cl.getFirstSelectedObject()).trackerId) ; + } else{ + sameObject = false; + } LatLon mapLoc = new LatLon(map.getMapView().getLatitude(), map.getMapView().getLongitude()); final boolean centered = Algorithms.objectEquals(followMapLocation, mapLoc); if(sameObject || centered) { + final LatLon loc; if(centered ) { - followMapLocation = new LatLon(l.getLatitude(), l.getLongitude()); + loc = new LatLon(l.getLatitude(), l.getLongitude()); } else if(!map.getMapView().getAnimatedDraggingThread().isAnimating()) { // disable tracking - followMapLocation = null; + loc = null; + } else { + loc = followMapLocation; } + followMapLocation = loc; + schedule = true; uiHandler.postDelayed(new Runnable() { @Override @@ -274,8 +284,8 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye cl.setSelectedObject(device); } if (centered) { - map.getMapView().setLatLon(followMapLocation.getLatitude(), - followMapLocation.getLongitude()); + map.getMapView().setLatLon(loc.getLatitude(), + loc.getLongitude()); } map.getMapView().refreshMap(); } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java index 72e656686d..1a6eb14b43 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoService.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue; import net.osmand.PlatformUtil; +import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; @@ -27,6 +28,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.os.AsyncTask; import android.os.Build; import android.provider.Settings.Secure; @@ -41,12 +43,14 @@ public class OsMoService implements OsMoReactor { public static final String SHARE_TRACKER_URL = "http://z.osmo.mobi/connect?id="; public static final String SHARE_GROUP_URL = "http://z.osmo.mobi/join?id="; public static final String TRACK_URL = "http://test1342.osmo.mobi/u/"; - private String lastRegistrationError = null; + private String lastRegistrationError = null; + private OsMoPlugin plugin; - public OsMoService(OsmandApplication app) { + public OsMoService(OsmandApplication app, OsMoPlugin plugin) { this.app = app; + this.plugin = plugin; listReactors.add(this); } @@ -269,13 +273,27 @@ public class OsMoService implements OsMoReactor { si.motd = data; } return true; + } else if(command.equals("GET_MY_TRACKS")) { + try { + JSONArray ar = new JSONArray(data); + AsyncTask task = plugin.getDownloadGpxTask(); + JSONObject[] a = new JSONObject[ar.length()]; + for(int i = 0; i < a.length; i++) { + a[i] = (JSONObject) ar.get(i); + } + task.execute(a); + } catch (JSONException e) { + e.printStackTrace(); + showErrorMessage(e.getMessage()); + } } return false; } + @Override public void reconnect() { - + pushCommand("GET_MY_TRACKS"); } public void reconnectToServer() { diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java index 7fd55ccc1d..a7b2eeb2ca 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoThread.java @@ -276,7 +276,11 @@ public class OsMoThread { if(obj != null && obj.has("error")) { error = true; try { - service.showErrorMessage(obj.getString("error")); + String s = obj.getString("error"); + if(obj.has("error_description")) { + s += " " +obj.getString("error_description"); + } + service.showErrorMessage(s); } catch (JSONException e) { e.printStackTrace(); } @@ -395,6 +399,9 @@ public class OsMoThread { private void cmd(String cmd, boolean send) { log.info("OsMO" + (send ? "> " : ">> ") + cmd); last100Commands.add((send ? "> " : ">> ") + df.format(new Date()) + " " + cmd); + while(last100Commands.size() > 100) { + last100Commands.poll(); + } } public SessionInfo getSessionInfo() {