Skip to content

Commit 5721667

Browse files
committed
Android Auto: add option to sort favorites by distance
1 parent a873e3e commit 5721667

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

OsmAnd/res/values-de/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5618,4 +5618,5 @@
56185618
<string name="shared_string_keywords">Schlüsselwörter</string>
56195619
<string name="shared_string_additional">Zusätzlich</string>
56205620
<string name="shared_string_activity">Aktivität</string>
5621+
<string name="settings_favorites_sortorder">Nach Entfernung sortieren</string>
56215622
</resources>

OsmAnd/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5519,4 +5519,5 @@ Download tile maps directly, or copy them as SQLite database files to OsmAnd\'s
55195519
<string name="routing_attr_freeride_policy_name">Off-piste</string>
55205520
<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>
55215521
<string name="voice_prompts_timetable">Voice prompts times</string>
5522+
<string name="settings_favorites_sortorder">Sort by distance</string>
55225523
</resources>

OsmAnd/src/net/osmand/plus/auto/screens/FavoritesScreen.java

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import java.util.ArrayList;
4646
import java.util.Collections;
47+
import java.util.Comparator;
4748
import java.util.List;
4849

4950
/**
@@ -58,6 +59,7 @@ public final class FavoritesScreen extends BaseAndroidAutoScreen {
5859
@Nullable
5960
private FavoriteGroup selectedGroup;
6061
private CompassMode initialCompassMode;
62+
private boolean isSortableByDistance;
6163

6264
public FavoritesScreen(
6365
@NonNull CarContext carContext,
@@ -66,6 +68,7 @@ public FavoritesScreen(
6668
super(carContext);
6769
this.settingsAction = settingsAction;
6870
selectedGroup = group;
71+
isSortableByDistance = group != null;
6972
getLifecycle().addObserver(new DefaultLifecycleObserver() {
7073
@Override
7174
public void onDestroy(@NonNull LifecycleOwner owner) {
@@ -109,18 +112,19 @@ protected int getConstraintLimitType() {
109112
}
110113

111114
private void setupFavorites(ItemList.Builder listBuilder) {
115+
OsmandSettings settings = getApp().getSettings();
116+
List<FavouritePoint> favoritePoints = getFavorites();
117+
int limitedSize = Math.min(favoritePoints.size(), getContentLimit() -1);
112118
LatLon location = getApp().getMapViewTrackingUtilities().getDefaultLocation();
113-
List<FavouritePoint> favoritesPoints = getFavorites();
114-
int favoritesPointsSize = favoritesPoints.size();
115-
List<FavouritePoint> limitedFavoritesPoints = favoritesPoints.subList(0, Math.min(favoritesPointsSize, getContentLimit() - 1));
116-
getApp().getOsmandMap().getMapLayers().getFavouritesLayer().setCustomMapObjects(limitedFavoritesPoints);
119+
List<FavoritePointDistance> limitedFavoritePointDistances = toLimitedSortedPointDistanceList(favoritePoints, location, isSortableByDistance && settings.SORT_FAV_BY_DISTANCE.get(), limitedSize);
120+
List<FavouritePoint> limitedFavoritePoints = new ArrayList<>(limitedSize);
117121
QuadRect mapRect = new QuadRect();
118-
if (!Algorithms.isEmpty(limitedFavoritesPoints)) {
119-
OsmandSettings settings = getApp().getSettings();
122+
if (!Algorithms.isEmpty(limitedFavoritePointDistances)) {
120123
initialCompassMode = settings.getCompassMode();
121124
getApp().getMapViewTrackingUtilities().switchCompassModeTo(CompassMode.NORTH_IS_UP);
122125
}
123-
for (FavouritePoint point : limitedFavoritesPoints) {
126+
for (FavoritePointDistance favoritePointDistance : limitedFavoritePointDistances) {
127+
FavouritePoint point = favoritePointDistance.favorite;
124128
double longitude = point.getLongitude();
125129
double latitude = point.getLatitude();
126130
Algorithms.extendRectToContainPoint(mapRect, longitude, latitude);
@@ -129,10 +133,8 @@ private void setupFavorites(ItemList.Builder listBuilder) {
129133
CarIcon icon = new CarIcon.Builder(IconCompat.createWithBitmap(
130134
AndroidUtils.drawableToBitmap(PointImageDrawable.getFromFavorite(getApp(), color, false, point)))).build();
131135
String description = point.getSpecialPointType() != null ? point.getDescription() : point.getCategory();
132-
double dist = MapUtils.getDistance(point.getLatitude(), point.getLongitude(),
133-
location.getLatitude(), location.getLongitude());
134136
SpannableString address = new SpannableString(Algorithms.isEmpty(description) ? " " : " • " + description);
135-
DistanceSpan distanceSpan = DistanceSpan.create(TripHelper.getDistance(getApp(), dist));
137+
DistanceSpan distanceSpan = DistanceSpan.create(TripHelper.getDistance(getApp(), favoritePointDistance.distance));
136138
address.setSpan(distanceSpan, 0, 1, SPAN_INCLUSIVE_INCLUSIVE);
137139
listBuilder.addItem(new Row.Builder()
138140
.setTitle(title)
@@ -142,10 +144,40 @@ private void setupFavorites(ItemList.Builder listBuilder) {
142144
.setMetadata(new Metadata.Builder().setPlace(new Place.Builder(
143145
CarLocation.create(point.getLatitude(), point.getLongitude())).build()).build())
144146
.build());
147+
limitedFavoritePoints.add(point);
145148
}
149+
getApp().getOsmandMap().getMapLayers().getFavouritesLayer().setCustomMapObjects(limitedFavoritePoints);
146150
adjustMapToRect(location, mapRect);
147151
}
148152

153+
private static class FavoritePointDistance {
154+
private final FavouritePoint favorite;
155+
private final double distance;
156+
FavoritePointDistance(FavouritePoint favorite, double dist) {
157+
this.favorite = favorite;
158+
this.distance = dist;
159+
}
160+
}
161+
162+
private static List<FavoritePointDistance> toPointDistanceList(List<FavouritePoint> points, LatLon location) {
163+
List<FavoritePointDistance> returnList = new ArrayList<>(points.size());
164+
for (FavouritePoint point : points) {
165+
returnList.add(new FavoritePointDistance(point, MapUtils.getDistance(point.getLatitude(), point.getLongitude(), location.getLatitude(), location.getLongitude())));
166+
}
167+
return returnList;
168+
};
169+
170+
private static List<FavoritePointDistance> toLimitedSortedPointDistanceList(List<FavouritePoint> points, LatLon location, boolean sortByDistance, int limitedSize) {
171+
if (sortByDistance) {
172+
List<FavoritePointDistance> pointDistances = toPointDistanceList(points, location);
173+
Collections.sort(pointDistances, Comparator.comparingDouble(pointDistance -> pointDistance.distance));
174+
return pointDistances.subList(0, limitedSize);
175+
} else {
176+
Collections.sort(points, (left, right) -> Long.compare(right.getTimestamp(), left.getTimestamp()));
177+
return toPointDistanceList(points.subList(0, limitedSize), location);
178+
}
179+
};
180+
149181
private void onClickFavorite(@NonNull FavouritePoint point) {
150182
SearchResult result = new SearchResult();
151183
result.location = new LatLon(point.getLatitude(), point.getLongitude());
@@ -163,7 +195,6 @@ private List<FavouritePoint> getFavorites() {
163195
} else {
164196
filteredFavorites.addAll(selectedGroup.getPoints());
165197
}
166-
Collections.sort(filteredFavorites, (left, right) -> Long.compare(right.getTimestamp(), left.getTimestamp()));
167198
return filteredFavorites;
168199
}
169200

OsmAnd/src/net/osmand/plus/auto/screens/SettingsScreen.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public Template onGetTemplate() {
5151
sectionABuilder.build(),
5252
getCarContext().getString(R.string.voice_pref_title)));
5353

54+
ItemList.Builder sectionBBuilder = new ItemList.Builder();
55+
sectionBBuilder.addItem(new Row.Builder()
56+
.setTitle(getCarContext().getString(R.string.settings_favorites_sortorder))
57+
.setToggle(
58+
new Toggle.Builder(
59+
(value) -> osmandSettings.SORT_FAV_BY_DISTANCE.set(value))
60+
.setChecked(osmandSettings.SORT_FAV_BY_DISTANCE.get())
61+
.build())
62+
.build()
63+
);
64+
65+
templateBuilder.addSectionedList(
66+
SectionedItemList.create(
67+
sectionBBuilder.build(),
68+
getCarContext().getString(R.string.shared_string_favorites)));
69+
5470
/*
5571
ItemList.Builder sectionBBuilder = new ItemList.Builder();
5672
sectionBBuilder.addItem(

OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,7 @@ protected boolean setValue(Object prefs, Boolean val) {
15811581
public final CommonPreference<Boolean> CURRENT_TRACK_SHOW_START_FINISH = new BooleanPreference(this, "current_track_show_start_finish", true).makeGlobal().makeShared().cache();
15821582
public final ListStringPreference CUSTOM_TRACK_COLORS = (ListStringPreference) new ListStringPreference(this, "custom_track_colors", null, ",").makeShared().makeGlobal();
15831583
public final ListStringPreference LAST_USED_FAV_ICONS = (ListStringPreference) new ListStringPreference(this, "last_used_favorite_icons", null, ",").makeShared().makeGlobal();
1584+
public final CommonPreference<Boolean> SORT_FAV_BY_DISTANCE = new BooleanPreference(this, "sort_fav_by_distance", false).makeGlobal().makeShared();
15841585

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

0 commit comments

Comments
 (0)