Search in sources :

Example 1 with NavigationService

use of net.osmand.plus.NavigationService 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_action_start_navigation;
    turnBitmap = null;
    ongoing = true;
    RoutingHelper routingHelper = app.getRoutingHelper();
    boolean followingMode = routingHelper.isFollowingMode() || app.getLocationProvider().getLocationSimulation().isRouteAnimating();
    if (service != null && (service.getUsedBy() & USED_BY_NAVIGATION) != 0) {
        color = app.getResources().getColor(R.color.osmand_orange);
        String distanceStr = OsmAndFormatter.getFormattedDistance(app.getRoutingHelper().getLeftDistance(), app);
        String timeStr = OsmAndFormatter.getFormattedDuration(app.getRoutingHelper().getLeftTime(), app);
        String etaStr = SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(new Date(System.currentTimeMillis() + app.getRoutingHelper().getLeftTime() * 1000));
        TurnType turnType = null;
        boolean deviatedFromRoute;
        int turnImminent = 0;
        int nextTurnDistance = 0;
        int nextNextTurnDistance = 0;
        RouteDirectionInfo ri = null;
        if (routingHelper.isRouteCalculated() && followingMode) {
            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);
        } 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).setStyle(new BigTextStyle().bigText(notificationText)).setLargeIcon(turnBitmap);
    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_action_remove_dark, app.getString(R.string.shared_string_control_stop), stopPendingIntent);
    if (routingHelper.isRouteCalculated() && followingMode) {
        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_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_play_dark, app.getString(R.string.shared_string_continue), resumePendingIntent);
    }
    return notificationBuilder;
}
Also used : Builder(android.support.v4.app.NotificationCompat.Builder) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RoutingHelper(net.osmand.plus.routing.RoutingHelper) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) TurnType(net.osmand.router.TurnType) Date(java.util.Date) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) NextDirectionInfo(net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo) NavigationService(net.osmand.plus.NavigationService) BigTextStyle(android.support.v4.app.NotificationCompat.BigTextStyle) RouteDirectionInfo(net.osmand.plus.routing.RouteDirectionInfo) TurnDrawable(net.osmand.plus.views.mapwidgets.NextTurnInfoWidget.TurnDrawable) PendingIntent(android.app.PendingIntent)

Aggregations

PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 BigTextStyle (android.support.v4.app.NotificationCompat.BigTextStyle)1 Builder (android.support.v4.app.NotificationCompat.Builder)1 Date (java.util.Date)1 NavigationService (net.osmand.plus.NavigationService)1 TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)1 NextDirectionInfo (net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo)1 RouteDirectionInfo (net.osmand.plus.routing.RouteDirectionInfo)1 RoutingHelper (net.osmand.plus.routing.RoutingHelper)1 TurnDrawable (net.osmand.plus.views.mapwidgets.NextTurnInfoWidget.TurnDrawable)1 TurnType (net.osmand.router.TurnType)1