use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class Trip method toConnectionPath.
public Path toConnectionPath(Network network) {
List<Connection> edges = new LinkedList<>();
List<Pair<Date, Date>> times = new ArrayList<>();
List<Boolean> manualEntry = new ArrayList<>();
Stop startVertex = null;
List<Stop> previous = new ArrayList<>();
for (StationUse use : path) {
switch(use.getType()) {
case INTERCHANGE:
Stop source = null;
Stop target = null;
for (Stop s : network.getStation(use.getStation().getId()).getStops()) {
if (s.getLine().getId().equals(use.getSourceLine())) {
source = s;
}
if (s.getLine().getId().equals(use.getTargetLine())) {
target = s;
}
}
for (Stop s : previous) {
Connection e = network.getEdge(s, source);
if (e != null) {
edges.add(e);
}
}
Connection edge = network.getEdge(source, target);
if (edge != null) {
edges.add(edge);
previous = new ArrayList<>();
previous.add(target);
// in a Path, there are originally two entries for an interchange
times.add(new Pair<>(use.getEntryDate(), use.getLeaveDate()));
times.add(new Pair<>(use.getEntryDate(), use.getLeaveDate()));
manualEntry.add(use.isManualEntry());
manualEntry.add(use.isManualEntry());
}
break;
case NETWORK_ENTRY:
previous = new ArrayList<>(network.getStation(use.getStation().getId()).getStops());
times.add(new Pair<>(use.getEntryDate(), use.getLeaveDate()));
manualEntry.add(use.isManualEntry());
break;
case NETWORK_EXIT:
case GONE_THROUGH:
for (Stop s : previous) {
for (Stop s2 : network.getStation(use.getStation().getId()).getStops()) {
Connection e = network.getEdge(s, s2);
// deal with our way of closing stations
// where closed stations only have their outbound edges
// (but the user may have travelled through a no longer existing
// inbound edge, before the station closed)
Connection otherDir = network.getEdge(s2, s);
if (e == null && otherDir != null) {
e = new Connection(true, s, s2, otherDir.getTimes(), otherDir.getWorldLength());
}
if (e != null) {
edges.add(e);
previous = new ArrayList<>();
previous.add(s2);
times.add(new Pair<>(use.getEntryDate(), use.getLeaveDate()));
manualEntry.add(use.isManualEntry());
}
}
}
break;
case VISIT:
times.add(new Pair<>(use.getEntryDate(), use.getLeaveDate()));
manualEntry.add(use.isManualEntry());
// a stop, any stop
startVertex = network.getStation(use.getStation().getId()).getStops().iterator().next();
break;
}
}
if (startVertex == null) {
startVertex = edges.get(0).getSource();
}
return new Path(network, startVertex, edges, times, manualEntry, 0);
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class Trip method persistConnectionPath.
public static String persistConnectionPath(Path path, String replaceTrip) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
RealmList<StationUse> uses = new RealmList<>();
List<Connection> edgeList = path.getEdgeList();
int size = edgeList.size();
if (size == 0) {
StationUse use = new StationUse();
use.setType(StationUse.UseType.VISIT);
use.setStation(realm.where(RStation.class).equalTo("id", path.getStartVertex().getStation().getId()).findFirst());
use.setEntryDate(path.getEntryExitTimes(0).first);
use.setLeaveDate(path.getEntryExitTimes(0).second);
use.setManualEntry(path.getManualEntry(0));
uses.add(realm.copyToRealm(use));
} else if (size == 1 && edgeList.get(0) instanceof Transfer) {
Connection c = edgeList.get(0);
StationUse use = new StationUse();
use.setType(StationUse.UseType.VISIT);
use.setSourceLine(c.getSource().getLine().getId());
use.setTargetLine(c.getTarget().getLine().getId());
use.setEntryDate(path.getEntryExitTimes(0).first);
use.setLeaveDate(path.getEntryExitTimes(0).second);
use.setManualEntry(path.getManualEntry(0));
use.setStation(realm.where(RStation.class).equalTo("id", c.getSource().getStation().getId()).findFirst());
uses.add(realm.copyToRealm(use));
} else {
int timeIdx = 0;
StationUse startUse = new StationUse();
startUse.setType(StationUse.UseType.NETWORK_ENTRY);
startUse.setStation(realm.where(RStation.class).equalTo("id", path.getStartVertex().getStation().getId()).findFirst());
startUse.setEntryDate(path.getEntryExitTimes(timeIdx).first);
startUse.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
startUse.setManualEntry(path.getManualEntry(timeIdx));
uses.add(realm.copyToRealm(startUse));
int i = 1;
timeIdx++;
if (edgeList.get(0) instanceof Transfer) {
i = 2;
timeIdx++;
}
for (; i < size; i++) {
Connection c = edgeList.get(i);
StationUse use = new StationUse();
if (c instanceof Transfer) {
if (i == size - 1)
break;
use.setType(StationUse.UseType.INTERCHANGE);
use.setSourceLine(c.getSource().getLine().getId());
use.setTargetLine(c.getTarget().getLine().getId());
use.setEntryDate(path.getEntryExitTimes(timeIdx).first);
timeIdx++;
use.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
use.setManualEntry(path.getManualEntry(timeIdx));
timeIdx++;
// skip next station as then we'd have duplicate uses for the same station ID
i++;
} else {
use.setType(StationUse.UseType.GONE_THROUGH);
use.setEntryDate(path.getEntryExitTimes(timeIdx).first);
use.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
use.setManualEntry(path.getManualEntry(timeIdx));
timeIdx++;
}
use.setStation(realm.where(RStation.class).equalTo("id", c.getSource().getStation().getId()).findFirst());
uses.add(realm.copyToRealm(use));
}
StationUse endUse = new StationUse();
endUse.setType(StationUse.UseType.NETWORK_EXIT);
endUse.setStation(realm.where(RStation.class).equalTo("id", path.getEndVertex().getStation().getId()).findFirst());
endUse.setEntryDate(path.getEntryExitTimes(timeIdx).first);
endUse.setLeaveDate(path.getEntryExitTimes(timeIdx).second);
endUse.setManualEntry(path.getManualEntry(timeIdx));
uses.add(realm.copyToRealm(endUse));
}
Trip trip;
if (replaceTrip != null) {
trip = realm.where(Trip.class).equalTo("id", replaceTrip).findFirst();
trip.getPath().deleteAllFromRealm();
trip.setUserConfirmed(true);
trip.setSynced(false);
} else {
trip = realm.createObject(Trip.class, UUID.randomUUID().toString());
trip.setUserConfirmed(false);
}
trip.setPath(uses);
String tripId = trip.getId();
realm.commitTransaction();
realm.close();
return tripId;
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class LineActivity method populateLineView.
public static void populateLineView(final Context context, final LayoutInflater inflater, final Line line, ViewGroup root) {
root.removeAllViews();
// TODO note: this doesn't work for circular lines
List<Station> stations = new ArrayList<>();
Stop s = line.getFirstStop();
Set<Stop> visited = new HashSet<>();
// terminus will only have one outgoing edge
Connection c = line.outgoingEdgesOf(s).iterator().next();
while (visited.add(c.getSource())) {
stations.add(c.getSource().getStation());
Stop curStop = c.getTarget();
if (line.outDegreeOf(curStop) == 1) {
// it's an end station
stations.add(curStop.getStation());
break;
}
boolean loop = true;
for (Connection inedge : line.incomingEdgesOf(curStop)) {
if (!visited.contains(inedge.getSource())) {
c = new Connection(true, inedge.getTarget(), inedge.getSource(), null, 0);
loop = false;
break;
}
}
if (!loop) {
continue;
}
for (Connection outedge : line.outgoingEdgesOf(curStop)) {
if (!visited.contains(outedge.getTarget())) {
c = outedge;
break;
}
}
}
int lineColor = line.getColor();
for (int i = 0; i < stations.size(); i++) {
final Station station = stations.get(i);
View stepview = inflater.inflate(R.layout.path_station, root, false);
TextView timeView = (TextView) stepview.findViewById(R.id.time_view);
timeView.setVisibility(View.INVISIBLE);
FrameLayout prevLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.prev_line_stripe_layout);
FrameLayout nextLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.next_line_stripe_layout);
FrameLayout leftLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.left_line_stripe_layout);
FrameLayout rightLineStripeLayout = (FrameLayout) stepview.findViewById(R.id.right_line_stripe_layout);
if (i == 0) {
prevLineStripeLayout.setVisibility(View.GONE);
nextLineStripeLayout.setBackgroundColor(lineColor);
} else if (i == stations.size() - 1) {
prevLineStripeLayout.setBackgroundColor(lineColor);
nextLineStripeLayout.setVisibility(View.GONE);
} else {
prevLineStripeLayout.setBackgroundColor(lineColor);
nextLineStripeLayout.setBackgroundColor(lineColor);
}
if (station.getLines().size() > 1) {
for (Stop stop : station.getStops()) {
if (stop.getLine() != line) {
rightLineStripeLayout.setVisibility(View.VISIBLE);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.RIGHT_LEFT, new int[] { 0, stop.getLine().getColor() });
gd.setCornerRadius(0f);
if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
rightLineStripeLayout.setBackgroundDrawable(gd);
} else {
rightLineStripeLayout.setBackground(gd);
}
if (stop.getLine().outDegreeOf(stop) > 1) {
leftLineStripeLayout.setVisibility(View.VISIBLE);
gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] { 0, stop.getLine().getColor() });
gd.setCornerRadius(0f);
if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
leftLineStripeLayout.setBackgroundDrawable(gd);
} else {
leftLineStripeLayout.setBackground(gd);
}
}
break;
}
}
}
ImageView crossView = (ImageView) stepview.findViewById(R.id.station_cross_image);
if (station.isAlwaysClosed()) {
crossView.setVisibility(View.VISIBLE);
}
stepview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, StationActivity.class);
intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
intent.putExtra(StationActivity.EXTRA_NETWORK_ID, station.getNetwork().getId());
context.startActivity(intent);
}
});
RouteFragment.populateStationView(context, station, stepview, true, false);
root.addView(stepview);
}
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class Path method manualExtendStart.
public void manualExtendStart(Stop vertex) {
// start by reverting to the path with the start without user-made extensions
int edgesToRemove = 0;
while (manualEntry.get(0)) {
manualEntry.remove(0);
times.remove(0);
edgesToRemove++;
}
for (int i = 0; i < edgesToRemove; i++) {
edgeList.remove(0);
}
if (edgeList.size() > 0) {
startVertex = edgeList.get(0).getSource();
}
// now go from the program-made start
Date time = times.get(0).first;
List<Connection> cs = Route.getShortestPath(graph, vertex, startVertex, new LeastHopsWeighter()).getEdgeList();
int size = cs.size();
int insertPos = 0;
for (int i = 0; i < size; i++) {
// never add a transfer as the first step
if (i == 0 && cs.get(i) instanceof Transfer) {
continue;
}
times.add(insertPos, new Pair<Date, Date>(time, time));
manualEntry.add(insertPos, true);
edgeList.add(insertPos++, cs.get(i));
}
this.startVertex = vertex;
for (OnPathChangedListener l : listeners) {
l.onPathChanged(this);
}
}
use of im.tny.segvault.subway.Connection in project underlx by underlx.
the class RouteFragment method showRoute.
private void showRoute(boolean realtimeEqualsNeutral) {
if (route == null) {
return;
}
layoutRoute.removeAllViews();
if (originPicker.getSelection().isAlwaysClosed()) {
viewOriginStationClosed.setText(String.format(getString(R.string.frag_route_station_closed_extended), originPicker.getSelection().getName()));
layoutOriginStationClosed.setVisibility(View.VISIBLE);
} else if (originPicker.getSelection().isExceptionallyClosed(network, new Date()) && useRealtimeCheckbox.isChecked()) {
viewOriginStationClosed.setText(String.format(getString(R.string.frag_route_station_closed_schedule), originPicker.getSelection().getName()));
layoutOriginStationClosed.setVisibility(View.VISIBLE);
} else {
layoutOriginStationClosed.setVisibility(View.GONE);
}
if (destinationPicker.getSelection().isAlwaysClosed()) {
viewDestinationStationClosed.setText(String.format(getString(R.string.frag_route_station_closed_extended), destinationPicker.getSelection().getName()));
layoutDestinationStationClosed.setVisibility(View.VISIBLE);
} else if (destinationPicker.getSelection().isExceptionallyClosed(network, new Date()) && useRealtimeCheckbox.isChecked()) {
viewDestinationStationClosed.setText(String.format(getString(R.string.frag_route_station_closed_schedule), destinationPicker.getSelection().getName()));
layoutDestinationStationClosed.setVisibility(View.VISIBLE);
} else {
layoutDestinationStationClosed.setVisibility(View.GONE);
}
for (Step step : route) {
View view = null;
if (step instanceof EnterStep) {
view = getActivity().getLayoutInflater().inflate(R.layout.step_enter_network, layoutRoute, false);
final Line line = step.getLine();
int lineColor = line.getColor();
FrameLayout lineStripeLayout = (FrameLayout) view.findViewById(R.id.line_stripe_layout);
lineStripeLayout.setBackgroundColor(lineColor);
populateStationView(getActivity(), step.getStation(), view);
if (step.getStation().getLines().size() > 1) {
Drawable drawable = ContextCompat.getDrawable(getContext(), Util.getDrawableResourceIdForLineId(line.getId()));
drawable.setColorFilter(lineColor, PorterDuff.Mode.SRC_ATOP);
FrameLayout iconFrame = (FrameLayout) view.findViewById(R.id.frame_icon);
if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
iconFrame.setBackgroundDrawable(drawable);
} else {
iconFrame.setBackground(drawable);
}
TextView lineView = (TextView) view.findViewById(R.id.line_name_view);
lineView.setText(String.format(getString(R.string.frag_route_line_name), Util.getLineNames(getContext(), line)[0]));
lineView.setTextColor(lineColor);
LinearLayout lineLayout = (LinearLayout) view.findViewById(R.id.line_layout);
lineLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), LineActivity.class);
intent.putExtra(LineActivity.EXTRA_LINE_ID, line.getId());
intent.putExtra(LineActivity.EXTRA_NETWORK_ID, network.getId());
startActivity(intent);
}
});
lineLayout.setVisibility(View.VISIBLE);
}
TextView directionView = (TextView) view.findViewById(R.id.direction_view);
directionView.setText(Util.fromHtml(String.format(getString(R.string.frag_route_direction), ((EnterStep) step).getDirection().getName())));
if (line.getUsualCarCount() < network.getUsualCarCount()) {
LinearLayout carsWarningLayout = (LinearLayout) view.findViewById(R.id.cars_warning_layout);
carsWarningLayout.setVisibility(View.VISIBLE);
}
if (mListener != null && mListener.getMainService() != null) {
Map<String, LineStatusCache.Status> statuses = mListener.getLineStatusCache().getLineStatus();
if (statuses.get(line.getId()) != null && statuses.get(line.getId()).down) {
LinearLayout disturbancesWarningLayout = (LinearLayout) view.findViewById(R.id.disturbances_warning_layout);
disturbancesWarningLayout.setVisibility(View.VISIBLE);
}
}
} else if (step instanceof ChangeLineStep) {
final ChangeLineStep lStep = (ChangeLineStep) step;
view = getActivity().getLayoutInflater().inflate(R.layout.step_change_line, layoutRoute, false);
int prevLineColor = lStep.getLine().getColor();
FrameLayout prevLineStripeLayout = (FrameLayout) view.findViewById(R.id.prev_line_stripe_layout);
prevLineStripeLayout.setBackgroundColor(prevLineColor);
int nextLineColor = lStep.getTarget().getColor();
FrameLayout nextLineStripeLayout = (FrameLayout) view.findViewById(R.id.next_line_stripe_layout);
nextLineStripeLayout.setBackgroundColor(nextLineColor);
populateStationView(getActivity(), lStep.getStation(), view);
Drawable drawable = ContextCompat.getDrawable(getContext(), Util.getDrawableResourceIdForLineId(lStep.getTarget().getId()));
drawable.setColorFilter(nextLineColor, PorterDuff.Mode.SRC_ATOP);
FrameLayout iconFrame = (FrameLayout) view.findViewById(R.id.frame_icon);
if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
iconFrame.setBackgroundDrawable(drawable);
} else {
iconFrame.setBackground(drawable);
}
TextView lineView = (TextView) view.findViewById(R.id.line_name_view);
lineView.setText(String.format(getString(R.string.frag_route_line_name), Util.getLineNames(getContext(), lStep.getTarget())[0]));
lineView.setTextColor(nextLineColor);
LinearLayout lineLayout = (LinearLayout) view.findViewById(R.id.line_layout);
lineLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), LineActivity.class);
intent.putExtra(LineActivity.EXTRA_LINE_ID, lStep.getTarget().getId());
intent.putExtra(LineActivity.EXTRA_NETWORK_ID, network.getId());
startActivity(intent);
}
});
lineLayout.setVisibility(View.VISIBLE);
TextView directionView = (TextView) view.findViewById(R.id.direction_view);
directionView.setText(Util.fromHtml(String.format(getString(R.string.frag_route_direction), lStep.getDirection().getName())));
if (lStep.getTarget().getUsualCarCount() < network.getUsualCarCount()) {
LinearLayout carsWarningLayout = (LinearLayout) view.findViewById(R.id.cars_warning_layout);
carsWarningLayout.setVisibility(View.VISIBLE);
}
if (mListener != null && mListener.getMainService() != null) {
Map<String, LineStatusCache.Status> statuses = mListener.getLineStatusCache().getLineStatus();
if (statuses.get(lStep.getTarget().getId()) != null && statuses.get(lStep.getTarget().getId()).down) {
LinearLayout disturbancesWarningLayout = (LinearLayout) view.findViewById(R.id.disturbances_warning_layout);
disturbancesWarningLayout.setVisibility(View.VISIBLE);
}
}
} else if (step instanceof ExitStep) {
view = getActivity().getLayoutInflater().inflate(R.layout.step_exit_network, layoutRoute, false);
int lineColor = step.getLine().getColor();
FrameLayout lineStripeLayout = (FrameLayout) view.findViewById(R.id.line_stripe_layout);
lineStripeLayout.setBackgroundColor(lineColor);
populateStationView(getActivity(), step.getStation(), view);
}
layoutRoute.addView(view);
}
if (layoutRoute.getChildCount() == 0) {
View view = getActivity().getLayoutInflater().inflate(R.layout.step_already_there, layoutRoute, false);
populateStationView(getActivity(), originPicker.getSelection(), view);
// TODO maybe revive this
/*if (originPicker.getSelection().getLine().getUsualCarCount() < network.getUsualCarCount() ||
destinationPicker.getSelection().getLine().getUsualCarCount() < network.getUsualCarCount()) {
LinearLayout carsWarningLayout = (LinearLayout) view.findViewById(R.id.cars_warning_layout);
carsWarningLayout.setVisibility(View.VISIBLE);
}*/
layoutRoute.addView(view);
}
if (network.isAboutToClose() && route.hasLineChange()) {
Formatter f = new Formatter();
DateUtils.formatDateRange(getContext(), f, network.getNextCloseTime(), network.getNextCloseTime(), DateUtils.FORMAT_SHOW_TIME, network.getTimezone().getID());
viewNetworkClosed.setText(String.format(getString(R.string.warning_network_about_to_close_transfers), f.toString(), destinationPicker.getSelection().getName()));
layoutNetworkClosed.setVisibility(View.VISIBLE);
} else
updateClosedWarning();
layoutInstructions.setVisibility(View.GONE);
layoutRoute.setVisibility(View.VISIBLE);
swapButton.setVisibility(View.VISIBLE);
if (realtimeEqualsNeutral) {
useRealtimeCheckbox.setVisibility(View.GONE);
} else {
useRealtimeCheckbox.setVisibility(View.VISIBLE);
}
int length = 0;
double realWeight = 0;
// TODO: switch to using route.getPath().getWeight() directly once DisturbanceAwareWeighter
// can actually provide proper weights for lines with disturbances, and not just some
// extremely large number
NeutralWeighter weighter = new NeutralWeighter();
for (Connection c : route.getPath().getEdgeList()) {
length += c.getWorldLength();
realWeight += weighter.getEdgeWeight(network, c);
}
if (length < 1000) {
routeEtaView.setText(String.format("%s (%d m)", DateUtils.formatElapsedTime((int) realWeight), length));
} else {
routeEtaView.setText(String.format("%s (%.01f km)", DateUtils.formatElapsedTime((int) realWeight), length / 1000.0));
}
SharedPreferences sharedPref = getContext().getSharedPreferences("settings", MODE_PRIVATE);
boolean locationEnabled = sharedPref.getBoolean(PreferenceNames.LocationEnable, true);
if (locationEnabled) {
layoutBottomSheet.setVisibility(View.VISIBLE);
}
}
Aggregations