Search in sources :

Example 1 with NeutralWeighter

use of im.tny.segvault.s2ls.routing.NeutralWeighter in project underlx by underlx.

the class RouteFragment method tryPlanRoute.

private void tryPlanRoute() {
    if (originPicker.getSelection() == null || destinationPicker.getSelection() == null) {
        // not enough information
        return;
    }
    Route realtimeRoute = Route.calculate(network, originPicker.getSelection(), destinationPicker.getSelection());
    Route neutralRoute = Route.calculate(network, originPicker.getSelection(), destinationPicker.getSelection(), new NeutralWeighter(), new NeutralQualifier());
    if (useRealtimeCheckbox.isChecked()) {
        route = realtimeRoute;
    } else {
        route = neutralRoute;
    }
    showRoute(realtimeRoute.getPath().getEdgeList().equals(neutralRoute.getPath().getEdgeList()));
}
Also used : NeutralWeighter(im.tny.segvault.s2ls.routing.NeutralWeighter) Route(im.tny.segvault.s2ls.routing.Route) NeutralQualifier(im.tny.segvault.s2ls.routing.NeutralQualifier)

Example 2 with NeutralWeighter

use of im.tny.segvault.s2ls.routing.NeutralWeighter 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);
    }
}
Also used : Formatter(java.util.Formatter) NeutralWeighter(im.tny.segvault.s2ls.routing.NeutralWeighter) ChangeLineStep(im.tny.segvault.s2ls.routing.ChangeLineStep) LineStatusCache(im.tny.segvault.disturbances.LineStatusCache) EnterStep(im.tny.segvault.s2ls.routing.EnterStep) ExitStep(im.tny.segvault.s2ls.routing.ExitStep) ChangeLineStep(im.tny.segvault.s2ls.routing.ChangeLineStep) Step(im.tny.segvault.s2ls.routing.Step) TextView(android.widget.TextView) SharedPreferences(android.content.SharedPreferences) Drawable(android.graphics.drawable.Drawable) Connection(im.tny.segvault.subway.Connection) EnterStep(im.tny.segvault.s2ls.routing.EnterStep) Intent(android.content.Intent) ImageView(android.widget.ImageView) StationPickerView(im.tny.segvault.disturbances.ui.widget.StationPickerView) View(android.view.View) TextView(android.widget.TextView) Date(java.util.Date) Line(im.tny.segvault.subway.Line) FrameLayout(android.widget.FrameLayout) ExitStep(im.tny.segvault.s2ls.routing.ExitStep) LineActivity(im.tny.segvault.disturbances.ui.activity.LineActivity) Map(java.util.Map) LinearLayout(android.widget.LinearLayout)

Aggregations

NeutralWeighter (im.tny.segvault.s2ls.routing.NeutralWeighter)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Drawable (android.graphics.drawable.Drawable)1 View (android.view.View)1 FrameLayout (android.widget.FrameLayout)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 LineStatusCache (im.tny.segvault.disturbances.LineStatusCache)1 LineActivity (im.tny.segvault.disturbances.ui.activity.LineActivity)1 StationPickerView (im.tny.segvault.disturbances.ui.widget.StationPickerView)1 ChangeLineStep (im.tny.segvault.s2ls.routing.ChangeLineStep)1 EnterStep (im.tny.segvault.s2ls.routing.EnterStep)1 ExitStep (im.tny.segvault.s2ls.routing.ExitStep)1 NeutralQualifier (im.tny.segvault.s2ls.routing.NeutralQualifier)1 Route (im.tny.segvault.s2ls.routing.Route)1 Step (im.tny.segvault.s2ls.routing.Step)1 Connection (im.tny.segvault.subway.Connection)1 Line (im.tny.segvault.subway.Line)1