Merge branches 'map_source_dialog_fix' and 'master' of https://github.com/osmandapp/Osmand into map_source_dialog_fix

This commit is contained in:
veliymolfar 2020-06-12 11:10:21 +03:00
commit 8f2df57a7e
10 changed files with 298 additions and 241 deletions

View file

@ -11,6 +11,7 @@ import java.util.PriorityQueue;
import net.osmand.PlatformUtil;
import net.osmand.binary.RouteDataObject;
import net.osmand.data.LatLon;
import net.osmand.osm.MapRenderingTypes;
import net.osmand.util.MapUtils;
@ -859,6 +860,12 @@ public class BinaryRoutePlanner {
public int preciseX;
public int preciseY;
public List<RouteSegmentPoint> others;
public LatLon getPreciseLatLon() {
return new LatLon(MapUtils.get31LatitudeY(preciseY), MapUtils.get31LongitudeX(preciseX));
}
}
public static class RouteSegment {

View file

@ -243,9 +243,7 @@ public class RoutePlannerFrontEnd {
return null;
}
ctx.calculationProgress.nextIteration();
List<RouteSegmentResult> res = searchRoute(ctx, points, routeDirection);
// make start and end more precise
makeStartEndPointsPrecise(res, start, end, intermediates);
List<RouteSegmentResult> res = searchRouteImpl(ctx, points, routeDirection);
if (res != null) {
new RouteResultPreparation().printResults(ctx, start, end, res);
}
@ -256,33 +254,6 @@ public class RoutePlannerFrontEnd {
if (res.size() > 0) {
updateResult(res.get(0), start, true);
updateResult(res.get(res.size() - 1), end, false);
if (intermediates != null) {
int k = 1;
for (int i = 0; i < intermediates.size(); i++) {
LatLon ll = intermediates.get(i);
int px = MapUtils.get31TileNumberX(ll.getLongitude());
int py = MapUtils.get31TileNumberY(ll.getLatitude());
for (; k < res.size(); k++) {
double currentsDist = projectDistance(res, k, px, py);
if (currentsDist < 500 * 500) {
for (int k1 = k + 1; k1 < res.size(); k1++) {
double c2 = projectDistance(res, k1, px, py);
if (c2 < currentsDist) {
k = k1;
currentsDist = c2;
} else if (k1 - k > 15) {
break;
}
}
updateResult(res.get(k), ll, false);
if (k < res.size() - 1) {
updateResult(res.get(k + 1), ll, true);
}
break;
}
}
}
}
}
}
@ -474,13 +445,18 @@ public class RoutePlannerFrontEnd {
}
private List<RouteSegmentResult> searchRoute(final RoutingContext ctx, List<RouteSegmentPoint> points, PrecalculatedRouteDirection routeDirection)
private List<RouteSegmentResult> searchRouteImpl(final RoutingContext ctx, List<RouteSegmentPoint> points, PrecalculatedRouteDirection routeDirection)
throws IOException, InterruptedException {
if (points.size() <= 2) {
// simple case 2 points only
if (!useSmartRouteRecalculation) {
ctx.previouslyCalculatedRoute = null;
}
return searchRoute(ctx, points.get(0), points.get(1), routeDirection);
pringGC(ctx, true);
List<RouteSegmentResult> res = searchRouteInternalPrepare(ctx, points.get(0), points.get(1), routeDirection);
pringGC(ctx, false);
makeStartEndPointsPrecise(res, points.get(0).getPreciseLatLon(), points.get(1).getPreciseLatLon(), null);
return res;
}
ArrayList<RouteSegmentResult> firstPartRecalculatedRoute = null;
@ -522,7 +498,7 @@ public class RoutePlannerFrontEnd {
local.visitor = ctx.visitor;
local.calculationProgress = ctx.calculationProgress;
List<RouteSegmentResult> res = searchRouteInternalPrepare(local, points.get(i), points.get(i + 1), routeDirection);
makeStartEndPointsPrecise(res, points.get(i).getPreciseLatLon(), points.get(i + 1).getPreciseLatLon(), null);
results.addAll(res);
ctx.distinctLoadedTiles += local.distinctLoadedTiles;
ctx.loadedTiles += local.loadedTiles;
@ -545,27 +521,22 @@ public class RoutePlannerFrontEnd {
}
@SuppressWarnings("static-access")
private List<RouteSegmentResult> searchRoute(final RoutingContext ctx, RouteSegmentPoint start, RouteSegmentPoint end,
PrecalculatedRouteDirection routeDirection) throws IOException, InterruptedException {
if (ctx.SHOW_GC_SIZE) {
long h1 = ctx.runGCUsedMemory();
private void pringGC(final RoutingContext ctx, boolean before) {
if (RoutingContext.SHOW_GC_SIZE && before) {
long h1 = RoutingContext.runGCUsedMemory();
float mb = (1 << 20);
log.warn("Used before routing " + h1 / mb + " actual");
}
List<RouteSegmentResult> result = searchRouteInternalPrepare(ctx, start, end, routeDirection);
if (RoutingContext.SHOW_GC_SIZE) {
} else if (RoutingContext.SHOW_GC_SIZE && !before) {
int sz = ctx.global.size;
log.warn("Subregion size " + ctx.subregionTiles.size() + " " + " tiles " + ctx.indexedSubregions.size());
ctx.runGCUsedMemory();
long h1 = ctx.runGCUsedMemory();
RoutingContext.runGCUsedMemory();
long h1 = RoutingContext.runGCUsedMemory();
ctx.unloadAllData();
ctx.runGCUsedMemory();
long h2 = ctx.runGCUsedMemory();
RoutingContext.runGCUsedMemory();
long h2 = RoutingContext.runGCUsedMemory();
float mb = (1 << 20);
log.warn("Unload context : estimated " + sz / mb + " ?= " + (h1 - h2) / mb + " actual");
}
return result;
}

View file

@ -36,7 +36,7 @@ import net.osmand.router.RoutePlannerFrontEnd.RouteCalculationMode;
public class RoutingContext {
public static final boolean SHOW_GC_SIZE = false;
public static boolean SHOW_GC_SIZE = false;
private final static Log log = PlatformUtil.getLog(RoutingContext.class);

View file

@ -524,7 +524,7 @@ dependencies {
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha06'
implementation 'com.google.android.material:material:1.2.0-beta01'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.preference:preference:1.1.0'
implementation fileTree(include: ['gnu-trove-osmand.jar', 'icu4j-49_1_patched.jar'], dir: 'libs')

View file

@ -275,7 +275,7 @@
android:textSize="@dimen/default_list_text_size"
tools:text="3" />
<com.google.android.material.slider.Slider
<com.google.android.material.slider.RangeSlider
android:id="@+id/zoom_slider"
style="@style/Widget.Styled.Slider"
android:layout_width="0dp"

View file

@ -65,7 +65,7 @@
android:textSize="@dimen/default_list_text_size"
tools:text="3" />
<com.google.android.material.slider.Slider
<com.google.android.material.slider.RangeSlider
android:id="@+id/zoom_slider"
style="@style/Widget.Styled.Slider"
android:layout_width="0dp"

View file

@ -37,6 +37,7 @@ import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import androidx.core.widget.TintableCompoundButton;
import com.google.android.material.slider.RangeSlider;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.slider.Slider;
import com.google.android.material.snackbar.Snackbar;
@ -524,29 +525,63 @@ public class UiUtilities {
activeColor = AndroidUtils.getColorFromAttr(ctx, R.attr.active_color_basic);
}
int activeDisableColor = getColorWithAlpha(activeColor, 0.25f);
ColorStateList activeCsl = new ColorStateList(states,
new int[] {activeColor, activeDisableColor});
int inactiveColor = ContextCompat.getColor(ctx,
nightMode ? R.color.icon_color_default_dark : R.color.icon_color_secondary_light);
ColorStateList inactiveCsl = new ColorStateList(states,
new int[] {inactiveColor, inactiveColor});
slider.setTrackColorActive(activeCsl);
slider.setTrackColorInactive(inactiveCsl);
slider.setHaloColor(activeCsl);
slider.setThumbColor(activeCsl);
ColorStateList activeCsl = new ColorStateList(states, new int[] {activeColor, activeDisableColor});
int inactiveColor = ContextCompat.getColor(ctx, nightMode ? R.color.icon_color_default_dark : R.color.icon_color_secondary_light);
ColorStateList inactiveCsl = new ColorStateList(states, new int[] {inactiveColor, inactiveColor});
slider.setTrackActiveTintList(activeCsl);
slider.setTrackInactiveTintList(inactiveCsl);
slider.setHaloTintList(activeCsl);
slider.setThumbTintList(activeCsl);
int colorBlack = ContextCompat.getColor(ctx, R.color.color_black);
int ticksColor = showTicks ?
(nightMode ? colorBlack : getColorWithAlpha(colorBlack, 0.5f)) :
Color.TRANSPARENT;
slider.setTickColor(new ColorStateList(states, new int[] {ticksColor, ticksColor}));
slider.setTickTintList(new ColorStateList(states, new int[] {ticksColor, ticksColor}));
// sizes
int thumbRadius = ctx.getResources().getDimensionPixelSize(R.dimen.slider_thumb_size);
int haloRadius = ctx.getResources().getDimensionPixelSize(R.dimen.slider_thumb_halo_size);
int trackHeight = ctx.getResources().getDimensionPixelSize(R.dimen.slider_track_height);
slider.setThumbRadius(thumbRadius);
slider.setHaloRadius(haloRadius);
slider.setTrackHeight(trackHeight);
slider.setThumbRadius(ctx.getResources().getDimensionPixelSize(R.dimen.slider_thumb_size));
slider.setHaloRadius(ctx.getResources().getDimensionPixelSize(R.dimen.slider_thumb_halo_size));
slider.setTrackHeight(ctx.getResources().getDimensionPixelSize(R.dimen.slider_track_height));
// label behavior
slider.setLabelBehavior(Slider.LABEL_GONE);
}
public static void setupSlider(RangeSlider slider, boolean nightMode,
@ColorInt Integer activeColor, boolean showTicks) {
Context ctx = slider.getContext();
if (ctx == null) {
return;
}
int themeId = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
ctx = new ContextThemeWrapper(ctx, themeId);
// colors
int[][] states = new int[][] {
new int[] {android.R.attr.state_enabled},
new int[] {-android.R.attr.state_enabled}
};
if (activeColor == null) {
activeColor = AndroidUtils.getColorFromAttr(ctx, R.attr.active_color_basic);
}
int activeDisableColor = getColorWithAlpha(activeColor, 0.25f);
ColorStateList activeCsl = new ColorStateList(states, new int[] {activeColor, activeDisableColor});
int inactiveColor = ContextCompat.getColor(ctx, nightMode ? R.color.icon_color_default_dark : R.color.icon_color_secondary_light);
ColorStateList inactiveCsl = new ColorStateList(states, new int[] {inactiveColor, inactiveColor});
slider.setTrackActiveTintList(activeCsl);
slider.setTrackInactiveTintList(inactiveCsl);
slider.setHaloTintList(activeCsl);
slider.setThumbTintList(activeCsl);
int colorBlack = ContextCompat.getColor(ctx, R.color.color_black);
int ticksColor = showTicks ?
(nightMode ? colorBlack : getColorWithAlpha(colorBlack, 0.5f)) :
Color.TRANSPARENT;
slider.setTickTintList(new ColorStateList(states, new int[] {ticksColor, ticksColor}));
// sizes
slider.setThumbRadius(ctx.getResources().getDimensionPixelSize(R.dimen.slider_thumb_size));
slider.setHaloRadius(ctx.getResources().getDimensionPixelSize(R.dimen.slider_thumb_halo_size));
slider.setTrackHeight(ctx.getResources().getDimensionPixelSize(R.dimen.slider_track_height));
// label behavior
slider.setLabelBehavior(Slider.LABEL_GONE);

View file

@ -17,7 +17,7 @@ import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.google.android.material.slider.Slider;
import com.google.android.material.slider.RangeSlider;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
@ -97,16 +97,16 @@ public class InputZoomLevelsBottomSheet extends MenuBottomSheetDialogFragment {
minZoomValue.setText(String.valueOf(minZoom));
final TextView maxZoomValue = sliderView.findViewById(R.id.zoom_value_max);
maxZoomValue.setText(String.valueOf(maxZoom));
Slider slider = sliderView.findViewById(R.id.zoom_slider);
RangeSlider slider = sliderView.findViewById(R.id.zoom_slider);
int colorProfileRes = app.getSettings().getApplicationMode().getIconColorInfo().getColor(nightMode);
int colorProfile = ContextCompat.getColor(app, colorProfileRes);
UiUtilities.setupSlider(slider, nightMode, colorProfile, true);
slider.setValueFrom(SLIDER_FROM);
slider.setValueTo(SLIDER_TO);
slider.setValues((float) minZoom, (float) maxZoom);
slider.addOnChangeListener(new Slider.OnChangeListener() {
slider.addOnChangeListener(new RangeSlider.OnChangeListener() {
@Override
public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
public void onValueChange(@NonNull RangeSlider slider, float value, boolean fromUser) {
List<Float> values = slider.getValues();
if (values.size() > 0) {
minZoomValue.setText(String.valueOf(values.get(0).intValue()));
@ -114,14 +114,13 @@ public class InputZoomLevelsBottomSheet extends MenuBottomSheetDialogFragment {
}
}
});
slider.addOnSliderTouchListener(new Slider.OnSliderTouchListener() {
slider.addOnSliderTouchListener(new RangeSlider.OnSliderTouchListener() {
@Override
public void onStartTrackingTouch(@NonNull Slider slider) {
public void onStartTrackingTouch(@NonNull RangeSlider slider) {
}
@Override
public void onStopTrackingTouch(@NonNull Slider slider) {
public void onStopTrackingTouch(@NonNull RangeSlider slider) {
List<Float> values = slider.getValues();
if (values.size() > 0) {
minZoom = values.get(0).intValue();

View file

@ -1,6 +1,7 @@
package net.osmand.plus.rastermaps;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
@ -28,9 +29,6 @@ import net.osmand.plus.ContextMenuItem;
import net.osmand.plus.DialogListItemAdapter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.backend.OsmandSettings.CommonPreference;
import net.osmand.plus.settings.backend.OsmandSettings.LayerTransparencySeekbarMode;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.activities.DownloadTilesDialog;
@ -40,10 +38,14 @@ import net.osmand.plus.activities.MapActivityLayers;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.dialogs.RasterMapMenu;
import net.osmand.plus.quickaction.QuickActionType;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.backend.OsmandSettings.CommonPreference;
import net.osmand.plus.settings.backend.OsmandSettings.LayerTransparencySeekbarMode;
import net.osmand.plus.views.MapTileLayer;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.Algorithms;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -119,8 +121,6 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
};
// mapView.addLayer(overlayLayer, 0.7f);
settings.MAP_OVERLAY_TRANSPARENCY.addListener(overlayLayerListener);
}
@Override
@ -129,8 +129,9 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}
public void updateMapLayers(OsmandMapTileView mapView, CommonPreference<String> settingsToWarnAboutMap,
final MapActivityLayers layers) {
public void updateMapLayers(OsmandMapTileView mapView,
CommonPreference<String> settingsToWarnAboutMap,
MapActivityLayers layers) {
if (overlayLayer == null) {
createLayers();
}
@ -160,7 +161,8 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}
public void updateLayer(OsmandMapTileView mapView, OsmandSettings settings,
MapTileLayer layer, CommonPreference<String> preference, float layerOrder, boolean warnWhenSelected) {
MapTileLayer layer, CommonPreference<String> preference,
float layerOrder, boolean warnWhenSelected) {
ITileSource overlay = settings.getTileSourceByName(preference.get(), warnWhenSelected);
if (!Algorithms.objectEquals(overlay, layer.getMap())) {
if (overlay == null) {
@ -173,25 +175,25 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}
}
public void selectMapOverlayLayer(@NonNull final OsmandMapTileView mapView,
public void selectMapOverlayLayer(@NonNull OsmandMapTileView mapView,
@NonNull final CommonPreference<String> mapPref,
@NonNull final CommonPreference<String> exMapPref,
boolean force,
@NonNull final MapActivity activity,
@NonNull MapActivity mapActivity,
@Nullable final OnMapSelectedCallback callback) {
final MapActivityLayers layers = activity.getMapLayers();
final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
if (!force && exMapPref.get() != null) {
mapPref.set(exMapPref.get());
if (callback != null) {
callback.onMapSelected(false);
}
updateMapLayers(mapView, mapPref, layers);
updateMapLayers(mapView, mapPref, mapActivity.getMapLayers());
return;
}
final OsmandSettings settings = app.getSettings();
OsmandSettings settings = app.getSettings();
Map<String, String> entriesMap = settings.getTileSourceEntries();
final ArrayList<String> keys = new ArrayList<>(entriesMap.keySet());
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity, app)));
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(mapActivity, getThemeRes(mapActivity)));
final String[] items = new String[entriesMap.size() + 1];
int i = 0;
for (String it : entriesMap.values()) {
@ -202,13 +204,24 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity == null || mapActivity.isFinishing()) {
return;
}
if (which == items.length - 1) {
installMapLayers(activity, new ResultMatcher<TileSourceTemplate>() {
installMapLayers(mapActivity, new ResultMatcher<TileSourceTemplate>() {
TileSourceTemplate template = null;
int count = 0;
boolean cancel = false;
@Override
public boolean publish(TileSourceTemplate object) {
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity == null || mapActivity.isFinishing()) {
cancel = true;
return false;
}
OsmandMapTileView mapView = mapActivity.getMapView();
if (object == null) {
if (count == 1) {
mapPref.set(template.getName());
@ -216,9 +229,9 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
if (callback != null) {
callback.onMapSelected(false);
}
updateMapLayers(mapView, mapPref, layers);
updateMapLayers(mapView, mapPref, mapActivity.getMapLayers());
} else {
selectMapOverlayLayer(mapView, mapPref, exMapPref, false, activity, null);
selectMapOverlayLayer(mapView, mapPref, exMapPref, false, mapActivity, null);
}
} else {
count++;
@ -229,7 +242,7 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
@Override
public boolean isCancelled() {
return false;
return cancel;
}
});
} else {
@ -238,11 +251,10 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
if (callback != null) {
callback.onMapSelected(false);
}
updateMapLayers(mapView, mapPref, layers);
updateMapLayers(mapActivity.getMapView(), mapPref, mapActivity.getMapLayers());
}
dialog.dismiss();
}
})
.setNegativeButton(R.string.shared_string_cancel, null)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@ -257,43 +269,52 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}
@Override
public void registerLayerContextMenuActions(final OsmandMapTileView mapView,
public void registerLayerContextMenuActions(OsmandMapTileView mapView,
ContextMenuAdapter adapter,
final MapActivity mapActivity) {
MapActivity mapActivity) {
final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() {
@Override
public boolean onRowItemClick(ArrayAdapter<ContextMenuItem> adapter, View view, int itemId, int position) {
int[] viewCoordinates = AndroidUtils.getCenterViewCoordinates(view);
if (itemId == R.string.layer_overlay) {
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.OVERLAY_MAP, viewCoordinates);
return false;
} else if (itemId == R.string.layer_underlay) {
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.UNDERLAY_MAP, viewCoordinates);
return false;
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity != null && !mapActivity.isFinishing()) {
int[] viewCoordinates = AndroidUtils.getCenterViewCoordinates(view);
if (itemId == R.string.layer_overlay) {
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.OVERLAY_MAP, viewCoordinates);
return false;
} else if (itemId == R.string.layer_underlay) {
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.UNDERLAY_MAP, viewCoordinates);
return false;
}
}
return true;
}
@Override
public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> adapter, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
final OsmandSettings settings = mapActivity.getMyApplication().getSettings();
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity == null || mapActivity.isFinishing()) {
return false;
}
if (itemId == R.string.layer_overlay) {
toggleUnderlayState(mapActivity, RasterMapType.OVERLAY,
new OnMapSelectedCallback() {
@Override
public void onMapSelected(boolean canceled) {
ContextMenuItem item = adapter.getItem(pos);
String overlayMapDescr = settings.MAP_OVERLAY.get();
boolean hasOverlayDescription = overlayMapDescr != null;
overlayMapDescr = hasOverlayDescription ? overlayMapDescr
: mapActivity.getString(R.string.shared_string_none);
item.setDescription(overlayMapDescr);
item.setSelected(hasOverlayDescription);
item.setColorRes(hasOverlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
adapter.notifyDataSetChanged();
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity != null && !mapActivity.isFinishing()) {
ContextMenuItem item = adapter.getItem(pos);
if (item != null) {
String overlayMapDescr = mapActivity.getMyApplication().getSettings().MAP_OVERLAY.get();
boolean hasOverlayDescription = overlayMapDescr != null;
overlayMapDescr = hasOverlayDescription ? overlayMapDescr
: mapActivity.getString(R.string.shared_string_none);
item.setDescription(overlayMapDescr);
item.setSelected(hasOverlayDescription);
item.setColorRes(hasOverlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
adapter.notifyDataSetChanged();
}
}
}
});
return false;
@ -302,24 +323,29 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
new OnMapSelectedCallback() {
@Override
public void onMapSelected(boolean canceled) {
ContextMenuItem item = adapter.getItem(pos);
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity != null && !mapActivity.isFinishing()) {
ContextMenuItem item = adapter.getItem(pos);
if (item != null) {
String underlayMapDescr = settings.MAP_UNDERLAY.get();
String underlayMapDescr = settings.MAP_UNDERLAY.get();
boolean hasUnderlayDescription = underlayMapDescr != null;
underlayMapDescr = hasUnderlayDescription
? underlayMapDescr
: mapActivity.getString(R.string.shared_string_none);
boolean hasUnderlayDescription = underlayMapDescr != null;
underlayMapDescr = hasUnderlayDescription ? underlayMapDescr
: mapActivity.getString(R.string.shared_string_none);
item.setDescription(underlayMapDescr);
item.setSelected(hasUnderlayDescription);
item.setColorRes(hasUnderlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
item.setDescription(underlayMapDescr);
item.setSelected(hasUnderlayDescription);
item.setColorRes(hasUnderlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
adapter.notifyDataSetChanged();
adapter.notifyDataSetChanged();
final CommonPreference<Boolean> hidePolygonsPref =
mapActivity.getMyApplication().getSettings().getCustomRenderBooleanProperty("noPolygons");
hidePolygonsPref.set(hasUnderlayDescription);
RasterMapMenu.refreshMapComplete(mapActivity);
final CommonPreference<Boolean> hidePolygonsPref =
mapActivity.getMyApplication().getSettings().getCustomRenderBooleanProperty("noPolygons");
hidePolygonsPref.set(hasUnderlayDescription);
RasterMapMenu.refreshMapComplete(mapActivity);
}
}
}
});
return false;
@ -372,18 +398,23 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
@Override
public void registerMapContextMenuActions(final MapActivity mapActivity, final double latitude, final double longitude, ContextMenuAdapter adapter,
Object selectedObj) {
final OsmandMapTileView mapView = mapActivity.getMapView();
if (mapView.getMainLayer() instanceof MapTileLayer) {
public void registerMapContextMenuActions(MapActivity mapActivity,
final double latitude, final double longitude,
ContextMenuAdapter adapter, Object selectedObj) {
final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
if (mapActivity.getMapView().getMainLayer() instanceof MapTileLayer) {
ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() {
@Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int resId, int pos, boolean isChecked, int[] viewCoordinates) {
if (resId == R.string.context_menu_item_update_map) {
mapActivity.getMapActions().reloadTile(mapView.getZoom(), latitude, longitude);
} else if (resId == R.string.shared_string_download_map) {
DownloadTilesDialog dlg = new DownloadTilesDialog(mapActivity, (OsmandApplication) mapActivity.getApplication(), mapView);
dlg.openDialog();
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity != null && !mapActivity.isFinishing()) {
OsmandMapTileView mapView = mapActivity.getMapView();
if (resId == R.string.context_menu_item_update_map) {
mapActivity.getMapActions().reloadTile(mapView.getZoom(), latitude, longitude);
} else if (resId == R.string.shared_string_download_map) {
DownloadTilesDialog dlg = new DownloadTilesDialog(mapActivity, (OsmandApplication) mapActivity.getApplication(), mapView);
dlg.openDialog();
}
}
return true;
}
@ -403,10 +434,10 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}
}
public static void installMapLayers(final Activity activity, final ResultMatcher<TileSourceTemplate> result) {
public static void installMapLayers(Activity activity, final ResultMatcher<TileSourceTemplate> result) {
final WeakReference<Activity> activityRef = new WeakReference<>(activity);
final OsmandApplication app = (OsmandApplication) activity.getApplication();
final OsmandSettings settings = app.getSettings();
final Map<String, String> entriesMap = settings.getTileSourceEntries();
OsmandSettings settings = app.getSettings();
if (!settings.isInternetConnectionAvailable(true)) {
Toast.makeText(activity, R.string.internet_not_available, Toast.LENGTH_LONG).show();
return;
@ -418,27 +449,36 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}
protected void onPostExecute(final java.util.List<TileSourceTemplate> downloaded) {
Activity activity = activityRef.get();
if (activity == null || activity.isFinishing()) {
return;
}
OsmandApplication app = (OsmandApplication) activity.getApplication();
if (downloaded == null || downloaded.isEmpty()) {
Toast.makeText(activity, R.string.shared_string_io_error, Toast.LENGTH_SHORT).show();
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity, app)));
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity)));
String[] names = new String[downloaded.size()];
for (int i = 0; i < names.length; i++) {
names[i] = downloaded.get(i).getName();
}
final boolean[] selected = new boolean[downloaded.size()];
boolean nightMode = isNightMode(activity, app);
int themeResId = getThemeRes(activity, app);
boolean nightMode = isNightMode(activity);
int themeResId = getThemeRes(activity);
int selectedProfileColor = ContextCompat.getColor(app, app.getSettings().getApplicationMode().getIconColorInfo().getColor(nightMode));
DialogListItemAdapter dialogAdapter = DialogListItemAdapter.createMultiChoiceAdapter(names, nightMode, selected, app,
selectedProfileColor, themeResId, new View.OnClickListener() {
@Override
public void onClick(View v) {
int which = (int) v.getTag();
selected[which] = !selected[which];
if (entriesMap.containsKey(downloaded.get(which).getName()) && selected[which]) {
Toast.makeText(activity, R.string.tile_source_already_installed, Toast.LENGTH_SHORT).show();
Activity activity = activityRef.get();
if (activity != null && !activity.isFinishing()) {
Map<String, String> entriesMap = ((OsmandApplication)activity.getApplication()).getSettings().getTileSourceEntries();
int which = (int) v.getTag();
selected[which] = !selected[which];
if (entriesMap.containsKey(downloaded.get(which).getName()) && selected[which]) {
Toast.makeText(activity, R.string.tile_source_already_installed, Toast.LENGTH_SHORT).show();
}
}
}
}
@ -449,22 +489,26 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
builder.setPositiveButton(R.string.shared_string_apply, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
List<TileSourceTemplate> toInstall = new ArrayList<>();
for (int i = 0; i < selected.length; i++) {
if (selected[i]) {
toInstall.add(downloaded.get(i));
}
}
for (TileSourceTemplate ts : toInstall) {
if (settings.installTileSource(ts)) {
if (result != null) {
result.publish(ts);
Activity activity = activityRef.get();
if (activity != null && !activity.isFinishing()) {
OsmandSettings settings = ((OsmandApplication) activity.getApplication()).getSettings();
List<TileSourceTemplate> toInstall = new ArrayList<>();
for (int i = 0; i < selected.length; i++) {
if (selected[i]) {
toInstall.add(downloaded.get(i));
}
}
}
// at the end publish null to show end of process
if (!toInstall.isEmpty() && result != null) {
result.publish(null);
for (TileSourceTemplate ts : toInstall) {
if (settings.installTileSource(ts)) {
if (result != null) {
result.publish(ts);
}
}
}
// at the end publish null to show end of process
if (!toInstall.isEmpty() && result != null) {
result.publish(null);
}
}
}
});
@ -492,7 +536,6 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
OsmandMapTileView mapView = mapActivity.getMapView();
CommonPreference<String> mapTypePreference;
CommonPreference<String> exMapTypePreference;
OsmandSettings.CommonPreference<Integer> mapTransparencyPreference;
//boolean isMapSelected;
MapTileLayer layer;
@ -529,15 +572,16 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}
}
private static boolean isNightMode(Activity activity, OsmandApplication app) {
if (activity == null || app == null) {
private static boolean isNightMode(Context context) {
if (context == null) {
return false;
}
return activity instanceof MapActivity ? app.getDaynightHelper().isNightModeForMapControls() : !app.getSettings().isLightContent();
OsmandApplication app = (OsmandApplication) context.getApplicationContext();
return context instanceof MapActivity ? app.getDaynightHelper().isNightModeForMapControls() : !app.getSettings().isLightContent();
}
private static int getThemeRes(Activity activity, OsmandApplication app) {
return isNightMode(activity, app) ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
private static int getThemeRes(Context context) {
return isNightMode(context) ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
}
public enum RasterMapType {

View file

@ -27,6 +27,7 @@ import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import com.github.ksoichiro.android.observablescrollview.ObservableListView;
import com.google.android.material.slider.RangeSlider;
import com.google.android.material.slider.Slider;
import net.osmand.AndroidUtils;
@ -51,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;
@ -62,7 +64,7 @@ import static net.osmand.plus.srtmplugin.SRTMPlugin.TERRAIN_MIN_ZOOM;
public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickListener,
Slider.OnSliderTouchListener, Slider.OnChangeListener, DownloadIndexesThread.DownloadEvents {
DownloadIndexesThread.DownloadEvents {
public static final String TAG = TerrainFragment.class.getSimpleName();
private static final Log LOG = PlatformUtil.getLog(TerrainFragment.class.getSimpleName());
@ -101,12 +103,46 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL
private View downloadTopDivider;
private View downloadBottomDivider;
private Slider transparencySlider;
private Slider zoomSlider;
private RangeSlider zoomSlider;
private ObservableListView observableListView;
private View bottomEmptySpace;
private ArrayAdapter<ContextMenuItem> listAdapter;
private Slider.OnChangeListener transparencySliderChangeListener = new Slider.OnChangeListener() {
@Override
public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
if (fromUser) {
String transparencyStr = (int) value + "%";
transparencyValueTv.setText(transparencyStr);
srtmPlugin.setTerrainTransparency((int) Math.ceil(value * 2.55), srtmPlugin.getTerrainMode());
refreshMap();
}
}
};
private RangeSlider.OnChangeListener zoomSliderChangeListener = new RangeSlider.OnChangeListener() {
@Override
public void onValueChange(@NonNull RangeSlider slider, float value, boolean fromUser) {
List<Float> values = slider.getValues();
if (values.size() > 0) {
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();
@ -175,10 +211,8 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL
UiUtilities.setupSlider(transparencySlider, nightMode, colorProfile);
UiUtilities.setupSlider(zoomSlider, nightMode, colorProfile, true);
transparencySlider.addOnSliderTouchListener(this);
zoomSlider.addOnSliderTouchListener(this);
transparencySlider.addOnChangeListener(this);
zoomSlider.addOnChangeListener(this);
transparencySlider.addOnChangeListener(transparencySliderChangeListener);
zoomSlider.addOnChangeListener(zoomSliderChangeListener);
transparencySlider.setValueTo(100);
transparencySlider.setValueFrom(0);
zoomSlider.setValueTo(TERRAIN_MAX_ZOOM);
@ -207,47 +241,6 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL
}
}
@Override
public void onStartTrackingTouch(@NonNull Slider slider) {
}
@Override
public void onStopTrackingTouch(@NonNull Slider slider) {
switch (slider.getId()) {
case R.id.transparency_slider:
double d = slider.getValue() * 2.55;
srtmPlugin.setTerrainTransparency((int) Math.ceil(d), srtmPlugin.getTerrainMode());
break;
case R.id.zoom_slider:
List<Float> values = slider.getValues();
if (values.size() > 0) {
srtmPlugin.setTerrainZoomValues(values.get(0).intValue(), values.get(1).intValue(), srtmPlugin.getTerrainMode());
}
break;
}
updateLayers();
}
@Override
public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) {
if (fromUser) {
switch (slider.getId()) {
case R.id.transparency_slider:
String transparency = (int) value + "%";
transparencyValueTv.setText(transparency);
break;
case R.id.zoom_slider:
List<Float> values = slider.getValues();
if (values.size() > 0) {
minZoomTv.setText(String.valueOf(values.get(0).intValue()));
maxZoomTv.setText(String.valueOf(values.get(1).intValue()));
}
break;
}
}
}
private void updateUiMode() {
TerrainMode mode = srtmPlugin.getTerrainMode();
if (terrainEnabled) {
@ -384,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);
}
}
@ -400,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<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
final DownloadIndexesThread downloadThread = app.getDownloadThread();
if (!downloadThread.getIndexes().isDownloadedFromInternet) {
@ -444,22 +442,25 @@ public class TerrainFragment extends BaseOsmAndFragment implements View.OnClickL
.setListener(new ContextMenuAdapter.ItemClickListener() {
@Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> 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;