Search in sources :

Example 1 with Maneuver

use of androidx.car.app.navigation.model.Maneuver 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();
}
Also used : Destination(androidx.car.app.navigation.model.Destination) Step(androidx.car.app.navigation.model.Step) Maneuver(androidx.car.app.navigation.model.Maneuver) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) Bitmap(android.graphics.Bitmap) RouteCalculationResult(net.osmand.plus.routing.RouteCalculationResult) DateTimeWithZone(androidx.car.app.model.DateTimeWithZone) TurnDrawable(net.osmand.plus.views.mapwidgets.TurnDrawable) TargetPointsHelper(net.osmand.plus.helpers.TargetPointsHelper) Distance(androidx.car.app.model.Distance) Trip(androidx.car.app.navigation.model.Trip) Lane(androidx.car.app.navigation.model.Lane) CurrentStreetName(net.osmand.plus.routing.CurrentStreetName) RoutingHelper(net.osmand.plus.routing.RoutingHelper) TurnType(net.osmand.router.TurnType) LanesDrawable(net.osmand.plus.views.mapwidgets.LanesDrawable) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) TravelEstimate(androidx.car.app.navigation.model.TravelEstimate) NonNull(androidx.annotation.NonNull)

Example 2 with Maneuver

use of androidx.car.app.navigation.model.Maneuver in project Osmand by osmandapp.

the class NavigationScreen method onGetTemplate.

@NonNull
@Override
public Template onGetTemplate() {
    NavigationTemplate.Builder builder = new NavigationTemplate.Builder();
    builder.setBackgroundColor(CarColor.SECONDARY);
    // Set the action strip.
    ActionStrip.Builder actionStripBuilder = new ActionStrip.Builder();
    updateCompass();
    actionStripBuilder.addAction(new Action.Builder().setIcon(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), compassResId)).build()).setOnClickListener(this::compassClick).build());
    actionStripBuilder.addAction(settingsAction);
    if (navigating) {
        actionStripBuilder.addAction(new Action.Builder().setTitle(getApp().getString(R.string.shared_string_control_stop)).setOnClickListener(this::stopNavigation).build());
    } else {
        actionStripBuilder.addAction(new Action.Builder().setIcon(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), R.drawable.ic_action_search_dark)).build()).setOnClickListener(this::openSearch).build());
        actionStripBuilder.addAction(new Action.Builder().setTitle(getApp().getString(R.string.shared_string_favorites)).setOnClickListener(this::openFavorites).build());
    }
    builder.setActionStrip(actionStripBuilder.build());
    // Set the map action strip with the pan and zoom buttons.
    // CarIcon.Builder panIconBuilder = new CarIcon.Builder(
    // IconCompat.createWithResource(getCarContext(), R.drawable.ic_action_item_move));
    // if (mIsInPanMode) {
    // panIconBuilder.setTint(CarColor.BLUE);
    // }
    builder.setMapActionStrip(new ActionStrip.Builder().addAction(new Action.Builder(Action.PAN).build()).addAction(new Action.Builder().setIcon(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), R.drawable.ic_my_location)).build()).setOnClickListener(() -> {
        if (!listener.requestLocationNavigation()) {
            surfaceRenderer.handleRecenter();
        }
    }).build()).addAction(new Action.Builder().setIcon(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), R.drawable.ic_zoom_in)).build()).setOnClickListener(() -> surfaceRenderer.handleScale(INVALID_FOCAL_POINT_VAL, INVALID_FOCAL_POINT_VAL, ZOOM_IN_BUTTON_SCALE_FACTOR)).build()).addAction(new Action.Builder().setIcon(new CarIcon.Builder(IconCompat.createWithResource(getCarContext(), R.drawable.ic_zoom_out)).build()).setOnClickListener(() -> surfaceRenderer.handleScale(INVALID_FOCAL_POINT_VAL, INVALID_FOCAL_POINT_VAL, ZOOM_OUT_BUTTON_SCALE_FACTOR)).build()).build());
    // When the user enters the pan mode, remind the user that they can exit the pan mode by
    // pressing the select button again.
    builder.setPanModeListener(isInPanMode -> {
        if (isInPanMode) {
            CarToast.makeText(getCarContext(), R.string.exit_pan_mode_descr, CarToast.LENGTH_LONG).show();
        }
        panMode = isInPanMode;
        invalidate();
    });
    if (navigating) {
        if (destinationTravelEstimate != null) {
            builder.setDestinationTravelEstimate(destinationTravelEstimate);
        }
        if (isRerouting()) {
            builder.setNavigationInfo(new RoutingInfo.Builder().setLoading(true).build());
        } else if (arrived) {
            MessageInfo messageInfo = new MessageInfo.Builder(getCarContext().getString(R.string.arrived_at_destination)).build();
            builder.setNavigationInfo(messageInfo);
        } else if (!Algorithms.isEmpty(steps)) {
            RoutingInfo.Builder info = new RoutingInfo.Builder();
            Step firstStep = steps.get(0);
            Step.Builder currentStep = new Step.Builder();
            CarText cue = firstStep.getCue();
            if (cue != null) {
                currentStep.setCue(cue.toCharSequence());
            }
            Maneuver maneuver = firstStep.getManeuver();
            if (maneuver != null) {
                currentStep.setManeuver(maneuver);
            }
            CarText road = firstStep.getRoad();
            if (road != null) {
                currentStep.setRoad(road.toCharSequence());
            }
            if (shouldShowLanes) {
                for (Lane lane : firstStep.getLanes()) {
                    currentStep.addLane(lane);
                }
                CarIcon lanesImage = firstStep.getLanesImage();
                if (lanesImage != null) {
                    currentStep.setLanesImage(lanesImage);
                }
            }
            if (stepRemainingDistance != null) {
                info.setCurrentStep(currentStep.build(), stepRemainingDistance);
                if (shouldShowNextStep && steps.size() > 1) {
                    info.setNextStep(steps.get(1));
                }
            }
            if (junctionImage != null) {
                info.setJunctionImage(junctionImage);
            }
            builder.setNavigationInfo(info.build());
        }
    }
    return builder.build();
}
Also used : Action(androidx.car.app.model.Action) NavigationTemplate(androidx.car.app.navigation.model.NavigationTemplate) ActionStrip(androidx.car.app.model.ActionStrip) Lane(androidx.car.app.navigation.model.Lane) Step(androidx.car.app.navigation.model.Step) Maneuver(androidx.car.app.navigation.model.Maneuver) RoutingInfo(androidx.car.app.navigation.model.RoutingInfo) MessageInfo(androidx.car.app.navigation.model.MessageInfo) CarText(androidx.car.app.model.CarText) CarIcon(androidx.car.app.model.CarIcon) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)2 Lane (androidx.car.app.navigation.model.Lane)2 Maneuver (androidx.car.app.navigation.model.Maneuver)2 Step (androidx.car.app.navigation.model.Step)2 Bitmap (android.graphics.Bitmap)1 Action (androidx.car.app.model.Action)1 ActionStrip (androidx.car.app.model.ActionStrip)1 CarIcon (androidx.car.app.model.CarIcon)1 CarText (androidx.car.app.model.CarText)1 DateTimeWithZone (androidx.car.app.model.DateTimeWithZone)1 Distance (androidx.car.app.model.Distance)1 Destination (androidx.car.app.navigation.model.Destination)1 MessageInfo (androidx.car.app.navigation.model.MessageInfo)1 NavigationTemplate (androidx.car.app.navigation.model.NavigationTemplate)1 RoutingInfo (androidx.car.app.navigation.model.RoutingInfo)1 TravelEstimate (androidx.car.app.navigation.model.TravelEstimate)1 Trip (androidx.car.app.navigation.model.Trip)1 TargetPointsHelper (net.osmand.plus.helpers.TargetPointsHelper)1 TargetPoint (net.osmand.plus.helpers.TargetPointsHelper.TargetPoint)1 CurrentStreetName (net.osmand.plus.routing.CurrentStreetName)1