From 2912163ca1e77f7d5ee91d6a24eabd1d0a646ca9 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Fri, 12 Jun 2020 11:05:24 +0300 Subject: [PATCH] Fix terrain transparency slider response --- .../plus/srtmplugin/TerrainFragment.java | 68 ++++++++++++------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/srtmplugin/TerrainFragment.java b/OsmAnd/src/net/osmand/plus/srtmplugin/TerrainFragment.java index 8aa60d902e..9c905bf8e1 100644 --- a/OsmAnd/src/net/osmand/plus/srtmplugin/TerrainFragment.java +++ b/OsmAnd/src/net/osmand/plus/srtmplugin/TerrainFragment.java @@ -52,6 +52,7 @@ import net.osmand.plus.widgets.style.CustomTypefaceSpan; import org.apache.commons.logging.Log; import java.io.IOException; +import java.lang.ref.WeakReference; import java.util.List; import static net.osmand.plus.download.DownloadActivityType.HILLSHADE_FILE; @@ -115,6 +116,7 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL String transparencyStr = (int) value + "%"; transparencyValueTv.setText(transparencyStr); srtmPlugin.setTerrainTransparency((int) Math.ceil(value * 2.55), srtmPlugin.getTerrainMode()); + refreshMap(); } } }; @@ -127,10 +129,20 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL minZoomTv.setText(String.valueOf(values.get(0).intValue())); maxZoomTv.setText(String.valueOf(values.get(1).intValue())); srtmPlugin.setTerrainZoomValues(values.get(0).intValue(), values.get(1).intValue(), srtmPlugin.getTerrainMode()); + refreshMap(); } } }; + @Nullable + private MapActivity getMapActivity() { + Activity activity = getActivity(); + if (activity instanceof MapActivity && !activity.isFinishing()) { + return (MapActivity) activity; + } + return null; + } + @Override public void onCreate(@Nullable Bundle savedInstanceState) { app = requireMyApplication(); @@ -365,13 +377,17 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL } } + private void refreshMap() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.getMapView().refreshMap(); + } + } + private void updateLayers() { - Activity activity = getActivity(); - if (activity instanceof MapActivity) { - srtmPlugin.updateLayers( - ((MapActivity) activity).getMapView(), - (MapActivity) activity - ); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + srtmPlugin.updateLayers(mapActivity.getMapView(), mapActivity); } } @@ -381,10 +397,11 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL adapter.setProfileDependent(true); adapter.setNightMode(nightMode); - final Activity mapActivity = getActivity(); - if (!(mapActivity instanceof MapActivity)) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity == null) { return; } + final WeakReference mapActivityRef = new WeakReference<>(mapActivity); final DownloadIndexesThread downloadThread = app.getDownloadThread(); if (!downloadThread.getIndexes().isDownloadedFromInternet) { @@ -425,22 +442,25 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL .setListener(new ContextMenuAdapter.ItemClickListener() { @Override public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked, int[] viewCoordinates) { - ContextMenuItem item = adapter.getItem(position); - if (downloadThread.isDownloading(indexItem)) { - downloadThread.cancelDownload(indexItem); - if (item != null) { - item.setProgress(ContextMenuItem.INVALID_ID); - item.setLoading(false); - item.setSecondaryIcon(R.drawable.ic_action_import); - adapter.notifyDataSetChanged(); - } - } else { - new DownloadValidationManager(app).startDownload((MapActivity) mapActivity, indexItem); - if (item != null) { - item.setProgress(ContextMenuItem.INVALID_ID); - item.setLoading(true); - item.setSecondaryIcon(R.drawable.ic_action_remove_dark); - adapter.notifyDataSetChanged(); + MapActivity mapActivity = mapActivityRef.get(); + if (mapActivity != null && !mapActivity.isFinishing()) { + ContextMenuItem item = adapter.getItem(position); + if (downloadThread.isDownloading(indexItem)) { + downloadThread.cancelDownload(indexItem); + if (item != null) { + item.setProgress(ContextMenuItem.INVALID_ID); + item.setLoading(false); + item.setSecondaryIcon(R.drawable.ic_action_import); + adapter.notifyDataSetChanged(); + } + } else { + new DownloadValidationManager(app).startDownload(mapActivity, indexItem); + if (item != null) { + item.setProgress(ContextMenuItem.INVALID_ID); + item.setLoading(true); + item.setSecondaryIcon(R.drawable.ic_action_remove_dark); + adapter.notifyDataSetChanged(); + } } } return false;