Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/r4.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
vshcherb committed Nov 20, 2024
2 parents 505cad6 + 1b6811c commit bb765dd
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 143 deletions.
3 changes: 3 additions & 0 deletions OsmAnd/src/net/osmand/plus/auto/NavigationSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public void setMapView(OsmandMapTileView mapView) {
SurfaceRenderer navigationCarSurface = this.navigationCarSurface;
if (navigationCarSurface != null) {
navigationCarSurface.setMapView(hasStarted() ? mapView : null);
if (mapView != null) {
navigationCarSurface.handleRecenter();
}
}
}

Expand Down
43 changes: 40 additions & 3 deletions OsmAnd/src/net/osmand/plus/auto/screens/BaseAndroidAutoScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ import androidx.car.app.constraints.ConstraintManager
import androidx.car.app.model.Action
import androidx.car.app.model.CarIcon
import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import net.osmand.data.LatLon
import net.osmand.data.QuadRect
import net.osmand.plus.OsmandApplication
import net.osmand.plus.R
import net.osmand.plus.settings.enums.CompassMode
import net.osmand.plus.views.Zoom
import net.osmand.search.core.SearchResult
import net.osmand.util.Algorithms

abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext) {
abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext),
DefaultLifecycleObserver {

protected var prevElevationAngle = 90f
protected var prevRotationAngle = 0f
protected var prevZoom: Zoom? = null
protected var prevMapLinkedToLocation = false
protected val ANIMATION_RETURN_FROM_PREVIEW_TIME = 1500

protected val app: OsmandApplication
get() {
Expand Down Expand Up @@ -88,7 +97,6 @@ abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext

protected open fun adjustMapToRect(location: LatLon, mapRect: QuadRect) {
app.mapViewTrackingUtilities.isMapLinkedToLocation = false
// app.getSettings().setCompassMode(CompassMode.NORTH_IS_UP);
Algorithms.extendRectToContainPoint(mapRect, location.longitude, location.latitude)
app.carNavigationSession?.navigationCarSurface?.let { surfaceRenderer ->
if (!mapRect.hasInitialState()) {
Expand All @@ -110,6 +118,35 @@ abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext
session?.navigationCarSurface?.handleRecenter()
}

override fun onStop(owner: LifecycleOwner) {
if (prevMapLinkedToLocation != app.mapViewTrackingUtilities.isMapLinkedToLocation) {
app.mapViewTrackingUtilities.isMapLinkedToLocation = prevMapLinkedToLocation
}
restoreMapState()
}

protected open fun restoreMapState() {
val mapView = app.osmandMap.mapView
val locationProvider = app.locationProvider
val lastKnownLocation = locationProvider.lastKnownLocation
mapView.animateToState(
lastKnownLocation?.latitude ?: mapView.latitude,
lastKnownLocation?.longitude ?: mapView.longitude,
prevZoom ?: mapView.currentZoom,
prevRotationAngle,
prevElevationAngle,
ANIMATION_RETURN_FROM_PREVIEW_TIME.toLong(),
false)
}

override fun onStart(owner: LifecycleOwner) {
val mapView = app.osmandMap.mapView
prevMapLinkedToLocation = app.mapViewTrackingUtilities.isMapLinkedToLocation
prevZoom = mapView.currentZoom
prevRotationAngle = mapView.rotate
prevElevationAngle = mapView.normalizeElevationAngle(mapView.elevationAngle)
}

companion object {
private const val DEFAULT_CONTENT_LIMIT = 12
}
Expand Down
40 changes: 15 additions & 25 deletions OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public final class FavoritesScreen extends BaseAndroidAutoScreen {

@Nullable
private FavoriteGroup selectedGroup;
private CompassMode initialCompassMode;

public FavoritesScreen(
@NonNull CarContext carContext,
Expand All @@ -66,24 +65,20 @@ public FavoritesScreen(
super(carContext);
this.settingsAction = settingsAction;
selectedGroup = group;
getLifecycle().addObserver(new DefaultLifecycleObserver() {
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onDestroy(owner);
getFavouritesLayer().setCustomMapObjects(null);
getFavouritesLayer().customObjectsDelegate = null;
getApp().getOsmandMap().getMapView().backToLocation();
if (initialCompassMode != null) {
getApp().getMapViewTrackingUtilities().switchCompassModeTo(initialCompassMode);
}
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onStart(owner);
getFavouritesLayer().customObjectsDelegate = new OsmandMapLayer.CustomMapObjects<>();
}
});
getLifecycle().addObserver(this);
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
super.onDestroy(owner);
getFavouritesLayer().setCustomMapObjects(null);
getFavouritesLayer().customObjectsDelegate = null;
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
super.onStart(owner);
getFavouritesLayer().customObjectsDelegate = new OsmandMapLayer.CustomMapObjects<>();
}

private FavouritesLayer getFavouritesLayer() {
Expand Down Expand Up @@ -115,11 +110,6 @@ private void setupFavorites(ItemList.Builder listBuilder) {
List<FavouritePoint> limitedFavoritesPoints = favoritesPoints.subList(0, Math.min(favoritesPointsSize, getContentLimit() - 1));
getApp().getOsmandMap().getMapLayers().getFavouritesLayer().setCustomMapObjects(limitedFavoritesPoints);
QuadRect mapRect = new QuadRect();
if (!Algorithms.isEmpty(limitedFavoritesPoints)) {
OsmandSettings settings = getApp().getSettings();
initialCompassMode = settings.getCompassMode();
getApp().getMapViewTrackingUtilities().switchCompassModeTo(CompassMode.NORTH_IS_UP);
}
for (FavouritePoint point : limitedFavoritesPoints) {
double longitude = point.getLongitude();
double latitude = point.getLatitude();
Expand All @@ -143,7 +133,7 @@ private void setupFavorites(ItemList.Builder listBuilder) {
CarLocation.create(point.getLatitude(), point.getLongitude())).build()).build())
.build());
}
// adjustMapToRect(location, mapRect);
adjustMapToRect(location, mapRect);
}

private void onClickFavorite(@NonNull FavouritePoint point) {
Expand Down
8 changes: 7 additions & 1 deletion OsmAnd/src/net/osmand/plus/auto/screens/LandingScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package net.osmand.plus.auto.screens
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.car.app.CarContext
import androidx.car.app.model.*
import androidx.car.app.model.Action
import androidx.car.app.model.ActionStrip
import androidx.car.app.model.CarIcon
import androidx.car.app.model.Item
import androidx.car.app.model.ItemList
import androidx.car.app.model.Row
import androidx.car.app.model.Template
import androidx.car.app.navigation.model.PlaceListNavigationTemplate
import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.DefaultLifecycleObserver
Expand Down
31 changes: 11 additions & 20 deletions OsmAnd/src/net/osmand/plus/auto/screens/MapMarkersScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,9 @@ import net.osmand.util.MapUtils
class MapMarkersScreen(
carContext: CarContext,
private val settingsAction: Action) : BaseAndroidAutoScreen(carContext) {
private var initialCompassMode: CompassMode? = null

init {
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
app.osmandMap.mapLayers.mapMarkersLayer.setCustomMapObjects(null)
app.osmandMap.mapLayers.mapMarkersLayer.customObjectsDelegate = null
app.osmandMap.mapView.backToLocation()
initialCompassMode?.let {
app.mapViewTrackingUtilities.switchCompassModeTo(it)
}
}
override fun onStart(owner: LifecycleOwner) {
recenterMap()
app.osmandMap.mapLayers.mapMarkersLayer.customObjectsDelegate = CustomMapObjects()
}
})
lifecycle.addObserver(this)
}

override fun onGetTemplate(): Template {
Expand All @@ -61,10 +46,6 @@ class MapMarkersScreen(
val location = app.mapViewTrackingUtilities.defaultLocation
app.osmandMap.mapLayers.mapMarkersLayer.setCustomMapObjects(markers)
val mapRect = QuadRect()
if (!Algorithms.isEmpty(markers)) {
initialCompassMode = app.settings.compassMode
app.mapViewTrackingUtilities.switchCompassModeTo(CompassMode.NORTH_IS_UP)
}
for (marker in markers) {
val longitude = marker.longitude
val latitude = marker.latitude
Expand Down Expand Up @@ -118,4 +99,14 @@ class MapMarkersScreen(
openRoutePreview(settingsAction, result)
}

override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
app.osmandMap.mapLayers.mapMarkersLayer.setCustomMapObjects(null)
app.osmandMap.mapLayers.mapMarkersLayer.customObjectsDelegate = null
}

override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
app.osmandMap.mapLayers.mapMarkersLayer.customObjectsDelegate = CustomMapObjects()
}
}
9 changes: 7 additions & 2 deletions OsmAnd/src/net/osmand/plus/auto/screens/NavigationScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void onCreate(@NonNull LifecycleOwner owner) {

@Override
public void onResume(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onResume(owner);
super.onResume(owner);
NavigationSession navigationSession = getApp().getCarNavigationSession();
if (navigationSession != null) {
SurfaceRenderer surfaceRenderer = navigationSession.getNavigationCarSurface();
Expand All @@ -117,7 +117,7 @@ public void onResume(@NonNull LifecycleOwner owner) {

@Override
public void onPause(@NonNull LifecycleOwner owner) {
DefaultLifecycleObserver.super.onPause(owner);
super.onPause(owner);
NavigationSession navigationSession = getApp().getCarNavigationSession();
if (navigationSession != null) {
SurfaceRenderer surfaceRenderer = navigationSession.getNavigationCarSurface();
Expand Down Expand Up @@ -398,6 +398,11 @@ public Template onGetTemplate() {
return builder.build();
}

@Override
protected void restoreMapState() {
//no automatic map adjust
}

private void updateCompass() {
OsmandSettings settings = getApp().getSettings();
boolean nightMode = getCarContext().isDarkMode();
Expand Down
35 changes: 18 additions & 17 deletions OsmAnd/src/net/osmand/plus/auto/screens/POIScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,7 @@ class POIScreen(

init {
loadPOI()
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
app.osmandMap.mapLayers.poiMapLayer.setCustomMapObjects(null)
app.osmandMap.mapLayers.poiMapLayer.customObjectsDelegate = null
app.osmandMap.mapView.backToLocation()
initialCompassMode?.let {
app.mapViewTrackingUtilities.switchCompassModeTo(it)
}
}

override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
app.osmandMap.mapLayers.poiMapLayer.customObjectsDelegate =
OsmandMapLayer.CustomMapObjects()
}
})
lifecycle.addObserver(this)
}

override fun onGetTemplate(): Template {
Expand Down Expand Up @@ -183,4 +167,21 @@ class POIScreen(
result.`object` = point.`object`
openRoutePreview(settingsAction, result)
}

override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
app.osmandMap.mapLayers.poiMapLayer.setCustomMapObjects(null)
app.osmandMap.mapLayers.poiMapLayer.customObjectsDelegate = null
app.osmandMap.mapView.backToLocation()
initialCompassMode?.let {
app.mapViewTrackingUtilities.switchCompassModeTo(it)
}
}

override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
app.osmandMap.mapLayers.poiMapLayer.customObjectsDelegate =
OsmandMapLayer.CustomMapObjects()

}
}
50 changes: 3 additions & 47 deletions OsmAnd/src/net/osmand/plus/auto/screens/RoutePreviewScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,8 @@ public final class RoutePreviewScreen extends BaseAndroidAutoScreen implements I
@Nullable
private GpxFile routeGpxFile;

private CompassMode savedCompassMode = CompassMode.NORTH_IS_UP;
private float prevElevationAngle = 90;
private float prevRotationAngle = 0;
private int prevZoom = 15;
private boolean prevMapLinkedToLocation = false;

private boolean calculateRoute = false;

private boolean calculateRoute;
private boolean calculating;

private final StateChangedListener<Void> stateChangedListener = new StateChangedListener<Void>() {
Expand All @@ -84,7 +78,6 @@ public void stateChanged(Void change) {
if (routeGpxFile != null) {
QuadRect mapRect = new QuadRect();
Algorithms.extendRectToContainRect(mapRect, SharedUtil.jQuadRect(routeGpxFile.getRect()));

adjustMapToRect(getApp().getMapViewTrackingUtilities().getDefaultLocation(), mapRect);
}
}
Expand All @@ -96,7 +89,7 @@ public RoutePreviewScreen(@NonNull CarContext carContext, @NonNull Action settin
super(carContext);
this.settingsAction = settingsAction;
this.searchResult = searchResult;
this.calculateRoute = calculateRoute;
this.calculateRoute = calculateRoute;
getLifecycle().addObserver(this);
calculating = calculateRoute;
}
Expand Down Expand Up @@ -157,16 +150,10 @@ private void updateRoute(boolean newRoute) {

@Override
public void onCreate(@NonNull LifecycleOwner owner) {
super.onCreate(owner);
OsmandApplication app = getApp();
app.getRoutingHelper().addListener(this);
app.getTargetPointsHelper().addListener(stateChangedListener);
prevMapLinkedToLocation = app.getMapViewTrackingUtilities().isMapLinkedToLocation();
OsmandMapTileView mapView = app.getOsmandMap().getMapView();
savedCompassMode = app.getSettings().getCompassMode();
prevZoom = mapView.getBaseZoom();
prevRotationAngle = mapView.getRotate();
prevElevationAngle = mapView.normalizeElevationAngle(mapView.getElevationAngle());
NavigationSession navigationSession = getSession();
if (calculateRoute) {
prepareRoute();
} else {
Expand All @@ -186,38 +173,13 @@ public void onDestroy(@NonNull LifecycleOwner owner) {
getLifecycle().removeObserver(this);
}

@Override
public void onStart(@NonNull LifecycleOwner owner) {
recenterMap();
}

@Override
public void onResume(@NonNull LifecycleOwner owner) {
if (getApp().getRoutingHelper().isRouteCalculated()) {
zoomMapToRoute();
}
}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
if(getApp().getSettings().getCompassMode() != savedCompassMode) {
getApp().getSettings().setCompassMode(savedCompassMode);
}
OsmandMapTileView mapView = getApp().getOsmandMap().getMapView();
if (mapView.getElevationAngle() != prevElevationAngle) {
mapView.setElevationAngle(prevElevationAngle);
}
if (mapView.getRotate() != prevRotationAngle) {
mapView.setRotate(prevRotationAngle, true);
}
if (mapView.getZoom() != prevZoom) {
mapView.setIntZoom(prevZoom);
}
if (prevMapLinkedToLocation != getApp().getMapViewTrackingUtilities().isMapLinkedToLocation()) {
getApp().getMapViewTrackingUtilities().setMapLinkedToLocation(prevMapLinkedToLocation);
}
}

@NonNull
@Override
public Template onGetTemplate() {
Expand Down Expand Up @@ -284,10 +246,4 @@ public void routeWasCancelled() {
@Override
public void routeWasFinished() {
}


@Override
protected void adjustMapToRect(@NonNull LatLon location, @NonNull QuadRect mapRect) {
super.adjustMapToRect(location, mapRect);
}
}
Loading

0 comments on commit bb765dd

Please sign in to comment.