Prevent filling disk with map tile cache
package de.stephanlindauer.criticalmaps.utils;
import android.app.Activity;
+import android.support.v4.content.ContextCompat;
import android.view.ViewGroup;
import org.osmdroid.config.Configuration;
+import org.osmdroid.config.IConfigurationProvider;
import org.osmdroid.tileprovider.MapTileProviderBasic;
+import org.osmdroid.tileprovider.modules.SqlTileWriter;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
+import java.io.File;
import de.stephanlindauer.criticalmaps.BuildConfig;
import de.stephanlindauer.criticalmaps.R;
-import static android.support.v4.content.ContextCompat.getColor;
public class MapViewUtils {
private MapViewUtils() {}
public static MapView createMapView(Activity activity) {
+ IConfigurationProvider configuration = Configuration.getInstance();
- Configuration.getInstance().setMapViewHardwareAccelerated(true);
- Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID + "/"
+
+ setMaxCacheSize(configuration);
+
+ configuration.setMapViewHardwareAccelerated(true);
+ configuration.setUserAgentValue(BuildConfig.APPLICATION_ID + "/"
+ BuildConfig.VERSION_NAME + " " + org.osmdroid.library.BuildConfig.APPLICATION_ID
+ "/" + org.osmdroid.library.BuildConfig.VERSION_NAME);
@@ -39,11 +46,30 @@ public class MapViewUtils {
mapView.setTilesScaledToDpi(true);
mapView.getOverlayManager()
.getTilesOverlay()
- .setLoadingBackgroundColor(getColor(activity, R.color.map_loading_tile_color));
+ .setLoadingBackgroundColor(
+ ContextCompat.getColor(activity, R.color.map_loading_tile_color));
mapView.getOverlayManager()
.getTilesOverlay()
- .setLoadingLineColor(getColor(activity, R.color.map_loading_line_color));
+ .setLoadingLineColor(
+ ContextCompat.getColor(activity, R.color.map_loading_line_color));
return mapView;
}
+
+ private static void setMaxCacheSize(IConfigurationProvider configuration) {
+ // code adapted from osmdroid's DefaulConfigurationProvider.load()
+ long cacheSize = 0;
+ File dbFile = new File(configuration.getOsmdroidTileCache().getAbsolutePath()
+ + File.separator + SqlTileWriter.DATABASE_FILENAME);
+ if (dbFile.exists()) {
+ cacheSize = dbFile.length();
+ }
+
+ long freeSpace = configuration.getOsmdroidTileCache().getFreeSpace();
+
+ if (configuration.getTileFileSystemCacheMaxBytes() > (freeSpace + cacheSize)) {
+ configuration.setTileFileSystemCacheMaxBytes((long)((freeSpace + cacheSize) * 0.95));
+ configuration.setTileFileSystemCacheTrimBytes((long)((freeSpace + cacheSize) * 0.90));
+ }
+ }
}
