From 897ed3107c8e27b366d2adb24d23e1437bcc4f86 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sun, 8 Nov 2015 22:16:09 +0300 Subject: [PATCH] Fix location/direction update for context menu --- .../plus/base/MapViewTrackingUtilities.java | 20 ++++++- .../plus/mapcontextmenu/MapContextMenu.java | 56 +++++++++++++++++++ .../MapContextMenuFragment.java | 49 +++++----------- .../mapcontextmenu/MenuTitleController.java | 3 +- 4 files changed, 88 insertions(+), 40 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java b/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java index c3506fbd36..493dbe8abf 100644 --- a/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java +++ b/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java @@ -16,7 +16,9 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings.AutoZoomMap; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper.TargetPoint; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.dashboard.DashboardOnMap; +import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper.IRouteInformationListener; import net.osmand.plus.views.AnimateDraggingMapThread; @@ -32,6 +34,7 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc private boolean sensorRegistered = false; private OsmandMapTileView mapView; private DashboardOnMap dashboard; + private MapContextMenu contextMenu; private OsmandSettings settings; private OsmandApplication app; private boolean isMapLinkedToLocation = true; @@ -85,12 +88,19 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc if(dashboard != null) { dashboard.updateCompassValue(val); } + if(contextMenu != null) { + contextMenu.updateCompassValue(val); + } } public void setDashboard(DashboardOnMap dashboard) { this.dashboard = dashboard; } - + + public void setContextMenu(MapContextMenu contextMenu) { + this.contextMenu = contextMenu; + } + @Override public void updateLocation(Location location) { showViewAngle = false; @@ -130,10 +140,13 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc // When location is changed we need to refresh map in order to show movement! mapView.refreshMap(); } - + if(dashboard != null) { dashboard.updateMyLocation(location); } + if(contextMenu != null) { + contextMenu.updateMyLocation(location); + } } private boolean isSmallSpeedForCompass(Location location) { @@ -170,8 +183,9 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc } private void registerUnregisterSensor(net.osmand.Location location) { + int currentMapRotation = settings.ROTATE_MAP.get(); - boolean registerCompassListener = (showViewAngle && location != null) + boolean registerCompassListener = ((showViewAngle || contextMenu != null) && location != null) || (currentMapRotation == OsmandSettings.ROTATE_MAP_COMPASS && !routePlanningMode); // show point view only if gps enabled if(sensorRegistered != registerCompassListener) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 4a5b1ae24d..ad529e716b 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -15,6 +15,7 @@ import net.osmand.plus.mapcontextmenu.MenuController.TitleProgressController; import net.osmand.plus.mapcontextmenu.other.ShareMenu; import net.osmand.plus.views.ContextMenuLayer; import net.osmand.plus.views.OsmandMapLayer; +import net.osmand.util.MapUtils; public class MapContextMenu extends MenuTitleController { @@ -29,6 +30,10 @@ public class MapContextMenu extends MenuTitleController { private LatLon mapCenter; private int mapPosition = 0; + private LatLon myLocation; + private Float heading; + private boolean inLocationUpdate = false; + private int favActionIconId; @Override @@ -97,6 +102,11 @@ public class MapContextMenu extends MenuTitleController { } public boolean init(LatLon latLon, PointDescription pointDescription, Object object, boolean update) { + + if (myLocation == null) { + myLocation = getMapActivity().getMyApplication().getSettings().getLastKnownMapLocation(); + } + if (!update && isVisible()) { if (this.object == null || !this.object.equals(object)) { hide(); @@ -408,4 +418,50 @@ public class MapContextMenu extends MenuTitleController { menuController.updateData(); } } + + public LatLon getMyLocation() { + return myLocation; + } + + public Float getHeading() { + return heading; + } + + public void updateMyLocation(net.osmand.Location location) { + if (location != null) { + myLocation = new LatLon(location.getLatitude(), location.getLongitude()); + updateLocation(false, true, false); + } + } + + public void updateCompassValue(float value) { + // 99 in next line used to one-time initialize arrows (with reference vs. fixed-north direction) + // on non-compass devices + float lastHeading = heading != null ? heading : 99; + heading = value; + if (Math.abs(MapUtils.degreesDiff(lastHeading, heading)) > 5) { + updateLocation(false, false, true); + } else { + heading = lastHeading; + } + } + + public void updateLocation(final boolean centerChanged, final boolean locationChanged, + final boolean compassChanged) { + if (inLocationUpdate) { + return; + } + inLocationUpdate = true; + mapActivity.runOnUiThread(new Runnable() { + @Override + public void run() { + inLocationUpdate = false; + MapContextMenuFragment menuFragment = findMenuFragment(); + if (menuFragment != null) { + menuFragment.updateLocation(centerChanged, locationChanged, compassChanged); + } + } + }); + } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 53af782668..7a61106de7 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -29,13 +29,10 @@ import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; -import net.osmand.Location; import net.osmand.data.LatLon; import net.osmand.data.QuadPoint; import net.osmand.data.RotatedTileBox; import net.osmand.plus.IconsCache; -import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener; -import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; @@ -46,15 +43,13 @@ import net.osmand.plus.mapcontextmenu.MenuController.TitleButtonController; import net.osmand.plus.mapcontextmenu.MenuController.TitleProgressController; import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.OsmandMapTileView; -import net.osmand.util.MapUtils; import static android.util.TypedValue.COMPLEX_UNIT_DIP; import static net.osmand.plus.mapcontextmenu.MenuBuilder.SHADOW_HEIGHT_BOTTOM_DP; import static net.osmand.plus.mapcontextmenu.MenuBuilder.SHADOW_HEIGHT_TOP_DP; -public class MapContextMenuFragment extends Fragment implements DownloadEvents, OsmAndLocationListener, OsmAndCompassListener { - +public class MapContextMenuFragment extends Fragment implements DownloadEvents { public static final String TAG = "MapContextMenuFragment"; public static final float FAB_PADDING_TOP_DP = 4f; @@ -89,8 +84,6 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents, private int origMarkerY; private boolean customMapCenter; - private LatLon location; - private Float heading; private int screenOrientation; private class SingleTapConfirm implements OnGestureListener { @@ -564,18 +557,13 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents, super.onResume(); screenOrientation = DashLocationFragment.getScreenOrientation(getActivity()); if (menu.displayDistanceDirection()) { - if (location == null) { - location = getMyApplication().getSettings().getLastKnownMapLocation(); - } - getMyApplication().getLocationProvider().addLocationListener(this); - getMyApplication().getLocationProvider().addCompassListener(this); + getMapActivity().getMapViewTrackingUtilities().setContextMenu(menu); } } @Override public void onPause() { - getMyApplication().getLocationProvider().removeLocationListener(this); - getMyApplication().getLocationProvider().removeCompassListener(this); + getMapActivity().getMapViewTrackingUtilities().setContextMenu(null); super.onPause(); } @@ -691,8 +679,12 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents, private void updateDistanceDirection() { TextView distanceText = (TextView) view.findViewById(R.id.distance); ImageView direction = (ImageView) view.findViewById(R.id.direction); - boolean mapLinked = getMapActivity().getMapViewTrackingUtilities().isMapLinkedToLocation() && location != null; - DashLocationFragment.updateLocationView(!mapLinked, location, heading, direction, distanceText, + + boolean mapLinked = getMapActivity().getMapViewTrackingUtilities().isMapLinkedToLocation() && menu.getMyLocation() != null; + float myHeading = menu.getHeading() == null ? 0f : menu.getHeading(); + float h = !mapLinked ? -getMapActivity().getMapRotate() : myHeading; + + DashLocationFragment.updateLocationView(!mapLinked, menu.getMyLocation(), h, direction, distanceText, menu.getLatLon().getLatitude(), menu.getLatLon().getLongitude(), screenOrientation, getMyApplication(), getActivity()); } @@ -917,25 +909,12 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents, return dm.heightPixels; } - @Override - public void updateLocation(Location location) { - if (location != null) { - this.location = new LatLon(location.getLatitude(), location.getLongitude()); - updateDistanceDirection(); - } -; } - - @Override - public void updateCompassValue(float value) { - // 99 in next line used to one-time initalize arrows (with reference vs. fixed-north direction) on non-compass - // devices - float lastHeading = heading != null ? heading : 99; - heading = value; - if (Math.abs(MapUtils.degreesDiff(lastHeading, heading)) > 5) { - updateDistanceDirection(); - } else { - heading = lastHeading; + public void updateLocation(boolean centerChanged, boolean locationChanged, boolean compassChanged) { + boolean mapLinkedToLocation = getMapActivity().getMapViewTrackingUtilities().isMapLinkedToLocation(); + if (compassChanged && !mapLinkedToLocation) { + return; } + updateDistanceDirection(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java index 6c3d30eef4..fabbdf3a71 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java @@ -59,8 +59,7 @@ public abstract class MenuTitleController { return typeStr; } else { if (Algorithms.isEmpty(streetStr)) { - return "";//PointDescription.getLocationName(getMapActivity(), - //getLatLon().getLatitude(), getLatLon().getLongitude(), true).replaceAll("\n", ""); + return typeStr; } else { return streetStr; }