From 67d2166d0fb6cb8f6304b620a7c94d521f2bc0db Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 14 Jul 2020 11:49:44 +0300 Subject: [PATCH] Move GpxSplitType and GradientScaleType to android module --- .../main/java/net/osmand/GPXUtilities.java | 69 ++++--------------- OsmAnd/src/net/osmand/plus/GPXDatabase.java | 34 ++++++--- OsmAnd/src/net/osmand/plus/GpxDbHelper.java | 4 +- .../net/osmand/plus/GpxSelectionHelper.java | 18 ++--- .../TrackActivityFragmentAdapter.java | 2 +- .../net/osmand/plus/track/GpxSplitType.java | 32 +++++++++ .../osmand/plus/track/GradientScaleType.java | 40 +++++++++++ 7 files changed, 122 insertions(+), 77 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/track/GpxSplitType.java create mode 100644 OsmAnd/src/net/osmand/plus/track/GradientScaleType.java diff --git a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java index 3498450ab4..9b055cd01f 100644 --- a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java +++ b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java @@ -1514,52 +1514,38 @@ public class GPXUtilities { return new QuadRect(left, top, right, bottom); } - public int getGradientScaleColor(GradientScaleType gradientScaleType, int defColor) { + public int getGradientScaleColor(String gradientScaleType, int defColor) { String clrValue = null; if (extensions != null) { - clrValue = extensions.get(gradientScaleType.getTypeName()); + clrValue = extensions.get(gradientScaleType); } return parseColor(clrValue, defColor); } - public void setGradientScaleColor(GradientScaleType gradientScaleType, int gradientScaleColor) { - getExtensionsToWrite().put(gradientScaleType.getTypeName(), Algorithms.colorToString(gradientScaleColor)); + public void setGradientScaleColor(String gradientScaleType, int gradientScaleColor) { + getExtensionsToWrite().put(gradientScaleType, Algorithms.colorToString(gradientScaleColor)); } - public GradientScaleType getGradientScaleType() { + public String getGradientScaleType() { if (extensions != null) { - String gradientScaleTypeName = extensions.get("gradient_scale_type"); - if (!Algorithms.isEmpty(gradientScaleTypeName)) { - try { - return GradientScaleType.valueOf(gradientScaleTypeName); - } catch (IllegalArgumentException e) { - log.error("Error reading gradientScaleType", e); - } - } + return extensions.get("gradient_scale_type"); } return null; } - public void setGradientScaleType(GradientScaleType gradientScaleType) { - getExtensionsToWrite().put("gradient_scale_type", gradientScaleType.name()); + public void setGradientScaleType(String gradientScaleType) { + getExtensionsToWrite().put("gradient_scale_type", gradientScaleType); } - public GpxSplitType getSplitType() { + public String getSplitType() { if (extensions != null) { - String gradientScaleTypeName = extensions.get("split_type"); - if (!Algorithms.isEmpty(gradientScaleTypeName)) { - try { - return GpxSplitType.valueOf(gradientScaleTypeName); - } catch (IllegalArgumentException e) { - log.error("Error reading GpxSplitType", e); - } - } + return extensions.get("split_type"); } return null; } - public void setSplitType(GpxSplitType gpxSplitType) { - getExtensionsToWrite().put("split_type", gpxSplitType.name()); + public void setSplitType(String gpxSplitType) { + getExtensionsToWrite().put("split_type", gpxSplitType); } public double getSplitInterval() { @@ -1615,37 +1601,6 @@ public class GPXUtilities { getExtensionsToWrite().put("show_start_finish", String.valueOf(showStartFinish)); } - public enum GradientScaleType { - SPEED("gradient_speed_color"), - ALTITUDE("gradient_altitude_color"), - SLOPE("gradient_slope_color"); - - private String typeName; - - GradientScaleType(String typeName) { - this.typeName = typeName; - } - - public String getTypeName() { - return typeName; - } - } - - public enum GpxSplitType { - NO_SPLIT(-1), - DISTANCE(1), - TIME(2); - - private int type; - - GpxSplitType(int type) { - this.type = type; - } - - public int getType() { - return type; - } - } } 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 d0a19e80b9..7a3ec03992 100644 --- a/OsmAnd/src/net/osmand/plus/GPXDatabase.java +++ b/OsmAnd/src/net/osmand/plus/GPXDatabase.java @@ -4,11 +4,12 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import net.osmand.GPXUtilities.GPXFile; -import net.osmand.GPXUtilities.GPXFile.GradientScaleType; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.IndexConstants; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; +import net.osmand.plus.track.GpxSplitType; +import net.osmand.plus.track.GradientScaleType; import net.osmand.util.Algorithms; import java.io.File; @@ -201,17 +202,34 @@ public class GPXDatabase { public GpxDataItem(File file, @NonNull GPXFile gpxFile) { this.file = file; + readGpxParams(gpxFile); + } + + private void readGpxParams(GPXFile gpxFile) { color = gpxFile.getColor(0); width = gpxFile.getWidth(null); showArrows = gpxFile.isShowArrows(); showStartFinish = gpxFile.isShowStartFinish(); - gradientScaleType = gpxFile.getGradientScaleType(); - gradientSpeedColor = gpxFile.getGradientScaleColor(GradientScaleType.SPEED, 0); - gradientSlopeColor = gpxFile.getGradientScaleColor(GradientScaleType.SLOPE, 0); - gradientAltitudeColor = gpxFile.getGradientScaleColor(GradientScaleType.ALTITUDE, 0); - if (gpxFile.getSplitType() != null && gpxFile.getSplitInterval() != 0) { - splitType = gpxFile.getSplitType().getType(); - splitInterval = gpxFile.getSplitInterval(); + gradientSpeedColor = gpxFile.getGradientScaleColor(GradientScaleType.SPEED.getTypeName(), 0); + gradientSlopeColor = gpxFile.getGradientScaleColor(GradientScaleType.SLOPE.getTypeName(), 0); + gradientAltitudeColor = gpxFile.getGradientScaleColor(GradientScaleType.ALTITUDE.getTypeName(), 0); + + if (!Algorithms.isEmpty(gpxFile.getSplitType()) && gpxFile.getSplitInterval() != 0) { + for (GpxSplitType gpxSplitType : GpxSplitType.values()) { + if (gpxSplitType.name().equalsIgnoreCase(gpxFile.getSplitType())) { + splitType = gpxSplitType.getType(); + splitInterval = gpxFile.getSplitInterval(); + break; + } + } + } + if (!Algorithms.isEmpty(gpxFile.getGradientScaleType())) { + for (GradientScaleType scaleType : GradientScaleType.values()) { + if (scaleType.name().equalsIgnoreCase(gpxFile.getGradientScaleType())) { + gradientScaleType = scaleType; + break; + } + } } } diff --git a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java index fc0ffb636e..d5e05e8db3 100644 --- a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java @@ -8,11 +8,11 @@ import androidx.annotation.Nullable; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; -import net.osmand.GPXUtilities.GPXFile.GpxSplitType; -import net.osmand.GPXUtilities.GPXFile.GradientScaleType; +import net.osmand.plus.track.GpxSplitType; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; +import net.osmand.plus.track.GradientScaleType; import java.io.File; import java.util.List; diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 6f239e2286..f75a5bf829 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -11,8 +11,7 @@ import androidx.core.content.ContextCompat; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; -import net.osmand.GPXUtilities.GPXFile.GpxSplitType; -import net.osmand.GPXUtilities.GPXFile.GradientScaleType; +import net.osmand.plus.track.GpxSplitType; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.GPXUtilities.Route; import net.osmand.GPXUtilities.Track; @@ -29,6 +28,7 @@ import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.settings.backend.OsmandSettings.MetricsConstants; +import net.osmand.plus.track.GradientScaleType; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -524,7 +524,7 @@ public class GpxSelectionHelper { for (GradientScaleType scaleType : GradientScaleType.values()) { if (obj.has(scaleType.getTypeName())) { int clr = Algorithms.parseColor(obj.getString(scaleType.getTypeName())); - gpx.setGradientScaleColor(scaleType, clr); + gpx.setGradientScaleColor(scaleType.getTypeName(), clr); } } if (obj.has(SHOW_ARROWS)) { @@ -534,7 +534,7 @@ public class GpxSelectionHelper { if (obj.has(GRADIENT_SCALE_TYPE)) { String gradientScaleTypeName = obj.optString(GRADIENT_SCALE_TYPE); if (!Algorithms.isEmpty(gradientScaleTypeName)) { - gpx.setGradientScaleType(GradientScaleType.valueOf(gradientScaleTypeName)); + gpx.setGradientScaleType(GradientScaleType.valueOf(gradientScaleTypeName).getTypeName()); } } if (obj.has(SHOW_START_FINISH)) { @@ -592,7 +592,7 @@ public class GpxSelectionHelper { 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); + int gradientScaleColor = s.gpxFile.getGradientScaleColor(scaleType.getTypeName(), 0); if (gradientScaleColor != 0) { obj.put(scaleType.getTypeName(), Algorithms.colorToString(gradientScaleColor)); } @@ -650,16 +650,16 @@ public class GpxSelectionHelper { gpx.setColor(dataItem.getColor()); } if (dataItem.getGradientSpeedColor() != 0) { - gpx.setGradientScaleColor(GradientScaleType.SPEED, dataItem.getGradientSpeedColor()); + gpx.setGradientScaleColor(GradientScaleType.SPEED.getTypeName(), dataItem.getGradientSpeedColor()); } if (dataItem.getGradientAltitudeColor() != 0) { - gpx.setGradientScaleColor(GradientScaleType.ALTITUDE, dataItem.getGradientAltitudeColor()); + gpx.setGradientScaleColor(GradientScaleType.ALTITUDE.getTypeName(), dataItem.getGradientAltitudeColor()); } if (dataItem.getGradientSlopeColor() != 0) { - gpx.setGradientScaleColor(GradientScaleType.SLOPE, dataItem.getGradientSlopeColor()); + gpx.setGradientScaleColor(GradientScaleType.SLOPE.getTypeName(), dataItem.getGradientSlopeColor()); } if (dataItem.getGradientScaleType() != null) { - gpx.setGradientScaleType(dataItem.getGradientScaleType()); + gpx.setGradientScaleType(dataItem.getGradientScaleType().getTypeName()); } if (dataItem.getWidth() != null) { gpx.setWidth(dataItem.getWidth()); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java index 260d7fccda..99cfa60ddc 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java @@ -39,7 +39,7 @@ import com.squareup.picasso.RequestCreator; import net.osmand.AndroidUtils; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; -import net.osmand.GPXUtilities.GPXFile.GpxSplitType; +import net.osmand.plus.track.GpxSplitType; import net.osmand.GPXUtilities.WptPt; import net.osmand.PicassoUtils; import net.osmand.data.LatLon; diff --git a/OsmAnd/src/net/osmand/plus/track/GpxSplitType.java b/OsmAnd/src/net/osmand/plus/track/GpxSplitType.java new file mode 100644 index 0000000000..76659a36ce --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/track/GpxSplitType.java @@ -0,0 +1,32 @@ +package net.osmand.plus.track; + +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.annotation.StringRes; + +import net.osmand.plus.R; + +public enum GpxSplitType { + + NO_SPLIT(-1, R.string.shared_string_none), + DISTANCE(1, R.string.distance), + TIME(2, R.string.shared_string_time); + + private int type; + @StringRes + private int resId; + + GpxSplitType(int type, @StringRes int resId) { + this.type = type; + this.resId = resId; + } + + public int getType() { + return type; + } + + public String getHumanString(@NonNull Context ctx) { + return ctx.getString(resId); + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/track/GradientScaleType.java b/OsmAnd/src/net/osmand/plus/track/GradientScaleType.java new file mode 100644 index 0000000000..9c2ae36a59 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/track/GradientScaleType.java @@ -0,0 +1,40 @@ +package net.osmand.plus.track; + +import android.content.Context; + +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.StringRes; + +import net.osmand.plus.R; + +public enum GradientScaleType { + + SPEED("gradient_speed_color", R.string.map_widget_speed, R.drawable.ic_action_speed), + ALTITUDE("gradient_altitude_color", R.string.altitude, R.drawable.ic_action_altitude_average), + SLOPE("gradient_slope_color", R.string.shared_string_slope, R.drawable.ic_action_altitude_ascent); + + private String typeName; + @StringRes + private int resId; + @DrawableRes + private int iconId; + + GradientScaleType(@NonNull String typeName, @StringRes int resId, @DrawableRes int iconId) { + this.typeName = typeName; + this.resId = resId; + this.iconId = iconId; + } + + public String getTypeName() { + return typeName; + } + + public int getIconId() { + return iconId; + } + + public String getHumanString(@NonNull Context ctx) { + return ctx.getString(resId); + } +}