Search in sources :

Example 1 with Path

use of im.tny.segvault.s2ls.Path 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);
}
Also used : Path(im.tny.segvault.s2ls.Path) Stop(im.tny.segvault.subway.Stop) Connection(im.tny.segvault.subway.Connection) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Pair(android.util.Pair)

Example 2 with Path

use of im.tny.segvault.s2ls.Path in project underlx by underlx.

the class TripFragment method refreshUI.

private void refreshUI() {
    Network network = this.network;
    if (network == null) {
        return;
    }
    Realm realm = Application.getDefaultRealmInstance(getContext());
    Trip trip = realm.where(Trip.class).equalTo("id", tripId).findFirst();
    if (trip == null) {
        return;
    }
    Path path = trip.toConnectionPath(network);
    Station origin = path.getStartVertex().getStation();
    Station dest = path.getEndVertex().getStation();
    SpannableStringBuilder builder = new SpannableStringBuilder();
    if (path.getEdgeList().size() == 0) {
        builder.append("#");
        builder.setSpan(new ImageSpan(getActivity(), R.drawable.ic_beenhere_black_24dp), builder.length() - 1, builder.length(), 0);
        builder.append(" " + origin.getName());
    } else {
        builder.append(origin.getName() + " ").append("#");
        builder.setSpan(new ImageSpan(getActivity(), R.drawable.ic_arrow_forward_black_24dp), builder.length() - 1, builder.length(), 0);
        builder.append(" " + dest.getName());
    }
    stationNamesView.setText(builder);
    dateView.setText(DateUtils.formatDateTime(getContext(), path.getEntryTime(0).getTime(), DateUtils.FORMAT_SHOW_DATE));
    int length = path.getTimeablePhysicalLength();
    long time = path.getMovementMilliseconds();
    if (length == 0 || time == 0) {
        statsLayout.setVisibility(View.GONE);
    } else {
        statsView.setText(String.format(getString(R.string.frag_trip_stats), length, DateUtils.formatElapsedTime(time / 1000), (((double) length / (double) (time / 1000)) * 3.6)));
    }
    populatePathView(getContext(), inflater, path, layoutRoute, true);
    if (!trip.canBeCorrected()) {
        correctButton.setVisibility(View.GONE);
    }
    realm.close();
}
Also used : Path(im.tny.segvault.s2ls.Path) Station(im.tny.segvault.subway.Station) Trip(im.tny.segvault.disturbances.model.Trip) Network(im.tny.segvault.subway.Network) Realm(io.realm.Realm) SpannableStringBuilder(android.text.SpannableStringBuilder) ImageSpan(android.text.style.ImageSpan)

Example 3 with Path

use of im.tny.segvault.s2ls.Path in project underlx by underlx.

the class TripCorrectionActivity method redrawPath.

private void redrawPath() {
    newPath = new Path(originalPath);
    boolean hasChanges = partsDeleted;
    if (startPicker.getSelection() != null && !startPicker.getSelection().isAlwaysClosed() && startPicker.getSelection() != originalPath.getStartVertex().getStation()) {
        newPath.manualExtendStart(startPicker.getSelection().getStops().iterator().next());
        hasChanges = true;
    }
    if (endPicker.getSelection() != null && !endPicker.getSelection().isAlwaysClosed() && endPicker.getSelection() != originalPath.getEndVertex().getStation()) {
        newPath.manualExtendEnd(endPicker.getSelection().getStops().iterator().next());
        hasChanges = true;
    }
    TripFragment.populatePathView(this, getLayoutInflater(), newPath, pathLayout, false);
    final ViewTreeObserver vto = pathLayout.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            public void onGlobalLayout() {
                buttonsLayout.removeAllViews();
                int manualOffset = 0;
                for (int i = 0; i < pathLayout.getChildCount(); i++) {
                    View stepview = getLayoutInflater().inflate(R.layout.path_button, buttonsLayout, false);
                    if (newPath.getManualEntry(i)) {
                        manualOffset++;
                    } else {
                        final int idxOnTrip = i - manualOffset;
                        if (canRemoveVertex(idxOnTrip)) {
                            stepview.findViewById(R.id.delete_button).setVisibility(View.VISIBLE);
                            stepview.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    removeVertex(idxOnTrip);
                                }
                            });
                        }
                    }
                    stepview.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, pathLayout.getChildAt(i).getHeight()));
                    buttonsLayout.addView(stepview);
                }
                // remove the listener... or we'll be doing this a lot.
                ViewTreeObserver obs = pathLayout.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
            }
        });
    }
    if (hasChanges) {
        saveButton.setText(getString(R.string.act_trip_correction_save));
    } else {
        saveButton.setText(getString(R.string.act_trip_correction_correct));
    }
}
Also used : Path(im.tny.segvault.s2ls.Path) ViewTreeObserver(android.view.ViewTreeObserver) StationPickerView(im.tny.segvault.disturbances.ui.widget.StationPickerView) View(android.view.View)

Example 4 with Path

use of im.tny.segvault.s2ls.Path 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

Path (im.tny.segvault.s2ls.Path)4 Stop (im.tny.segvault.subway.Stop)2 ArrayList (java.util.ArrayList)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 ImageSpan (android.text.style.ImageSpan)1 StyleSpan (android.text.style.StyleSpan)1 Pair (android.util.Pair)1 View (android.view.View)1 ViewTreeObserver (android.view.ViewTreeObserver)1 Trip (im.tny.segvault.disturbances.model.Trip)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