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 ele = Double.NaN;
public double speed = 0; public double speed = 0;
public double hdop = Double.NaN; public double hdop = Double.NaN;
public boolean deleted = false;
public WptPt() { public WptPt() {
} }

View file

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

View file

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