Skip to content

Commit

Permalink
New widget (#21476)
Browse files Browse the repository at this point in the history
* New widget

* Set voice navigation

* Fix destination shields creating

* Simplify

* Formatting

* Destination ref vs exit ref
  • Loading branch information
ivanPyrohivskyi authored Nov 29, 2024
1 parent 325fa37 commit 5b14c7c
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,15 @@ public String getDestinationRef(String lang, boolean transliterate, boolean dire
int k = kt[i];
if (region.routeEncodingRules.size() > k) {
if (refTag.equals(region.routeEncodingRules.get(k).getTag())) {
return names.get(k);
return Algorithms.splitAndClearRepeats(names.get(k), ";");
}
if (refTagDefault.equals(region.routeEncodingRules.get(k).getTag())) {
refDefault = names.get(k);
}
}
}
if (refDefault != null) {
return refDefault;
return Algorithms.splitAndClearRepeats(refDefault, ";");
}
//return names.get(region.refTypeRule);
}
Expand Down
4 changes: 4 additions & 0 deletions OsmAnd-java/src/main/java/net/osmand/router/ExitInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public String getExitStreetName() {
public void setExitStreetName(String exitStreetName) {
this.exitStreetName = exitStreetName;
}

public boolean isEmpty() {
return ref == null && exitStreetName == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ public void addTurnInfoDescriptions(List<RouteSegmentResult> result) {
if (!Algorithms.isEmpty(nm) || !Algorithms.isEmpty(ref)) {
streetName = String.format("onto %s %s " , nm, ref);
}
String to = result.get(prevSegment + 1).getDestinationName("", false, result, prevSegment + 1);
String to = result.get(prevSegment + 1).getDestinationName("", false, result, prevSegment + 1, true);
if(!Algorithms.isEmpty(to)) {
streetName = "to " + to;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ public String toString() {
return object.toString() + ": " + startPointIndex + "-" + endPointIndex;
}

public String getDestinationName(String lang, boolean transliterate, List<RouteSegmentResult> list, int routeInd) {
public String getDestinationName(String lang, boolean transliterate, List<RouteSegmentResult> list, int routeInd, boolean withRef) {
String dnRef = getObject().getDestinationRef(lang, transliterate, isForwardDirection());
String destinationName = getObject().getDestinationName(lang, transliterate, isForwardDirection());
if (Algorithms.isEmpty(destinationName)) {
Expand All @@ -678,10 +678,12 @@ public String getDestinationName(String lang, boolean transliterate, List<RouteS
}
}
}
if (!Algorithms.isEmpty(dnRef) && !Algorithms.isEmpty(destinationName)) {
destinationName = dnRef + ", " + destinationName;
} else if (!Algorithms.isEmpty(dnRef) && Algorithms.isEmpty(destinationName)) {
destinationName = dnRef;
if (withRef) {
if (!Algorithms.isEmpty(dnRef) && !Algorithms.isEmpty(destinationName)) {
destinationName = dnRef + ", " + destinationName;
} else if (!Algorithms.isEmpty(dnRef) && Algorithms.isEmpty(destinationName)) {
destinationName = dnRef;
}
}
return destinationName;
}
Expand Down
16 changes: 16 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 @@ -1349,4 +1349,20 @@ public static String[] deserializeStringArray(String serialized, String delimite
return resultList.toArray(new String[resultList.size()]);
}

public static String splitAndClearRepeats(String ref, String symbol) {
String[] arr = ref.split(symbol);
String res = "";
String prev = "";
for (String s : arr) {
if (Algorithms.isEmpty(s) || prev.equals(s))
continue;
if (!res.isEmpty()) {
res += symbol;
}
res += s;
prev = s;
}
return res;
}

}
27 changes: 2 additions & 25 deletions OsmAnd/src/net/osmand/plus/auto/TripUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.routing.CurrentStreetName;
import net.osmand.plus.routing.RoadShield;
import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo;
import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.plus.routing.RoutingHelper;
Expand Down Expand Up @@ -226,30 +227,6 @@ public static String defineStreetName(@NonNull OsmandApplication app, @Nullable

@NonNull
public static CurrentStreetName getStreetName(@NonNull OsmandApplication app, @NonNull NextDirectionInfo info, @NonNull RouteDirectionInfo routeInfo) {
CurrentStreetName streetName = new CurrentStreetName(info);
if (Algorithms.isEmpty(streetName.text)) {
streetName.text = getTurnDescription(app, info, routeInfo);
}
return streetName;
}

@Nullable
private static String getTurnDescription(@NonNull OsmandApplication app, @NonNull NextDirectionInfo info, @NonNull RouteDirectionInfo routeInfo) {
String description = routeInfo.getRef();
if (Algorithms.isEmpty(description)) {
TurnType turnType = routeInfo.getTurnType();
NextDirectionInfo nextInfo = getNextDirectionInfoAfter(app, info);
TurnType nextTurnType = nextInfo != null && nextInfo.directionInfo != null ? nextInfo.directionInfo.getTurnType() : null;

description = turnType != null ? nextTurnsToString(app, turnType, nextTurnType) : null;
}
return description;
}

@Nullable
private static NextDirectionInfo getNextDirectionInfoAfter(@NonNull OsmandApplication app, @NonNull NextDirectionInfo info) {
RoutingHelper helper = app.getRoutingHelper();
boolean onRoute = !helper.isDeviatedFromRoute();
return onRoute ? helper.getNextRouteDirectionInfoAfter(info, new NextDirectionInfo(), true) : null;
return new CurrentStreetName(info, true);
}
}
20 changes: 18 additions & 2 deletions OsmAnd/src/net/osmand/plus/routing/CurrentStreetName.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ public class CurrentStreetName {
public List<RoadShield> shields = new ArrayList<>();
public String exitRef;

private boolean useDestination = false;

public CurrentStreetName() {
}

public CurrentStreetName(@NonNull NextDirectionInfo info) {
setupStreetName(info);
}

public CurrentStreetName(@NonNull NextDirectionInfo info, boolean useDestination) {
this.useDestination = useDestination;
setupStreetName(info);
}

public CurrentStreetName(@NonNull RoutingHelper routingHelper, @NonNull NextDirectionInfo info) {
setupCurrentName(routingHelper, info);
}
Expand All @@ -55,10 +62,12 @@ public void setupCurrentName(@NonNull RoutingHelper routingHelper, @NonNull Next
boolean isSet = false;
// 1. turn is imminent
if (info.distanceTo > 0 && adt.isTurnStateActive(adt.getSpeed(l), info.distanceTo * 1.3, STATE_PREPARE_TURN)) {
useDestination = true;
isSet = setupStreetName(info);
}
// 2. display current road street name
if (!isSet) {
useDestination = false;
RouteSegmentResult rs = routingHelper.getCurrentSegmentResult();
if (rs != null) {
text = getRouteSegmentStreetName(routingHelper, rs, false);
Expand Down Expand Up @@ -93,8 +102,15 @@ public boolean setupStreetName(@NonNull NextDirectionInfo info) {
isSet = !(Algorithms.isEmpty(name) && Algorithms.isEmpty(ref) && Algorithms.isEmpty(destinationName));

RouteDataObject dataObject = info.directionInfo.getRouteDataObject();
shields = RoadShield.create(dataObject);
text = RoutingHelperUtils.formatStreetName(name, ref, destinationName, "»", shields);
if (useDestination) {
shields = RoadShield.createDestination(dataObject, info.directionInfo);
} else {
shields = RoadShield.create(dataObject);
}
if (shields.isEmpty()) {
destinationName = info.directionInfo.getDestinationRefAndName();
}
text = RoutingHelperUtils.formatStreetName(name, ref, destinationName, "", shields);
turnType = info.directionInfo.getTurnType();
if (turnType == null) {
turnType = TurnType.valueOf(TurnType.C, false);
Expand Down
40 changes: 40 additions & 0 deletions OsmAnd/src/net/osmand/plus/routing/RoadShield.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import androidx.annotation.Nullable;

import net.osmand.binary.RouteDataObject;
import net.osmand.util.Algorithms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class RoadShield {
Expand Down Expand Up @@ -45,6 +49,42 @@ public static List<RoadShield> create(@Nullable RouteDataObject rdo) {
return shields;
}

@NonNull
public static List<RoadShield> createDestination(@Nullable RouteDataObject rdo, RouteDirectionInfo info) {
List<RoadShield> shields = create(rdo);
String destRef = info.getDestinationRef();
if (rdo != null && !Algorithms.isEmpty(destRef) && !shields.isEmpty()) {
String refs = Algorithms.splitAndClearRepeats(destRef, ";");
List<String> split = Arrays.asList(refs.split(";"));
Map<String, RoadShield> map = new HashMap<>();
String tag = null;
StringBuilder additional = new StringBuilder();
for (RoadShield s : shields) {
map.put(s.value, s);
if (split.contains(s.value)) {
tag = s.tag;
}
additional = s.additional;
}

shields.clear();
if (tag == null) {
return shields;
}
for (String s : split) {
RoadShield shield = map.get(s);
if (shield == null) {
shield = new RoadShield(rdo, tag, s);
shield.additional = additional;
}
shields.add(shield);
map.remove(s);
}
shields.addAll(map.values());
}
return shields;
}

public RouteDataObject getRdo() {
return rdo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ private static List<RouteSegmentResult> convertVectorResult(List<RouteDirectionI
String lang = ctx.getSettings().MAP_PREFERRED_LOCALE.get();
boolean transliterate = ctx.getSettings().MAP_TRANSLITERATE_NAMES.get();
info.setStreetName(current.getStreetName(lang, transliterate, list, lind));
info.setDestinationName(current.getDestinationName(lang, transliterate, list, lind));
info.setDestinationName(current.getDestinationName(lang, transliterate, list, lind, false));
info.setDestinationRef(current.getObject().getDestinationRef(lang, transliterate, current.isForwardDirection()));

RouteDataObject rdoWithShield = null;
RouteDataObject rdoWithoutShield = null;
Expand All @@ -458,7 +459,7 @@ private static List<RouteSegmentResult> convertVectorResult(List<RouteDirectionI
exitInfo.setRef(current.getObject().getExitRef());
exitInfo.setExitStreetName(current.getObject().getExitName());
info.setExitInfo(exitInfo);
if (routeInd > 0 && (exitInfo.getRef() != null || exitInfo.getExitStreetName() != null)) {
if (!exitInfo.isEmpty() && info.getDestinationRef() == null && routeInd > 0) {
// set ref and road name (or shield icon) from previous segment because exit point is not consist of highway ref
RouteSegmentResult previous;
previous = list.get(routeInd - 1);
Expand Down Expand Up @@ -492,7 +493,7 @@ private static List<RouteSegmentResult> convertVectorResult(List<RouteDirectionI
}

String description = toString(turn, ctx, false) + " " + RoutingHelperUtils.formatStreetName(info.getStreetName(),
info.getRef(), info.getDestinationName(), ctx.getString(R.string.towards));
info.getRef(), info.getDestinationRefAndName(), ctx.getString(R.string.towards));
description = description.trim();
String[] pointNames = s.getObject().getPointNames(s.getStartPointIndex());
if (pointNames != null) {
Expand Down
25 changes: 25 additions & 0 deletions OsmAnd/src/net/osmand/plus/routing/RouteDirectionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.osmand.plus.OsmandApplication;
import net.osmand.router.ExitInfo;
import net.osmand.router.TurnType;
import net.osmand.util.Algorithms;

public class RouteDirectionInfo {
// location when you should action (turn or go ahead)
Expand All @@ -26,6 +27,8 @@ public class RouteDirectionInfo {

private String destinationName;

private String destinationRef;

private RouteDataObject routeDataObject;

@Nullable
Expand Down Expand Up @@ -132,4 +135,26 @@ public ExitInfo getExitInfo() {
public void setExitInfo(@Nullable ExitInfo exitInfo) {
this.exitInfo = exitInfo;
}

public String getDestinationRef() {
return destinationRef;
}

public void setDestinationRef(String destinationRef) {
this.destinationRef = destinationRef;
}

public String getDestinationRefAndName() {
String[] destinations = {destinationRef, destinationName};
String res = "";
for (String s : destinations) {
if (!res.isEmpty()) {
res += ", ";
}
if (!Algorithms.isEmpty(s)) {
res += s;
}
}
return res;
}
}
4 changes: 2 additions & 2 deletions OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ private StreetName getSpeakableStreetName(RouteSegmentResult currentSegment, Rou
if (includeDest == true) {
result.put(TO_REF, getNonNullString(getSpeakablePointName(i.getRef())));
result.put(TO_STREET_NAME, getNonNullString(getSpeakablePointName(i.getStreetName())));
String dest = getSpeakablePointName(cutLongDestination(i.getDestinationName()));
String dest = getSpeakablePointName(cutLongDestination(i.getDestinationRefAndName()));
result.put(TO_DEST, getNonNullString(dest));
} else {
result.put(TO_REF, getNonNullString(getSpeakablePointName(i.getRef())));
Expand Down Expand Up @@ -634,7 +634,7 @@ private StreetName getSpeakableExitName(RouteDirectionInfo routeInfo, ExitInfo e
return new StreetName(result);
}
result.put(TO_REF, getNonNullString(getSpeakablePointName(exitInfo.getRef())));
String dest = getSpeakablePointName(cutLongDestination(routeInfo.getDestinationName()));
String dest = getSpeakablePointName(cutLongDestination(routeInfo.getDestinationRefAndName()));
result.put(TO_DEST, getNonNullString(dest));
result.put(TO_STREET_NAME, "");
return new StreetName(result);
Expand Down

0 comments on commit 5b14c7c

Please sign in to comment.