Implemented synchronization of point deletion and addition in OsMo

This commit is contained in:
GaidamakUA 2016-02-01 17:47:26 +02:00
parent eddcce825d
commit 56ea9d74a3
3 changed files with 114 additions and 83 deletions

View file

@ -97,6 +97,7 @@ public class GPXUtilities {
public double ele = Double.NaN;
public double speed = 0;
public double hdop = Double.NaN;
public boolean deleted = false;
public WptPt() {
}

View file

@ -164,7 +164,7 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
if (command.equalsIgnoreCase("GP")) {
group = storage.getGroup(gid);
if (group != null && gid.length() > 0) {
List<OsMoDevice> delta = mergeGroup(group, obj, false);
List<OsMoDevice> delta = mergeGroup(group, obj, false, false);
String mygid = service.getMyGroupTrackerId();
StringBuilder b = new StringBuilder();
for (OsMoDevice d : delta) {
@ -197,7 +197,7 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
} else if (command.equalsIgnoreCase("GROUP_CONNECT")) {
group = storage.getGroup(gid);
if (group != null) {
mergeGroup(group, obj, true);
mergeGroup(group, obj, true, true);
group.active = true;
// connect to enabled devices in group
for (OsMoDevice d : group.getGroupUsers(null)) {
@ -326,7 +326,8 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
}
private List<OsMoDevice> mergeGroup(OsMoGroup gr, JSONObject obj, boolean deleteUsers) {
private List<OsMoDevice> mergeGroup(OsMoGroup gr, JSONObject obj, boolean deleteUsers,
boolean isGroupConnect) {
List<OsMoDevice> delta = new ArrayList<OsMoDevice>();
try {
if (obj.has("group")) {
@ -392,6 +393,9 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
a[i] = (JSONObject) ar.get(i);
final JSONObject jobj = a[i];
WptPt pt = new WptPt();
if (jobj.has("deleted")) {
pt.deleted = true;
} else {
pt.lat = a[i].getDouble("lat");
pt.lon = a[i].getDouble("lon");
if (jobj.has("name")) {
@ -409,10 +413,15 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
if (jobj.has("visible")) {
pt.getExtensionsToWrite().put("visible", a[i].getBoolean("visible") + "");
}
}
if (jobj.has("u")) {
pt.getExtensionsToWrite().put("u", String.valueOf(a[i].getLong("u")));
}
points.add(pt);
}
if (points.size() > 0) {
plugin.getSaveGpxTask(gr.name + " points", modify, false).execute(points.toArray(new WptPt[points.size()]));
plugin.getSaveGpxTask(gr.name + " points", modify, false, isGroupConnect)
.execute(points.toArray(new WptPt[points.size()]));
}
}
if (deleteUsers) {

View file

@ -39,7 +39,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
@ -189,6 +188,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
final TextInfoWidget osmoControl = new TextInfoWidget(map) {
long lastUpdateTime;
private int blinkImg;
@Override
public boolean updateInfo(DrawSettings drawSettings) {
String txt = "OsMo";
@ -303,7 +303,10 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
return service;
}
public AsyncTask<WptPt, String, String> getSaveGpxTask(final String name, final long timestamp, final boolean generateToast) {
public AsyncTask<WptPt, String, String> getSaveGpxTask(final String name,
final long timestamp,
final boolean generateToast,
final boolean isGroupConnect) {
return new AsyncTask<WptPt, String, String>() {
protected void onProgressUpdate(String... values) {
@ -327,8 +330,24 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
boolean changed = false;
if (!ps.exists() || (ps.lastModified() / 1000) != (timestamp / 1000)) {
changed = true;
GPXFile g = new GPXFile();
g.points.addAll(Arrays.asList(params));
GPXFile g;
if (isGroupConnect) {
g = new GPXFile();
} else {
g = GPXUtilities.loadGPXFile(app, ps);
}
for (WptPt point : params) {
if (point.deleted) {
for (WptPt pointInTrack : g.points) {
if (pointInTrack.getExtensionsToRead().get("u").equals(
point.getExtensionsToRead().get("u"))) {
g.points.remove(pointInTrack);
}
}
} else {
g.points.add(point);
}
}
errors = GPXUtilities.writeGpxFile(ps, g, app);
ps.setLastModified(timestamp);
if (errors == null) {
@ -447,7 +466,9 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
app.showToastMessage(t.trim());
refreshMap();
}
};
}
;
@Override
protected void onPostExecute(String result) {