Search in sources :

Example 1 with ExitStep

use of im.tny.segvault.s2ls.routing.ExitStep 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)

Example 2 with ExitStep

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

the class MainService method updateRouteNotification.

private void updateRouteNotification(S2LS loc, boolean highPriorityNotification) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(MainActivity.EXTRA_INITIAL_FRAGMENT, "nav_home");
    PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
    Path currentPath = loc.getCurrentTrip();
    Route currentRoute = loc.getCurrentTargetRoute();
    if (currentPath == null && currentRoute == null) {
        Log.e("MainService", "Attempt to create notification when there's no path or planned route");
        return;
    }
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    CharSequence title = "";
    List<CharSequence> statusLines = new ArrayList<>();
    int color = -1;
    if (currentRoute != null) {
        inboxStyle.setSummaryText(String.format(getString(R.string.notif_route_navigating_status), currentRoute.getTarget().getName()));
        Step nextStep = currentRoute.getNextStep(currentPath);
        if (nextStep instanceof EnterStep) {
            if (currentPath != null && currentPath.getCurrentStop() != null && currentRoute.checkPathStartsRoute(currentPath)) {
                title = String.format(getString(R.string.notif_route_catch_train_title), ((EnterStep) nextStep).getDirection().getName(12));
            } else {
                title = String.format(getString(R.string.notif_route_enter_station_title), nextStep.getStation().getName(15));
            }
            // TODO: show "encurtamentos" warnings here if applicable
            statusLines.add(String.format(getString(R.string.notif_route_catch_train_status), ((EnterStep) nextStep).getDirection().getName()));
            color = currentRoute.getSourceStop().getLine().getColor();
        } else if (nextStep instanceof ChangeLineStep) {
            ChangeLineStep clStep = (ChangeLineStep) nextStep;
            String lineName = Util.getLineNames(this, clStep.getTarget())[0];
            String titleStr;
            if (currentPath != null && currentPath.getCurrentStop() != null && currentPath.getCurrentStop().getStation() == nextStep.getStation()) {
                titleStr = String.format(getString(R.string.notif_route_catch_train_line_change_title), lineName);
            } else {
                titleStr = String.format(getString(R.string.notif_route_line_change_title), nextStep.getStation().getName(10), lineName);
            }
            int lStart = titleStr.indexOf(lineName);
            int lEnd = lStart + lineName.length();
            Spannable sb = new SpannableString(titleStr);
            sb.setSpan(new ForegroundColorSpan(clStep.getTarget().getColor()), lStart, lEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            title = sb;
            // TODO: show "encurtamentos" warnings here if applicable
            sb = new SpannableString(String.format(getString(R.string.notif_route_catch_train_status), clStep.getDirection().getName()));
            sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            statusLines.add(sb);
            color = clStep.getTarget().getColor();
        } else if (nextStep instanceof ExitStep) {
            if (currentPath != null && currentPath.getCurrentStop().getStation() == nextStep.getStation()) {
                title = getString(R.string.notif_route_leave_train_now);
            } else if (currentPath != null && currentPath.getNextStop() != null && new Date().getTime() - currentPath.getCurrentStopEntryTime().getTime() > 30 * 1000 && nextStep.getStation() == currentPath.getNextStop().getStation()) {
                title = getString(R.string.notif_route_leave_train_next);
            } else {
                title = String.format(getString(R.string.notif_route_leave_train), nextStep.getStation().getName(20));
            }
        }
    }
    if (currentPath != null) {
        Stop curStop = currentPath.getCurrentStop();
        if (color == -1) {
            color = curStop.getLine().getColor();
        }
        if (currentRoute != null) {
            statusLines.add(String.format(getString(R.string.notif_route_current_station), curStop.getStation().getName()));
        } else {
            title = curStop.getStation().getName();
        }
        if (currentPath.isWaitingFirstTrain()) {
            statusLines.add(getString(R.string.notif_route_waiting));
        } else {
            Stop direction = currentPath.getDirection();
            Stop next = currentPath.getNextStop();
            if (direction != null && next != null) {
                statusLines.add(String.format(getString(R.string.notif_route_next_station), next.getStation().getName()));
                if (currentRoute == null) {
                    statusLines.add(String.format(getString(R.string.notif_route_direction), direction.getStation().getName()));
                }
            } else {
                statusLines.add(getString(R.string.notif_route_left_station));
            }
        }
    }
    CharSequence singleLineStatus = "";
    for (CharSequence s : statusLines) {
        inboxStyle.addLine(s);
        singleLineStatus = TextUtils.concat(singleLineStatus, s) + " | ";
    }
    singleLineStatus = singleLineStatus.subSequence(0, singleLineStatus.length() - 3);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setStyle(inboxStyle).setColor(color).setContentTitle(title).setContentText(singleLineStatus).setAutoCancel(false).setContentIntent(pendingIntent).setVisibility(Notification.VISIBILITY_PUBLIC).setOngoing(true);
    if (highPriorityNotification) {
        stopForeground(true);
        notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
        notificationBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
    }
    if (currentRoute != null) {
        notificationBuilder.setSmallIcon(R.drawable.ic_navigation_white_24dp);
        Intent stopIntent = new Intent(this, MainService.class);
        stopIntent.setAction(ACTION_END_NAVIGATION);
        stopIntent.putExtra(EXTRA_NAVIGATION_NETWORK, loc.getNetwork().getId());
        PendingIntent pendingStopIntent = PendingIntent.getService(this, (int) System.currentTimeMillis(), stopIntent, 0);
        notificationBuilder.addAction(R.drawable.ic_close_black_24dp, getString(R.string.notif_route_end_navigation), pendingStopIntent);
    } else {
        notificationBuilder.setSmallIcon(R.drawable.ic_trip_notif);
        if (loc.canRequestEndOfTrip()) {
            Intent stopIntent = new Intent(this, MainService.class);
            stopIntent.setAction(ACTION_END_TRIP);
            stopIntent.putExtra(EXTRA_TRIP_NETWORK, loc.getNetwork().getId());
            PendingIntent pendingStopIntent = PendingIntent.getService(this, (int) System.currentTimeMillis(), stopIntent, 0);
            notificationBuilder.addAction(R.drawable.ic_stop_black_24dp, getString(R.string.notif_route_end_trip), pendingStopIntent);
        }
    }
    startForeground(ROUTE_NOTIFICATION_ID, notificationBuilder.build());
}
Also used : Path(im.tny.segvault.s2ls.Path) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Stop(im.tny.segvault.subway.Stop) ArrayList(java.util.ArrayList) EnterStep(im.tny.segvault.s2ls.routing.EnterStep) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) ChangeLineStep(im.tny.segvault.s2ls.routing.ChangeLineStep) 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) SpannableString(android.text.SpannableString) Date(java.util.Date) SpannableString(android.text.SpannableString) StyleSpan(android.text.style.StyleSpan) NotificationCompat(android.support.v4.app.NotificationCompat) ExitStep(im.tny.segvault.s2ls.routing.ExitStep) PendingIntent(android.app.PendingIntent) Route(im.tny.segvault.s2ls.routing.Route) Spannable(android.text.Spannable)

Aggregations

Intent (android.content.Intent)2 ChangeLineStep (im.tny.segvault.s2ls.routing.ChangeLineStep)2 EnterStep (im.tny.segvault.s2ls.routing.EnterStep)2 ExitStep (im.tny.segvault.s2ls.routing.ExitStep)2 Step (im.tny.segvault.s2ls.routing.Step)2 Date (java.util.Date)2 PendingIntent (android.app.PendingIntent)1 SharedPreferences (android.content.SharedPreferences)1 Drawable (android.graphics.drawable.Drawable)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 StyleSpan (android.text.style.StyleSpan)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