From 95a1d5c9eedf10075f6484317bdc673cdf2d75e8 Mon Sep 17 00:00:00 2001 From: cepprice Date: Sun, 18 Apr 2021 14:26:10 +0500 Subject: [PATCH] Add clearing of tracks' cache --- .../osmand/plus/views/layers/GPXLayer.java | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/views/layers/GPXLayer.java b/OsmAnd/src/net/osmand/plus/views/layers/GPXLayer.java index 3e040a534c..7873dcbbab 100644 --- a/OsmAnd/src/net/osmand/plus/views/layers/GPXLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/layers/GPXLayer.java @@ -83,6 +83,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import static net.osmand.plus.dialogs.ConfigureMapMenu.CURRENT_TRACK_COLOR_ATTR; import static net.osmand.plus.dialogs.ConfigureMapMenu.CURRENT_TRACK_WIDTH_ATTR; @@ -276,6 +277,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM } } cache.clear(); + fireUnselectedGpxFiles(selectedGPXFiles); if (!selectedGPXFiles.isEmpty()) { drawSelectedFilesSegments(canvas, tileBox, selectedGPXFiles, settings); canvas.rotate(-tileBox.getRotate(), tileBox.getCenterPixelX(), tileBox.getCenterPixelY()); @@ -455,22 +457,26 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM private void drawDirectionArrows(Canvas canvas, RotatedTileBox tileBox, List selectedGPXFiles) { if (!tileBox.isZoomAnimated()) { + QuadRect correctedQuadRect = getCorrectedQuadRect(tileBox.getLatLonBounds()); for (SelectedGpxFile selectedGpxFile : selectedGPXFiles) { boolean showArrows = isShowArrowsForTrack(selectedGpxFile.getGpxFile()); - if (showArrows) { - QuadRect correctedQuadRect = getCorrectedQuadRect(tileBox.getLatLonBounds()); - String width = getTrackWidthName(selectedGpxFile.getGpxFile(), defaultTrackWidthPref.get()); - float trackWidth = getTrackWidth(width, defaultTrackWidth); - int trackColor = getTrackColor(selectedGpxFile.getGpxFile(), cachedColor); - int arrowColor = UiUtilities.getContrastColor(view.getApplication(), trackColor, false); - GradientScaleType scaleType = getGradientScaleType(selectedGpxFile.getGpxFile()); - List segments = scaleType != null ? - getCachedSegments(selectedGpxFile, scaleType) : selectedGpxFile.getPointsToDisplay(); - for (TrkSegment segment : segments) { - if (segment.renderer instanceof Renderable.RenderableSegment) { - ((Renderable.RenderableSegment) segment.renderer) - .drawGeometry(canvas, tileBox, correctedQuadRect, arrowColor, trackColor, trackWidth); - } + if (!showArrows) { + continue; + } + if (!QuadRect.trivialOverlap(correctedQuadRect, GPXUtilities.calculateTrackBounds(selectedGpxFile.getPointsToDisplay()))) { + continue; + } + String width = getTrackWidthName(selectedGpxFile.getGpxFile(), defaultTrackWidthPref.get()); + float trackWidth = getTrackWidth(width, defaultTrackWidth); + int trackColor = getTrackColor(selectedGpxFile.getGpxFile(), cachedColor); + int arrowColor = UiUtilities.getContrastColor(view.getApplication(), trackColor, false); + GradientScaleType scaleType = getGradientScaleType(selectedGpxFile.getGpxFile()); + List segments = scaleType != null ? + getCachedSegments(selectedGpxFile, scaleType) : selectedGpxFile.getPointsToDisplay(); + for (TrkSegment segment : segments) { + if (segment.renderer instanceof Renderable.RenderableSegment) { + ((Renderable.RenderableSegment) segment.renderer) + .drawGeometry(canvas, tileBox, correctedQuadRect, arrowColor, trackColor, trackWidth); } } } @@ -690,6 +696,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM RotatedTileBox tileBox, DrawSettings settings) { boolean visible = QuadRect.trivialOverlap(tileBox.getLatLonBounds(), GPXUtilities.calculateTrackBounds(selectedGpxFile.getPointsToDisplay())); if (!selectedGpxFile.getGpxFile().hasTrkPt() || !visible) { + segmentsCache.remove(selectedGpxFile.getGpxFile().path); return; } @@ -730,13 +737,12 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM private List getCachedSegments(SelectedGpxFile selectedGpxFile, GradientScaleType scaleType) { GPXFile gpxFile = selectedGpxFile.getGpxFile(); String path = gpxFile.path; - long modifiedTime = gpxFile.modifiedTime; CachedTrack cachedTrack = segmentsCache.get(path); if (cachedTrack == null) { - cachedTrack = new CachedTrack(view.getApplication(), modifiedTime); + cachedTrack = new CachedTrack(view.getApplication(), selectedGpxFile); segmentsCache.put(path, cachedTrack); } - return cachedTrack.getCachedSegments(selectedGpxFile, view.getZoom(), scaleType, getColorizationPalette(gpxFile, scaleType)); + return cachedTrack.getCachedSegments(view.getZoom(), scaleType, getColorizationPalette(gpxFile, scaleType)); } private float getTrackWidth(String width, float defaultTrackWidth) { @@ -1052,6 +1058,19 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM return name.replace('_', ' '); } + private void fireUnselectedGpxFiles(List selectedGpxFiles) { + Set cachedTracksPaths = segmentsCache.keySet(); + List selectedTracksPaths = new ArrayList<>(); + for (SelectedGpxFile gpx : selectedGpxFiles) { + selectedTracksPaths.add(gpx.getGpxFile().path); + } + for (String cachedPath : cachedTracksPaths) { + if (!selectedTracksPaths.contains(cachedPath)) { + cachedTracksPaths.remove(cachedPath); + } + } + } + @Override public boolean disableSingleTap() { return isInTrackAppearanceMode(); @@ -1221,20 +1240,21 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM private OsmandApplication app; - private long modifiedTime; + private final SelectedGpxFile selectedGpxFile; private final Map> cache = new HashMap<>(); - public CachedTrack(@NonNull OsmandApplication app, long modifiedTime) { + private long prevModifiedTime = -1; + + public CachedTrack(@NonNull OsmandApplication app, @NonNull SelectedGpxFile selectedGpxFile) { this.app = app; - this.modifiedTime = modifiedTime; + this.selectedGpxFile = selectedGpxFile; } - public List getCachedSegments(@NonNull SelectedGpxFile selectedGpxFile, int zoom, - @NonNull GradientScaleType scaleType, + public List getCachedSegments(int zoom, @NonNull GradientScaleType scaleType, int[] gradientPalette) { GPXFile gpxFile = selectedGpxFile.getGpxFile(); String trackId = zoom + "_" + scaleType.toString(); - if (modifiedTime == gpxFile.modifiedTime) { + if (prevModifiedTime == gpxFile.modifiedTime) { List segments = cache.get(trackId); if (segments == null) { segments = calculateGradientTrack(selectedGpxFile, zoom, scaleType, gradientPalette); @@ -1243,7 +1263,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM return segments; } else { cache.clear(); - modifiedTime = gpxFile.modifiedTime; + prevModifiedTime = gpxFile.modifiedTime; List segments = calculateGradientTrack(selectedGpxFile, zoom, scaleType, gradientPalette); cache.put(trackId, segments); return segments;