Skip to content

Commit

Permalink
Android Auto: add option to sort favorites by distance
Browse files Browse the repository at this point in the history
  • Loading branch information
ntruchsess committed Feb 8, 2024
1 parent a873e3e commit 5721667
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions OsmAnd/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5618,4 +5618,5 @@
<string name="shared_string_keywords">Schlüsselwörter</string>
<string name="shared_string_additional">Zusätzlich</string>
<string name="shared_string_activity">Aktivität</string>
<string name="settings_favorites_sortorder">Nach Entfernung sortieren</string>
</resources>
1 change: 1 addition & 0 deletions OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5519,4 +5519,5 @@ Download tile maps directly, or copy them as SQLite database files to OsmAnd\'s
<string name="routing_attr_freeride_policy_name">Off-piste</string>
<string name="routing_attr_freeride_policy_description">\'Freeride\' and \'Off-piste\' are unofficial routes and passages. Typically ungroomed, unmaintained and not checked in the evening. Enter at your own risk.</string>
<string name="voice_prompts_timetable">Voice prompts times</string>
<string name="settings_favorites_sortorder">Sort by distance</string>
</resources>
53 changes: 42 additions & 11 deletions OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
Expand All @@ -58,6 +59,7 @@ public final class FavoritesScreen extends BaseAndroidAutoScreen {
@Nullable
private FavoriteGroup selectedGroup;
private CompassMode initialCompassMode;
private boolean isSortableByDistance;

public FavoritesScreen(
@NonNull CarContext carContext,
Expand All @@ -66,6 +68,7 @@ public FavoritesScreen(
super(carContext);
this.settingsAction = settingsAction;
selectedGroup = group;
isSortableByDistance = group != null;
getLifecycle().addObserver(new DefaultLifecycleObserver() {
@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
Expand Down Expand Up @@ -109,18 +112,19 @@ protected int getConstraintLimitType() {
}

private void setupFavorites(ItemList.Builder listBuilder) {
OsmandSettings settings = getApp().getSettings();
List<FavouritePoint> favoritePoints = getFavorites();
int limitedSize = Math.min(favoritePoints.size(), getContentLimit() -1);
LatLon location = getApp().getMapViewTrackingUtilities().getDefaultLocation();
List<FavouritePoint> favoritesPoints = getFavorites();
int favoritesPointsSize = favoritesPoints.size();
List<FavouritePoint> limitedFavoritesPoints = favoritesPoints.subList(0, Math.min(favoritesPointsSize, getContentLimit() - 1));
getApp().getOsmandMap().getMapLayers().getFavouritesLayer().setCustomMapObjects(limitedFavoritesPoints);
List<FavoritePointDistance> limitedFavoritePointDistances = toLimitedSortedPointDistanceList(favoritePoints, location, isSortableByDistance && settings.SORT_FAV_BY_DISTANCE.get(), limitedSize);
List<FavouritePoint> limitedFavoritePoints = new ArrayList<>(limitedSize);
QuadRect mapRect = new QuadRect();
if (!Algorithms.isEmpty(limitedFavoritesPoints)) {
OsmandSettings settings = getApp().getSettings();
if (!Algorithms.isEmpty(limitedFavoritePointDistances)) {
initialCompassMode = settings.getCompassMode();
getApp().getMapViewTrackingUtilities().switchCompassModeTo(CompassMode.NORTH_IS_UP);
}
for (FavouritePoint point : limitedFavoritesPoints) {
for (FavoritePointDistance favoritePointDistance : limitedFavoritePointDistances) {
FavouritePoint point = favoritePointDistance.favorite;
double longitude = point.getLongitude();
double latitude = point.getLatitude();
Algorithms.extendRectToContainPoint(mapRect, longitude, latitude);
Expand All @@ -129,10 +133,8 @@ private void setupFavorites(ItemList.Builder listBuilder) {
CarIcon icon = new CarIcon.Builder(IconCompat.createWithBitmap(
AndroidUtils.drawableToBitmap(PointImageDrawable.getFromFavorite(getApp(), color, false, point)))).build();
String description = point.getSpecialPointType() != null ? point.getDescription() : point.getCategory();
double dist = MapUtils.getDistance(point.getLatitude(), point.getLongitude(),
location.getLatitude(), location.getLongitude());
SpannableString address = new SpannableString(Algorithms.isEmpty(description) ? " " : " • " + description);
DistanceSpan distanceSpan = DistanceSpan.create(TripHelper.getDistance(getApp(), dist));
DistanceSpan distanceSpan = DistanceSpan.create(TripHelper.getDistance(getApp(), favoritePointDistance.distance));
address.setSpan(distanceSpan, 0, 1, SPAN_INCLUSIVE_INCLUSIVE);
listBuilder.addItem(new Row.Builder()
.setTitle(title)
Expand All @@ -142,10 +144,40 @@ private void setupFavorites(ItemList.Builder listBuilder) {
.setMetadata(new Metadata.Builder().setPlace(new Place.Builder(
CarLocation.create(point.getLatitude(), point.getLongitude())).build()).build())
.build());
limitedFavoritePoints.add(point);
}
getApp().getOsmandMap().getMapLayers().getFavouritesLayer().setCustomMapObjects(limitedFavoritePoints);
adjustMapToRect(location, mapRect);
}

private static class FavoritePointDistance {
private final FavouritePoint favorite;
private final double distance;
FavoritePointDistance(FavouritePoint favorite, double dist) {
this.favorite = favorite;
this.distance = dist;
}
}

private static List<FavoritePointDistance> toPointDistanceList(List<FavouritePoint> points, LatLon location) {
List<FavoritePointDistance> returnList = new ArrayList<>(points.size());
for (FavouritePoint point : points) {
returnList.add(new FavoritePointDistance(point, MapUtils.getDistance(point.getLatitude(), point.getLongitude(), location.getLatitude(), location.getLongitude())));
}
return returnList;
};

private static List<FavoritePointDistance> toLimitedSortedPointDistanceList(List<FavouritePoint> points, LatLon location, boolean sortByDistance, int limitedSize) {
if (sortByDistance) {
List<FavoritePointDistance> pointDistances = toPointDistanceList(points, location);
Collections.sort(pointDistances, Comparator.comparingDouble(pointDistance -> pointDistance.distance));
return pointDistances.subList(0, limitedSize);
} else {
Collections.sort(points, (left, right) -> Long.compare(right.getTimestamp(), left.getTimestamp()));
return toPointDistanceList(points.subList(0, limitedSize), location);
}
};

private void onClickFavorite(@NonNull FavouritePoint point) {
SearchResult result = new SearchResult();
result.location = new LatLon(point.getLatitude(), point.getLongitude());
Expand All @@ -163,7 +195,6 @@ private List<FavouritePoint> getFavorites() {
} else {
filteredFavorites.addAll(selectedGroup.getPoints());
}
Collections.sort(filteredFavorites, (left, right) -> Long.compare(right.getTimestamp(), left.getTimestamp()));
return filteredFavorites;
}

Expand Down
16 changes: 16 additions & 0 deletions OsmAnd/src/net/osmand/plus/auto/screens/SettingsScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public Template onGetTemplate() {
sectionABuilder.build(),
getCarContext().getString(R.string.voice_pref_title)));

ItemList.Builder sectionBBuilder = new ItemList.Builder();
sectionBBuilder.addItem(new Row.Builder()
.setTitle(getCarContext().getString(R.string.settings_favorites_sortorder))
.setToggle(
new Toggle.Builder(
(value) -> osmandSettings.SORT_FAV_BY_DISTANCE.set(value))
.setChecked(osmandSettings.SORT_FAV_BY_DISTANCE.get())
.build())
.build()
);

templateBuilder.addSectionedList(
SectionedItemList.create(
sectionBBuilder.build(),
getCarContext().getString(R.string.shared_string_favorites)));

/*
ItemList.Builder sectionBBuilder = new ItemList.Builder();
sectionBBuilder.addItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,7 @@ protected boolean setValue(Object prefs, Boolean val) {
public final CommonPreference<Boolean> CURRENT_TRACK_SHOW_START_FINISH = new BooleanPreference(this, "current_track_show_start_finish", true).makeGlobal().makeShared().cache();
public final ListStringPreference CUSTOM_TRACK_COLORS = (ListStringPreference) new ListStringPreference(this, "custom_track_colors", null, ",").makeShared().makeGlobal();
public final ListStringPreference LAST_USED_FAV_ICONS = (ListStringPreference) new ListStringPreference(this, "last_used_favorite_icons", null, ",").makeShared().makeGlobal();
public final CommonPreference<Boolean> SORT_FAV_BY_DISTANCE = new BooleanPreference(this, "sort_fav_by_distance", false).makeGlobal().makeShared();

public final CommonPreference<Integer> SAVE_TRACK_INTERVAL = new IntPreference(this, "save_track_interval", 5000).makeProfile();

Expand Down

0 comments on commit 5721667

Please sign in to comment.