use of net.osmand.plus.helpers.TargetPointsHelper 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();
}
use of net.osmand.plus.helpers.TargetPointsHelper in project Osmand by osmandapp.
the class PlanRouteFragment method createOptionsFragmentListener.
private PlanRouteOptionsFragmentListener createOptionsFragmentListener() {
return new PlanRouteOptionsFragmentListener() {
private MapActivity mapActivity = getMapActivity();
@Override
public void selectOnClick() {
selectAllOnClick();
}
@Override
public void navigateOnClick() {
if (mapActivity != null) {
boolean hasTargets = false;
TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper();
List<MapMarker> markers = markersHelper.getSelectedMarkers();
if (markers.size() > 0) {
int i = 0;
if (markersHelper.isStartFromMyLocation()) {
targetPointsHelper.clearStartPoint(false);
} else {
MapMarker m = markers.get(i++);
targetPointsHelper.setStartPoint(new LatLon(m.getLatitude(), m.getLongitude()), false, m.getPointDescription(mapActivity));
}
List<TargetPoint> targetPoints = new ArrayList<>();
for (int k = i; k < markers.size(); k++) {
MapMarker m = markers.get(k);
TargetPoint t = new TargetPoint(new LatLon(m.getLatitude(), m.getLongitude()), m.getPointDescription(mapActivity));
targetPoints.add(t);
}
if (mapActivity.getMyApplication().getSettings().ROUTE_MAP_MARKERS_ROUND_TRIP.get()) {
TargetPoint end = targetPointsHelper.getPointToStart();
if (end == null) {
Location loc = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation();
if (loc != null) {
end = TargetPoint.createStartPoint(new LatLon(loc.getLatitude(), loc.getLongitude()), new PointDescription(PointDescription.POINT_TYPE_MY_LOCATION, mapActivity.getString(R.string.shared_string_my_location)));
}
}
if (end != null) {
targetPoints.add(end);
}
}
RoutingHelper routingHelper = mapActivity.getRoutingHelper();
boolean updateRoute = routingHelper.isFollowingMode() || routingHelper.isRoutePlanningMode();
targetPointsHelper.reorderAllTargetPoints(targetPoints, updateRoute);
hasTargets = true;
} else {
targetPointsHelper.clearStartPoint(false);
targetPointsHelper.clearPointToNavigate(false);
}
planRouteContext.setNavigationFromMarkers(true);
dismiss();
mapActivity.getMapLayers().getMapControlsLayer().doRoute(hasTargets);
}
}
@Override
public void makeRoundTripOnClick() {
roundTripOnClick();
}
@Override
public void doorToDoorOnClick() {
if (mapActivity != null) {
OsmandApplication app = mapActivity.getMyApplication();
Location myLoc = app.getLocationProvider().getLastStaleKnownLocation();
boolean startFromLocation = app.getMapMarkersHelper().isStartFromMyLocation() && myLoc != null;
if (selectedCount > (startFromLocation ? 0 : 1)) {
sortSelectedMarkersDoorToDoor(mapActivity, startFromLocation, myLoc);
}
}
}
@Override
public void reverseOrderOnClick() {
if (mapActivity != null) {
markersHelper.reverseActiveMarkersOrder();
adapter.reloadData();
adapter.notifyDataSetChanged();
planRouteContext.recreateSnapTrkSegment(false);
}
}
};
}
use of net.osmand.plus.helpers.TargetPointsHelper in project Osmand by osmandapp.
the class AddPointBottomSheetDialog method getAdapterOnClickListener.
private OnClickListener getAdapterOnClickListener(final List<Object> items) {
return new OnClickListener() {
@Override
public void onClick(View v) {
MapActivity mapActivity = (MapActivity) getActivity();
RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) v.getTag();
int position = viewHolder != null ? viewHolder.getAdapterPosition() : RecyclerView.NO_POSITION;
if (mapActivity == null || position == RecyclerView.NO_POSITION) {
return;
}
Object item = items.get(position);
if (item.equals(FAVORITES)) {
openFavoritesDialog();
} else if (item.equals(MARKERS)) {
MapRouteInfoMenu menu = mapActivity.getMapRouteInfoMenu();
menu.selectMapMarker(-1, pointType);
dismiss();
} else if (item instanceof MapMarker) {
MapRouteInfoMenu menu = mapActivity.getMapRouteInfoMenu();
menu.selectMapMarker((MapMarker) item, pointType);
dismiss();
} else {
TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper();
Pair<LatLon, PointDescription> pair = getLocationAndDescrFromItem(item);
LatLon ll = pair.first;
PointDescription name = pair.second;
if (ll == null) {
if (item instanceof PointType) {
AddPointBottomSheetDialog.showInstance(mapActivity, (PointType) item);
} else {
dismiss();
}
} else {
FavouritesHelper favorites = requiredMyApplication().getFavoritesHelper();
switch(pointType) {
case START:
targetPointsHelper.setStartPoint(ll, true, name);
break;
case TARGET:
targetPointsHelper.navigateToPoint(ll, true, -1, name);
break;
case INTERMEDIATE:
targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), name);
break;
case HOME:
favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.HOME, null);
break;
case WORK:
favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.WORK, null);
break;
case PARKING:
favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.PARKING, null);
break;
}
dismiss();
}
}
}
};
}
use of net.osmand.plus.helpers.TargetPointsHelper in project Osmand by osmandapp.
the class MapRouteInfoMenu method updateStartPointView.
private void updateStartPointView() {
MapActivity mapActivity = getMapActivity();
final View mainView = getMainView();
if (mapActivity == null || mainView == null) {
return;
}
setupFromText(mainView);
View fromLayout = mainView.findViewById(R.id.FromLayout);
fromLayout.setOnClickListener(v -> {
MapActivity mapActv = getMapActivity();
if (mapActv != null) {
AddPointBottomSheetDialog.showInstance(mapActv, PointType.START);
}
});
FrameLayout fromButton = mainView.findViewById(R.id.from_button);
boolean isFollowTrack = mapActivity.getMyApplication().getRoutingHelper().getCurrentGPXRoute() != null;
if (isFollowTrack) {
fromButton.setVisibility(View.GONE);
} else {
fromButton.setVisibility(View.VISIBLE);
}
LinearLayout fromButtonContainer = mainView.findViewById(R.id.from_button_container);
setupButtonBackground(fromButton, fromButtonContainer);
ImageView swapDirectionView = mainView.findViewById(R.id.from_button_image_view);
setupButtonIcon(swapDirectionView, R.drawable.ic_action_change_navigation_points);
fromButton.setOnClickListener(view -> {
MapActivity mapActv = getMapActivity();
if (mapActv != null) {
OsmandApplication app = mapActv.getMyApplication();
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
TargetPoint startPoint = targetPointsHelper.getPointToStart();
TargetPoint endPoint = targetPointsHelper.getPointToNavigate();
Location loc = app.getLocationProvider().getLastKnownLocation();
if (loc == null && startPoint == null && endPoint == null) {
app.showShortToastMessage(R.string.add_start_and_end_points);
} else if (endPoint == null) {
app.showShortToastMessage(R.string.mark_final_location_first);
} else {
GPXRouteParamsBuilder gpxParams = app.getRoutingHelper().getCurrentGPXRoute();
if (gpxParams != null) {
boolean reverse = !gpxParams.isReverse();
LocalRoutingParameter parameter = new OtherLocalRoutingParameter(R.string.gpx_option_reverse_route, app.getString(R.string.gpx_option_reverse_route), reverse);
app.getRoutingOptionsHelper().applyRoutingParameter(parameter, reverse);
} else {
if (startPoint == null && loc != null) {
startPoint = TargetPoint.createStartPoint(new LatLon(loc.getLatitude(), loc.getLongitude()), new PointDescription(PointDescription.POINT_TYPE_MY_LOCATION, mapActv.getString(R.string.shared_string_my_location)));
}
if (startPoint != null) {
int intermediateSize = targetPointsHelper.getIntermediatePoints().size();
if (intermediateSize > 1) {
WaypointDialogHelper.reverseAllPoints(app, mapActv, mapActv.getDashboard().getWaypointDialogHelper());
} else {
WaypointDialogHelper.switchStartAndFinish(mapActv.getMyApplication(), mapActv, mapActv.getDashboard().getWaypointDialogHelper(), true);
}
} else {
app.showShortToastMessage(R.string.route_add_start_point);
}
}
}
}
});
updateFromIcon(mainView);
final View textView = mainView.findViewById(R.id.from_button_description);
if (!swapButtonCollapsing && !swapButtonCollapsed && fromButton.getVisibility() == View.VISIBLE && textView.getVisibility() == View.VISIBLE) {
swapButtonCollapsing = true;
collapseButtonAnimated(R.id.from_button, R.id.from_button_description, success -> {
swapButtonCollapsing = false;
swapButtonCollapsed = success;
});
} else if (swapButtonCollapsed) {
textView.setVisibility(View.GONE);
}
}
use of net.osmand.plus.helpers.TargetPointsHelper in project Osmand by osmandapp.
the class FailSafeFuntions method restoreRoutingMode.
public static void restoreRoutingMode(final MapActivity ma) {
final OsmandApplication app = ma.getMyApplication();
final OsmandSettings settings = app.getSettings();
final Handler uiHandler = new Handler();
final String gpxPath = settings.FOLLOW_THE_GPX_ROUTE.get();
final TargetPointsHelper targetPoints = app.getTargetPointsHelper();
final TargetPoint pointToNavigate = targetPoints.getPointToNavigate();
if (pointToNavigate == null && gpxPath == null) {
notRestoreRoutingMode(ma, app);
} else {
quitRouteRestoreDialog = false;
Runnable encapsulate = new Runnable() {
int delay = 7;
Runnable delayDisplay = null;
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(ma);
final TextView tv = new TextView(ma);
tv.setText(ma.getString(R.string.continue_follow_previous_route_auto, delay + ""));
tv.setPadding(7, 5, 7, 5);
builder.setView(tv);
builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
quitRouteRestoreDialog = true;
restoreRoutingModeInner();
}
});
builder.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
quitRouteRestoreDialog = true;
notRestoreRoutingMode(ma, app);
}
});
final AlertDialog dlg = builder.show();
dlg.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
quitRouteRestoreDialog = true;
}
});
dlg.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
quitRouteRestoreDialog = true;
}
});
delayDisplay = new Runnable() {
@Override
public void run() {
if (!quitRouteRestoreDialog) {
delay--;
tv.setText(ma.getString(R.string.continue_follow_previous_route_auto, delay + ""));
if (delay <= 0) {
try {
if (dlg.isShowing() && !quitRouteRestoreDialog) {
dlg.dismiss();
}
quitRouteRestoreDialog = true;
restoreRoutingModeInner();
} catch (Exception e) {
// swalow view not attached exception
log.error(e.getMessage() + "", e);
}
} else {
uiHandler.postDelayed(delayDisplay, 1000);
}
}
}
};
delayDisplay.run();
}
private void restoreRoutingModeInner() {
AsyncTask<String, Void, GPXFile> task = new AsyncTask<String, Void, GPXFile>() {
@Override
protected GPXFile doInBackground(String... params) {
if (gpxPath != null) {
// Reverse also should be stored ?
GPXFile f = GPXUtilities.loadGPXFile(new File(gpxPath));
if (f.error != null) {
return null;
}
return f;
} else {
return null;
}
}
@Override
protected void onPostExecute(GPXFile result) {
final GPXRouteParamsBuilder gpxRoute;
if (result != null) {
gpxRoute = new GPXRouteParamsBuilder(result, settings);
if (settings.GPX_ROUTE_CALC_OSMAND_PARTS.get()) {
gpxRoute.setCalculateOsmAndRouteParts(true);
}
if (settings.GPX_ROUTE_CALC.get()) {
gpxRoute.setCalculateOsmAndRoute(true);
}
if (settings.GPX_ROUTE_SEGMENT.get() != -1) {
gpxRoute.setSelectedSegment(settings.GPX_ROUTE_SEGMENT.get());
}
} else {
gpxRoute = null;
}
TargetPoint endPoint = pointToNavigate;
if (endPoint == null) {
notRestoreRoutingMode(ma, app);
} else {
enterRoutingMode(ma, gpxRoute);
}
}
};
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, gpxPath);
}
};
encapsulate.run();
}
}
Aggregations