From 2d9fbc9ad69d83846d2eaee5be466f92842a6082 Mon Sep 17 00:00:00 2001 From: madwasp79 Date: Mon, 21 Jan 2019 17:59:09 +0200 Subject: [PATCH] Feature request: retain the original color coding when importing GPX #6386 - in progress --- .../main/java/net/osmand/util/Algorithms.java | 17 +++++++++++++++++ OsmAnd/src/net/osmand/plus/GPXUtilities.java | 18 +++++++++++++++--- .../net/osmand/plus/helpers/ImportHelper.java | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java index dcf6082a7f..f52a9cd855 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java @@ -646,6 +646,23 @@ public class Algorithms { } } + public static String colorNameToString(String colorName) { + Map colorTable = new HashMap<>(); + colorTable.put("black", "#000000"); + colorTable.put("white", "#FFFFFF"); + colorTable.put("red", "#ff0000"); + colorTable.put("orange", "#ff8800"); + colorTable.put("green", "#23b03b"); + colorTable.put("blue", "#2f7af5"); + for (String key : colorTable.keySet()) { + if(colorName.toLowerCase().contains(key)) { + return colorTable.get(key); + } + } + + return "#FF0000"; + } + private static String format(int i, String hexString) { while (hexString.length() < i) { hexString = "0" + hexString; diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index 166aa89fce..269ffcdadb 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -99,6 +99,10 @@ public class GPXUtilities { getExtensionsToWrite().put("color", Algorithms.colorToString(color)); } + public void setColor(String colorName) { + getExtensionsToWrite().put("color", Algorithms.colorNameToString(colorName)); + } + public void removeColor() { getExtensionsToWrite().remove("color"); } @@ -1510,18 +1514,26 @@ public class GPXUtilities { if (tok == XmlPullParser.START_TAG) { Object parse = parserState.peek(); String tag = parser.getName(); - if (extensionReadMode && parse instanceof GPXExtensions) { + if (extensionReadMode && parse != null) { String value = readText(parser, tag); if (value != null) { ((GPXExtensions) parse).getExtensionsToWrite().put(tag.toLowerCase(), value); if (tag.equals("speed") && parse instanceof WptPt) { try { ((WptPt) parse).speed = Float.parseFloat(value); - } catch (NumberFormatException e) { + } catch (NumberFormatException e) {} + } + if (tag.equals("trackextension") || tag.equals("color") || tag.equals("colour") || tag.equals("displaycolor")) { + int color; + try { + color = Integer.parseInt(value); + ((GPXExtensions) parse).setColor(color); + } catch (NumberFormatException nfe) { + ((GPXExtensions) parse).setColor(value); } } - } + } } else if (parse instanceof GPXExtensions && tag.equals("extensions")) { extensionReadMode = true; } else { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index b162a9b1fe..0b76162d07 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -261,6 +261,7 @@ public class ImportHelper { final List favourites = asFavourites(gpxFile.getPoints(), fileName, forceImportFavourites); final FavouritesDbHelper favoritesHelper = app.getFavorites(); for (final FavouritePoint favourite : favourites) { + favoritesHelper.deleteFavourite(favourite, false); favoritesHelper.addFavourite(favourite, false); } favoritesHelper.sortAll();