Search in sources :

Example 1 with NavigationSession

use of net.osmand.plus.auto.NavigationSession in project Osmand by osmandapp.

the class DayNightHelper method isNightModeForProfile.

public boolean isNightModeForProfile(ApplicationMode mode) {
    DayNightMode dayNightMode = app.getSettings().DAYNIGHT_MODE.getModeValue(mode);
    NavigationSession carNavigationSession = app.getCarNavigationSession();
    if (carNavigationSession != null && carNavigationSession.isStateAtLeast(State.CREATED)) {
        boolean carDarkMode = carNavigationSession.getCarContext().isDarkMode();
        dayNightMode = carDarkMode ? DayNightMode.NIGHT : DayNightMode.DAY;
    }
    if (dayNightMode.isDay()) {
        return false;
    } else if (dayNightMode.isNight()) {
        return true;
    } else if (dayNightMode.isAuto()) {
        // We are in auto mode!
        long currentTime = System.currentTimeMillis();
        // allow recalculation each 60 seconds
        if (currentTime - lastTime > 60000) {
            lastTime = System.currentTimeMillis();
            try {
                SunriseSunset daynightSwitch = getSunriseSunset();
                if (daynightSwitch != null) {
                    boolean daytime = daynightSwitch.isDaytime();
                    // $NON-NLS-1$
                    log.debug("Sunrise/sunset setting to day: " + daytime);
                    lastNightMode = !daytime;
                }
            } catch (IllegalArgumentException e) {
                // $NON-NLS-1$
                log.warn("Network location provider not available");
            } catch (SecurityException e) {
                // $NON-NLS-1$
                log.warn("Missing permissions to get actual location!");
            }
        }
        return lastNightMode;
    } else if (dayNightMode.isSensor()) {
        return lastNightMode;
    }
    return false;
}
Also used : SunriseSunset(net.osmand.util.SunriseSunset) DayNightMode(net.osmand.plus.settings.enums.DayNightMode) NavigationSession(net.osmand.plus.auto.NavigationSession)

Example 2 with NavigationSession

use of net.osmand.plus.auto.NavigationSession 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)

Example 3 with NavigationSession

use of net.osmand.plus.auto.NavigationSession in project Osmand by osmandapp.

the class OsmandMap method setupOpenGLView.

public void setupOpenGLView(boolean init) {
    OsmandMapTileView mapView = app.getOsmandMap().getMapView();
    for (OsmandMapListener listener : listeners) {
        listener.onSetupOpenGLView(init);
    }
    NavigationSession navigationSession = app.getCarNavigationSession();
    if (navigationSession != null) {
        navigationSession.setMapView(mapView);
        app.getMapViewTrackingUtilities().setMapView(mapView);
    } else if (mapView.getMapActivity() == null) {
        app.getMapViewTrackingUtilities().setMapView(null);
    }
}
Also used : NavigationSession(net.osmand.plus.auto.NavigationSession)

Example 4 with NavigationSession

use of net.osmand.plus.auto.NavigationSession in project Osmand by osmandapp.

the class InAppPurchaseHelper method refreshAndroidAuto.

private void refreshAndroidAuto() {
    NavigationSession carNavigationSession = ctx.getCarNavigationSession();
    if (carNavigationSession != null) {
        logDebug("Call Android Auto");
        carNavigationSession.onPurchaseDone();
    }
}
Also used : NavigationSession(net.osmand.plus.auto.NavigationSession)

Example 5 with NavigationSession

use of net.osmand.plus.auto.NavigationSession in project Osmand by osmandapp.

the class RoutingHelper method updateCurrentRouteStatus.

private boolean updateCurrentRouteStatus(Location currentLocation, double posTolerance) {
    List<Location> routeNodes = route.getImmutableAllLocations();
    int currentRoute = route.currentRoute;
    // 1. Try to proceed to next point using orthogonal distance (finding minimum orthogonal dist)
    while (currentRoute + 1 < routeNodes.size()) {
        double dist = currentLocation.distanceTo(routeNodes.get(currentRoute));
        if (currentRoute > 0) {
            dist = RoutingHelperUtils.getOrthogonalDistance(currentLocation, routeNodes.get(currentRoute - 1), routeNodes.get(currentRoute));
        }
        boolean processed = false;
        // if we are still too far try to proceed many points
        // if not then look ahead only 3 in order to catch sharp turns
        boolean longDistance = dist >= 250;
        int newCurrentRoute = RoutingHelperUtils.lookAheadFindMinOrthogonalDistance(currentLocation, routeNodes, currentRoute, longDistance ? 15 : 8);
        double newDist = RoutingHelperUtils.getOrthogonalDistance(currentLocation, routeNodes.get(newCurrentRoute), routeNodes.get(newCurrentRoute + 1));
        if (longDistance) {
            if (newDist < dist) {
                if (log.isDebugEnabled()) {
                    // $NON-NLS-1$//$NON-NLS-2$
                    log.debug("Processed by distance : (new) " + newDist + " (old) " + dist);
                }
                processed = true;
            }
        } else if (newDist < dist || newDist < posTolerance / 8) {
            // newDist < posTolerance / 8 - 4-8 m (avoid distance 0 till next turn)
            if (dist > posTolerance) {
                processed = true;
                if (log.isDebugEnabled()) {
                    // $NON-NLS-1$//$NON-NLS-2$
                    log.debug("Processed by distance : " + newDist + " " + dist);
                }
            } else {
                // but you have not yet turned (could be checked bearing)
                if (currentLocation.hasBearing() || lastFixedLocation != null) {
                    float bearingToRoute = currentLocation.bearingTo(routeNodes.get(currentRoute));
                    float bearingRouteNext = routeNodes.get(newCurrentRoute).bearingTo(routeNodes.get(newCurrentRoute + 1));
                    float bearingMotion = currentLocation.hasBearing() ? currentLocation.getBearing() : lastFixedLocation.bearingTo(currentLocation);
                    double diff = Math.abs(MapUtils.degreesDiff(bearingMotion, bearingToRoute));
                    double diffToNext = Math.abs(MapUtils.degreesDiff(bearingMotion, bearingRouteNext));
                    if (diff > diffToNext) {
                        if (log.isDebugEnabled()) {
                            log.debug("Processed point bearing deltas : " + diff + " " + diffToNext);
                        }
                        processed = true;
                    }
                }
            }
        }
        if (processed) {
            // that node already passed
            route.updateCurrentRoute(newCurrentRoute + 1);
            currentRoute = newCurrentRoute + 1;
            app.getNotificationHelper().refreshNotification(NotificationType.NAVIGATION);
            fireRoutingDataUpdateEvent();
        } else {
            break;
        }
    }
    // 2. check if intermediate found
    if (route.getIntermediatePointsToPass() > 0 && route.getDistanceToNextIntermediate(lastFixedLocation) < voiceRouter.getArrivalDistance() && !isRoutePlanningMode) {
        showMessage(app.getString(R.string.arrived_at_intermediate_point));
        route.passIntermediatePoint();
        TargetPointsHelper targets = app.getTargetPointsHelper();
        String name = "";
        if (intermediatePoints != null && !intermediatePoints.isEmpty()) {
            LatLon rm = intermediatePoints.remove(0);
            List<TargetPoint> ll = targets.getIntermediatePointsNavigation();
            int ind = -1;
            for (int i = 0; i < ll.size(); i++) {
                if (ll.get(i).point != null && MapUtils.getDistance(ll.get(i).point, rm) < 5) {
                    name = ll.get(i).getOnlyName();
                    ind = i;
                    break;
                }
            }
            if (ind >= 0) {
                targets.removeWayPoint(false, ind);
            }
        }
        if (isFollowingMode) {
            voiceRouter.arrivedIntermediatePoint(name);
        }
        // double check
        while (intermediatePoints != null && route.getIntermediatePointsToPass() < intermediatePoints.size()) {
            intermediatePoints.remove(0);
        }
    }
    // 3. check if destination found
    Location lastPoint = routeNodes.get(routeNodes.size() - 1);
    if (currentRoute > routeNodes.size() - 3 && currentLocation.distanceTo(lastPoint) < voiceRouter.getArrivalDistance() && !isRoutePlanningMode) {
        // showMessage(app.getString(R.string.arrived_at_destination));
        TargetPointsHelper targets = app.getTargetPointsHelper();
        TargetPoint tp = targets.getPointToNavigate();
        String description = tp == null ? "" : tp.getOnlyName();
        if (isFollowingMode) {
            voiceRouter.arrivedDestinationPoint(description);
        }
        boolean onDestinationReached = true;
        if (onDestinationReached) {
            clearCurrentRoute(null, null);
            setRoutePlanningMode(false);
            app.runInUIThread(new Runnable() {

                @Override
                public void run() {
                    settings.LAST_ROUTING_APPLICATION_MODE = settings.APPLICATION_MODE.get();
                // settings.setApplicationMode(settings.DEFAULT_APPLICATION_MODE.get());
                }
            });
            finishCurrentRoute();
            // targets.clearPointToNavigate(false);
            return true;
        }
    }
    // 4. update angle point
    if (route.getRouteVisibleAngle() > 0) {
        // proceed to the next point with min acceptable bearing
        double ANGLE_TO_DECLINE = route.getRouteVisibleAngle();
        int nextPoint = route.currentRoute;
        for (; nextPoint < routeNodes.size() - 1; nextPoint++) {
            float bearingTo = currentLocation.bearingTo(routeNodes.get(nextPoint));
            float bearingTo2 = routeNodes.get(nextPoint).bearingTo(routeNodes.get(nextPoint + 1));
            if (Math.abs(MapUtils.degreesDiff(bearingTo2, bearingTo)) <= ANGLE_TO_DECLINE) {
                break;
            }
        }
        if (nextPoint > 0) {
            Location next = routeNodes.get(nextPoint);
            Location prev = routeNodes.get(nextPoint - 1);
            float bearing = prev.bearingTo(next);
            double bearingTo = Math.abs(MapUtils.degreesDiff(bearing, currentLocation.bearingTo(next)));
            double bearingPrev = Math.abs(MapUtils.degreesDiff(bearing, currentLocation.bearingTo(prev)));
            while (true) {
                Location mp = MapUtils.calculateMidPoint(prev, next);
                if (mp.distanceTo(next) <= 100) {
                    break;
                }
                double bearingMid = Math.abs(MapUtils.degreesDiff(bearing, currentLocation.bearingTo(mp)));
                if (bearingPrev < ANGLE_TO_DECLINE) {
                    next = mp;
                    bearingTo = bearingMid;
                } else if (bearingTo < ANGLE_TO_DECLINE) {
                    prev = mp;
                    bearingPrev = bearingMid;
                } else {
                    break;
                }
            }
            route.updateNextVisiblePoint(nextPoint, next);
        }
    }
    // 5. Update car navigation
    NavigationSession carNavigationSession = app.getCarNavigationSession();
    NavigationService navigationService = app.getNavigationService();
    if (carNavigationSession != null && navigationService != null && System.currentTimeMillis() - lastCarNavUpdateTime > 1000) {
        lastCarNavUpdateTime = System.currentTimeMillis();
        app.runInUIThread(navigationService::updateCarNavigation);
    }
    return false;
}
Also used : TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) GpxPoint(net.osmand.router.RoutePlannerFrontEnd.GpxPoint) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) LatLon(net.osmand.data.LatLon) NavigationService(net.osmand.plus.NavigationService) TargetPointsHelper(net.osmand.plus.helpers.TargetPointsHelper) NavigationSession(net.osmand.plus.auto.NavigationSession) Location(net.osmand.Location)

Aggregations

NavigationSession (net.osmand.plus.auto.NavigationSession)16 RoutingHelper (net.osmand.plus.routing.RoutingHelper)3 Location (net.osmand.Location)2 NavigationService (net.osmand.plus.NavigationService)2 NavigationScreen (net.osmand.plus.auto.NavigationScreen)2 TargetPoint (net.osmand.plus.helpers.TargetPointsHelper.TargetPoint)2 Notification (android.app.Notification)1 PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 View (android.view.View)1 ViewStub (android.view.ViewStub)1 TextView (android.widget.TextView)1 Destination (androidx.car.app.navigation.model.Destination)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