Skip to content

Commit

Permalink
Fix #17599 Crash on raster map download
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-kutz committed Jul 19, 2023
1 parent 1ff2bee commit bb3de64
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.osmand.plus.plugins.rastermaps.CalculateMissingTilesTask.MissingTilesInfo;
import net.osmand.plus.plugins.rastermaps.DownloadTilesHelper.DownloadType;
import net.osmand.plus.resources.BitmapTilesCache;
import net.osmand.plus.resources.SQLiteTileSource;
import net.osmand.plus.settings.enums.MapLayerType;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.plus.utils.ColorUtilities;
Expand Down Expand Up @@ -115,7 +116,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
downloadTilesHelper = app.getDownloadTilesHelper();
mapView = requireMapActivity().getMapView();
tilesPreviewDrawer = new TilesPreviewDrawer(app);
tileSource = settings.getLayerTileSource(layerToDownload.getMapLayerSettings(app), false);
tileSource = loadTileSource();
handler = new UpdateTilesHandler(() -> {
setupTilesDownloadInfo();
updateTilesPreview();
Expand Down Expand Up @@ -239,7 +240,7 @@ private void setupMapSourceSetting() {
mapActivity.getMapLayers().selectMapLayer(mapActivity, false,
layerToDownload.getMapLayerSettings(app), mapSourceName -> {
if (shouldShowDialog(app)) {
tileSource = settings.getLayerTileSource(layerToDownload.getMapLayerSettings(app), false);
tileSource = loadTileSource();
int currentZoom = mapView.getZoom();
selectedMaxZoom = tileSource.getMaximumZoomSupported();
selectedMinZoom = Math.min(currentZoom, selectedMaxZoom);
Expand Down Expand Up @@ -610,6 +611,14 @@ private MapActivity getMapActivity() {
return activity == null ? null : ((MapActivity) activity);
}

private ITileSource loadTileSource() {
ITileSource tileSource = settings.getLayerTileSource(layerToDownload.getMapLayerSettings(app), false);
if (tileSource instanceof SQLiteTileSource) {
((SQLiteTileSource) tileSource).initDatabaseIfNeeded();
}
return tileSource;
}

public static boolean shouldShowDialog(@NonNull OsmandApplication app) {
List<OsmandMapLayer> layers = app.getOsmandMap().getMapView().getLayers();
for (OsmandMapLayer layer : layers) {
Expand Down
4 changes: 4 additions & 0 deletions OsmAnd/src/net/osmand/plus/resources/SQLiteTileSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ public boolean equals(Object obj) {
return Algorithms.stringsEqual(fileName, other.fileName);
}

public void initDatabaseIfNeeded() {
getDatabase();
}

protected synchronized SQLiteConnection getDatabase() {
if ((db == null || db.isClosed()) && file.exists()) {
LOG.debug("Open " + file.getAbsolutePath());
Expand Down

0 comments on commit bb3de64

Please sign in to comment.