From 15e26432f8a87f1dddff7e599d4916dba6af127c Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 6 Jul 2020 15:01:47 +0300 Subject: [PATCH] Add selected gradient scale type for track --- .../main/java/net/osmand/GPXUtilities.java | 18 +++++ OsmAnd/src/net/osmand/plus/GPXDatabase.java | 80 ++++++++++++++++--- OsmAnd/src/net/osmand/plus/GpxDbHelper.java | 6 ++ .../net/osmand/plus/GpxSelectionHelper.java | 36 ++++++--- 4 files changed, 115 insertions(+), 25 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java index 077e249ac8..c1ad5d5cef 100644 --- a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java +++ b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java @@ -1542,6 +1542,24 @@ public class GPXUtilities { return parseColor(clrValue, defColor); } + public void setGradientScaleType(GradientScaleType gradientScaleType) { + getExtensionsToWrite().put("gradientScaleType", gradientScaleType != null ? gradientScaleType.name() : null); + } + + public GradientScaleType getGradientScaleType() { + if (extensions != null) { + String gradientScaleTypeName = extensions.get("gradientScaleType"); + if (!Algorithms.isEmpty(gradientScaleTypeName)) { + try { + return GradientScaleType.valueOf(gradientScaleTypeName); + } catch (IllegalArgumentException e) { + log.error("Error reading gradientScaleType", e); + } + } + } + return null; + } + public String getWidth(String defWidth) { String widthValue = null; if (extensions != null) { diff --git a/OsmAnd/src/net/osmand/plus/GPXDatabase.java b/OsmAnd/src/net/osmand/plus/GPXDatabase.java index b45e5582a2..e5bd1c3ad0 100644 --- a/OsmAnd/src/net/osmand/plus/GPXDatabase.java +++ b/OsmAnd/src/net/osmand/plus/GPXDatabase.java @@ -70,6 +70,8 @@ public class GPXDatabase { private static final String GPX_COL_GRADIENT_SLOPE_COLOR = "gradientSlopeColor"; + private static final String GPX_COL_GRADIENT_SCALE_TYPE = "gradientScaleType"; + 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; @@ -109,7 +111,8 @@ public class GPXDatabase { GPX_COL_WIDTH + " TEXT, " + GPX_COL_GRADIENT_SPEED_COLOR + " TEXT, " + GPX_COL_GRADIENT_ALTITUDE_COLOR + " TEXT, " + - GPX_COL_GRADIENT_SLOPE_COLOR + " TEXT);"; + GPX_COL_GRADIENT_SLOPE_COLOR + " TEXT, " + + GPX_COL_GRADIENT_SCALE_TYPE+ " TEXT);"; private static final String GPX_TABLE_SELECT = "SELECT " + GPX_COL_NAME + ", " + @@ -142,8 +145,9 @@ public class GPXDatabase { GPX_COL_WIDTH + ", " + GPX_COL_GRADIENT_SPEED_COLOR + ", " + GPX_COL_GRADIENT_ALTITUDE_COLOR + ", " + - GPX_COL_GRADIENT_SLOPE_COLOR + - " FROM " + GPX_TABLE_NAME; + GPX_COL_GRADIENT_SLOPE_COLOR + ", " + + GPX_COL_GRADIENT_SCALE_TYPE + + " FROM " + GPX_TABLE_NAME; private static final String GPX_TABLE_UPDATE_ANALYSIS = "UPDATE " + GPX_TABLE_NAME + " SET " + @@ -173,6 +177,7 @@ public class GPXDatabase { private File file; private GPXTrackAnalysis analysis; private String width; + private GradientScaleType gradientScaleType; private int color; private int gradientSpeedColor; private int gradientAltitudeColor; @@ -209,6 +214,14 @@ public class GPXDatabase { return color; } + public GradientScaleType getGradientScaleType() { + return gradientScaleType; + } + + public void setGradientScaleType(GradientScaleType gradientScaleType) { + this.gradientScaleType = gradientScaleType; + } + public int getGradientSpeedColor() { return gradientSpeedColor; } @@ -428,6 +441,7 @@ public class GPXDatabase { 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("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_GRADIENT_SCALE_TYPE + " TEXT"); db.execSQL("UPDATE " + GPX_TABLE_NAME + " SET " + GPX_COL_SHOW_ARROWS + " = ? " + "WHERE " + GPX_COL_SHOW_ARROWS + " IS NULL", new Object[]{0}); @@ -524,6 +538,24 @@ public class GPXDatabase { return false; } + public boolean updateGradientScaleType(@NonNull GpxDataItem item, GradientScaleType gradientScaleType) { + SQLiteConnection db = openConnection(false); + if (db != null) { + try { + String fileName = getFileName(item.file); + String fileDir = getFileDir(item.file); + db.execSQL("UPDATE " + GPX_TABLE_NAME + " SET " + GPX_COL_GRADIENT_SCALE_TYPE + " = ? " + + " WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?", + new Object[] {(gradientScaleType == null ? "" : gradientScaleType.name()), fileName, fileDir}); + item.gradientScaleType = gradientScaleType; + } finally { + db.close(); + } + return true; + } + return false; + } + public boolean updateShowArrows(GpxDataItem item, boolean showArrows) { SQLiteConnection db = openConnection(false); if (db != null) { @@ -690,15 +722,17 @@ public class GPXDatabase { } else { color = Algorithms.colorToString(item.color); } + String gradientScaleType = item.gradientScaleType != null ? item.gradientScaleType.name() : null; if (a != null) { db.execSQL( - "INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] {fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime, a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown, a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, color, item.file.lastModified(), item.splitType, item.splitInterval, item.apiImported ? 1 : 0, Algorithms.encodeStringSet(item.analysis.wptCategoryNames), item.showAsMarkers ? 1 : 0, - item.joinSegments ? 1 : 0, item.showArrows ? 1 : 0, item.showStartFinish ? 1 : 0, item.width}); + item.joinSegments ? 1 : 0, item.showArrows ? 1 : 0, item.showStartFinish ? 1 : 0, item.width, + item.gradientSpeedColor, item.gradientAltitudeColor, item.gradientSlopeColor, gradientScaleType}); } else { db.execSQL("INSERT INTO " + GPX_TABLE_NAME + "(" + GPX_COL_NAME + ", " + @@ -715,11 +749,13 @@ public class GPXDatabase { GPX_COL_WIDTH + ", " + GPX_COL_GRADIENT_SPEED_COLOR + ", " + GPX_COL_GRADIENT_ALTITUDE_COLOR + ", " + - GPX_COL_GRADIENT_SLOPE_COLOR + - ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + GPX_COL_GRADIENT_SLOPE_COLOR + ", " + + GPX_COL_GRADIENT_SCALE_TYPE + + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] {fileName, fileDir, color, 0, item.splitType, item.splitInterval, item.apiImported ? 1 : 0, item.showAsMarkers ? 1 : 0, item.joinSegments ? 1 : 0, - item.showArrows ? 1 : 0, item.showStartFinish ? 1 : 0, item.width}); + item.showArrows ? 1 : 0, item.showStartFinish ? 1 : 0, item.width, + item.gradientSpeedColor, item.gradientAltitudeColor, item.gradientSlopeColor, gradientScaleType}); } } @@ -803,6 +839,10 @@ public class GPXDatabase { boolean showArrows = query.getInt(26) == 1; boolean showStartFinish = query.getInt(27) == 1; String width = query.getString(28); + String gradientSpeedColor = query.getString(29); + String gradientAltitudeColor = query.getString(30); + String gradientSlopeColor = query.getString(31); + String gradientScaleType = query.getString(32); GPXTrackAnalysis a = new GPXTrackAnalysis(); a.totalDistance = totalDistance; @@ -833,11 +873,7 @@ public class GPXDatabase { dir = context.getAppPath(IndexConstants.GPX_INDEX_DIR); } GpxDataItem item = new GpxDataItem(new File(dir, fileName), a); - try { - item.color = Algorithms.isEmpty(color) ? 0 : Algorithms.parseColor(color); - } catch (IllegalArgumentException e) { - item.color = 0; - } + item.color = parseColor(color); item.fileLastModifiedTime = fileLastModifiedTime; item.splitType = splitType; item.splitInterval = splitInterval; @@ -847,9 +883,27 @@ public class GPXDatabase { item.showArrows = showArrows; item.showStartFinish = showStartFinish; item.width = width; + item.gradientSpeedColor = parseColor(gradientSpeedColor); + item.gradientAltitudeColor = parseColor(gradientAltitudeColor); + item.gradientSlopeColor = parseColor(gradientSlopeColor); + + try { + item.gradientScaleType = Algorithms.isEmpty(gradientScaleType) ? null : GradientScaleType.valueOf(gradientScaleType); + } catch (IllegalArgumentException e) { + item.gradientScaleType = null; + } + return item; } + private int parseColor(String color) { + try { + return Algorithms.isEmpty(color) ? 0 : Algorithms.parseColor(color); + } catch (IllegalArgumentException e) { + return 0; + } + } + @NonNull public List getItems() { List items = new ArrayList<>(); diff --git a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java index cb011336bb..0762982a33 100644 --- a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java @@ -84,6 +84,12 @@ public class GpxDbHelper { return res; } + public boolean updateGradientScaleType(@NonNull GpxDataItem item, @Nullable GradientScaleType gradientScaleType) { + boolean res = db.updateGradientScaleType(item, gradientScaleType); + putToCache(item); + return res; + } + public boolean updateShowAsMarkers(GpxDataItem item, boolean showAsMarkers) { boolean res = db.updateShowAsMarkers(item, showAsMarkers); putToCache(item); diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 8d689978a1..3959318853 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -55,6 +55,7 @@ public class GpxSelectionHelper { private static final String WIDTH = "width"; private static final String SELECTED_BY_USER = "selected_by_user"; private static final String SHOW_ARROWS = "showArrows"; + private static final String GRADIENT_SCALE_TYPE = "gradientScaleType"; private static final String SHOW_START_FINISH = "showStartFinish"; private OsmandApplication app; @@ -519,11 +520,22 @@ public class GpxSelectionHelper { int clr = Algorithms.parseColor(obj.getString(COLOR)); gpx.setColor(clr); } - loadGpxScaleTypes(obj, gpx); + for (GradientScaleType scaleType : GradientScaleType.values()) { + if (obj.has(scaleType.getTypeName())) { + int clr = Algorithms.parseColor(obj.getString(scaleType.getTypeName())); + gpx.setGradientScaleColor(scaleType, clr); + } + } if (obj.has(SHOW_ARROWS)) { boolean showArrows = obj.optBoolean(SHOW_ARROWS, false); gpx.setShowArrows(showArrows); } + if (obj.has(GRADIENT_SCALE_TYPE)) { + String gradientScaleTypeName = obj.optString(GRADIENT_SCALE_TYPE); + if (!Algorithms.isEmpty(gradientScaleTypeName)) { + gpx.setGradientScaleType(GradientScaleType.valueOf(gradientScaleTypeName)); + } + } if (obj.has(SHOW_START_FINISH)) { boolean showStartFinish = obj.optBoolean(SHOW_START_FINISH, false); gpx.setShowStartFinish(showStartFinish); @@ -557,15 +569,6 @@ 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) { @@ -582,8 +585,17 @@ public class GpxSelectionHelper { if (s.gpxFile.getWidth(null) != null) { obj.put(WIDTH, s.gpxFile.getWidth(null)); } + if (s.gpxFile.getGradientScaleType() != null) { + obj.put(GRADIENT_SCALE_TYPE, s.gpxFile.getGradientScaleType()); + } obj.put(SHOW_ARROWS, s.gpxFile.isShowArrows()); obj.put(SHOW_START_FINISH, s.gpxFile.isShowStartFinish()); + for (GradientScaleType scaleType : GradientScaleType.values()) { + int gradientScaleColor = s.gpxFile.getGradientScaleColor(scaleType, 0); + if (gradientScaleColor != 0) { + obj.put(scaleType.getTypeName(), Algorithms.colorToString(gradientScaleColor)); + } + } } obj.put(SELECTED_BY_USER, s.selectedByUser); } catch (JSONException e) { @@ -1059,13 +1071,13 @@ public class GpxSelectionHelper { @Override protected void onProgressUpdate(Void... values) { - gpxTaskListener.gpxSelectionInProgress(); + gpxTaskListener.gpxSelectionInProgress(); } @Override protected void onPreExecute() { collectSelectedItems(); - gpxTaskListener.gpxSelectionStarted(); + gpxTaskListener.gpxSelectionStarted(); } private void collectSelectedItems() {