private void convertToDirectionList() { int index = 0; for (Leg leg : legs) { index++; setTotalDistance(getTotalDistance() + leg.distance); TraverseMode traverseMode = TraverseMode.valueOf((String) leg.mode); if (traverseMode.isOnStreetNonTransit()) { Direction dir = generateNonTransitDirections(leg); if (dir == null) { continue; } dir.setDirectionIndex(index); addDirection(dir); } else { ArrayList<Direction> directions = generateTransitDirections(leg); if (directions == null) { continue; } if (directions.get(0) != null) { directions.get(0).setDirectionIndex(index); addDirection(directions.get(0)); } if (directions.get(1) != null) { if (directions.get(0) != null) { index++; } directions.get(1).setDirectionIndex(index); addDirection(directions.get(1)); } } } }
public Direction generateTransitSubdirection(Leg leg, boolean isOnDirection) { Direction direction = new Direction(); direction.setRealTimeInfo(leg.realTime); // Set icon String mode = getLocalizedMode(TraverseMode.valueOf(leg.mode), applicationContext.getResources()); int modeIcon; String route; String agencyName = leg.agencyName; Place from = leg.from; Place to = leg.to; Calendar newTime = Calendar.getInstance(); Calendar oldTime = Calendar.getInstance(); String shortName; // As a work-around for #662, we always use routeShortName and not tripShortName shortName = leg.routeShortName; route = ConversionUtils.getRouteLongNameSafe(leg.routeLongName, shortName, true); direction.setTransit(true); String action, placeAndHeadsign, extra = ""; if (isOnDirection) { action = applicationContext.getResources().getString(R.string.step_by_step_transit_get_on); placeAndHeadsign = from.name; TraverseModeSet modeSet = new TraverseModeSet(leg.mode); modeIcon = getModeIcon(modeSet); newTime.setTime(new Date(Long.parseLong(leg.startTime))); oldTime.setTime(new Date(newTime.getTimeInMillis())); oldTime.add(Calendar.SECOND, -leg.departureDelay); // Only onDirection has subdirection (list of stops in between) ArrayList<Place> stopsInBetween = new ArrayList<Place>(); if ((leg.getIntermediateStops() != null) && !leg.getIntermediateStops().isEmpty()) { stopsInBetween.addAll(leg.getIntermediateStops()); } else if ((leg.stop != null) && !leg.stop.isEmpty()) { stopsInBetween.addAll(leg.stop); } // sub-direction ArrayList<Direction> subDirections = new ArrayList<Direction>(); for (int i = 0; i < stopsInBetween.size(); i++) { Direction subDirection = new Direction(); Place stop = stopsInBetween.get(i); String extraStopInformation = stop.stopCode; String subDirectionText = Integer.toString(i + 1) + ". " + stop.name; if (!TextUtils.isEmpty(extraStopInformation)) { subDirectionText += " (" + extraStopInformation + ")"; } subDirection.setDirectionText(subDirectionText); subDirection.setIcon(DirectionsGenerator.getStopIcon(modeSet)); subDirections.add(subDirection); } direction.setSubDirections(subDirections); if (stopsInBetween.size() > 0) { String connector = applicationContext .getResources() .getString(R.string.step_by_step_transit_stops_in_between); if (stopsInBetween.size() == 1) { connector = applicationContext .getResources() .getString(R.string.step_by_step_transit_stops_in_between_singular); } extra = stopsInBetween.size() + " " + connector; } if (!TextUtils.isEmpty(leg.headsign)) { placeAndHeadsign += " " + applicationContext .getResources() .getString(R.string.step_by_step_transit_connector_headsign) + " " + leg.headsign; } } else { action = applicationContext.getResources().getString(R.string.step_by_step_transit_get_off); placeAndHeadsign = to.name; modeIcon = -1; newTime.setTime(new Date(Long.parseLong(leg.endTime))); oldTime.setTime(new Date(newTime.getTimeInMillis())); oldTime.add(Calendar.SECOND, -leg.arrivalDelay); } direction.setIcon(modeIcon); direction.setPlaceAndHeadsign( applicationContext .getResources() .getString(R.string.step_by_step_transit_connector_stop_name) + " " + placeAndHeadsign); direction.setService(action + " " + mode + " " + route); direction.setAgency(agencyName); direction.setExtra(extra); SpannableString oldTimeString; if (leg.realTime) { CharSequence newTimeString; newTimeString = ConversionUtils.getTimeUpdated( applicationContext, leg.agencyTimeZoneOffset, oldTime.getTimeInMillis(), newTime.getTimeInMillis()); direction.setNewTime(newTimeString); } oldTimeString = new SpannableString( ConversionUtils.getTimeWithContext( applicationContext, leg.agencyTimeZoneOffset, oldTime.getTimeInMillis(), true)); direction.setOldTime(oldTimeString); return direction; }
private Direction generateNonTransitDirections(Leg leg) { Direction direction = new Direction(); // http://opentripplanner.usf.edu/opentripplanner-api-webapp/ws/plan?optimize=QUICK&time=09:24pm&arriveBy=false&wheelchair=false&maxWalkDistance=7600.0&fromPlace=28.033389%2C+-82.521034&toPlace=28.064709%2C+-82.471618&date=03/07/12&mode=WALK,TRAM,SUBWAY,RAIL,BUS,FERRY,CABLE_CAR,GONDOLA,FUNICULAR,TRANSIT,TRAINISH,BUSISH // Get appropriate action and icon String action = applicationContext .getResources() .getString(R.string.step_by_step_non_transit_mode_walk_action); TraverseMode mode = TraverseMode.valueOf((String) leg.mode); int icon = getModeIcon(new TraverseModeSet(mode)); if (mode.compareTo(TraverseMode.BICYCLE) == 0) { action = applicationContext .getResources() .getString(R.string.step_by_step_non_transit_mode_bicycle_action); } else if (mode.compareTo(TraverseMode.CAR) == 0) { action = applicationContext .getResources() .getString(R.string.step_by_step_non_transit_mode_car_action); } direction.setIcon(icon); // Main direction Place fromPlace = leg.from; Place toPlace = leg.to; String mainDirectionText = action; mainDirectionText += fromPlace.name == null ? "" : " " + applicationContext .getResources() .getString(R.string.step_by_step_non_transit_from) + " " + getLocalizedStreetName(fromPlace.name, applicationContext.getResources()); mainDirectionText += toPlace.name == null ? "" : " " + applicationContext.getResources().getString(R.string.step_by_step_non_transit_to) + " " + getLocalizedStreetName(toPlace.name, applicationContext.getResources()); String extraStopInformation = toPlace.stopCode; long legDuration; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext); if (prefs.getInt(OTPConstants.PREFERENCE_KEY_API_VERSION, OTPConstants.API_VERSION_V1) == OTPConstants.API_VERSION_V1) { legDuration = leg.duration; } else { legDuration = leg.duration / 1000; } if (!TextUtils.isEmpty(extraStopInformation)) { mainDirectionText += " (" + extraStopInformation + ")"; } mainDirectionText += "\n[" + ConversionUtils.getFormattedDistance(leg.distance, applicationContext) + " - " + ConversionUtils.getFormattedDurationTextNoSeconds( legDuration, false, applicationContext) + " ]"; direction.setDirectionText(mainDirectionText); // Sub-direction List<WalkStep> walkSteps = leg.getSteps(); if (walkSteps == null) { return direction; } ArrayList<Direction> subDirections = new ArrayList<Direction>(walkSteps.size()); for (WalkStep step : walkSteps) { int subdirection_icon = -1; Direction dir = new Direction(); String subDirectionText = ""; double distance = step.distance; RelativeDirection relativeDir = step.relativeDirection; String relativeDirString = getLocalizedRelativeDir(relativeDir, applicationContext.getResources()); String streetName = step.streetName; AbsoluteDirection absoluteDir = step.absoluteDirection; String absoluteDirString = getLocalizedAbsoluteDir(absoluteDir, applicationContext.getResources()); String exit = step.exit; boolean isStayOn = (step.stayOn == null ? false : step.stayOn); boolean isBogusName = (step.bogusName == null ? false : step.bogusName); double lon = step.lon; double lat = step.lat; String streetConnector = applicationContext .getResources() .getString(R.string.step_by_step_non_transit_connector_street_name); // Elevation[] elevation = step.getElevation(); //Removed elevation for now, since we're not // doing anything with it and it causes version issues between OTP server APIs v0.9.1-SNAPSHOT // and v0.9.2-SNAPSHOT List<Alerts> alert = step.alerts; // Walk East if (relativeDir == null) { subDirectionText += action + " " + applicationContext .getResources() .getString(R.string.step_by_step_non_transit_heading) + " "; subDirectionText += absoluteDirString + " "; } // (Turn left)/(Continue) else { RelativeDirection rDir = RelativeDirection.valueOf(relativeDir.name()); subdirection_icon = getRelativeDirectionIcon(rDir, applicationContext.getResources()); // Do not need TURN Continue if (rDir.compareTo(RelativeDirection.RIGHT) == 0 || rDir.compareTo(RelativeDirection.LEFT) == 0) { subDirectionText += applicationContext.getResources().getString(R.string.step_by_step_non_transit_turn) + " "; } subDirectionText += relativeDirString + " "; if (rDir.compareTo(RelativeDirection.CIRCLE_CLOCKWISE) == 0 || rDir.compareTo(RelativeDirection.CIRCLE_COUNTERCLOCKWISE) == 0) { if (step.exit != null) { try { String ordinal = getOrdinal(Integer.parseInt(step.exit), applicationContext.getResources()); if (ordinal != null) { subDirectionText += ordinal + " "; } else { subDirectionText += applicationContext .getResources() .getString(R.string.step_by_step_non_transit_roundabout_number) + " " + ordinal + " "; } } catch (NumberFormatException e) { // If is not a step_by_step_non_transit_roundabout_number and is not null is better to // try to display it subDirectionText += step.exit + " "; } subDirectionText += applicationContext .getResources() .getString(R.string.step_by_step_non_transit_roundabout_exit) + " "; streetConnector = applicationContext .getResources() .getString(R.string.step_by_step_non_transit_connector_street_name_roundabout); } } } subDirectionText += streetConnector + " " + getLocalizedStreetName(streetName, applicationContext.getResources()) + " "; subDirectionText += "\n[" + ConversionUtils.getFormattedDistance(distance, applicationContext) + " ]"; dir.setDirectionText(subDirectionText); dir.setIcon(subdirection_icon); // Add new sub-direction subDirections.add(dir); } direction.setSubDirections(subDirections); return direction; }