Skip to content

Commit

Permalink
#16632: Fixes after review; fixed show route in RoutePreviewScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
Corwin-Kh committed Jul 24, 2023
1 parent 2553b6e commit f6b837f
Show file tree
Hide file tree
Showing 23 changed files with 182 additions and 197 deletions.
14 changes: 14 additions & 0 deletions OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.osmand.IProgress;
import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.data.QuadRect;
import net.osmand.router.RouteColorize;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -1361,4 +1362,17 @@ public static <T> List<T> removeAllFromList(Collection<T> original, Collection<T
return copy;
}

public static void extendRectToContainPoint(QuadRect mapRect, double longitude, double latitude) {
mapRect.left = mapRect.left == 0.0 ? longitude : Math.min(mapRect.left, longitude);
mapRect.right = Math.max(mapRect.right, longitude);
mapRect.bottom = mapRect.bottom == 0.0 ? latitude : Math.min(mapRect.bottom, latitude);
mapRect.top = Math.max(mapRect.top, latitude);
}

public static void extendRectToContainRect(QuadRect mapRect, QuadRect gpxRect) {
mapRect.left = mapRect.left == 0.0 ? gpxRect.left : Math.min(mapRect.left, gpxRect.left);
mapRect.right = Math.max(mapRect.right, gpxRect.right);
mapRect.top = Math.max(mapRect.top, gpxRect.top);
mapRect.bottom = mapRect.bottom == 0.0 ? gpxRect.bottom : Math.min(mapRect.bottom, gpxRect.bottom);
}
}
2 changes: 1 addition & 1 deletion OsmAnd/src/net/osmand/plus/NavigationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void updateCarNavigation(Location currentLocation) {
navigationScreen = carNavigationSession.getNavigationScreen();
}
if (navigationScreen != null) {
float density = navigationScreen.getSurfaceRenderer().getDensity();
float density = carNavigationSession.getNavigationCarSurface().getDensity();
if (density == 0) {
density = 1;
}
Expand Down
63 changes: 31 additions & 32 deletions OsmAnd/src/net/osmand/plus/auto/BaseOsmAndAndroidAutoScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import net.osmand.search.core.SearchResult
import kotlin.math.abs

abstract class BaseOsmAndAndroidAutoScreen(
carContext: CarContext,
val surfaceRenderer: SurfaceRenderer) : Screen(carContext) {
carContext: CarContext) : Screen(carContext) {

protected val app: OsmandApplication
get() {
Expand All @@ -42,10 +41,9 @@ abstract class BaseOsmAndAndroidAutoScreen(

protected fun openRoutePreview(
settingsAction: Action,
surfaceRenderer: SurfaceRenderer,
result: SearchResult) {
screenManager.pushForResult(
RoutePreviewScreen(carContext, settingsAction, surfaceRenderer, result)
RoutePreviewScreen(carContext, settingsAction, result)
) { obj: Any? ->
obj?.let {
onSearchResultSelected(result)
Expand Down Expand Up @@ -79,38 +77,39 @@ abstract class BaseOsmAndAndroidAutoScreen(
screenManager.pushForResult(
SearchScreen(
carContext,
navigationSession.settingsAction,
navigationSession.navigationCarSurface)) { _: Any? -> }
navigationSession.settingsAction)) { _: Any? -> }
}
}

protected fun adjustMapToRect(location: LatLon, mapRect: QuadRect) {
if ((mapRect.left != 0.0) && (mapRect.right != 0.0) && (mapRect.top != 0.0) && (mapRect.bottom != 0.0)) {
val mapView = app.osmandMap.mapView
val tileBox = mapView.rotatedTileBox
val leftRightDelta = abs(location.longitude - mapRect.left)
.coerceAtLeast(abs(location.longitude - mapRect.right))
val topBottomDelta = abs(location.latitude - mapRect.top)
.coerceAtLeast(abs(location.latitude - mapRect.bottom))
val rectLeft = location.longitude - leftRightDelta
val rectRight = location.longitude + leftRightDelta
val rectWidth = rectRight - rectLeft
val coef: Double = surfaceRenderer.visibleAreaWidth / tileBox.pixWidth
val left = rectLeft - rectWidth * coef
val right = rectRight + rectWidth * coef
val rectInVisibleArea = QuadRect()
rectInVisibleArea.left = left
rectInVisibleArea.right = right
rectInVisibleArea.top = location.latitude - topBottomDelta
rectInVisibleArea.bottom = location.latitude + topBottomDelta
mapView.fitRectToMap(
rectInVisibleArea.left,
rectInVisibleArea.right,
rectInVisibleArea.top,
rectInVisibleArea.bottom,
tileBox.pixWidth,
tileBox.pixHeight,
0)
app.carNavigationSession?.navigationCarSurface?.let { surfaceRenderer ->
if (!mapRect.hasInitialState()) {
val mapView = app.osmandMap.mapView
val tileBox = mapView.rotatedTileBox
val leftRightDelta = abs(location.longitude - mapRect.left)
.coerceAtLeast(abs(location.longitude - mapRect.right))
val topBottomDelta = abs(location.latitude - mapRect.top)
.coerceAtLeast(abs(location.latitude - mapRect.bottom))
val rectLeft = location.longitude - leftRightDelta
val rectRight = location.longitude + leftRightDelta
val rectWidth = rectRight - rectLeft
val coef: Double = surfaceRenderer.visibleAreaWidth / tileBox.pixWidth
val left = rectLeft - rectWidth * coef
val right = rectRight + rectWidth * coef
val rectInVisibleArea = QuadRect()
rectInVisibleArea.left = left
rectInVisibleArea.right = right
rectInVisibleArea.top = location.latitude - topBottomDelta
rectInVisibleArea.bottom = location.latitude + topBottomDelta
mapView.fitRectToMap(
rectInVisibleArea.left,
rectInVisibleArea.right,
rectInVisibleArea.top,
rectInVisibleArea.bottom,
tileBox.pixWidth,
tileBox.pixHeight,
0)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import net.osmand.plus.auto.SearchHelper.SearchHelperListener
import net.osmand.search.core.SearchCoreFactory

abstract class BaseOsmAndAndroidAutoSearchScreen(
carContext: CarContext,
surfaceRenderer: SurfaceRenderer) :
BaseOsmAndAndroidAutoScreen(carContext, surfaceRenderer), SearchHelperListener {
carContext: CarContext) :
BaseOsmAndAndroidAutoScreen(carContext), SearchHelperListener {

protected var loading = false
protected val searchHelper: SearchHelper by lazy(::createSearchHelper)
Expand Down
4 changes: 1 addition & 3 deletions OsmAnd/src/net/osmand/plus/auto/FavoriteGroupsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import net.osmand.plus.utils.AndroidUtils

class FavoriteGroupsScreen(
carContext: CarContext,
private val settingsAction: Action,
surfaceRenderer: SurfaceRenderer) : BaseOsmAndAndroidAutoScreen(carContext, surfaceRenderer) {
private val settingsAction: Action) : BaseOsmAndAndroidAutoScreen(carContext) {

override fun onGetTemplate(): Template {
val listBuilder = ItemList.Builder()
Expand Down Expand Up @@ -70,7 +69,6 @@ class FavoriteGroupsScreen(
FavoritesScreen(
carContext,
settingsAction,
surfaceRenderer,
group))
}

Expand Down
22 changes: 3 additions & 19 deletions OsmAnd/src/net/osmand/plus/auto/FavoritesScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ public final class FavoritesScreen extends BaseOsmAndAndroidAutoScreen {
public FavoritesScreen(
@NonNull CarContext carContext,
@NonNull Action settingsAction,
@NonNull SurfaceRenderer surfaceRenderer,
@Nullable FavoriteGroup group) {
super(carContext, surfaceRenderer);
super(carContext);
this.settingsAction = settingsAction;
selectedGroup = group;
getLifecycle().addObserver(new DefaultLifecycleObserver() {
Expand Down Expand Up @@ -109,7 +108,7 @@ private void setupFavorites(ItemList.Builder listBuilder) {
for (FavouritePoint point : limitedFavoritesPoints) {
double longitude = point.getLongitude();
double latitude = point.getLatitude();
extendRectToContainPoint(mapRect, longitude, latitude);
Algorithms.extendRectToContainPoint(mapRect, longitude, latitude);
String title = point.getDisplayName(getApp());
int color = getApp().getFavoritesHelper().getColorWithCategory(point, ContextCompat.getColor(getApp(), R.color.color_favorite));
CarIcon icon = new CarIcon.Builder(IconCompat.createWithBitmap(
Expand All @@ -132,28 +131,13 @@ private void setupFavorites(ItemList.Builder listBuilder) {
adjustMapToRect(location, mapRect);
}

private void extendRectToContainPoint(QuadRect mapRect, double longitude, double latitude) {
if (mapRect.left == 0.0) {
mapRect.left = longitude;
} else {
mapRect.left = Math.min(mapRect.left, longitude);
}
mapRect.right = Math.max(mapRect.right, longitude);
if (mapRect.bottom == 0.0) {
mapRect.bottom = latitude;
} else {
mapRect.bottom = Math.min(mapRect.bottom, latitude);
}
mapRect.top = Math.max(mapRect.top, latitude);
}

private void onClickFavorite(@NonNull FavouritePoint point) {
SearchResult result = new SearchResult();
result.location = new LatLon(point.getLatitude(), point.getLongitude());
result.objectType = ObjectType.FAVORITE;
result.object = point;
result.localeName = point.getAddress();
openRoutePreview(settingsAction, getSurfaceRenderer(), result);
openRoutePreview(settingsAction, result);
}

@NonNull
Expand Down
8 changes: 3 additions & 5 deletions OsmAnd/src/net/osmand/plus/auto/HistoryScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import net.osmand.util.MapUtils

class HistoryScreen(
carContext: CarContext,
private val settingsAction: Action,
surfaceRenderer: SurfaceRenderer) : BaseOsmAndAndroidAutoScreen(carContext, surfaceRenderer) {
private val settingsAction: Action) : BaseOsmAndAndroidAutoScreen(carContext) {

override fun onGetTemplate(): Template {
val listBuilder = ItemList.Builder()
Expand Down Expand Up @@ -90,14 +89,13 @@ class HistoryScreen(
historyItem.searchResult.location.longitude)
result.objectType = ObjectType.RECENT_OBJ
result.`object` = historyItem.searchResult.`object`
openRoutePreview(settingsAction, surfaceRenderer, result)
openRoutePreview(settingsAction, result)
}

private fun openSearch() {
screenManager.pushForResult(
SearchScreen(
carContext,
settingsAction,
surfaceRenderer)) { }
settingsAction)) { }
}
}
26 changes: 10 additions & 16 deletions OsmAnd/src/net/osmand/plus/auto/LandingScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import net.osmand.plus.R

class LandingScreen(
carContext: CarContext,
private val settingsAction: Action,
surfaceRenderer: SurfaceRenderer) : BaseOsmAndAndroidAutoScreen(carContext, surfaceRenderer) {
private val settingsAction: Action) : BaseOsmAndAndroidAutoScreen(carContext) {
@DrawableRes
private var compassResId = R.drawable.ic_compass_niu

Expand Down Expand Up @@ -46,7 +45,7 @@ class LandingScreen(
R.drawable.ic_zoom_in))
.build())
.setOnClickListener {
surfaceRenderer.handleScale(
app.carNavigationSession?.navigationCarSurface?.handleScale(
NavigationSession.INVALID_FOCAL_POINT_VAL,
NavigationSession.INVALID_FOCAL_POINT_VAL,
NavigationSession.ZOOM_IN_BUTTON_SCALE_FACTOR)
Expand All @@ -61,7 +60,7 @@ class LandingScreen(
R.drawable.ic_zoom_out))
.build())
.setOnClickListener {
surfaceRenderer.handleScale(
app.carNavigationSession?.navigationCarSurface?.handleScale(
NavigationSession.INVALID_FOCAL_POINT_VAL,
NavigationSession.INVALID_FOCAL_POINT_VAL,
NavigationSession.ZOOM_OUT_BUTTON_SCALE_FACTOR)
Expand Down Expand Up @@ -93,40 +92,35 @@ class LandingScreen(
NavigationScreen(
carContext,
settingsAction,
it,
surfaceRenderer
it
))
}

PlaceCategory.FAVORITES -> screenManager.push(
FavoriteGroupsScreen(
carContext,
settingsAction,
surfaceRenderer))
settingsAction
))

PlaceCategory.HISTORY -> screenManager.push(
HistoryScreen(
carContext,
settingsAction,
surfaceRenderer))
settingsAction))

PlaceCategory.MAP_MARKERS -> screenManager.push(
MapMarkersScreen(
carContext,
settingsAction,
surfaceRenderer))
settingsAction))

PlaceCategory.POI -> screenManager.push(
POICategoriesScreen(
carContext,
settingsAction,
surfaceRenderer))
settingsAction))

PlaceCategory.TRACKS -> screenManager.push(
TracksFoldersScreen(
carContext,
settingsAction,
surfaceRenderer))
settingsAction))
}
}

Expand Down
31 changes: 14 additions & 17 deletions OsmAnd/src/net/osmand/plus/auto/MapMarkersScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import net.osmand.data.LatLon
import net.osmand.data.QuadRect
import net.osmand.data.RotatedTileBox
import net.osmand.plus.R
import net.osmand.plus.mapmarkers.MapMarker
import net.osmand.plus.settings.enums.CompassMode
import net.osmand.search.core.ObjectType
import net.osmand.search.core.SearchResult
import net.osmand.util.Algorithms
import net.osmand.util.MapUtils
import kotlin.math.max
import kotlin.math.min

class MapMarkersScreen(
carContext: CarContext,
private val settingsAction: Action,
surfaceRenderer: SurfaceRenderer) : BaseOsmAndAndroidAutoScreen(carContext, surfaceRenderer) {
private val settingsAction: Action) : BaseOsmAndAndroidAutoScreen(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.mapView.backToLocation()
initialCompassMode?.let {
app.mapViewTrackingUtilities.switchCompassModeTo(it)
}
}
})
}
Expand All @@ -52,10 +54,15 @@ class MapMarkersScreen(
val location = app.settings.lastKnownMapLocation
app.osmandMap.mapLayers.mapMarkersLayer.setCustomMapObjects(markers)
val mapRect = QuadRect()
if (!Algorithms.isEmpty(markers)) {
val settings = app.settings
initialCompassMode = settings.compassMode
app.mapViewTrackingUtilities.switchCompassModeTo(CompassMode.NORTH_IS_UP)
}
for (marker in markers) {
val longitude = marker.longitude
val latitude = marker.latitude
extendRectToContainPoint(mapRect, longitude, latitude)
Algorithms.extendRectToContainPoint(mapRect, longitude, latitude)
val title = marker.getName(app)
val markerColor = MapMarker.getColorId(marker.colorIndex)
val icon = CarIcon.Builder(
Expand Down Expand Up @@ -95,24 +102,14 @@ class MapMarkersScreen(
.build()
}

private fun extendRectToContainPoint(
mapRect: QuadRect,
longitude: Double,
latitude: Double) {
mapRect.left = if (mapRect.left == 0.0) longitude else min(mapRect.left, longitude)
mapRect.right = max(mapRect.right, longitude)
mapRect.bottom = if (mapRect.bottom == 0.0) latitude else min(mapRect.bottom, latitude)
mapRect.top = max(mapRect.top, latitude)
}

private fun onClickMarkerItem(mapMarker: MapMarker) {
val result = SearchResult()
result.location = LatLon(
mapMarker.point.latitude,
mapMarker.point.longitude)
result.objectType = ObjectType.MAP_MARKER
result.`object` = mapMarker
openRoutePreview(settingsAction, surfaceRenderer, result)
openRoutePreview(settingsAction, result)
}

}
Loading

0 comments on commit f6b837f

Please sign in to comment.