Add clearing of tracks' cache

This commit is contained in:
cepprice 2021-04-18 14:26:10 +05:00
parent 15fb2b6774
commit 95a1d5c9ee

View file

@ -83,6 +83,7 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; 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_COLOR_ATTR;
import static net.osmand.plus.dialogs.ConfigureMapMenu.CURRENT_TRACK_WIDTH_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(); cache.clear();
fireUnselectedGpxFiles(selectedGPXFiles);
if (!selectedGPXFiles.isEmpty()) { if (!selectedGPXFiles.isEmpty()) {
drawSelectedFilesSegments(canvas, tileBox, selectedGPXFiles, settings); drawSelectedFilesSegments(canvas, tileBox, selectedGPXFiles, settings);
canvas.rotate(-tileBox.getRotate(), tileBox.getCenterPixelX(), tileBox.getCenterPixelY()); canvas.rotate(-tileBox.getRotate(), tileBox.getCenterPixelX(), tileBox.getCenterPixelY());
@ -455,10 +457,15 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
private void drawDirectionArrows(Canvas canvas, RotatedTileBox tileBox, List<SelectedGpxFile> selectedGPXFiles) { private void drawDirectionArrows(Canvas canvas, RotatedTileBox tileBox, List<SelectedGpxFile> selectedGPXFiles) {
if (!tileBox.isZoomAnimated()) { if (!tileBox.isZoomAnimated()) {
QuadRect correctedQuadRect = getCorrectedQuadRect(tileBox.getLatLonBounds());
for (SelectedGpxFile selectedGpxFile : selectedGPXFiles) { for (SelectedGpxFile selectedGpxFile : selectedGPXFiles) {
boolean showArrows = isShowArrowsForTrack(selectedGpxFile.getGpxFile()); boolean showArrows = isShowArrowsForTrack(selectedGpxFile.getGpxFile());
if (showArrows) { if (!showArrows) {
QuadRect correctedQuadRect = getCorrectedQuadRect(tileBox.getLatLonBounds()); continue;
}
if (!QuadRect.trivialOverlap(correctedQuadRect, GPXUtilities.calculateTrackBounds(selectedGpxFile.getPointsToDisplay()))) {
continue;
}
String width = getTrackWidthName(selectedGpxFile.getGpxFile(), defaultTrackWidthPref.get()); String width = getTrackWidthName(selectedGpxFile.getGpxFile(), defaultTrackWidthPref.get());
float trackWidth = getTrackWidth(width, defaultTrackWidth); float trackWidth = getTrackWidth(width, defaultTrackWidth);
int trackColor = getTrackColor(selectedGpxFile.getGpxFile(), cachedColor); int trackColor = getTrackColor(selectedGpxFile.getGpxFile(), cachedColor);
@ -475,7 +482,6 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
} }
} }
} }
}
private void drawSelectedFilesStartEndPoints(Canvas canvas, RotatedTileBox tileBox, List<SelectedGpxFile> selectedGPXFiles) { private void drawSelectedFilesStartEndPoints(Canvas canvas, RotatedTileBox tileBox, List<SelectedGpxFile> selectedGPXFiles) {
if (tileBox.getZoom() >= START_ZOOM) { if (tileBox.getZoom() >= START_ZOOM) {
@ -690,6 +696,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
RotatedTileBox tileBox, DrawSettings settings) { RotatedTileBox tileBox, DrawSettings settings) {
boolean visible = QuadRect.trivialOverlap(tileBox.getLatLonBounds(), GPXUtilities.calculateTrackBounds(selectedGpxFile.getPointsToDisplay())); boolean visible = QuadRect.trivialOverlap(tileBox.getLatLonBounds(), GPXUtilities.calculateTrackBounds(selectedGpxFile.getPointsToDisplay()));
if (!selectedGpxFile.getGpxFile().hasTrkPt() || !visible) { if (!selectedGpxFile.getGpxFile().hasTrkPt() || !visible) {
segmentsCache.remove(selectedGpxFile.getGpxFile().path);
return; return;
} }
@ -730,13 +737,12 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
private List<TrkSegment> getCachedSegments(SelectedGpxFile selectedGpxFile, GradientScaleType scaleType) { private List<TrkSegment> getCachedSegments(SelectedGpxFile selectedGpxFile, GradientScaleType scaleType) {
GPXFile gpxFile = selectedGpxFile.getGpxFile(); GPXFile gpxFile = selectedGpxFile.getGpxFile();
String path = gpxFile.path; String path = gpxFile.path;
long modifiedTime = gpxFile.modifiedTime;
CachedTrack cachedTrack = segmentsCache.get(path); CachedTrack cachedTrack = segmentsCache.get(path);
if (cachedTrack == null) { if (cachedTrack == null) {
cachedTrack = new CachedTrack(view.getApplication(), modifiedTime); cachedTrack = new CachedTrack(view.getApplication(), selectedGpxFile);
segmentsCache.put(path, cachedTrack); 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) { private float getTrackWidth(String width, float defaultTrackWidth) {
@ -1052,6 +1058,19 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
return name.replace('_', ' '); return name.replace('_', ' ');
} }
private void fireUnselectedGpxFiles(List<SelectedGpxFile> selectedGpxFiles) {
Set<String> cachedTracksPaths = segmentsCache.keySet();
List<String> selectedTracksPaths = new ArrayList<>();
for (SelectedGpxFile gpx : selectedGpxFiles) {
selectedTracksPaths.add(gpx.getGpxFile().path);
}
for (String cachedPath : cachedTracksPaths) {
if (!selectedTracksPaths.contains(cachedPath)) {
cachedTracksPaths.remove(cachedPath);
}
}
}
@Override @Override
public boolean disableSingleTap() { public boolean disableSingleTap() {
return isInTrackAppearanceMode(); return isInTrackAppearanceMode();
@ -1221,20 +1240,21 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
private OsmandApplication app; private OsmandApplication app;
private long modifiedTime; private final SelectedGpxFile selectedGpxFile;
private final Map<String, List<TrkSegment>> cache = new HashMap<>(); private final Map<String, List<TrkSegment>> 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.app = app;
this.modifiedTime = modifiedTime; this.selectedGpxFile = selectedGpxFile;
} }
public List<TrkSegment> getCachedSegments(@NonNull SelectedGpxFile selectedGpxFile, int zoom, public List<TrkSegment> getCachedSegments(int zoom, @NonNull GradientScaleType scaleType,
@NonNull GradientScaleType scaleType,
int[] gradientPalette) { int[] gradientPalette) {
GPXFile gpxFile = selectedGpxFile.getGpxFile(); GPXFile gpxFile = selectedGpxFile.getGpxFile();
String trackId = zoom + "_" + scaleType.toString(); String trackId = zoom + "_" + scaleType.toString();
if (modifiedTime == gpxFile.modifiedTime) { if (prevModifiedTime == gpxFile.modifiedTime) {
List<TrkSegment> segments = cache.get(trackId); List<TrkSegment> segments = cache.get(trackId);
if (segments == null) { if (segments == null) {
segments = calculateGradientTrack(selectedGpxFile, zoom, scaleType, gradientPalette); segments = calculateGradientTrack(selectedGpxFile, zoom, scaleType, gradientPalette);
@ -1243,7 +1263,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
return segments; return segments;
} else { } else {
cache.clear(); cache.clear();
modifiedTime = gpxFile.modifiedTime; prevModifiedTime = gpxFile.modifiedTime;
List<TrkSegment> segments = calculateGradientTrack(selectedGpxFile, zoom, scaleType, gradientPalette); List<TrkSegment> segments = calculateGradientTrack(selectedGpxFile, zoom, scaleType, gradientPalette);
cache.put(trackId, segments); cache.put(trackId, segments);
return segments; return segments;