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.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,10 +457,15 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
private void drawDirectionArrows(Canvas canvas, RotatedTileBox tileBox, List<SelectedGpxFile> 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());
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);
@ -475,7 +482,6 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
}
}
}
}
private void drawSelectedFilesStartEndPoints(Canvas canvas, RotatedTileBox tileBox, List<SelectedGpxFile> selectedGPXFiles) {
if (tileBox.getZoom() >= START_ZOOM) {
@ -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<TrkSegment> 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<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
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<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.modifiedTime = modifiedTime;
this.selectedGpxFile = selectedGpxFile;
}
public List<TrkSegment> getCachedSegments(@NonNull SelectedGpxFile selectedGpxFile, int zoom,
@NonNull GradientScaleType scaleType,
public List<TrkSegment> 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<TrkSegment> 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<TrkSegment> segments = calculateGradientTrack(selectedGpxFile, zoom, scaleType, gradientPalette);
cache.put(trackId, segments);
return segments;