Search in sources :

Example 11 with TargetPoint

use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint 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 12 with TargetPoint

use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class MapContextMenu method init.

public boolean init(@NonNull LatLon latLon, @Nullable PointDescription pointDescription, @Nullable Object object, boolean update, boolean restorePrevious) {
    MapActivity mapActivity = getMapActivity();
    if (mapActivity == null) {
        return false;
    }
    OsmandApplication app = mapActivity.getMyApplication();
    Object thisObject = getObject();
    if (!update && isVisible()) {
        if (thisObject == null || !thisObject.equals(object)) {
            hide();
        } else {
            return false;
        }
    }
    setSelectedObject(object);
    if (pointDescription == null) {
        this.pointDescription = new PointDescription(latLon.getLatitude(), latLon.getLongitude());
    } else {
        this.pointDescription = pointDescription;
    }
    MenuController menuController = getMenuController();
    boolean needAcquireMenuController = menuController == null || appModeChanged || !update || thisObject == null && object != null || thisObject != null && object == null || (thisObject != null && !thisObject.getClass().equals(object.getClass()));
    this.latLon = latLon;
    this.object = object;
    active = true;
    appModeChanged = false;
    if (needAcquireMenuController) {
        if (menuController != null) {
            menuController.setMapContextMenu(null);
        }
        if (!acquireMenuController(restorePrevious)) {
            active = false;
            clearSelectedObject(object);
            return false;
        }
        menuController = getMenuController();
    } else {
        menuController.update(pointDescription, object);
    }
    initTitle();
    if (menuController != null) {
        menuController.clearPlainMenuItems();
        menuController.addPlainMenuItems(typeStr, getPointDescription(), getLatLon());
    }
    if (mapPosition != 0) {
        mapActivity.getMapView().setMapPosition(0);
    }
    mapActivity.refreshMap();
    if (object instanceof MapMarker) {
        app.getMapMarkersHelper().addListener(this);
    } else if (object instanceof TargetPoint) {
        app.getTargetPointsHelper().addPointListener(this);
    }
    return true;
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) MapMarker(net.osmand.plus.mapmarkers.MapMarker) PointDescription(net.osmand.data.PointDescription) MapDataMenuController(net.osmand.plus.mapcontextmenu.controllers.MapDataMenuController) CallbackWithObject(net.osmand.CallbackWithObject) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) MapActivity(net.osmand.plus.activities.MapActivity)

Example 13 with TargetPoint

use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint 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)

Example 14 with TargetPoint

use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class NavigationHistorySettingsFragment method updateHistoryItems.

@Override
protected void updateHistoryItems() {
    clearItems();
    List<SearchResult> searchResults = getNavigationHistoryResults(app);
    sortSearchResults(searchResults);
    TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
    TargetPoint startBackup = targetPointsHelper.getPointToStartBackup();
    if (startBackup == null) {
        startBackup = targetPointsHelper.getMyLocationToStart();
    }
    TargetPoint destinationBackup = targetPointsHelper.getPointToNavigateBackup();
    if (startBackup != null && destinationBackup != null) {
        items.add(PREVIOUS_ROUTE_HEADER);
        items.add(destinationBackup);
        itemsGroups.put(PREVIOUS_ROUTE_HEADER, new ArrayList<>(Collections.singleton(destinationBackup)));
    }
    Map<Integer, List<SearchResult>> historyGroups = new HashMap<>();
    List<Pair<Long, SearchResult>> pairs = createHistoryPairsByDate(searchResults);
    HistoryAdapter.createHistoryGroups(pairs, historyGroups, items);
    for (Map.Entry<Integer, List<SearchResult>> entry : historyGroups.entrySet()) {
        itemsGroups.put(entry.getKey(), new ArrayList<>(entry.getValue()));
    }
}
Also used : HashMap(java.util.HashMap) SearchResult(net.osmand.search.core.SearchResult) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) ArrayList(java.util.ArrayList) List(java.util.List) TargetPointsHelper(net.osmand.plus.helpers.TargetPointsHelper) HashMap(java.util.HashMap) Map(java.util.Map) Pair(android.util.Pair)

Example 15 with TargetPoint

use of net.osmand.plus.helpers.TargetPointsHelper.TargetPoint in project Osmand by osmandapp.

the class MapControlsLayer method onNavigationClick.

private void onNavigationClick() {
    MapActivity mapActivity = requireMapActivity();
    if (mapRouteInfoMenu != null) {
        mapRouteInfoMenu.cancelSelectionFromMap();
    }
    MapActivity.clearPrevActivityIntent();
    RoutingHelper routingHelper = app.getRoutingHelper();
    if (!routingHelper.isFollowingMode() && !routingHelper.isRoutePlanningMode()) {
        mapRouteInfoMenu.clearSuggestedMissingMaps();
        TargetPoint start = getTargets().getPointToStart();
        if (start != null) {
            mapActivity.getMapActions().enterRoutePlanningMode(new LatLon(start.getLatitude(), start.getLongitude()), start.getOriginalPointDescription());
        } else {
            mapActivity.getMapActions().enterRoutePlanningMode(null, null);
        }
    } else {
        showRouteInfoControlDialog();
    }
    hasTargets = false;
}
Also used : LatLon(net.osmand.data.LatLon) RoutingHelper(net.osmand.plus.routing.RoutingHelper) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) MapActivity(net.osmand.plus.activities.MapActivity)

Aggregations

TargetPoint (net.osmand.plus.helpers.TargetPointsHelper.TargetPoint)49 TargetPointsHelper (net.osmand.plus.helpers.TargetPointsHelper)23 OsmandApplication (net.osmand.plus.OsmandApplication)20 LatLon (net.osmand.data.LatLon)19 MapActivity (net.osmand.plus.activities.MapActivity)15 View (android.view.View)13 TextView (android.widget.TextView)13 Location (net.osmand.Location)13 ArrayList (java.util.ArrayList)12 ImageView (android.widget.ImageView)11 FavouritePoint (net.osmand.data.FavouritePoint)10 PointDescription (net.osmand.data.PointDescription)10 MapMarker (net.osmand.plus.mapmarkers.MapMarker)8 List (java.util.List)7 RoutingHelper (net.osmand.plus.routing.RoutingHelper)7 SuppressLint (android.annotation.SuppressLint)6 FrameLayout (android.widget.FrameLayout)6 LinearLayout (android.widget.LinearLayout)6 OsmandSettings (net.osmand.plus.settings.backend.OsmandSettings)6 OnClickListener (android.view.View.OnClickListener)5