From 1b6811c5fa6d7b26765bdd5fd3dbcfbc36044550 Mon Sep 17 00:00:00 2001 From: Corwin-Kh Date: Wed, 20 Nov 2024 14:25:55 +0200 Subject: [PATCH] Aa zoom improvements (#21394) * Added exit preview animation * Implement restore map state on AA screen axit for non route preview screens * Fixes after review --- .../osmand/plus/auto/NavigationSession.java | 3 ++ .../auto/screens/BaseAndroidAutoScreen.kt | 43 ++++++++++++++-- .../plus/auto/screens/FavoritesScreen.java | 40 ++++++--------- .../osmand/plus/auto/screens/LandingScreen.kt | 8 ++- .../plus/auto/screens/MapMarkersScreen.kt | 31 ++++-------- .../plus/auto/screens/NavigationScreen.java | 9 +++- .../net/osmand/plus/auto/screens/POIScreen.kt | 35 ++++++------- .../plus/auto/screens/RoutePreviewScreen.java | 50 ++----------------- .../osmand/plus/auto/screens/TracksScreen.kt | 41 ++++++--------- .../osmand/plus/views/OsmandMapTileView.java | 10 ++-- 10 files changed, 127 insertions(+), 143 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/auto/NavigationSession.java b/OsmAnd/src/net/osmand/plus/auto/NavigationSession.java index ef44e03c3ce..0e3fb1c493f 100644 --- a/OsmAnd/src/net/osmand/plus/auto/NavigationSession.java +++ b/OsmAnd/src/net/osmand/plus/auto/NavigationSession.java @@ -160,6 +160,9 @@ public void setMapView(OsmandMapTileView mapView) { SurfaceRenderer navigationCarSurface = this.navigationCarSurface; if (navigationCarSurface != null) { navigationCarSurface.setMapView(hasStarted() ? mapView : null); + if (mapView != null) { + navigationCarSurface.handleRecenter(); + } } } diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/BaseAndroidAutoScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/BaseAndroidAutoScreen.kt index fcaae10459f..ebfe74795d2 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/BaseAndroidAutoScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/BaseAndroidAutoScreen.kt @@ -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() { @@ -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()) { @@ -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 } diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java b/OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java index 8d3a2bc0aeb..3f69bc3619f 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java +++ b/OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java @@ -57,7 +57,6 @@ public final class FavoritesScreen extends BaseAndroidAutoScreen { @Nullable private FavoriteGroup selectedGroup; - private CompassMode initialCompassMode; public FavoritesScreen( @NonNull CarContext carContext, @@ -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() { @@ -115,11 +110,6 @@ private void setupFavorites(ItemList.Builder listBuilder) { List 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(); @@ -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) { diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/LandingScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/LandingScreen.kt index 107c58cebaf..1b3587b2e7d 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/LandingScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/LandingScreen.kt @@ -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 diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/MapMarkersScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/MapMarkersScreen.kt index d77d7861a96..a84b953010b 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/MapMarkersScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/MapMarkersScreen.kt @@ -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 { @@ -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 @@ -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() + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/NavigationScreen.java b/OsmAnd/src/net/osmand/plus/auto/screens/NavigationScreen.java index 871d512dabc..ff3ff48762c 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/NavigationScreen.java +++ b/OsmAnd/src/net/osmand/plus/auto/screens/NavigationScreen.java @@ -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(); @@ -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(); @@ -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(); diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/POIScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/POIScreen.kt index e35607af4d6..1c5433aba53 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/POIScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/POIScreen.kt @@ -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 { @@ -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() + + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/RoutePreviewScreen.java b/OsmAnd/src/net/osmand/plus/auto/screens/RoutePreviewScreen.java index f95bec2d1f5..1cb6ffd63a0 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/RoutePreviewScreen.java +++ b/OsmAnd/src/net/osmand/plus/auto/screens/RoutePreviewScreen.java @@ -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 stateChangedListener = new StateChangedListener() { @@ -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); } } @@ -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; } @@ -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 { @@ -186,11 +173,6 @@ 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()) { @@ -198,26 +180,6 @@ public void onResume(@NonNull LifecycleOwner owner) { } } - @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() { @@ -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); - } } diff --git a/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt b/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt index d378c771795..60cc38ee25f 100644 --- a/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt @@ -46,31 +46,26 @@ class TracksScreen( val gpxDbHelper: GpxDbHelper = app.gpxDbHelper private var loadedGpxFiles = HashMap() private lateinit var loadTracksTask: LoadTracksTask - private var initialCompassMode: CompassMode? = null init { - lifecycle.addObserver(object : DefaultLifecycleObserver { - override fun onCreate(owner: LifecycleOwner) { - super.onCreate(owner) - loadTracksTask = LoadTracksTask() - loadTracksTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) - } + lifecycle.addObserver(this) + } - override fun onDestroy(owner: LifecycleOwner) { - super.onDestroy(owner) - app.osmandMap.mapLayers.gpxLayer.setCustomMapObjects(null) - app.osmandMap.mapLayers.gpxLayer.customObjectsDelegate = null - app.osmandMap.mapView.backToLocation() - initialCompassMode?.let { - app.mapViewTrackingUtilities.switchCompassModeTo(it) - } - } + override fun onCreate(owner: LifecycleOwner) { + super.onCreate(owner) + loadTracksTask = LoadTracksTask() + loadTracksTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) + } + + override fun onDestroy(owner: LifecycleOwner) { + super.onDestroy(owner) + app.osmandMap.mapLayers.gpxLayer.setCustomMapObjects(null) + app.osmandMap.mapLayers.gpxLayer.customObjectsDelegate = null + } - override fun onStart(owner: LifecycleOwner) { - recenterMap() - app.osmandMap.mapLayers.gpxLayer.customObjectsDelegate = CustomMapObjects() - } - }) + override fun onStart(owner: LifecycleOwner) { + super.onStart(owner) + app.osmandMap.mapLayers.gpxLayer.customObjectsDelegate = CustomMapObjects() } private inner class LoadTracksTask : AsyncTask() { @@ -131,10 +126,6 @@ class TracksScreen( val selectedGpxFiles = ArrayList() val tracks = trackTab.trackItems.subList(0, tracksSize.coerceAtMost(contentLimit - 1)) val mapRect = KQuadRect() - if (!Algorithms.isEmpty(tracks)) { - initialCompassMode = app.settings.compassMode - app.mapViewTrackingUtilities.switchCompassModeTo(CompassMode.NORTH_IS_UP) - } for (track in tracks) { val gpxFile = loadedGpxFiles[track] gpxFile?.let { diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java index 9b38ed16fa6..d8cdcda0853 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapTileView.java @@ -1744,9 +1744,7 @@ public void fitRectToMap(RotatedTileBox tb, double left, double right, double to clat = tb.getLatFromPixel(x, y); clon = tb.getLonFromPixel(x, y); if (rotate) { -// animatedDraggingThread.startMoving(clat, clon, zoom.getBaseZoom(), zoom.getZoomFloatPart()); -// LOG.debug("Target animation - " + zoom.getBaseZoom() + " " + zoom.getZoomFloatPart()); - animatedDraggingThread.animateToPreview(clat, clon, + animateToState(clat, clon, zoom, 0f, 90f, ANIMATION_PREVIEW_TIME, false); } else { animatedDraggingThread.startMoving(clat, clon, zoom.getBaseZoom(), zoom.getZoomFloatPart()); @@ -1754,6 +1752,12 @@ public void fitRectToMap(RotatedTileBox tb, double left, double right, double to } + public void animateToState(double clat, double clon, @NonNull Zoom zoom, + float finalRotation, float elevationAngle, long animationDuration, boolean notifyListener) { + animatedDraggingThread.animateToPreview(clat, clon, + zoom, finalRotation, elevationAngle, animationDuration, notifyListener); + } + public RotatedTileBox getTileBox(int tileBoxWidthPx, int tileBoxHeightPx, int marginTopPx) { RotatedTileBox tb = currentViewport.copy(); double border = 0.8;