Add gpx track download

This commit is contained in:
Victor Shcherb 2014-06-01 14:03:57 +02:00
parent fec6744d41
commit 1cb6b367c4
7 changed files with 140 additions and 21 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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();

View file

@ -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<JSONObject, GPXFile, String> getDownloadGpxTask() {
return new AsyncTask<JSONObject, GPXFile, String> (){
@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);
}
}
};
}
}

View file

@ -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();
}

View file

@ -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<JSONObject, GPXFile, String> 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() {

View file

@ -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() {