Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate 2015-06-14 14:29:39 +02:00
commit 63ef931a96
4 changed files with 98 additions and 36 deletions

View file

@ -27,6 +27,7 @@ public class GpxSelectionHelper {
private static final String CURRENT_TRACK = "currentTrack"; private static final String CURRENT_TRACK = "currentTrack";
private static final String FILE = "file"; private static final String FILE = "file";
private static final String COLOR = "color";
private OsmandApplication app; private OsmandApplication app;
// save into settings // save into settings
// public final CommonPreference<Boolean> SHOW_CURRENT_GPX_TRACK = // public final CommonPreference<Boolean> SHOW_CURRENT_GPX_TRACK =
@ -307,6 +308,10 @@ public class GpxSelectionHelper {
p.startTask(getString(R.string.loading_smth, fl.getName()), -1); p.startTask(getString(R.string.loading_smth, fl.getName()), -1);
} }
GPXFile gpx = GPXUtilities.loadGPXFile(app, fl); GPXFile gpx = GPXUtilities.loadGPXFile(app, fl);
if(obj.has(COLOR)) {
int clr = Algorithms.parseColor(obj.getString(COLOR));
gpx.setColor(clr);
}
if(gpx.warning != null) { if(gpx.warning != null) {
save = true; save = true;
} else { } else {
@ -336,6 +341,9 @@ public class GpxSelectionHelper {
obj.put(CURRENT_TRACK, true); obj.put(CURRENT_TRACK, true);
} else if(!Algorithms.isEmpty(s.gpxFile.path)) { } else if(!Algorithms.isEmpty(s.gpxFile.path)) {
obj.put(FILE, s.gpxFile.path); obj.put(FILE, s.gpxFile.path);
if(s.gpxFile.getColor(0) != 0) {
obj.put(COLOR, Algorithms.colorToString(s.gpxFile.getColor(0)));
}
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -1,9 +1,18 @@
package net.osmand.plus.osmo; package net.osmand.plus.osmo;
import android.text.TextUtils; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.osmand.Location; import net.osmand.Location;
import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice;
@ -15,14 +24,6 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
private static final String GROUP_NAME = "name"; private static final String GROUP_NAME = "name";
@ -206,6 +207,8 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
group = storage.getGroup(gid); group = storage.getGroup(gid);
if(group != null) { if(group != null) {
disconnectAllGroupUsers(group); disconnectAllGroupUsers(group);
disableGroupTracks(group, group.groupTracks);
disableGroupTracks(group, Collections.singleton(group.name + " points.gpx"));
} }
processed = true; processed = true;
} else if(command.equalsIgnoreCase("GROUP_CONNECT")) { } else if(command.equalsIgnoreCase("GROUP_CONNECT")) {
@ -354,6 +357,19 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
return processed; return processed;
} }
private void disableGroupTracks(OsMoGroup group, Collection<String> tracks) {
if(!tracks.isEmpty()) {
GpxSelectionHelper helper = app.getSelectedGpxHelper();
for(String t : tracks) {
SelectedGpxFile sg = helper.getSelectedFileByName("osmo/"+t);
if(sg != null && sg.getGpxFile() != null) {
helper.selectGpxFile(sg.getGpxFile(), false, false);
}
}
}
}
private OsMoGroup parseGroup(JSONObject obj) throws JSONException { private OsMoGroup parseGroup(JSONObject obj) throws JSONException {
OsMoGroup groupe = new OsMoGroup(); OsMoGroup groupe = new OsMoGroup();
groupe.enabled = !(obj.has(ACTIVE) && ("0".equals(obj.getString(ACTIVE)) || groupe.enabled = !(obj.has(ACTIVE) && ("0".equals(obj.getString(ACTIVE)) ||
@ -444,10 +460,16 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
if (obj.has(TRACK)) { if (obj.has(TRACK)) {
JSONArray ar = obj.getJSONArray(TRACK); JSONArray ar = obj.getJSONArray(TRACK);
JSONObject[] a = new JSONObject[ar.length()]; JSONObject[] a = new JSONObject[ar.length()];
Set<String> toDeleteT = new HashSet<String>(gr.groupTracks);
gr.groupTracks.clear();
for (int i = 0; i < a.length; i++) { for (int i = 0; i < a.length; i++) {
a[i] = (JSONObject) ar.get(i); a[i] = (JSONObject) ar.get(i);
String track = a[i].getString("name") + ".gpx";
gr.groupTracks.add(track);
toDeleteT.remove(track);
} }
plugin.getDownloadGpxTask(true).execute(a); plugin.getDownloadGpxTask(true).execute(a);
disableGroupTracks(gr, toDeleteT);
} }
if (obj.has(POINT)) { if (obj.has(POINT)) {

View file

@ -25,6 +25,7 @@ import android.graphics.Color;
public class OsMoGroupsStorage { public class OsMoGroupsStorage {
private static final String GROUPS = "groups"; private static final String GROUPS = "groups";
private static final String USERS = "users"; private static final String USERS = "users";
private static final String TRACKS = "tracks";
private static final String SERVER_NAME = "serverName"; private static final String SERVER_NAME = "serverName";
private static final String GROUP_ID = "group_id"; private static final String GROUP_ID = "group_id";
private static final String TRACKER_ID = "trackerId"; private static final String TRACKER_ID = "trackerId";
@ -102,6 +103,7 @@ public class OsMoGroupsStorage {
group.enabled = true; group.enabled = true;
} }
parseGroupUsers(group, o); parseGroupUsers(group, o);
// parseGroupTracks(group, obj);
this.groups.put(group.groupId, group); this.groups.put(group.groupId, group);
} }
} catch (JSONException e) { } catch (JSONException e) {
@ -141,6 +143,7 @@ public class OsMoGroupsStorage {
obj.put(GROUP_ID, gr.groupId); obj.put(GROUP_ID, gr.groupId);
ar.put(obj); ar.put(obj);
saveGroupUsers(gr, obj); saveGroupUsers(gr, obj);
saveGroupTracks(gr, obj);
} }
mainObj.put(GROUPS, ar); mainObj.put(GROUPS, ar);
} catch (JSONException e) { } catch (JSONException e) {
@ -181,8 +184,20 @@ public class OsMoGroupsStorage {
} }
grObj.put(USERS, ar); grObj.put(USERS, ar);
} }
private void saveGroupTracks(OsMoGroup gr, JSONObject grObj) throws JSONException {
if (gr.groupTracks.size() > 0) {
JSONArray ar = new JSONArray();
for (String u : gr.groupTracks) {
JSONObject obj = new JSONObject();
obj.put(NAME, u);
ar.put(obj);
}
grObj.put(TRACKS, ar);
}
}
private void parseGroupUsers(OsMoGroup gr, JSONObject obj) throws JSONException { protected void parseGroupUsers(OsMoGroup gr, JSONObject obj) throws JSONException {
if(!obj.has(USERS)) { if(!obj.has(USERS)) {
return; return;
} }
@ -216,6 +231,19 @@ public class OsMoGroupsStorage {
gr.users.put(user.trackerId, user); gr.users.put(user.trackerId, user);
} }
} }
protected void parseGroupTracks(OsMoGroup gr, JSONObject obj) throws JSONException {
if(!obj.has(TRACKS)) {
return;
}
JSONArray tracks = obj.getJSONArray(TRACKS);
for (int i = 0; i < tracks.length(); i++) {
JSONObject o = (JSONObject) tracks.get(i);
if(o.has(NAME)) {
gr.groupTracks.add(NAME);
}
}
}
public static class OsMoGroup { public static class OsMoGroup {
protected String name; protected String name;

View file

@ -1,6 +1,7 @@
package net.osmand.plus.osmo; package net.osmand.plus.osmo;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -400,8 +401,10 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
boolean visible = obj.has("visible"); boolean visible = obj.has("visible");
boolean changed = false; boolean changed = false;
if(!f.exists() || (f.lastModified() != timestamp) ) { if(!f.exists() || (f.lastModified() != timestamp) ) {
boolean sizeEqual = f.exists() && obj.has("size") && obj.getLong("size") == f.length(); long len = !f.exists() ? -1 : f.length();
if(sizeEqual && !f.setLastModified(timestamp - 1)){ boolean sizeEqual = obj.has("size") && obj.getLong("size") == len;
boolean modifySupported = f.setLastModified(timestamp - 1);
if(sizeEqual && !modifySupported){
// false alarm // false alarm
} else { } else {
changed = true; changed = true;
@ -409,35 +412,21 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
log.info("Download gpx " + url); log.info("Download gpx " + url);
DownloadFileHelper df = new DownloadFileHelper(app); DownloadFileHelper df = new DownloadFileHelper(app);
InputStream is = df.getInputStreamToDownload(new URL(url), false); InputStream is = df.getInputStreamToDownload(new URL(url), false);
FileOutputStream fout = new FileOutputStream(f); int av = is.available();
byte[] buf = new byte[1024]; if(av > 0 && !modifySupported && len == av) {
int k; // ignore
while ((k = is.read(buf)) >= 0) { is.close();
fout.write(buf, 0, k); } else {
} redownloadFile(f, timestamp, color, is);
fout.close(); publishProgress(app.getString(R.string.osmo_gpx_track_downloaded, obj.getString("name")));
is.close();
if(color != 0) {
try {
GPXFile loaded = GPXUtilities.loadGPXFile(app, f);
if(loaded.tracks.size() > 0) {
for(Track t : loaded.tracks) {
t.setColor(color);
}
GPXUtilities.writeGpxFile(f, loaded, app);
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
if(!f.setLastModified(timestamp)) {
log.error("Timestamp updates are not supported");
} }
} }
publishProgress(app.getString(R.string.osmo_gpx_track_downloaded, obj.getString("name")));
} }
if(visible && (changed || makeVisible)) { if(visible && (changed || makeVisible)) {
GPXFile selectGPXFile = GPXUtilities.loadGPXFile(app, f); GPXFile selectGPXFile = GPXUtilities.loadGPXFile(app, f);
if(color != 0) {
selectGPXFile.setColor(color);
}
app.getSelectedGpxHelper().setGpxFileToDisplay(selectGPXFile); app.getSelectedGpxHelper().setGpxFileToDisplay(selectGPXFile);
} }
} catch (JSONException e) { } catch (JSONException e) {
@ -450,6 +439,21 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
} }
return errors; return errors;
} }
private void redownloadFile(File f, long timestamp, int color, InputStream is)
throws FileNotFoundException, IOException {
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(!f.setLastModified(timestamp)) {
log.error("Timestamp updates are not supported");
}
}
protected void onProgressUpdate(String... values) { protected void onProgressUpdate(String... values) {
if (values != null) { if (values != null) {