Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate 2016-02-02 11:21:51 +01:00
commit 93c5b9e383
4 changed files with 144 additions and 90 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 {
@ -75,9 +74,9 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
deviceControl = new OsMoControlDevice(app, this, service, tracker);
groups = new OsMoGroups(this, service, tracker, app);
}
ApplicationMode.regWidget("osmo_control", (ApplicationMode[])null);
if(app.getSettings().OSMO_AUTO_CONNECT.get() ||
(System.currentTimeMillis() - app.getSettings().OSMO_LAST_PING.get() < 5 * 60 * 1000 )) {
ApplicationMode.regWidget("osmo_control", (ApplicationMode[]) null);
if (app.getSettings().OSMO_AUTO_CONNECT.get() ||
(System.currentTimeMillis() - app.getSettings().OSMO_LAST_PING.get() < 5 * 60 * 1000)) {
service.connect(true);
}
return true;
@ -127,11 +126,11 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
@Override
public void updateLayers(OsmandMapTileView mapView, MapActivity activity) {
if(isActive()) {
if(olayer == null) {
if (isActive()) {
if (olayer == null) {
registerLayers(activity);
}
if(osmoControl == null) {
if (osmoControl == null) {
registerSideWidget(activity);
}
} else {
@ -141,7 +140,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
osmoControl = null;
layer.recreateControls();
}
if(olayer != null) {
if (olayer != null) {
activity.getMapView().removeLayer(olayer);
olayer = null;
}
@ -151,7 +150,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
@Override
public void registerLayers(MapActivity activity) {
registerSideWidget(activity);
if(olayer != null) {
if (olayer != null) {
activity.getMapView().removeLayer(olayer);
}
olayer = new OsMoPositionLayer(activity, this);
@ -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";
@ -198,14 +198,14 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
String uname = si.username;
if (uname != null && uname.length() > 0) {
if (uname.length() > 7) {
for(int k = 4; k < uname.length(); k++) {
if(!Character.isLetterOrDigit(uname.charAt(k)) &&
for (int k = 4; k < uname.length(); k++) {
if (!Character.isLetterOrDigit(uname.charAt(k)) &&
uname.charAt(k) != '.' && uname.charAt(k) != '-') {
uname = uname.substring(0, k);
break;
}
}
if(uname.length() > 12) {
if (uname.length() > 12) {
uname = uname.substring(0, 12);
}
}
@ -223,7 +223,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
int big = srcSignalinactive; // tracker.isEnabledTracker() ? srcSignalinactive : srcinactive;
long last = service.getLastCommandTime();
if (service.isActive()) {
if(tracker.isEnabledTracker() ) {
if (tracker.isEnabledTracker()) {
small = night ? R.drawable.widget_osmo_connected_location_night : R.drawable.widget_osmo_connected_location_day;
big = night ? R.drawable.widget_osmo_connected_location_night : R.drawable.widget_osmo_connected_location_day;
} else {
@ -232,7 +232,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
}
}
setText(txt, subtxt);
if(blinkImg != small) {
if (blinkImg != small) {
setImageDrawable(small);
}
if (last != lastUpdateTime) {
@ -243,7 +243,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
return true;
}
private void blink(int bigger, final int smaller ) {
private void blink(int bigger, final int smaller) {
blinkImg = smaller;
setImageDrawable(bigger);
map.getMyApplication().runInUIThread(new Runnable() {
@ -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,14 +330,30 @@ 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) {
errors = "";
}
if(generateToast) {
if (generateToast) {
publishProgress(app.getString(R.string.osmo_gpx_points_downloaded, name));
}
}
@ -361,18 +380,18 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
public AsyncTask<JSONObject, String, String> getDownloadGpxTask(final boolean makeVisible) {
return new AsyncTask<JSONObject, String, String> (){
return new AsyncTask<JSONObject, String, String>() {
@Override
protected String doInBackground(JSONObject... params) {
final File fl = app.getAppPath(IndexConstants.GPX_INDEX_DIR+"/osmo");
if(!fl.exists()) {
final File fl = app.getAppPath(IndexConstants.GPX_INDEX_DIR + "/osmo");
if (!fl.exists()) {
fl.mkdirs();
}
String errors = "";
for(JSONObject obj : params) {
for (JSONObject obj : params) {
try {
File f = new File(fl, obj.getString("name")+".gpx");
File f = new File(fl, obj.getString("name") + ".gpx");
long timestamp = obj.getLong("created") * 1000;
int color = 0;
if (obj.has("color")) {
@ -383,11 +402,11 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
}
boolean visible = obj.has("visible");
boolean changed = false;
if(!f.exists() || (f.lastModified() != timestamp) ) {
if (!f.exists() || (f.lastModified() != timestamp)) {
long len = !f.exists() ? -1 : f.length();
boolean sizeEqual = obj.has("size") && obj.getLong("size") == len;
boolean modifySupported = f.setLastModified(timestamp - 1);
if(sizeEqual && !modifySupported){
if (sizeEqual && !modifySupported) {
// false alarm
} else {
changed = true;
@ -396,7 +415,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
DownloadFileHelper df = new DownloadFileHelper(app);
InputStream is = df.getInputStreamToDownload(new URL(url), false);
int av = is.available();
if(av > 0 && !modifySupported && len == av) {
if (av > 0 && !modifySupported && len == av) {
// ignore
is.close();
} else {
@ -405,19 +424,19 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
}
}
}
if(visible && (changed || makeVisible)) {
if (visible && (changed || makeVisible)) {
GPXFile selectGPXFile = GPXUtilities.loadGPXFile(app, f);
if(color != 0) {
if (color != 0) {
selectGPXFile.setColor(color);
}
app.getSelectedGpxHelper().setGpxFileToDisplay(selectGPXFile);
}
} catch (JSONException e) {
e.printStackTrace();
errors += e.getMessage() +"\n";
errors += e.getMessage() + "\n";
} catch (IOException e) {
e.printStackTrace();
errors += e.getMessage() +"\n";
errors += e.getMessage() + "\n";
}
}
return errors;
@ -433,7 +452,7 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
}
fout.close();
is.close();
if(!f.setLastModified(timestamp)) {
if (!f.setLastModified(timestamp)) {
log.error("Timestamp updates are not supported");
}
}
@ -447,11 +466,13 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
app.showToastMessage(t.trim());
refreshMap();
}
};
}
;
@Override
protected void onPostExecute(String result) {
if(result.length() > 0) {
if (result.length() > 0) {
app.showToastMessage(app.getString(R.string.osmo_io_error) + result);
}
}

View file

@ -9,11 +9,26 @@ import org.apache.commons.logging.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
public class OsMoTracker implements OsMoReactor {
// 130.5143667 Maximum is 8 digits and minimum 1
private static final DecimalFormat floatingPointFormat = new DecimalFormat("0.0#######");
private static final DecimalFormat twoDigitsFormat = new DecimalFormat("0.0#");
private static final DecimalFormat integerFormat = new DecimalFormat("0");
static {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);
floatingPointFormat.setDecimalFormatSymbols(symbols);
twoDigitsFormat.setDecimalFormatSymbols(symbols);
integerFormat.setDecimalFormatSymbols(symbols);
}
private ConcurrentLinkedQueue<Location> bufferOfLocations = new ConcurrentLinkedQueue<Location>();
private OsMoService service;
private int locationsSent = 0;
@ -95,22 +110,30 @@ 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());
StringBuffer cmd = new StringBuffer();
cmd.append("L");
floatingPointFormat.format(loc.getLatitude(), cmd, new FieldPosition(cmd.length()));
cmd.append(":");
floatingPointFormat.format(loc.getLongitude(), cmd, new FieldPosition(cmd.length()));
if (loc.hasAccuracy()) {
cmd.append("H").append((int) loc.getAccuracy());
cmd.append("H");
integerFormat.format(loc.getAccuracy(), cmd, new FieldPosition(cmd.length()));
}
if (loc.hasAltitude()) {
cmd.append("A").append((int) loc.getAltitude());
cmd.append("A");
integerFormat.format(loc.getAltitude(), cmd, new FieldPosition(cmd.length()));
}
if (loc.hasSpeed() && (int) (loc.getSpeed() * 100) != 0) {
cmd.append("S").append((float) ((int) (loc.getSpeed() * 100)) / 100f);
cmd.append("S");
twoDigitsFormat.format(loc.getSpeed(), cmd, new FieldPosition(cmd.length()));
}
if (loc.hasBearing()) {
cmd.append("C").append((int) loc.getBearing());
cmd.append("C");
integerFormat.format(loc.getBearing(), cmd, new FieldPosition(cmd.length()));
}
if ((System.currentTimeMillis() - loc.getTime()) > 30000 && loc.getTime() != 0) {
cmd.append("T").append(loc.getTime());
cmd.append("T");
integerFormat.format(loc.getTime(), cmd, new FieldPosition(cmd.length()));
}
LOG.debug("formatLocation cmd=" + cmd);
return cmd.toString();