Skip to content

Commit

Permalink
Merge pull request #17092 from osmandapp/MapDisplayPositionFix
Browse files Browse the repository at this point in the history
Improve applying of map display position
  • Loading branch information
Chumva authored May 2, 2023
2 parents 07a0f91 + 839b422 commit dbe3c9a
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 213 deletions.
1 change: 1 addition & 0 deletions OsmAnd/src/net/osmand/plus/AppInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import net.osmand.plus.helpers.FeedbackHelper;
import net.osmand.plus.helpers.LauncherShortcutsHelper;
import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.MapDisplayPositionManager;
import net.osmand.plus.helpers.TargetPointsHelper;
import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.inapp.InAppPurchaseHelperImpl;
Expand Down
1 change: 0 additions & 1 deletion OsmAnd/src/net/osmand/plus/activities/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,6 @@ public void readLocationToShow() {
getMapViewTrackingUtilities().setMapLinkedToLocation(false);
if (mapLabelToShow != null && !mapLabelToShow.contextMenuDisabled()) {
mapContextMenu.setMapCenter(latLonToShow);
mapContextMenu.setMapPosition(mapView.getMapPosition());
mapContextMenu.setCenterMarker(true);

RotatedTileBox tb = mapView.getCurrentRotatedTileBox().copy();
Expand Down
32 changes: 12 additions & 20 deletions OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.helpers.MapDisplayPositionManager;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.other.TrackDetailsMenu;
import net.osmand.plus.mapmarkers.MapMarker;
import net.osmand.plus.mapmarkers.MapMarkersHelper.MapMarkerChangedListener;
import net.osmand.plus.resources.DetectRegionTask;
Expand Down Expand Up @@ -57,11 +57,11 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc
private final OsmandApplication app;
private final OsmandSettings settings;
private final RoutingHelper routingHelper;
private final MapDisplayPositionManager mapDisplayPositionManager;

private OsmandMapTileView mapView;
private DashboardOnMap dashboard;
private MapContextMenu contextMenu;
private TrackDetailsMenu detailsMenu;
private StateChangedListener<Boolean> enable3DViewListener;

private boolean isMapLinkedToLocation = true;
Expand All @@ -78,10 +78,11 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc
private boolean drivingRegionUpdated;
private long compassRequest;

public MapViewTrackingUtilities(OsmandApplication app) {
public MapViewTrackingUtilities(@NonNull OsmandApplication app) {
this.app = app;
settings = app.getSettings();
routingHelper = app.getRoutingHelper();
mapDisplayPositionManager = new MapDisplayPositionManager(app);
myLocation = app.getLocationProvider().getLastKnownLocation();
app.getLocationProvider().addLocationListener(this);
app.getLocationProvider().addCompassListener(this);
Expand All @@ -91,6 +92,11 @@ public MapViewTrackingUtilities(OsmandApplication app) {
initMapLinkedToLocation();
}

@NonNull
public MapDisplayPositionManager getMapDisplayPositionManager() {
return mapDisplayPositionManager;
}

public void resetDrivingRegionUpdate() {
drivingRegionUpdated = false;
}
Expand Down Expand Up @@ -124,6 +130,7 @@ public void onMapMarkersChanged() {

public void setMapView(@Nullable OsmandMapTileView mapView) {
this.mapView = mapView;
mapDisplayPositionManager.setMapView(mapView);
if (mapView != null) {
WindowManager wm = (WindowManager) app.getSystemService(Context.WINDOW_SERVICE);
int orientation = 0;
Expand Down Expand Up @@ -180,10 +187,6 @@ public void setContextMenu(MapContextMenu contextMenu) {
this.contextMenu = contextMenu;
}

public void setDetailsMenu(TrackDetailsMenu detailsMenu) {
this.detailsMenu = detailsMenu;
}

public void detectDrivingRegion(@NonNull LatLon latLon) {
detectCurrentRegion(latLon, detectedRegion -> {
if (detectedRegion != null) {
Expand Down Expand Up @@ -325,19 +328,8 @@ public void switchRoutePlanningMode() {
}

public void updateSettings() {
if (mapView != null) {
if (isMapLinkedToLocation) {
boolean trackDetailsVisible = detailsMenu != null && detailsMenu.isVisible();
int displayPosition;
if (settings.POSITION_PLACEMENT_ON_MAP.get() == OsmandSettings.POSITION_PLACEMENT_CENTER
|| ((settings.POSITION_PLACEMENT_ON_MAP.get() == OsmandSettings.POSITION_PLACEMENT_AUTOMATIC) && (settings.ROTATE_MAP.get() != OsmandSettings.ROTATE_MAP_BEARING))
|| trackDetailsVisible) {
displayPosition = OsmandSettings.CENTER_CONSTANT;
} else {
displayPosition = OsmandSettings.BOTTOM_CONSTANT;
}
mapView.setMapPosition(displayPosition);
}
if (isMapLinkedToLocation) {
mapDisplayPositionManager.updateMapDisplayPosition();
}
registerUnregisterSensor(app.getLocationProvider().getLastKnownLocation(), false);
if (mapView != null) {
Expand Down
1 change: 0 additions & 1 deletion OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,6 @@ public static void updateRouteDirectionInfo(String prefix, Bundle bundle, RouteD
private void showOnMap(double lat, double lon, Object object, PointDescription pointDescription) {
MapContextMenu mapContextMenu = mapActivity.getContextMenu();
mapContextMenu.setMapCenter(new LatLon(lat, lon));
mapContextMenu.setMapPosition(mapActivity.getMapView().getMapPosition());
mapContextMenu.setCenterMarker(true);
mapContextMenu.setMapZoom(15);
mapContextMenu.show(new LatLon(lat, lon), pointDescription, object);
Expand Down
111 changes: 111 additions & 0 deletions OsmAnd/src/net/osmand/plus/helpers/MapDisplayPositionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package net.osmand.plus.helpers;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.Algorithms;

import java.util.ArrayList;
import java.util.List;

public class MapDisplayPositionManager {

private OsmandMapTileView mapView;
private final OsmandSettings settings;
private List<IMapDisplayPositionProvider> externalProviders = new ArrayList<>();

public MapDisplayPositionManager(@NonNull OsmandApplication app) {
this.settings = app.getSettings();
}

public void setMapView(@Nullable OsmandMapTileView mapView) {
this.mapView = mapView;
}

public void updateProviders(@NonNull IMapDisplayPositionProvider provider, boolean shouldRegister) {
if (shouldRegister) {
registerProvider(provider);
} else {
unregisterProvider(provider);
}
}

public void registerProvider(@NonNull IMapDisplayPositionProvider provider) {
if (!externalProviders.contains(provider)) {
externalProviders = Algorithms.addToList(externalProviders, provider);
}
}

public void unregisterProvider(@NonNull IMapDisplayPositionProvider provider) {
externalProviders = Algorithms.removeFromList(externalProviders, provider);
}

public void updateMapDisplayPosition() {
updateMapDisplayPosition(false);
}

public void updateMapDisplayPosition(boolean refreshMap) {
if (mapView != null) {
updateMapDisplayPositionImpl(refreshMap);
}
}

private void updateMapDisplayPositionImpl(boolean shouldRefreshMap) {
Integer position = getPositionFromProviders();
if (position == null) {
position = getPositionFromPreferences();
}
mapView.setMapPosition(position);
refreshMapIfNeeded(shouldRefreshMap);
}

private void refreshMapIfNeeded(boolean shouldRefreshMap) {
if (!shouldRefreshMap) {
return;
}
if (mapView != null) {
MapActivity mapActivity = mapView.getMapActivity();
if (mapActivity != null) {
mapActivity.refreshMap();
}
}
}

private Integer getPositionFromProviders() {
for (IMapDisplayPositionProvider provider : externalProviders) {
Integer position = provider.getMapDisplayPosition();
if (position != null) {
return position;
}
}
return null;
}

private int getPositionFromPreferences() {
if (useCenterByDefault() || (useAutomaticByDefault() && useCenterForAutomatic())) {
return OsmandSettings.CENTER_CONSTANT;
} else {
return OsmandSettings.BOTTOM_CONSTANT;
}
}

private boolean useCenterByDefault() {
return settings.POSITION_PLACEMENT_ON_MAP.get() == OsmandSettings.POSITION_PLACEMENT_CENTER;
}

private boolean useAutomaticByDefault() {
return settings.POSITION_PLACEMENT_ON_MAP.get() == OsmandSettings.POSITION_PLACEMENT_AUTOMATIC;
}

private boolean useCenterForAutomatic() {
return settings.ROTATE_MAP.get() != OsmandSettings.ROTATE_MAP_BEARING;
}

public static interface IMapDisplayPositionProvider {
@Nullable Integer getMapDisplayPosition();
}
}
41 changes: 26 additions & 15 deletions OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.MapDisplayPositionManager;
import net.osmand.plus.helpers.MapDisplayPositionManager.IMapDisplayPositionProvider;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.track.helpers.GpxUiHelper;
import net.osmand.plus.helpers.TargetPointsHelper.TargetPoint;
import net.osmand.plus.helpers.TargetPointsHelper.TargetPointChangedListener;
Expand Down Expand Up @@ -62,7 +65,7 @@
import java.util.List;

public class MapContextMenu extends MenuTitleController implements StateChangedListener<ApplicationMode>,
MapMarkerChangedListener, TargetPointChangedListener {
MapMarkerChangedListener, TargetPointChangedListener, IMapDisplayPositionProvider {

@Nullable
private MapActivity mapActivity;
Expand All @@ -86,7 +89,6 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
private MenuController menuController;

private LatLon mapCenter;
private int mapPosition;
private boolean centerMarker;
private boolean zoomOutOnly;
private int mapZoom;
Expand All @@ -95,6 +97,7 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
private boolean appModeChanged;
private boolean appModeListenerAdded;
private boolean autoHide;
private boolean shouldUpdateMapDisplayPosition;

private int favActionIconId;
private int waypointActionIconId;
Expand Down Expand Up @@ -296,10 +299,6 @@ public void updateMapCenter(LatLon mapCenter) {
}
}

public void setMapPosition(int mapPosition) {
this.mapPosition = mapPosition;
}

@Override
public PointDescription getPointDescription() {
return pointDescription;
Expand Down Expand Up @@ -370,6 +369,7 @@ public boolean init(@NonNull LatLon latLon,

active = true;
appModeChanged = false;
shouldUpdateMapDisplayPosition = true;

if (needAcquireMenuController) {
if (menuController != null) {
Expand All @@ -390,11 +390,7 @@ public boolean init(@NonNull LatLon latLon,
menuController.clearPlainMenuItems();
menuController.addPlainMenuItems(typeStr, getPointDescription(), getLatLon());
}

if (mapPosition != 0) {
mapActivity.getMapView().setMapPosition(0);
}

updateMapDisplayPosition();
mapActivity.refreshMap();

if (object instanceof MapMarker) {
Expand Down Expand Up @@ -528,10 +524,8 @@ public boolean hide(boolean animated) {
boolean result = false;
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (mapPosition != 0) {
mapActivity.getMapView().setMapPosition(mapPosition);
mapPosition = 0;
}
shouldUpdateMapDisplayPosition = false;
updateMapDisplayPosition();
MenuController menuController = getMenuController();
if (menuController != null) {
menuController.onHide();
Expand Down Expand Up @@ -1599,6 +1593,23 @@ public void updateLocation(boolean centerChanged, boolean locationChanged,
});
}

private void updateMapDisplayPosition() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
MapDisplayPositionManager manager = mapActivity.getMapViewTrackingUtilities().getMapDisplayPositionManager();
manager.updateProviders(this, shouldUpdateMapDisplayPosition);
manager.updateMapDisplayPosition();
}
}

@Nullable @Override
public Integer getMapDisplayPosition() {
if (shouldUpdateMapDisplayPosition) {
return OsmandSettings.CENTER_CONSTANT;
}
return null;
}

private abstract class MenuAction implements Runnable {
protected ProgressDialog dlg;
}
Expand Down
Loading

0 comments on commit dbe3c9a

Please sign in to comment.