From a0c83cf8b527645ce98d562a276c1c0708649403 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 6 Jul 2020 12:30:55 +0300 Subject: [PATCH] Add track gradient scale types --- .../main/java/net/osmand/GPXUtilities.java | 118 +++++++++++------- OsmAnd/src/net/osmand/plus/GPXDatabase.java | 83 +++++++++++- OsmAnd/src/net/osmand/plus/GpxDbHelper.java | 9 +- .../net/osmand/plus/GpxSelectionHelper.java | 11 ++ 4 files changed, 171 insertions(+), 50 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java index ef54ed7edb..077e249ac8 100644 --- a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java +++ b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java @@ -42,7 +42,9 @@ import java.util.Stack; import java.util.TimeZone; public class GPXUtilities { + public final static Log log = PlatformUtil.getLog(GPXUtilities.class); + private static final String ICON_NAME_EXTENSION = "icon"; private static final String DEFAULT_ICON_NAME = "special_star"; private static final String BACKGROUND_TYPE_EXTENSION = "background"; @@ -115,6 +117,13 @@ public class GPXUtilities { return extensions; } + public Map getExtensionsToWrite() { + if (extensions == null) { + extensions = new LinkedHashMap<>(); + } + return extensions; + } + public GPXExtensionsWriter getExtensionsWriter() { return extensionsWriter; } @@ -148,50 +157,7 @@ public class GPXUtilities { getExtensionsToWrite().remove("color"); } - public String getWidth(String defWidth) { - String widthValue = null; - if (extensions != null) { - widthValue = extensions.get("width"); - } - return widthValue != null ? widthValue : defWidth; - } - - public void setWidth(String width) { - getExtensionsToWrite().put("width", width); - } - - public boolean isShowArrows() { - String showArrows = null; - if (extensions != null) { - showArrows = extensions.get("showArrows"); - } - return Boolean.parseBoolean(showArrows); - } - - public void setShowArrows(boolean showArrows) { - getExtensionsToWrite().put("showArrows", String.valueOf(showArrows)); - } - - public boolean isShowStartFinish() { - String showStartFinish = null; - if (extensions != null) { - showStartFinish = extensions.get("showStartFinish"); - } - return Boolean.parseBoolean(showStartFinish); - } - - public void setShowStartFinish(boolean showStartFinish) { - getExtensionsToWrite().put("showStartFinish", String.valueOf(showStartFinish)); - } - - public Map getExtensionsToWrite() { - if (extensions == null) { - extensions = new LinkedHashMap<>(); - } - return extensions; - } - - private int parseColor(String colorString, int defColor) { + protected int parseColor(String colorString, int defColor) { if (!Algorithms.isEmpty(colorString)) { if (colorString.charAt(0) == '#') { long color = Long.parseLong(colorString.substring(1), 16); @@ -1547,6 +1513,70 @@ public class GPXUtilities { } return new QuadRect(left, top, right, bottom); } + + public enum GradientScaleType { + SPEED("gradientSpeedColor"), + ALTITUDE("gradientAltitudeColor"), + SLOPE("gradientSlopeColor"); + + private String typeName; + + GradientScaleType(String typeName) { + this.typeName = typeName; + } + + public String getTypeName() { + return typeName; + } + } + + public void setGradientScaleColor(GradientScaleType gradientScaleType, int gradientScaleSpeedColor) { + getExtensionsToWrite().put(gradientScaleType.getTypeName(), Algorithms.colorToString(gradientScaleSpeedColor)); + } + + public int getGradientScaleColor(GradientScaleType gradientScaleType, int defColor) { + String clrValue = null; + if (extensions != null) { + clrValue = extensions.get(gradientScaleType.getTypeName()); + } + return parseColor(clrValue, defColor); + } + + public String getWidth(String defWidth) { + String widthValue = null; + if (extensions != null) { + widthValue = extensions.get("width"); + } + return widthValue != null ? widthValue : defWidth; + } + + public void setWidth(String width) { + getExtensionsToWrite().put("width", width); + } + + public boolean isShowArrows() { + String showArrows = null; + if (extensions != null) { + showArrows = extensions.get("showArrows"); + } + return Boolean.parseBoolean(showArrows); + } + + public void setShowArrows(boolean showArrows) { + getExtensionsToWrite().put("showArrows", String.valueOf(showArrows)); + } + + public boolean isShowStartFinish() { + String showStartFinish = null; + if (extensions != null) { + showStartFinish = extensions.get("showStartFinish"); + } + return Boolean.parseBoolean(showStartFinish); + } + + public void setShowStartFinish(boolean showStartFinish) { + getExtensionsToWrite().put("showStartFinish", String.valueOf(showStartFinish)); + } } public static String asString(GPXFile file) { diff --git a/OsmAnd/src/net/osmand/plus/GPXDatabase.java b/OsmAnd/src/net/osmand/plus/GPXDatabase.java index 1595e2b3b5..b45e5582a2 100644 --- a/OsmAnd/src/net/osmand/plus/GPXDatabase.java +++ b/OsmAnd/src/net/osmand/plus/GPXDatabase.java @@ -3,6 +3,7 @@ package net.osmand.plus; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import net.osmand.GPXUtilities.GPXFile.GradientScaleType; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.IndexConstants; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; @@ -63,6 +64,12 @@ public class GPXDatabase { private static final String GPX_COL_WIDTH = "width"; + private static final String GPX_COL_GRADIENT_SPEED_COLOR = "gradientSpeedColor"; + + private static final String GPX_COL_GRADIENT_ALTITUDE_COLOR = "gradientAltitudeColor"; + + private static final String GPX_COL_GRADIENT_SLOPE_COLOR = "gradientSlopeColor"; + public static final int GPX_SPLIT_TYPE_NO_SPLIT = -1; public static final int GPX_SPLIT_TYPE_DISTANCE = 1; public static final int GPX_SPLIT_TYPE_TIME = 2; @@ -99,7 +106,10 @@ public class GPXDatabase { GPX_COL_JOIN_SEGMENTS + " int, " + // 1 = true, 0 = false GPX_COL_SHOW_ARROWS + " int, " + // 1 = true, 0 = false GPX_COL_SHOW_START_FINISH + " int, " + // 1 = true, 0 = false - GPX_COL_WIDTH + " TEXT);"; + GPX_COL_WIDTH + " TEXT, " + + GPX_COL_GRADIENT_SPEED_COLOR + " TEXT, " + + GPX_COL_GRADIENT_ALTITUDE_COLOR + " TEXT, " + + GPX_COL_GRADIENT_SLOPE_COLOR + " TEXT);"; private static final String GPX_TABLE_SELECT = "SELECT " + GPX_COL_NAME + ", " + @@ -129,8 +139,10 @@ public class GPXDatabase { GPX_COL_SHOW_AS_MARKERS + ", " + GPX_COL_JOIN_SEGMENTS + ", " + GPX_COL_SHOW_ARROWS + ", " + - GPX_COL_SHOW_START_FINISH + ", " + - GPX_COL_WIDTH + + GPX_COL_WIDTH + ", " + + GPX_COL_GRADIENT_SPEED_COLOR + ", " + + GPX_COL_GRADIENT_ALTITUDE_COLOR + ", " + + GPX_COL_GRADIENT_SLOPE_COLOR + " FROM " + GPX_TABLE_NAME; private static final String GPX_TABLE_UPDATE_ANALYSIS = "UPDATE " + @@ -162,6 +174,9 @@ public class GPXDatabase { private GPXTrackAnalysis analysis; private String width; private int color; + private int gradientSpeedColor; + private int gradientAltitudeColor; + private int gradientSlopeColor; private int splitType; private double splitInterval; private long fileLastModifiedTime; @@ -194,6 +209,30 @@ public class GPXDatabase { return color; } + public int getGradientSpeedColor() { + return gradientSpeedColor; + } + + public void setGradientSpeedColor(int gradientSpeedColor) { + this.gradientSpeedColor = gradientSpeedColor; + } + + public int getGradientAltitudeColor() { + return gradientAltitudeColor; + } + + public void setGradientAltitudeColor(int gradientAltitudeColor) { + this.gradientAltitudeColor = gradientAltitudeColor; + } + + public int getGradientSlopeColor() { + return gradientSlopeColor; + } + + public void setGradientSlopeColor(int gradientSlopeColor) { + this.gradientSlopeColor = gradientSlopeColor; + } + public String getWidth() { return width; } @@ -314,7 +353,7 @@ public class GPXDatabase { if (oldVersion < 3) { db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_FILE_LAST_MODIFIED_TIME + " long"); } - + if (oldVersion < 4) { db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_SPLIT_TYPE + " int"); db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_SPLIT_INTERVAL + " double"); @@ -386,6 +425,9 @@ public class GPXDatabase { db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_SHOW_ARROWS + " int"); db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_SHOW_START_FINISH + " int"); db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_WIDTH + " TEXT"); + db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_GRADIENT_SPEED_COLOR + " TEXT"); + db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_GRADIENT_ALTITUDE_COLOR + " TEXT"); + db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_GRADIENT_SLOPE_COLOR + " TEXT"); db.execSQL("UPDATE " + GPX_TABLE_NAME + " SET " + GPX_COL_SHOW_ARROWS + " = ? " + "WHERE " + GPX_COL_SHOW_ARROWS + " IS NULL", new Object[]{0}); @@ -454,6 +496,34 @@ public class GPXDatabase { return false; } + public boolean updateGradientScaleColor(@NonNull GpxDataItem item, @NonNull GradientScaleType gradientScaleType, int gradientScaleColor) { + SQLiteConnection db = openConnection(false); + if (db != null) { + try { + String fileName = getFileName(item.file); + String fileDir = getFileDir(item.file); + String columnName = null; + if (GradientScaleType.SPEED == gradientScaleType) { + columnName = GPX_COL_GRADIENT_SPEED_COLOR; + item.gradientSpeedColor = gradientScaleColor; + } else if (GradientScaleType.ALTITUDE == gradientScaleType) { + columnName = GPX_COL_GRADIENT_ALTITUDE_COLOR; + item.gradientAltitudeColor = gradientScaleColor; + } else if (GradientScaleType.SLOPE == gradientScaleType) { + columnName = GPX_COL_GRADIENT_SLOPE_COLOR; + item.gradientSlopeColor = gradientScaleColor; + } + db.execSQL("UPDATE " + GPX_TABLE_NAME + " SET " + columnName + " = ? " + + " WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?", + new Object[] {(gradientScaleColor == 0 ? "" : Algorithms.colorToString(gradientScaleColor)), fileName, fileDir}); + } finally { + db.close(); + } + return true; + } + return false; + } + public boolean updateShowArrows(GpxDataItem item, boolean showArrows) { SQLiteConnection db = openConnection(false); if (db != null) { @@ -642,7 +712,10 @@ public class GPXDatabase { GPX_COL_JOIN_SEGMENTS + ", " + GPX_COL_SHOW_ARROWS + ", " + GPX_COL_SHOW_START_FINISH + ", " + - GPX_COL_WIDTH + + GPX_COL_WIDTH + ", " + + GPX_COL_GRADIENT_SPEED_COLOR + ", " + + GPX_COL_GRADIENT_ALTITUDE_COLOR + ", " + + GPX_COL_GRADIENT_SLOPE_COLOR + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] {fileName, fileDir, color, 0, item.splitType, item.splitInterval, item.apiImported ? 1 : 0, item.showAsMarkers ? 1 : 0, item.joinSegments ? 1 : 0, diff --git a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java index c929348297..cb011336bb 100644 --- a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java @@ -8,6 +8,7 @@ import androidx.annotation.Nullable; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; +import net.osmand.GPXUtilities.GPXFile.GradientScaleType; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; @@ -77,6 +78,12 @@ public class GpxDbHelper { return res; } + public boolean updateGradientScaleColor(@NonNull GpxDataItem item, @NonNull GradientScaleType gradientScaleType, int color) { + boolean res = db.updateGradientScaleColor(item, gradientScaleType, color); + putToCache(item); + return res; + } + public boolean updateShowAsMarkers(GpxDataItem item, boolean showAsMarkers) { boolean res = db.updateShowAsMarkers(item, showAsMarkers); putToCache(item); @@ -107,7 +114,7 @@ public class GpxDbHelper { return res; } - public boolean updateJoinSegments(@NonNull GpxDataItem item, boolean joinSegments) { + public boolean updateJoinSegments(@NonNull GpxDataItem item, boolean joinSegments) { boolean res = db.updateJoinSegments(item, joinSegments); putToCache(item); return res; diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 31f4755803..8d689978a1 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -11,6 +11,7 @@ import androidx.core.content.ContextCompat; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; +import net.osmand.GPXUtilities.GPXFile.GradientScaleType; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.GPXUtilities.Route; import net.osmand.GPXUtilities.Track; @@ -518,6 +519,7 @@ public class GpxSelectionHelper { int clr = Algorithms.parseColor(obj.getString(COLOR)); gpx.setColor(clr); } + loadGpxScaleTypes(obj, gpx); if (obj.has(SHOW_ARROWS)) { boolean showArrows = obj.optBoolean(SHOW_ARROWS, false); gpx.setShowArrows(showArrows); @@ -555,6 +557,15 @@ public class GpxSelectionHelper { } } + private void loadGpxScaleTypes(JSONObject obj, GPXFile gpx) throws JSONException { + for (GradientScaleType scaleType : GradientScaleType.values()) { + if (obj.has(scaleType.getTypeName())) { + int clr = Algorithms.parseColor(obj.getString(scaleType.getTypeName())); + gpx.setGradientScaleColor(scaleType, clr); + } + } + } + private void saveCurrentSelections() { JSONArray ar = new JSONArray(); for (SelectedGpxFile s : selectedGPXFiles) {