Fix location/direction update for context menu

This commit is contained in:
Alexey Kulish 2015-11-08 22:16:09 +03:00
parent 1da1b25cbf
commit 897ed3107c
4 changed files with 88 additions and 40 deletions

View file

@ -16,7 +16,9 @@ import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.AutoZoomMap; import net.osmand.plus.OsmandSettings.AutoZoomMap;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper.TargetPoint; import net.osmand.plus.TargetPointsHelper.TargetPoint;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.routing.RoutingHelper.IRouteInformationListener; import net.osmand.plus.routing.RoutingHelper.IRouteInformationListener;
import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.AnimateDraggingMapThread;
@ -32,6 +34,7 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc
private boolean sensorRegistered = false; private boolean sensorRegistered = false;
private OsmandMapTileView mapView; private OsmandMapTileView mapView;
private DashboardOnMap dashboard; private DashboardOnMap dashboard;
private MapContextMenu contextMenu;
private OsmandSettings settings; private OsmandSettings settings;
private OsmandApplication app; private OsmandApplication app;
private boolean isMapLinkedToLocation = true; private boolean isMapLinkedToLocation = true;
@ -85,12 +88,19 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc
if(dashboard != null) { if(dashboard != null) {
dashboard.updateCompassValue(val); dashboard.updateCompassValue(val);
} }
if(contextMenu != null) {
contextMenu.updateCompassValue(val);
}
} }
public void setDashboard(DashboardOnMap dashboard) { public void setDashboard(DashboardOnMap dashboard) {
this.dashboard = dashboard; this.dashboard = dashboard;
} }
public void setContextMenu(MapContextMenu contextMenu) {
this.contextMenu = contextMenu;
}
@Override @Override
public void updateLocation(Location location) { public void updateLocation(Location location) {
showViewAngle = false; showViewAngle = false;
@ -134,6 +144,9 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc
if(dashboard != null) { if(dashboard != null) {
dashboard.updateMyLocation(location); dashboard.updateMyLocation(location);
} }
if(contextMenu != null) {
contextMenu.updateMyLocation(location);
}
} }
private boolean isSmallSpeedForCompass(Location location) { private boolean isSmallSpeedForCompass(Location location) {
@ -170,8 +183,9 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc
} }
private void registerUnregisterSensor(net.osmand.Location location) { private void registerUnregisterSensor(net.osmand.Location location) {
int currentMapRotation = settings.ROTATE_MAP.get(); int currentMapRotation = settings.ROTATE_MAP.get();
boolean registerCompassListener = (showViewAngle && location != null) boolean registerCompassListener = ((showViewAngle || contextMenu != null) && location != null)
|| (currentMapRotation == OsmandSettings.ROTATE_MAP_COMPASS && !routePlanningMode); || (currentMapRotation == OsmandSettings.ROTATE_MAP_COMPASS && !routePlanningMode);
// show point view only if gps enabled // show point view only if gps enabled
if(sensorRegistered != registerCompassListener) { if(sensorRegistered != registerCompassListener) {

View file

@ -15,6 +15,7 @@ import net.osmand.plus.mapcontextmenu.MenuController.TitleProgressController;
import net.osmand.plus.mapcontextmenu.other.ShareMenu; import net.osmand.plus.mapcontextmenu.other.ShareMenu;
import net.osmand.plus.views.ContextMenuLayer; import net.osmand.plus.views.ContextMenuLayer;
import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapLayer;
import net.osmand.util.MapUtils;
public class MapContextMenu extends MenuTitleController { public class MapContextMenu extends MenuTitleController {
@ -29,6 +30,10 @@ public class MapContextMenu extends MenuTitleController {
private LatLon mapCenter; private LatLon mapCenter;
private int mapPosition = 0; private int mapPosition = 0;
private LatLon myLocation;
private Float heading;
private boolean inLocationUpdate = false;
private int favActionIconId; private int favActionIconId;
@Override @Override
@ -97,6 +102,11 @@ public class MapContextMenu extends MenuTitleController {
} }
public boolean init(LatLon latLon, PointDescription pointDescription, Object object, boolean update) { public boolean init(LatLon latLon, PointDescription pointDescription, Object object, boolean update) {
if (myLocation == null) {
myLocation = getMapActivity().getMyApplication().getSettings().getLastKnownMapLocation();
}
if (!update && isVisible()) { if (!update && isVisible()) {
if (this.object == null || !this.object.equals(object)) { if (this.object == null || !this.object.equals(object)) {
hide(); hide();
@ -408,4 +418,50 @@ public class MapContextMenu extends MenuTitleController {
menuController.updateData(); 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);
}
}
});
}
} }

View file

@ -29,13 +29,10 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.Location;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.QuadPoint; import net.osmand.data.QuadPoint;
import net.osmand.data.RotatedTileBox; import net.osmand.data.RotatedTileBox;
import net.osmand.plus.IconsCache; 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.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; 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.mapcontextmenu.MenuController.TitleProgressController;
import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.AnimateDraggingMapThread;
import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.MapUtils;
import static android.util.TypedValue.COMPLEX_UNIT_DIP; 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_BOTTOM_DP;
import static net.osmand.plus.mapcontextmenu.MenuBuilder.SHADOW_HEIGHT_TOP_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 String TAG = "MapContextMenuFragment";
public static final float FAB_PADDING_TOP_DP = 4f; public static final float FAB_PADDING_TOP_DP = 4f;
@ -89,8 +84,6 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents,
private int origMarkerY; private int origMarkerY;
private boolean customMapCenter; private boolean customMapCenter;
private LatLon location;
private Float heading;
private int screenOrientation; private int screenOrientation;
private class SingleTapConfirm implements OnGestureListener { private class SingleTapConfirm implements OnGestureListener {
@ -564,18 +557,13 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents,
super.onResume(); super.onResume();
screenOrientation = DashLocationFragment.getScreenOrientation(getActivity()); screenOrientation = DashLocationFragment.getScreenOrientation(getActivity());
if (menu.displayDistanceDirection()) { if (menu.displayDistanceDirection()) {
if (location == null) { getMapActivity().getMapViewTrackingUtilities().setContextMenu(menu);
location = getMyApplication().getSettings().getLastKnownMapLocation();
}
getMyApplication().getLocationProvider().addLocationListener(this);
getMyApplication().getLocationProvider().addCompassListener(this);
} }
} }
@Override @Override
public void onPause() { public void onPause() {
getMyApplication().getLocationProvider().removeLocationListener(this); getMapActivity().getMapViewTrackingUtilities().setContextMenu(null);
getMyApplication().getLocationProvider().removeCompassListener(this);
super.onPause(); super.onPause();
} }
@ -691,8 +679,12 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents,
private void updateDistanceDirection() { private void updateDistanceDirection() {
TextView distanceText = (TextView) view.findViewById(R.id.distance); TextView distanceText = (TextView) view.findViewById(R.id.distance);
ImageView direction = (ImageView) view.findViewById(R.id.direction); 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()); menu.getLatLon().getLatitude(), menu.getLatLon().getLongitude(), screenOrientation, getMyApplication(), getActivity());
} }
@ -917,25 +909,12 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents,
return dm.heightPixels; return dm.heightPixels;
} }
@Override public void updateLocation(boolean centerChanged, boolean locationChanged, boolean compassChanged) {
public void updateLocation(Location location) { boolean mapLinkedToLocation = getMapActivity().getMapViewTrackingUtilities().isMapLinkedToLocation();
if (location != null) { if (compassChanged && !mapLinkedToLocation) {
this.location = new LatLon(location.getLatitude(), location.getLongitude()); return;
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(); updateDistanceDirection();
} else {
heading = lastHeading;
}
} }
} }

View file

@ -59,8 +59,7 @@ public abstract class MenuTitleController {
return typeStr; return typeStr;
} else { } else {
if (Algorithms.isEmpty(streetStr)) { if (Algorithms.isEmpty(streetStr)) {
return "";//PointDescription.getLocationName(getMapActivity(), return typeStr;
//getLatLon().getLatitude(), getLatLon().getLongitude(), true).replaceAll("\n", "");
} else { } else {
return streetStr; return streetStr;
} }