Skip to content

Commit

Permalink
Visualize current route, hide other routes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Mar 13, 2024
1 parent 73f9812 commit 12105f5
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 110 deletions.
104 changes: 49 additions & 55 deletions app/src/main/java/mobi/maptrek/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
import org.oscim.event.MotionEvent;
import org.oscim.layers.AbstractMapEventLayer;
import org.oscim.layers.Layer;
import org.oscim.layers.PathLayer;
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.tile.MapTile;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
Expand Down Expand Up @@ -545,18 +544,9 @@ protected void onCreate(Bundle savedInstanceState) {
mViews.license.setClickable(true);
mViews.license.setMovementMethod(LinkMovementMethod.getInstance());

mViews.navigationArrow.setOnClickListener(v -> {
MapObject mapObject = mNavigationService.getCurrentPoint();
setMapLocation(mapObject.coordinates);
});
mViews.navigationArrow.setOnLongClickListener(v -> {
showNavigationMenu();
return true;
});
mViews.navigationSign.setOnClickListener(v -> {
MapObject mapObject = mNavigationService.getCurrentPoint();
setMapLocation(mapObject.coordinates);
});
mViews.navigationArrow.setOnClickListener(v -> setMapLocation(mNavigationService.getCurrentPoint()));
mViews.navigationArrow.setOnLongClickListener(v -> showNavigationMenu());
mViews.navigationSign.setOnClickListener(v -> setMapLocation(mNavigationService.getCurrentPoint()));

mViews.extendPanel.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
Expand Down Expand Up @@ -710,6 +700,11 @@ public void onChildViewRemoved(View parent, View child) {
placeViewModel = new ViewModelProvider(this).get(PlaceViewModel.class);
trackViewModel = new ViewModelProvider(this).get(TrackViewModel.class);
routeViewModel = new ViewModelProvider(this).get(RouteViewModel.class);
routeViewModel.isRouting.observe(this, routing -> {
for (Layer layer : mMap.layers())
if (layer instanceof RouteLayer && layer != mNavigationLayer)
layer.setEnabled(!routing);
});
amenityViewModel = new ViewModelProvider(this).get(AmenityViewModel.class);
// Observe amenity state
amenityViewModel.getAmenity().observe(this, amenity -> {
Expand Down Expand Up @@ -770,10 +765,7 @@ public void onChildViewRemoved(View parent, View child) {
});
mViews.mapDownloadButton.setOnClickListener(v -> onMapDownloadClicked());
mViews.compass.setOnLongClickListener(this::onCompassLongClicked);
mViews.navigationSign.setOnClickListener(v -> {
MapObject mapObject = mNavigationService.getCurrentPoint();
setMapLocation(mapObject.coordinates);
});
mViews.navigationSign.setOnClickListener(v -> setMapLocation(mNavigationService.getCurrentPoint()));

// Resume state
int state = Configuration.getLocationState();
Expand Down Expand Up @@ -1580,7 +1572,6 @@ public void onGpsStatusChanged() {
updateLocationDrawable();
}
}
updateNavigationUI();
}

private void onLocationClicked() {
Expand Down Expand Up @@ -1921,13 +1912,11 @@ public void onServiceConnected(ComponentName className, IBinder binder) {
mLocationService = (ILocationService) binder;
mLocationService.registerLocationCallback(MainActivity.this);
mLocationService.setProgressListener(mProgressHandler);
updateNavigationUI();
}

public void onServiceDisconnected(ComponentName className) {
logger.debug("onServiceDisconnected: LocationService");
mLocationService = null;
updateNavigationUI();
}
};

Expand All @@ -1942,23 +1931,23 @@ private void disableNavigation() {
unbindService(mNavigationConnection);
mIsNavigationBound = false;
}
updateNavigationUI();
updateNavigationUI(true);
updatePanels();
}

private final ServiceConnection mNavigationConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
logger.warn("onServiceConnected: NavigationService");
mNavigationService = (INavigationService) binder;
updateNavigationUI();
updateNavigationUI(false);
updatePanels();
updateNavigationGauges(true);
}

public void onServiceDisconnected(ComponentName className) {
logger.warn("onServiceDisconnected: NavigationService");
mNavigationService = null;
updateNavigationUI();
updateNavigationUI(false);
updatePanels();
}
};
Expand Down Expand Up @@ -2049,8 +2038,7 @@ public boolean isNavigatingTo(@NonNull GeoPoint coordinates) {
return false;
if (!mNavigationService.isNavigating())
return false;
MapObject mapObject = mNavigationService.getCurrentPoint();
return mapObject.coordinates.equals(coordinates);
return coordinates.equals(mNavigationService.getCurrentPoint());
}

public void navigateVia(@NonNull Route route) {
Expand Down Expand Up @@ -2224,7 +2212,7 @@ private void adjustNavigationArrow(float turn) {
mViews.navigationArrow.setRotation(turn);
}

private void showNavigationMenu() {
private boolean showNavigationMenu() {
PopupMenu popup = new PopupMenu(this, mViews.mapButtonHolder);
popup.inflate(R.menu.context_menu_navigation);
Menu popupMenu = popup.getMenu();
Expand All @@ -2234,6 +2222,7 @@ private void showNavigationMenu() {
popupMenu.removeItem(R.id.actionPrevRoutePoint);
popup.setOnMenuItemClickListener(this);
popup.show();
return true;
}

private void updateLocationDrawable() {
Expand Down Expand Up @@ -2295,13 +2284,11 @@ private void updateLocationDrawable() {

private void updatePanels() {
logger.info("updatePanels()");
boolean isRouting = mNavigationService != null && mNavigationService.isNavigating() && mNavigationService.isNavigatingViaRoute();

TransitionSet transitionSet = new TransitionSet();
Transition transition = new Slide(Gravity.START);
transition.addTarget(mViews.gaugePanel);
transitionSet.addTransition(transition);
if (isRouting) {
if (Boolean.TRUE.equals(routeViewModel.isRouting.getValue())) {
transition = new Slide(Gravity.TOP);
transition.addTarget(mViews.navigationPanel);
transitionSet.addTransition(transition);
Expand Down Expand Up @@ -2353,7 +2340,7 @@ public void onTransitionResume(Transition transition) {
break;
case NORTH:
case TRACK:
if (isRouting) {
if (Boolean.TRUE.equals(routeViewModel.isRouting.getValue())) {
mViews.navigationPanel.setVisibility(View.VISIBLE);
getWindow().setStatusBarColor(getColor(R.color.colorNavigationBackground));
} else if (mViews.navigationPanel.getVisibility() == View.VISIBLE) {
Expand Down Expand Up @@ -2412,35 +2399,34 @@ private void updateNavigationGauges(boolean updateRoutePanels) {
mViews.gaugePanel.setValue(Gauge.TYPE_ETE, mNavigationService.getEte());
}

private void updateNavigationUI() {
logger.debug("updateNavigationUI()");
boolean enabled = mLocationService != null && mLocationService.getStatus() == BaseLocationService.GPS_OK &&
mNavigationService != null && mNavigationService.isNavigating();
if (mViews.gaugePanel.getNavigationMode() == enabled)
private void updateNavigationUI(boolean force) {
logger.debug("updateNavigationUI({})", force);
boolean isNavigating = mNavigationService != null && mNavigationService.isNavigating();

if (!force && mViews.gaugePanel.getNavigationMode() == isNavigating)
return; // nothing changed

mViews.gaugePanel.setNavigationMode(enabled);
if (enabled) {
mViews.gaugePanel.setNavigationMode(isNavigating);
if (isNavigating) {
if (mViews.navigationArrow.getVisibility() == View.GONE) {
mViews.navigationArrow.setAlpha(0f);
mViews.navigationArrow.setVisibility(View.VISIBLE);
mViews.navigationArrow.animate().alpha(1f).setDuration(MAP_POSITION_ANIMATION_DURATION).setListener(null);
}
GeoPoint destination = mNavigationService.getCurrentPoint().coordinates;
if (mNavigationLayer == null) {
int color = Configuration.loadInt(Configuration.PREF_ACTIVE_ROUTE_COLOR, getColor(R.color.activeRouteColor));
int width = Configuration.loadInt(Configuration.PREF_ACTIVE_ROUTE_WIDTH, getResources().getInteger(R.integer.default_route_width));
mNavigationLayer = new NavigationLayer(mMap, color, width);
mNavigationLayer.setDestination(destination);
Point point = mLocationOverlay.getPosition();
mNavigationLayer.setPosition(MercatorProjection.toLatitude(point.y), MercatorProjection.toLongitude(point.x));
Bitmap bitmap = new AndroidBitmap(MarkerFactory.getMarkerSymbol(this, R.drawable.route_point, color));
MarkerSymbol pointSymbol = new MarkerSymbol(bitmap, MarkerItem.HotspotPlace.CENTER);
bitmap = new AndroidBitmap(MarkerFactory.getMarkerSymbol(this, R.drawable.route_end, color));
MarkerSymbol endSymbol = new MarkerSymbol(bitmap, MarkerItem.HotspotPlace.CENTER);
mNavigationLayer = new NavigationLayer(mMap, color, width, pointSymbol, null, endSymbol);
mMap.layers().add(mNavigationLayer, MAP_DYNAMIC_DATA);
} else {
GeoPoint current = mNavigationLayer.getDestination();
if (!destination.equals(current)) {
mNavigationLayer.setDestination(destination);
}
}
Location location = mapViewModel.currentLocation.getValue();
if (location != null && !"unknown".equals(location.getProvider()))
mNavigationLayer.setPosition(location.getLatitude(), location.getLongitude());
mNavigationLayer.setRemainingPoints(mNavigationService.getRemainingPoints(), mNavigationService.getPrevRoutePoint());
} else {
if (mViews.navigationArrow.getAlpha() == 1f) {
mViews.navigationArrow.animate().alpha(0f).setDuration(MAP_POSITION_ANIMATION_DURATION).setListener(new AnimatorListenerAdapter() {
Expand All @@ -2455,6 +2441,7 @@ public void onAnimationEnd(Animator animation) {
mNavigationLayer = null;
}
}
routeViewModel.isRouting.setValue(isNavigating && mNavigationService.isNavigatingViaRoute());
}

@Override
Expand Down Expand Up @@ -4114,26 +4101,30 @@ public void onReceive(Context context, Intent intent) {
}
}
if (BaseNavigationService.BROADCAST_NAVIGATION_STATE.equals(action)) {
int state = intent.getIntExtra("state", -1);
int state = intent.getIntExtra(BaseNavigationService.EXTRA_STATE, -1);
if (state == BaseNavigationService.STATE_STARTED) {
enableNavigation();
updateNavigationUI();
updateNavigationUI(true);
updateNavigationGauges(true);
if (mLocationState != LocationState.DISABLED)
askForPermission(PERMISSIONS_REQUEST_NOTIFICATION);
}
if (state == BaseNavigationService.STATE_NEXT_ROUTE_POINT) {
updateNavigationGauges(true);
if (mNavigationLayer != null && mNavigationService != null)
mNavigationLayer.setDestination(mNavigationService.getCurrentPoint().coordinates);
mNavigationLayer.setRemainingPoints(mNavigationService.getRemainingPoints(), mNavigationService.getPrevRoutePoint());
}
if (state == BaseNavigationService.STATE_STOPPED) {
disableNavigation();
}
updatePanels();
}
if (BaseNavigationService.BROADCAST_NAVIGATION_STATUS.equals(action)) {
updateNavigationGauges(false);
if (mNavigationService != null) {
adjustNavigationArrow(mNavigationService.getTurn());
if (intent.getBooleanExtra("moving", false) && mNavigationLayer != null)
mNavigationLayer.setDestination(mNavigationService.getCurrentPoint().coordinates);
if (intent.getBooleanExtra(BaseNavigationService.EXTRA_MOVING_TARGET, false) && mNavigationLayer != null)
mNavigationLayer.setRemainingPoints(mNavigationService.getRemainingPoints(), null);
}
}
}
Expand Down Expand Up @@ -4180,6 +4171,7 @@ private void addSourceToMap(FileDataSource source) {
bitmap = new AndroidBitmap(MarkerFactory.getMarkerSymbol(this, R.drawable.route_end, route.style.color));
MarkerSymbol endSymbol = new MarkerSymbol(bitmap, MarkerItem.HotspotPlace.CENTER);
RouteLayer routeLayer = new RouteLayer(mMap, route, pointSymbol, startSymbol, endSymbol);
routeLayer.setEnabled(Boolean.FALSE.equals(routeViewModel.isRouting.getValue()));
mMap.layers().add(routeLayer, MAP_DATA);
mTotalDataItems++;
}
Expand Down Expand Up @@ -4239,7 +4231,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
for (Layer layer : mMap.layers()) {
if (layer instanceof TrackLayer || layer instanceof MapObjectLayer
|| layer instanceof MarkerLayer || layer instanceof PathLayer)
|| layer instanceof MarkerLayer || layer instanceof RouteLayer)
layer.setEnabled(false);
}
mMap.updateMap(true);
Expand All @@ -4265,8 +4257,10 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
for (Layer layer : mMap.layers()) {
if (layer instanceof TrackLayer || layer instanceof MapObjectLayer
|| layer instanceof MarkerLayer || layer instanceof PathLayer)
|| layer instanceof MarkerLayer)
layer.setEnabled(true);
if (layer instanceof RouteLayer)
layer.setEnabled(layer == mNavigationLayer || Boolean.FALSE.equals(routeViewModel.isRouting.getValue()));
}
mMap.updateMap(true);
return true;
Expand Down Expand Up @@ -4563,7 +4557,7 @@ public void onConfigurationChanged(Configuration.ChangedEvent event) {
case Configuration.PREF_ROUTE_WIDTH: {
int width = Configuration.loadInt(Configuration.PREF_ROUTE_WIDTH, getResources().getInteger(R.integer.default_route_width));
for (Layer layer : mMap.layers()) {
if (layer instanceof RouteLayer) {
if (layer instanceof RouteLayer && layer != mNavigationLayer) {
((RouteLayer) layer).setWidth(width);
}
}
Expand Down
60 changes: 26 additions & 34 deletions app/src/main/java/mobi/maptrek/layers/NavigationLayer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Andrey Novikov
* Copyright 2024 Andrey Novikov
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
Expand All @@ -16,51 +16,43 @@

package mobi.maptrek.layers;

import org.oscim.backend.canvas.Color;
import org.oscim.backend.canvas.Paint.Cap;
import org.oscim.core.GeoPoint;
import org.oscim.layers.PathLayer;
import org.oscim.map.Map;
import org.oscim.theme.styles.LineStyle;

/**
* This class draws a great circle navigation line.
*/
public class NavigationLayer extends PathLayer {
private GeoPoint mDestination;
private GeoPoint mPosition;
import java.util.List;

public NavigationLayer(Map map, int lineColor, float lineWidth) {
super(map, Color.setA(lineColor, 0x66), lineWidth);
}
import mobi.maptrek.data.Route;
import mobi.maptrek.layers.marker.MarkerSymbol;

public void setLineStyle(int lineColor, float lineWidth) {
super.setStyle(new LineStyle(Color.setA(lineColor, 0x66), lineWidth, Cap.BUTT));
}
public class NavigationLayer extends RouteLayer {
private GeoPoint mPosition;

public void setDestination(GeoPoint destination) {
synchronized (mPoints) {
mDestination = destination;
clearPath();
if (mPosition != null) {
addPoint(mPosition);
addGreatCircle(mPosition, mDestination);
addPoint(mDestination);
}
}
public NavigationLayer(Map map, int lineColor, float lineWidth, MarkerSymbol pointSymbol, MarkerSymbol startSymbol, MarkerSymbol endSymbol) {
super(map, new Route(), pointSymbol, startSymbol, endSymbol);
LINE_ALPHA = 0x66;
setLineStyle(lineColor, lineWidth);
}

public GeoPoint getDestination() {
return mDestination;
public void setRemainingPoints(List<GeoPoint> points, GeoPoint previous) {
//addGreatCircle(mPosition, mDestination);
if (mPosition != null)
points.add(0, mPosition);
else if (previous != null)
points.add(0, previous);
setPoints(points);
}

public void setPosition(double lat, double lon) {
boolean hadPosition = mPosition != null;
mPosition = new GeoPoint(lat, lon);
if (mPoints.isEmpty())
return;
synchronized (mPoints) {
mPosition = new GeoPoint(lat, lon);
clearPath();
addPoint(mPosition);
addGreatCircle(mPosition, mDestination);
addPoint(mDestination);
if (hadPosition || mPoints.size() > 1)
mPoints.set(0, mPosition);
else
mPoints.add(0, mPosition);
updatePoints();
}
}
}
Loading

0 comments on commit 12105f5

Please sign in to comment.