use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.
the class TripHelper method buildTrip.
@NonNull
public Trip buildTrip(float density) {
RoutingHelper routingHelper = app.getRoutingHelper();
OsmandSettings settings = app.getSettings();
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
TargetPoint pointToNavigate = targetPointsHelper.getPointToNavigate();
Trip.Builder tripBuilder = new Trip.Builder();
if (pointToNavigate != null) {
Pair<Destination, TravelEstimate> dest = getDestination(pointToNavigate);
tripBuilder.addDestination(dest.first, dest.second);
lastDestination = dest.first;
lastDestinationTravelEstimate = dest.second;
} else {
lastDestination = null;
lastDestinationTravelEstimate = null;
}
boolean routeBeingCalculated = routingHelper.isRouteBeingCalculated();
tripBuilder.setLoading(routeBeingCalculated);
if (!routeBeingCalculated) {
Step.Builder stepBuilder = new Step.Builder();
Maneuver.Builder turnBuilder;
TurnType turnType = null;
boolean leftSide = settings.DRIVING_REGION.get().leftHandDriving;
boolean deviatedFromRoute = routingHelper.isDeviatedFromRoute();
int turnImminent = 0;
int nextTurnDistance = 0;
RouteCalculationResult.NextDirectionInfo nextDirInfo;
RouteCalculationResult.NextDirectionInfo calc = new RouteCalculationResult.NextDirectionInfo();
if (deviatedFromRoute) {
turnType = TurnType.valueOf(TurnType.OFFR, leftSide);
nextTurnDistance = (int) routingHelper.getRouteDeviation();
} else {
nextDirInfo = routingHelper.getNextRouteDirectionInfo(calc, true);
if (nextDirInfo != null && nextDirInfo.distanceTo > 0 && nextDirInfo.directionInfo != null) {
turnType = nextDirInfo.directionInfo.getTurnType();
nextTurnDistance = nextDirInfo.distanceTo;
turnImminent = nextDirInfo.imminent;
}
}
if (turnType != null) {
TurnDrawable drawable = new TurnDrawable(app, false);
int height = (int) (TURN_IMAGE_SIZE_DP * density);
int width = (int) (TURN_IMAGE_SIZE_DP * density);
drawable.setBounds(0, 0, width, height);
drawable.setTurnType(turnType);
drawable.setTurnImminent(turnImminent, deviatedFromRoute);
Bitmap turnBitmap = drawableToBitmap(drawable, width, height);
turnBuilder = new Maneuver.Builder(getManeuverType(turnType));
if (turnType.isRoundAbout()) {
turnBuilder.setRoundaboutExitNumber(turnType.getExitOut());
}
turnBuilder.setIcon(new CarIcon.Builder(IconCompat.createWithBitmap(turnBitmap)).build());
} else {
turnBuilder = new Maneuver.Builder(Maneuver.TYPE_UNKNOWN);
}
Maneuver maneuver = turnBuilder.build();
String cue = turnType != null ? RouteCalculationResult.toString(turnType, app, true) : "";
stepBuilder.setManeuver(maneuver);
stepBuilder.setCue(cue);
nextDirInfo = routingHelper.getNextRouteDirectionInfo(calc, false);
if (nextDirInfo != null && nextDirInfo.directionInfo != null && nextDirInfo.directionInfo.getTurnType() != null) {
int[] lanes = nextDirInfo.directionInfo.getTurnType().getLanes();
int locimminent = nextDirInfo.imminent;
// Do not show too far
if ((nextDirInfo.distanceTo > 800 && nextDirInfo.directionInfo.getTurnType().isSkipToSpeak()) || nextDirInfo.distanceTo > 1200) {
lanes = null;
}
// int dist = nextDirInfo.distanceTo;
if (lanes != null) {
for (int lane : lanes) {
int firstTurnType = TurnType.getPrimaryTurn(lane);
int secondTurnType = TurnType.getSecondaryTurn(lane);
int thirdTurnType = TurnType.getTertiaryTurn(lane);
Lane.Builder laneBuilder = new Lane.Builder();
laneBuilder.addDirection(LaneDirection.create(getLaneDirection(TurnType.valueOf(firstTurnType, leftSide)), (lane & 1) == 1));
if (secondTurnType > 0) {
laneBuilder.addDirection(LaneDirection.create(getLaneDirection(TurnType.valueOf(secondTurnType, leftSide)), false));
}
if (thirdTurnType > 0) {
laneBuilder.addDirection(LaneDirection.create(getLaneDirection(TurnType.valueOf(thirdTurnType, leftSide)), false));
}
stepBuilder.addLane(laneBuilder.build());
}
LanesDrawable lanesDrawable = new LanesDrawable(app, 1f, TURN_LANE_IMAGE_SIZE * density, TURN_LANE_IMAGE_MIN_DELTA * density, TURN_LANE_IMAGE_MARGIN * density, TURN_LANE_IMAGE_SIZE * density);
lanesDrawable.lanes = lanes;
lanesDrawable.imminent = locimminent == 0;
lanesDrawable.isNightMode = app.getDaynightHelper().isNightMode();
// prefer 500 x 74 dp
lanesDrawable.updateBounds();
Bitmap lanesBitmap = drawableToBitmap(lanesDrawable, lanesDrawable.getIntrinsicWidth(), lanesDrawable.getIntrinsicHeight());
stepBuilder.setLanesImage(new CarIcon.Builder(IconCompat.createWithBitmap(lanesBitmap)).build());
}
}
int leftTurnTimeSec = routingHelper.getLeftTimeNextTurn();
long turnArrivalTime = System.currentTimeMillis() + leftTurnTimeSec * 1000L;
Distance stepDistance = getDistance(app, nextTurnDistance);
DateTimeWithZone stepDateTime = DateTimeWithZone.create(turnArrivalTime, TimeZone.getDefault());
TravelEstimate.Builder stepTravelEstimateBuilder = new TravelEstimate.Builder(stepDistance, stepDateTime);
stepTravelEstimateBuilder.setRemainingTimeSeconds(leftTurnTimeSec);
Step step = stepBuilder.build();
TravelEstimate stepTravelEstimate = stepTravelEstimateBuilder.build();
tripBuilder.addStep(step, stepTravelEstimate);
lastStep = step;
lastStepTravelEstimate = stepTravelEstimate;
if (!deviatedFromRoute) {
nextDirInfo = routingHelper.getNextRouteDirectionInfo(calc, true);
CurrentStreetName currentName = routingHelper.getCurrentName(nextDirInfo);
tripBuilder.setCurrentRoad(currentName.text);
lastCurrentRoad = currentName.text;
} else {
lastCurrentRoad = null;
}
} else {
lastStep = null;
lastStepTravelEstimate = null;
}
return tripBuilder.build();
}
use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.
the class TripHelper method getDestination.
@NonNull
public Pair<Destination, TravelEstimate> getDestination(@NonNull TargetPoint pointToNavigate) {
RoutingHelper routingHelper = app.getRoutingHelper();
Destination.Builder destBuilder = new Destination.Builder();
String name = pointToNavigate.getOnlyName();
if (Algorithms.isEmpty(name)) {
name = app.getString(R.string.route_descr_destination);
}
destBuilder.setName(name);
destBuilder.setImage(new CarIcon.Builder(IconCompat.createWithResource(app, R.drawable.ic_action_point_destination)).build());
Distance distance = getDistance(app, routingHelper.getLeftDistance());
int leftTimeSec = routingHelper.getLeftTime();
DateTimeWithZone dateTime = DateTimeWithZone.create(System.currentTimeMillis() + leftTimeSec * 1000L, TimeZone.getDefault());
TravelEstimate.Builder travelEstimateBuilder = new TravelEstimate.Builder(distance, dateTime);
travelEstimateBuilder.setRemainingTimeSeconds(leftTimeSec);
Destination destination = destBuilder.build();
TravelEstimate travelEstimate = travelEstimateBuilder.build();
return new Pair<>(destination, travelEstimate);
}
use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.
the class WaypointDialogHelper method switchStartAndFinish.
public static void switchStartAndFinish(OsmandApplication app, Activity ctx, WaypointDialogHelper helper, boolean updateRoute) {
TargetPointsHelper targetsHelper = app.getTargetPointsHelper();
TargetPoint finish = targetsHelper.getPointToNavigate();
TargetPoint start = targetsHelper.getPointToStart();
if (finish == null) {
app.showShortToastMessage(R.string.mark_final_location_first);
} else {
switchStartAndFinish(app, start, finish, updateRoute);
updateControls(ctx, helper);
}
}
use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.
the class WaypointDialogHelper method updatePointInfoView.
public static void updatePointInfoView(final OsmandApplication app, final Activity activity, View localView, final LocationPointWrapper ps, final boolean mapCenter, final boolean nightMode, final boolean edit, final boolean topBar) {
WaypointHelper wh = app.getWaypointHelper();
final LocationPoint point = ps.getPoint();
TextView text = localView.findViewById(R.id.waypoint_text);
if (!topBar) {
AndroidUtils.setTextPrimaryColor(activity, text, nightMode);
}
TextView textShadow = localView.findViewById(R.id.waypoint_text_shadow);
if (!edit) {
localView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showOnMap(app, activity, point, mapCenter);
}
});
}
TextView textDist = localView.findViewById(R.id.waypoint_dist);
((ImageView) localView.findViewById(R.id.waypoint_icon)).setImageDrawable(ps.getDrawable(activity, app, nightMode));
int dist = -1;
boolean startPoint = ps.type == WaypointHelper.TARGETS && ((TargetPoint) ps.point).start;
if (!startPoint) {
if (!wh.isRouteCalculated()) {
if (activity instanceof MapActivity) {
dist = (int) MapUtils.getDistance(((MapActivity) activity).getMapView().getLatitude(), ((MapActivity) activity).getMapView().getLongitude(), point.getLatitude(), point.getLongitude());
}
} else {
dist = wh.getRouteDistance(ps);
}
}
if (dist > 0) {
textDist.setText(OsmAndFormatter.getFormattedDistance(dist, app));
} else {
textDist.setText("");
}
TextView textDeviation = localView.findViewById(R.id.waypoint_deviation);
if (textDeviation != null) {
if (dist > 0 && ps.deviationDistance > 0) {
String devStr = "+" + OsmAndFormatter.getFormattedDistance(ps.deviationDistance, app);
textDeviation.setText(devStr);
if (!topBar) {
int colorId = ColorUtilities.getSecondaryTextColorId(nightMode);
AndroidUtils.setTextSecondaryColor(activity, textDeviation, nightMode);
if (ps.deviationDirectionRight) {
textDeviation.setCompoundDrawablesWithIntrinsicBounds(app.getUIUtilities().getIcon(R.drawable.ic_small_turn_right, colorId), null, null, null);
} else {
textDeviation.setCompoundDrawablesWithIntrinsicBounds(app.getUIUtilities().getIcon(R.drawable.ic_small_turn_left, colorId), null, null, null);
}
}
textDeviation.setVisibility(View.VISIBLE);
} else {
textDeviation.setText("");
textDeviation.setVisibility(View.GONE);
}
}
String descr;
PointDescription pd = point.getPointDescription(app);
if (Algorithms.isEmpty(pd.getName())) {
descr = pd.getTypeName();
} else {
descr = pd.getName();
}
if (textShadow != null) {
textShadow.setText(descr);
}
text.setText(descr);
String pointDescription = "";
TextView descText = localView.findViewById(R.id.waypoint_desc_text);
if (descText != null) {
AndroidUtils.setTextSecondaryColor(activity, descText, nightMode);
switch(ps.type) {
case WaypointHelper.TARGETS:
TargetPoint targetPoint = (TargetPoint) ps.point;
if (targetPoint.start) {
pointDescription = activity.getResources().getString(R.string.starting_point);
} else {
pointDescription = targetPoint.getPointDescription(activity).getTypeName();
}
break;
case WaypointHelper.FAVORITES:
FavouritePoint favPoint = (FavouritePoint) ps.point;
pointDescription = Algorithms.isEmpty(favPoint.getCategory()) ? activity.getResources().getString(R.string.shared_string_favorites) : favPoint.getCategory();
break;
}
}
if (Algorithms.objectEquals(descr, pointDescription)) {
pointDescription = "";
}
if (dist > 0 && !Algorithms.isEmpty(pointDescription)) {
pointDescription = " • " + pointDescription;
}
if (descText != null) {
descText.setText(pointDescription);
}
}
use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.
the class WaypointDialogHelper method sortAllTargets.
@SuppressLint("StaticFieldLeak")
public static void sortAllTargets(final OsmandApplication app, final Activity activity, final WaypointDialogHelper helper) {
new AsyncTask<Void, Void, int[]>() {
ProgressDialog dlg = null;
long startDialogTime = 0;
List<TargetPoint> intermediates;
protected void onPreExecute() {
startDialogTime = System.currentTimeMillis();
dlg = new ProgressDialog(activity);
dlg.setTitle("");
dlg.setMessage(activity.getResources().getString(R.string.intermediate_items_sort_by_distance));
dlg.show();
}
protected int[] doInBackground(Void[] params) {
TargetPointsHelper targets = app.getTargetPointsHelper();
intermediates = targets.getIntermediatePointsWithTarget();
Location cll = app.getLocationProvider().getLastKnownLocation();
ArrayList<TargetPoint> lt = new ArrayList<>(intermediates);
TargetPoint start;
if (cll != null) {
LatLon ll = new LatLon(cll.getLatitude(), cll.getLongitude());
start = TargetPoint.create(ll, null);
} else if (app.getTargetPointsHelper().getPointToStart() != null) {
TargetPoint ps = app.getTargetPointsHelper().getPointToStart();
LatLon ll = new LatLon(ps.getLatitude(), ps.getLongitude());
start = TargetPoint.create(ll, null);
} else {
start = lt.get(0);
}
TargetPoint end = lt.remove(lt.size() - 1);
ArrayList<LatLon> al = new ArrayList<>();
for (TargetPoint p : lt) {
al.add(p.point);
}
try {
return new TspAnt().readGraph(al, start.point, end.point).solve();
} catch (Exception e) {
return null;
}
}
protected void onPostExecute(int[] result) {
if (dlg != null) {
long t = System.currentTimeMillis();
if (t - startDialogTime < 500) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
dlg.dismiss();
}
}, 500 - (t - startDialogTime));
} else {
dlg.dismiss();
}
}
if (result == null) {
return;
}
List<TargetPoint> alocs = new ArrayList<>();
for (int i : result) {
if (i > 0) {
TargetPoint loc = intermediates.get(i - 1);
alocs.add(loc);
}
}
intermediates.clear();
intermediates.addAll(alocs);
TargetPointsHelper targets = app.getTargetPointsHelper();
List<TargetPoint> cur = targets.getIntermediatePointsWithTarget();
boolean eq = true;
for (int j = 0; j < cur.size() && j < intermediates.size(); j++) {
if (cur.get(j) != intermediates.get(j)) {
eq = false;
break;
}
}
if (!eq) {
targets.reorderAllTargetPoints(intermediates, true);
}
if (!helper.helperCallbacks.isEmpty()) {
for (WaypointDialogHelperCallback callback : helper.helperCallbacks) {
callback.reloadAdapter();
}
}
updateRouteInfoMenu(activity);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations