Search in sources :

Example 1 with TurnDrawable

use of net.osmand.plus.views.mapwidgets.TurnDrawable 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 TurnDrawable

use of net.osmand.plus.views.mapwidgets.TurnDrawable in project Osmand by osmandapp.

the class NavigationNotification method buildNotification.

@Override
public Builder buildNotification(boolean wearable) {
    if (!isEnabled()) {
        return null;
    }
    NavigationService service = app.getNavigationService();
    String notificationTitle;
    StringBuilder notificationText = new StringBuilder();
    color = 0;
    icon = R.drawable.ic_notification_start_navigation;
    Bitmap turnBitmap = null;
    ongoing = true;
    RoutingHelper routingHelper = app.getRoutingHelper();
    if (service != null && (service.getUsedBy() & USED_BY_NAVIGATION) != 0) {
        color = app.getResources().getColor(R.color.osmand_orange);
        String distanceStr = OsmAndFormatter.getFormattedDistance(routingHelper.getLeftDistance(), app);
        String timeStr = OsmAndFormatter.getFormattedDuration(routingHelper.getLeftTime(), app);
        String etaStr = SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(new Date(System.currentTimeMillis() + routingHelper.getLeftTime() * 1000L));
        String speedStr = null;
        Location location = getLastKnownLocation();
        if (location != null && location.hasSpeed()) {
            speedStr = OsmAndFormatter.getFormattedSpeed(location.getSpeed(), app);
        }
        TurnType turnType = null;
        boolean deviatedFromRoute;
        int turnImminent = 0;
        int nextTurnDistance = 0;
        int nextNextTurnDistance = 0;
        RouteDirectionInfo ri = null;
        if (routingHelper.isRouteCalculated() && routingHelper.isFollowingMode()) {
            deviatedFromRoute = routingHelper.isDeviatedFromRoute();
            if (deviatedFromRoute) {
                turnImminent = 0;
                turnType = TurnType.valueOf(TurnType.OFFR, leftSide);
                nextTurnDistance = (int) routingHelper.getRouteDeviation();
            } else {
                NextDirectionInfo calc1 = new NextDirectionInfo();
                NextDirectionInfo r = routingHelper.getNextRouteDirectionInfo(calc1, true);
                if (r != null && r.distanceTo > 0 && r.directionInfo != null) {
                    ri = r.directionInfo;
                    turnType = r.directionInfo.getTurnType();
                    nextTurnDistance = r.distanceTo;
                    turnImminent = r.imminent;
                    NextDirectionInfo next = routingHelper.getNextRouteDirectionInfoAfter(r, calc1, true);
                    nextNextTurnDistance = next.distanceTo;
                }
            }
            if (turnType != null) {
                TurnDrawable drawable = new TurnDrawable(app, false);
                int height = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_height);
                int width = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_width);
                drawable.setBounds(0, 0, width, height);
                drawable.setTurnType(turnType);
                drawable.setTurnImminent(turnImminent, deviatedFromRoute);
                turnBitmap = drawableToBitmap(drawable);
            }
            notificationTitle = OsmAndFormatter.getFormattedDistance(nextTurnDistance, app) + (turnType != null ? " • " + RouteCalculationResult.toString(turnType, app, true) : "");
            if (ri != null && !Algorithms.isEmpty(ri.getDescriptionRoutePart())) {
                notificationText.append(ri.getDescriptionRoutePart());
                if (nextNextTurnDistance > 0) {
                    notificationText.append(" ").append(OsmAndFormatter.getFormattedDistance(nextNextTurnDistance, app));
                }
                notificationText.append("\n");
            }
            int distanceToNextIntermediate = routingHelper.getLeftDistanceNextIntermediate();
            if (distanceToNextIntermediate > 0) {
                int nextIntermediateIndex = routingHelper.getRoute().getNextIntermediate();
                List<TargetPoint> intermediatePoints = app.getTargetPointsHelper().getIntermediatePoints();
                if (nextIntermediateIndex < intermediatePoints.size()) {
                    TargetPoint nextIntermediate = intermediatePoints.get(nextIntermediateIndex);
                    notificationText.append(OsmAndFormatter.getFormattedDistance(distanceToNextIntermediate, app)).append(" • ").append(nextIntermediate.getOnlyName());
                    notificationText.append("\n");
                }
            }
            notificationText.append(distanceStr).append(" • ").append(timeStr).append(" • ").append(etaStr);
            if (speedStr != null) {
                notificationText.append(" • ").append(speedStr);
            }
        } else {
            notificationTitle = app.getString(R.string.shared_string_navigation);
            String error = routingHelper.getLastRouteCalcErrorShort();
            if (Algorithms.isEmpty(error)) {
                notificationText.append(app.getString(R.string.route_calculation)).append("...");
            } else {
                notificationText.append(error);
            }
        }
    } else if (routingHelper.isRoutePlanningMode() && routingHelper.isPauseNavigation()) {
        ongoing = false;
        notificationTitle = app.getString(R.string.shared_string_navigation);
        notificationText.append(app.getString(R.string.shared_string_paused));
    } else {
        return null;
    }
    final Builder notificationBuilder = createBuilder(wearable).setContentTitle(notificationTitle).setCategory(NotificationCompat.CATEGORY_NAVIGATION).setStyle(new BigTextStyle().bigText(notificationText)).setLargeIcon(turnBitmap);
    NavigationSession carNavigationSession = app.getCarNavigationSession();
    if (carNavigationSession != null) {
        Intent intent = new Intent(Intent.ACTION_VIEW).setComponent(new ComponentName(app, NavigationCarAppService.class)).setData(NavigationCarAppService.createDeepLinkUri(DEEP_LINK_ACTION_OPEN_ROOT_SCREEN));
        notificationBuilder.extend(new CarAppExtender.Builder().setContentIntent(CarPendingIntent.getCarApp(app, intent.hashCode(), intent, 0)).build());
    }
    Intent stopIntent = new Intent(OSMAND_STOP_NAVIGATION_SERVICE_ACTION);
    PendingIntent stopPendingIntent = PendingIntent.getBroadcast(app, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.addAction(R.drawable.ic_notification_remove, app.getString(R.string.shared_string_control_stop), stopPendingIntent);
    if (routingHelper.isRouteCalculated() && routingHelper.isFollowingMode()) {
        Intent pauseIntent = new Intent(OSMAND_PAUSE_NAVIGATION_SERVICE_ACTION);
        PendingIntent pausePendingIntent = PendingIntent.getBroadcast(app, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.addAction(R.drawable.ic_notification_pause, app.getString(R.string.shared_string_pause), pausePendingIntent);
    } else if (routingHelper.isRouteCalculated() && routingHelper.isPauseNavigation()) {
        Intent resumeIntent = new Intent(OSMAND_RESUME_NAVIGATION_SERVICE_ACTION);
        PendingIntent resumePendingIntent = PendingIntent.getBroadcast(app, 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.addAction(R.drawable.ic_notification_play, app.getString(R.string.shared_string_resume), resumePendingIntent);
    }
    return notificationBuilder;
}
Also used : Builder(androidx.core.app.NotificationCompat.Builder) CarPendingIntent(androidx.car.app.notification.CarPendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RoutingHelper(net.osmand.plus.routing.RoutingHelper) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) TurnType(net.osmand.router.TurnType) Date(java.util.Date) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) NextDirectionInfo(net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo) Bitmap(android.graphics.Bitmap) NavigationService(net.osmand.plus.NavigationService) BigTextStyle(androidx.core.app.NotificationCompat.BigTextStyle) CarAppExtender(androidx.car.app.notification.CarAppExtender) RouteDirectionInfo(net.osmand.plus.routing.RouteDirectionInfo) TurnDrawable(net.osmand.plus.views.mapwidgets.TurnDrawable) ComponentName(android.content.ComponentName) CarPendingIntent(androidx.car.app.notification.CarPendingIntent) PendingIntent(android.app.PendingIntent) NavigationSession(net.osmand.plus.auto.NavigationSession) Location(net.osmand.Location)

Aggregations

Bitmap (android.graphics.Bitmap)2 TargetPoint (net.osmand.plus.helpers.TargetPointsHelper.TargetPoint)2 RoutingHelper (net.osmand.plus.routing.RoutingHelper)2 TurnDrawable (net.osmand.plus.views.mapwidgets.TurnDrawable)2 TurnType (net.osmand.router.TurnType)2 PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 NonNull (androidx.annotation.NonNull)1 DateTimeWithZone (androidx.car.app.model.DateTimeWithZone)1 Distance (androidx.car.app.model.Distance)1 Destination (androidx.car.app.navigation.model.Destination)1 Lane (androidx.car.app.navigation.model.Lane)1 Maneuver (androidx.car.app.navigation.model.Maneuver)1 Step (androidx.car.app.navigation.model.Step)1 TravelEstimate (androidx.car.app.navigation.model.TravelEstimate)1 Trip (androidx.car.app.navigation.model.Trip)1 CarAppExtender (androidx.car.app.notification.CarAppExtender)1 CarPendingIntent (androidx.car.app.notification.CarPendingIntent)1 BigTextStyle (androidx.core.app.NotificationCompat.BigTextStyle)1