Skip to content

Commit

Permalink
Fix 2D/3D setting in AA
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-osm committed Dec 19, 2024
1 parent a851b33 commit d392a02
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 44 deletions.
53 changes: 36 additions & 17 deletions OsmAnd/src/net/osmand/plus/auto/screens/BaseAndroidAutoScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ import net.osmand.data.LatLon
import net.osmand.data.QuadRect
import net.osmand.plus.OsmandApplication
import net.osmand.plus.R
import net.osmand.plus.auto.views.CarSurfaceView
import net.osmand.plus.views.Zoom
import net.osmand.search.core.SearchResult
import net.osmand.util.Algorithms

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
private val ANIMATION_RETURN_FROM_PREVIEW_TIME = 1500

private var prevElevationAngle = 90f
private var prevRotationAngle = 0f
private var prevZoom: Zoom? = null
private var prevMapLinkedToLocation = false
private var prevStateSaved = false

protected val app: OsmandApplication
get() {
Expand All @@ -47,6 +48,8 @@ abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext
)
}

protected open fun shouldRestoreMapState(): Boolean = false

protected open fun getConstraintLimitType(): Int {
return ConstraintManager.CONTENT_LIMIT_TYPE_LIST
}
Expand All @@ -59,6 +62,7 @@ abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext
RoutePreviewScreen(carContext, settingsAction, result, true)
) { obj: Any? ->
obj?.let {
screenManager.popToRoot()
startNavigation()
finish()
}
Expand Down Expand Up @@ -121,14 +125,37 @@ abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext
session?.navigationCarSurface?.handleRecenter()
}

override fun onStop(owner: LifecycleOwner) {
override fun onCreate(owner: LifecycleOwner) {
if (shouldSaveMapState()) {
saveMapState()
}
}

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

private fun shouldSaveMapState(): Boolean {
return shouldRestoreMapState() && screenManager.screenStack
.filterIsInstance<BaseAndroidAutoScreen>()
.none { it != this && it.shouldRestoreMapState() }
}

private fun saveMapState() {
val mapView = app.osmandMap.mapView
prevMapLinkedToLocation = app.mapViewTrackingUtilities.isMapLinkedToLocation
prevZoom = mapView.currentZoom
prevRotationAngle = mapView.rotate
prevElevationAngle = mapView.normalizeElevationAngle(mapView.elevationAngle)
prevStateSaved = true
}

protected open fun restoreMapState() {
private fun restoreMapState() {
val mapView = app.osmandMap.mapView
val locationProvider = app.locationProvider
val lastKnownLocation = locationProvider.lastKnownLocation
Expand All @@ -142,14 +169,6 @@ abstract class BaseAndroidAutoScreen(carContext: CarContext) : Screen(carContext
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
9 changes: 7 additions & 2 deletions OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public FavoritesScreen(
getLifecycle().addObserver(this);
}

@Override
protected boolean shouldRestoreMapState() {
return true;
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
super.onDestroy(owner);
Expand All @@ -73,8 +78,8 @@ public void onDestroy(@NonNull LifecycleOwner owner) {
}

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

Expand Down
6 changes: 4 additions & 2 deletions OsmAnd/src/net/osmand/plus/auto/screens/MapMarkersScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class MapMarkersScreen(
lifecycle.addObserver(this)
}

override fun shouldRestoreMapState() = true

override fun onGetTemplate(): Template {
val listBuilder = ItemList.Builder()
val markersSize = app.mapMarkersHelper.mapMarkers.size
Expand Down Expand Up @@ -105,8 +107,8 @@ class MapMarkersScreen(
app.osmandMap.mapLayers.mapMarkersLayer.customObjectsDelegate = null
}

override fun onStart(owner: LifecycleOwner) {
super.onStart(owner)
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
app.osmandMap.mapLayers.mapMarkersLayer.customObjectsDelegate = CustomMapObjects()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -130,6 +129,7 @@ public void onPause(@NonNull LifecycleOwner owner) {

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
super.onDestroy(owner);
adjustMapPosition(false);
getApp().getRoutingHelper().removeListener(this);
getLifecycle().removeObserver(this);
Expand Down Expand Up @@ -399,11 +399,6 @@ 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
10 changes: 5 additions & 5 deletions OsmAnd/src/net/osmand/plus/auto/screens/POIScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class POIScreen(
lifecycle.addObserver(this)
}

override fun shouldRestoreMapState() = true

override fun onGetTemplate(): Template {
val templateBuilder = PlaceListNavigationTemplate.Builder()
if (loading) {
Expand Down Expand Up @@ -178,10 +180,8 @@ class POIScreen(
}
}

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

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
app.osmandMap.mapLayers.poiMapLayer.customObjectsDelegate = OsmandMapLayer.CustomMapObjects()
}
}
16 changes: 7 additions & 9 deletions OsmAnd/src/net/osmand/plus/auto/screens/RoutePreviewScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static net.osmand.search.core.ObjectType.GPX_TRACK;

import android.os.Handler;
import android.os.Looper;
import android.text.SpannableString;

import androidx.annotation.NonNull;
Expand All @@ -23,11 +21,9 @@


import net.osmand.PlatformUtil;
import net.osmand.plus.auto.NavigationSession;
import net.osmand.plus.auto.TripUtils;
import net.osmand.plus.shared.SharedUtil;
import net.osmand.StateChangedListener;
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
import net.osmand.data.ValueHolder;
import net.osmand.plus.OsmandApplication;
Expand All @@ -36,12 +32,9 @@
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.routing.RoutingHelperUtils;
import net.osmand.plus.search.listitems.QuickSearchListItem;
import net.osmand.plus.settings.enums.CompassMode;
import net.osmand.plus.shared.SharedUtil;
import net.osmand.plus.track.data.GPXInfo;
import net.osmand.plus.track.helpers.GpxFileLoaderTask;
import net.osmand.plus.track.helpers.SelectedGpxFile;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.search.core.SearchResult;
import net.osmand.shared.gpx.GpxFile;
import net.osmand.util.Algorithms;
Expand Down Expand Up @@ -72,7 +65,7 @@ public final class RoutePreviewScreen extends BaseAndroidAutoScreen implements I
private boolean calculateRoute;
private boolean calculating;

private final StateChangedListener<Void> stateChangedListener = new StateChangedListener<Void>() {
private final StateChangedListener<Void> stateChangedListener = new StateChangedListener<>() {
@Override
public void stateChanged(Void change) {
if (routeGpxFile != null) {
Expand All @@ -83,7 +76,6 @@ public void stateChanged(Void change) {
}
};


public RoutePreviewScreen(@NonNull CarContext carContext, @NonNull Action settingsAction,
@NonNull SearchResult searchResult, boolean calculateRoute) {
super(carContext);
Expand All @@ -94,6 +86,11 @@ public RoutePreviewScreen(@NonNull CarContext carContext, @NonNull Action settin
calculating = calculateRoute;
}

@Override
protected boolean shouldRestoreMapState() {
return true;
}

private void prepareRoute() {
if (searchResult.objectType == GPX_TRACK) {
GPXInfo gpxInfo = ((GPXInfo) searchResult.relatedObject);
Expand Down Expand Up @@ -163,6 +160,7 @@ public void onCreate(@NonNull LifecycleOwner owner) {

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
super.onDestroy(owner);
OsmandApplication app = getApp();
RoutingHelper routingHelper = app.getRoutingHelper();
routingHelper.removeListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
public final class SearchResultsScreen extends BaseSearchScreen implements DefaultLifecycleObserver,
AppInitializeListener {


@NonNull
private final Action settingsAction;
@NonNull
Expand Down Expand Up @@ -77,6 +76,7 @@ public Template onGetTemplate() {

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
super.onDestroy(owner);
getApp().getAppInitializer().removeListener(this);
getLifecycle().removeObserver(this);
destroyed = true;
Expand Down
3 changes: 1 addition & 2 deletions OsmAnd/src/net/osmand/plus/auto/screens/SearchScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public final class SearchScreen extends BaseSearchScreen implements DefaultLifec
private static final Log LOG = PlatformUtil.getLog(SearchScreen.class);
private static final int MAP_MARKERS_LIMIT = 3;


@NonNull
private final Action settingsAction;

Expand All @@ -73,14 +72,14 @@ public SearchScreen(@NonNull CarContext carContext, @NonNull Action settingsActi
reloadHistory();
}


@NonNull
public SearchUICore getSearchUICore() {
return getApp().getSearchUICore().getCore();
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
super.onDestroy(owner);
getApp().getAppInitializer().removeListener(this);
getLifecycle().removeObserver(this);
destroyed = true;
Expand Down
2 changes: 2 additions & 0 deletions OsmAnd/src/net/osmand/plus/auto/screens/TracksScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class TracksScreen(
lifecycle.addObserver(this)
}

override fun shouldRestoreMapState() = true

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
loadTracksTask = LoadTracksTask()
Expand Down

0 comments on commit d392a02

Please sign in to comment.