From 7b4b8bda5cfe4e66b2a1ccb62d5ad5ccc4d43cf1 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 12 Jun 2015 10:26:12 +0200 Subject: [PATCH 1/2] Save color selection --- OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index ee998b1b57..6a0bf34452 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -27,6 +27,7 @@ public class GpxSelectionHelper { private static final String CURRENT_TRACK = "currentTrack"; private static final String FILE = "file"; + private static final String COLOR = "color"; private OsmandApplication app; // save into settings // public final CommonPreference SHOW_CURRENT_GPX_TRACK = @@ -307,6 +308,10 @@ public class GpxSelectionHelper { p.startTask(getString(R.string.loading_smth, fl.getName()), -1); } GPXFile gpx = GPXUtilities.loadGPXFile(app, fl); + if(obj.has(COLOR)) { + int clr = Algorithms.parseColor(obj.getString(COLOR)); + gpx.setColor(clr); + } if(gpx.warning != null) { save = true; } else { @@ -336,6 +341,9 @@ public class GpxSelectionHelper { obj.put(CURRENT_TRACK, true); } else if(!Algorithms.isEmpty(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) { e.printStackTrace(); From f05fb1e440cbc632d5831f22aedd8b3645da1629 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 14 Jun 2015 14:08:46 +0200 Subject: [PATCH 2/2] Update osmo groups/poitns --- .../src/net/osmand/plus/osmo/OsMoGroups.java | 40 ++++++++++--- .../osmand/plus/osmo/OsMoGroupsStorage.java | 30 +++++++++- .../src/net/osmand/plus/osmo/OsMoPlugin.java | 56 ++++++++++--------- 3 files changed, 90 insertions(+), 36 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java index f09b205d65..0220285c1a 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroups.java @@ -1,9 +1,18 @@ 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.plus.GPXUtilities.WptPt; +import net.osmand.plus.GpxSelectionHelper; +import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; @@ -15,14 +24,6 @@ import org.json.JSONArray; import org.json.JSONException; 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 { private static final String GROUP_NAME = "name"; @@ -206,6 +207,8 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { group = storage.getGroup(gid); if(group != null) { disconnectAllGroupUsers(group); + disableGroupTracks(group, group.groupTracks); + disableGroupTracks(group, Collections.singleton(group.name + " points.gpx")); } processed = true; } else if(command.equalsIgnoreCase("GROUP_CONNECT")) { @@ -354,6 +357,19 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { return processed; } + private void disableGroupTracks(OsMoGroup group, Collection 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 { OsMoGroup groupe = new OsMoGroup(); groupe.enabled = !(obj.has(ACTIVE) && ("0".equals(obj.getString(ACTIVE)) || @@ -444,10 +460,16 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener { if (obj.has(TRACK)) { JSONArray ar = obj.getJSONArray(TRACK); JSONObject[] a = new JSONObject[ar.length()]; + Set toDeleteT = new HashSet(gr.groupTracks); + gr.groupTracks.clear(); for (int i = 0; i < a.length; 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); + disableGroupTracks(gr, toDeleteT); } if (obj.has(POINT)) { diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java index f4432324e7..c87f4ce8cc 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoGroupsStorage.java @@ -25,6 +25,7 @@ import android.graphics.Color; public class OsMoGroupsStorage { private static final String GROUPS = "groups"; private static final String USERS = "users"; + private static final String TRACKS = "tracks"; private static final String SERVER_NAME = "serverName"; private static final String GROUP_ID = "group_id"; private static final String TRACKER_ID = "trackerId"; @@ -102,6 +103,7 @@ public class OsMoGroupsStorage { group.enabled = true; } parseGroupUsers(group, o); +// parseGroupTracks(group, obj); this.groups.put(group.groupId, group); } } catch (JSONException e) { @@ -141,6 +143,7 @@ public class OsMoGroupsStorage { obj.put(GROUP_ID, gr.groupId); ar.put(obj); saveGroupUsers(gr, obj); + saveGroupTracks(gr, obj); } mainObj.put(GROUPS, ar); } catch (JSONException e) { @@ -181,8 +184,20 @@ public class OsMoGroupsStorage { } 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)) { return; } @@ -216,6 +231,19 @@ public class OsMoGroupsStorage { 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 { protected String name; diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java index b9bd206fd0..78e68ec435 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPlugin.java @@ -1,6 +1,7 @@ package net.osmand.plus.osmo; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -400,8 +401,10 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor { boolean visible = obj.has("visible"); boolean changed = false; if(!f.exists() || (f.lastModified() != timestamp) ) { - boolean sizeEqual = f.exists() && obj.has("size") && obj.getLong("size") == f.length(); - if(sizeEqual && !f.setLastModified(timestamp - 1)){ + 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){ // false alarm } else { changed = true; @@ -409,35 +412,21 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor { log.info("Download gpx " + 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(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"); + int av = is.available(); + if(av > 0 && !modifySupported && len == av) { + // ignore + is.close(); + } else { + redownloadFile(f, timestamp, color, is); + publishProgress(app.getString(R.string.osmo_gpx_track_downloaded, obj.getString("name"))); } } - publishProgress(app.getString(R.string.osmo_gpx_track_downloaded, obj.getString("name"))); } if(visible && (changed || makeVisible)) { GPXFile selectGPXFile = GPXUtilities.loadGPXFile(app, f); + if(color != 0) { + selectGPXFile.setColor(color); + } app.getSelectedGpxHelper().setGpxFileToDisplay(selectGPXFile); } } catch (JSONException e) { @@ -450,6 +439,21 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor { } 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) { if (values != null) {