From a2b6a24e5cc2a3833f4b12ed77e60243e60bbc3a Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 12 Apr 2021 18:15:22 +0300 Subject: [PATCH] Move update cache size to AsyncLoadingThread --- .../plus/resources/AsyncLoadingThread.java | 21 ++++++++++++++++ .../plus/resources/ResourceManager.java | 25 ++++--------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java index 59f62e30b5..6a81f84472 100644 --- a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java +++ b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java @@ -7,6 +7,7 @@ import net.osmand.data.RotatedTileBox; import net.osmand.map.ITileSource; import net.osmand.map.MapTileDownloader.DownloadRequest; import net.osmand.plus.SQLiteTileSource; +import net.osmand.plus.views.MapTileLayer; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -15,6 +16,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Map.Entry; import java.util.Stack; /** @@ -38,6 +40,7 @@ public class AsyncLoadingThread extends Thread { public void run() { while (true) { try { + updateBitmapTilesCache(); boolean tileLoaded = false; boolean mapLoaded = false; while (!requests.isEmpty()) { @@ -69,6 +72,24 @@ public class AsyncLoadingThread extends Thread { } } + private void updateBitmapTilesCache() { + int maxCacheSize = 0; + for (Entry entry : resourceManger.getMapTileLayerSizes().entrySet()) { + MapTileLayer layer = entry.getKey(); + if (layer.isVisible()) { + maxCacheSize += entry.getValue(); + } + } + BitmapTilesCache bitmapTilesCache = resourceManger.getBitmapTilesCache(); + if (maxCacheSize > 0 && maxCacheSize != bitmapTilesCache.getMaxCacheSize()) { + long freeMemory = Runtime.getRuntime().freeMemory() / (1024 * 1024L); + if ((freeMemory > 0 || maxCacheSize < bitmapTilesCache.getMaxCacheSize())) { + log.info("Bitmap tiles to load in memory : " + maxCacheSize); + bitmapTilesCache.setMaxCacheSize(maxCacheSize); + } + } + } + public void requestToLoadTile(TileLoadDownloadRequest req) { requests.push(req); } diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 51aa73e4b5..31282e6232 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -90,7 +90,6 @@ public class ResourceManager { public static final String VECTOR_MAP = "#vector_map"; //$NON-NLS-1$ private static final String INDEXES_CACHE = "ind.cache"; public static final String DEFAULT_WIKIVOYAGE_TRAVEL_OBF = "Default_wikivoyage.travel.obf"; - private static final int CACHE_SIZE_UPDATE_INTERVAL_MS = 10 * 1000; private static final Log log = PlatformUtil.getLog(ResourceManager.class); @@ -101,8 +100,7 @@ public class ResourceManager { private List tilesCacheList = new ArrayList<>(); private BitmapTilesCache bitmapTilesCache; private GeometryTilesCache geometryTilesCache; - private Map mapTileLayerSizes = new HashMap<>(); - private long lastCacheSizeUpdateTime; + private Map mapTileLayerSizes = new ConcurrentHashMap<>(); private final OsmandApplication context; private List resourceListeners = new ArrayList<>(); @@ -290,29 +288,16 @@ public class ResourceManager { resourceListeners.remove(listener); } + public Map getMapTileLayerSizes() { + return mapTileLayerSizes; + } + public void setMapTileLayerSizes(MapTileLayer layer, int tiles) { mapTileLayerSizes.put(layer, tiles); - updateBitmapTilesCache(); } public void removeMapTileLayerSize(MapTileLayer layer) { mapTileLayerSizes.remove(layer); - updateBitmapTilesCache(); - } - - private void updateBitmapTilesCache() { - if (System.currentTimeMillis() - lastCacheSizeUpdateTime > CACHE_SIZE_UPDATE_INTERVAL_MS) { - lastCacheSizeUpdateTime = System.currentTimeMillis(); - int maxCacheSize = 0; - for (Integer layerTiles : mapTileLayerSizes.values()) { - maxCacheSize += layerTiles; - } - long freeMemory = Runtime.getRuntime().freeMemory() / (1024 * 1024L); - if (maxCacheSize != bitmapTilesCache.getMaxCacheSize() && (freeMemory > 0 || maxCacheSize < bitmapTilesCache.getMaxCacheSize())) { - log.info("Bitmap tiles to load in memory : " + maxCacheSize); - bitmapTilesCache.setMaxCacheSize(maxCacheSize); - } - } } public void resetStoreDirectory() {