Search in sources :

Example 11 with Stop

use of im.tny.segvault.subway.Stop in project underlx by underlx.

the class HomeFragment method refreshCurrentTrip.

private void refreshCurrentTrip() {
    if (mListener == null)
        return;
    final MainService m = mListener.getMainService();
    if (m == null)
        return;
    S2LS loc = m.getS2LS(MainService.PRIMARY_NETWORK_ID);
    if (loc == null || loc.getCurrentTrip() == null) {
        ongoingTripCard.setVisibility(View.GONE);
    } else {
        final Station station = loc.getCurrentTrip().getCurrentStop().getStation();
        curStationNameView.setText(station.getName());
        Stop direction = loc.getCurrentTrip().getDirection();
        Stop next = loc.getCurrentTrip().getNextStop();
        if (direction != null && next != null) {
            directionView.setText(String.format(getString(R.string.frag_home_trip_direction), direction.getStation().getName()));
            nextStationView.setText(String.format(getString(R.string.frag_home_trip_next_station), next.getStation().getName()));
            Stop likelyExit = m.getLikelyNextExit(loc.getCurrentTrip().getEdgeList(), 1);
            int resId = android.support.v7.appcompat.R.style.TextAppearance_AppCompat_Small;
            if (next == likelyExit) {
                resId = android.support.v7.appcompat.R.style.TextAppearance_AppCompat_Medium;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                nextStationView.setTextAppearance(resId);
            } else {
                nextStationView.setTextAppearance(getContext(), resId);
            }
            directionView.setVisibility(View.VISIBLE);
            nextStationView.setVisibility(View.VISIBLE);
        } else {
            directionView.setVisibility(View.GONE);
            nextStationView.setVisibility(View.GONE);
        }
        redrawCurrentStationLineIcons(station);
        curStationLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), StationActivity.class);
                intent.putExtra(StationActivity.EXTRA_STATION_ID, station.getId());
                intent.putExtra(StationActivity.EXTRA_NETWORK_ID, MainService.PRIMARY_NETWORK_ID);
                startActivity(intent);
            }
        });
        curTripIncorrectLocationButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                new FeedbackUtil.IncorrectLocation(getContext(), m, station).showReportWizard();
            }
        });
        if (loc.canRequestEndOfTrip()) {
            curTripEndButton.setVisibility(View.VISIBLE);
            curTripIncorrectLocationButton.setVisibility(View.GONE);
        } else {
            curTripEndButton.setVisibility(View.GONE);
            curTripIncorrectLocationButton.setVisibility(View.VISIBLE);
        }
        ongoingTripCard.setVisibility(View.VISIBLE);
    }
}
Also used : MainService(im.tny.segvault.disturbances.MainService) S2LS(im.tny.segvault.s2ls.S2LS) Station(im.tny.segvault.subway.Station) Stop(im.tny.segvault.subway.Stop) StationActivity(im.tny.segvault.disturbances.ui.activity.StationActivity) Intent(android.content.Intent) View(android.view.View) CardView(android.support.v7.widget.CardView) TextView(android.widget.TextView)

Example 12 with Stop

use of im.tny.segvault.subway.Stop 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)

Example 13 with Stop

use of im.tny.segvault.subway.Stop in project underlx by underlx.

the class MainService method getLikelyNextExit.

public Stop getLikelyNextExit(List<Connection> path, double threshold) {
    if (path.size() == 0) {
        return null;
    }
    // get the line for the latest connection
    Connection last = path.get(path.size() - 1);
    Line line = null;
    for (Line l : getAllLines()) {
        if (l.edgeSet().contains(last)) {
            line = l;
            break;
        }
    }
    if (line == null) {
        return null;
    }
    Set<Stop> alreadyVisited = new HashSet<>();
    for (Connection c : path) {
        alreadyVisited.add(c.getSource());
        alreadyVisited.add(c.getTarget());
    }
    // get all the stops till the end of the line, after the given connection
    // (or in the case of circular lines, all stops of the line)
    Stop maxStop = null;
    double max = 0;
    Set<Stop> stops = new HashSet<>();
    while (stops.add(last.getSource())) {
        Stop curStop = last.getTarget();
        if (!alreadyVisited.contains(curStop)) {
            double r = getLeaveTrainFactorForStop(curStop);
            if (maxStop == null || r > max) {
                maxStop = curStop;
                max = r;
            }
        }
        if (line.outDegreeOf(curStop) == 1) {
            break;
        }
        for (Connection outedge : line.outgoingEdgesOf(curStop)) {
            if (!stops.contains(outedge.getTarget())) {
                last = outedge;
                break;
            }
        }
    }
    if (max < threshold) {
        // most relevant station is not relevant enough
        return null;
    }
    return maxStop;
}
Also used : Line(im.tny.segvault.subway.Line) Stop(im.tny.segvault.subway.Stop) Connection(im.tny.segvault.subway.Connection) HashSet(java.util.HashSet)

Example 14 with Stop

use of im.tny.segvault.subway.Stop in project underlx by underlx.

the class MainService method dumpDebugInfo.

// DEBUG:
public String dumpDebugInfo() {
    String s = "Service created on " + creationDate.toString();
    s += (wfc.isScanning() ? String.format(". WFC scanning every %d s", wfc.getScanInterval() / 1000) : ". WFC not scanning") + "\n";
    for (Network n : getNetworks()) {
        S2LS loc;
        synchronized (lock) {
            loc = locServices.get(n.getId());
        }
        s += "Network " + n.getNames("en")[0] + "\n";
        s += "State machine: " + loc.getState().toString() + "\n";
        s += String.format("\tIn network? %b\n\tNear network? %b\n", loc.inNetwork(), loc.nearNetwork());
        if (loc.getState() instanceof InNetworkState) {
            s += "\tPossible stops:\n";
            for (Stop stop : loc.getLocation().vertexSet()) {
                s += String.format("\t\t%s (%s)\n", stop.getStation().getName(), stop.getLine().getNames("en")[0]);
            }
            s += "\tPath:\n";
            if (loc.getCurrentTrip() != null) {
                for (Connection c : loc.getCurrentTrip().getEdgeList()) {
                    s += String.format("\t\t%s -> %s\n", c.getSource().toString(), c.getTarget().toString());
                }
                if (loc.getCurrentTargetRoute() != null) {
                    s += String.format("\t\tCurrent path complies? %b\n", loc.getCurrentTargetRoute().checkPathCompliance(loc.getCurrentTrip()));
                }
            }
        }
    }
    try {
        API.AuthTest authTest = API.getInstance().getAuthTest();
        s += "API auth test result: " + authTest.result + "\n";
        s += "API key as perceived by server: " + authTest.key + "\n";
    } catch (APIException e) {
        e.printStackTrace();
        s += "Error testing API authentication\n";
    }
    synchronizer.attemptSync();
    return s;
}
Also used : S2LS(im.tny.segvault.s2ls.S2LS) APIException(im.tny.segvault.disturbances.exception.APIException) Stop(im.tny.segvault.subway.Stop) Network(im.tny.segvault.subway.Network) Connection(im.tny.segvault.subway.Connection) SpannableString(android.text.SpannableString) InNetworkState(im.tny.segvault.s2ls.InNetworkState)

Example 15 with Stop

use of im.tny.segvault.subway.Stop in project underlx by underlx.

the class MainService method mockLocation.

public void mockLocation(Station station) {
    if (BuildConfig.DEBUG && station.getStops().size() > 0) {
        List<BSSID> bssids = new ArrayList<>();
        for (Stop s : station.getStops()) {
            bssids.addAll(WiFiLocator.getBSSIDsForStop(s));
        }
        wfc.updateBSSIDsDebug(bssids);
    }
}
Also used : BSSID(im.tny.segvault.s2ls.wifi.BSSID) Stop(im.tny.segvault.subway.Stop) ArrayList(java.util.ArrayList)

Aggregations

Stop (im.tny.segvault.subway.Stop)15 ArrayList (java.util.ArrayList)8 Connection (im.tny.segvault.subway.Connection)6 Station (im.tny.segvault.subway.Station)5 Line (im.tny.segvault.subway.Line)4 Network (im.tny.segvault.subway.Network)4 Intent (android.content.Intent)3 S2LS (im.tny.segvault.s2ls.S2LS)3 HashSet (java.util.HashSet)3 SpannableString (android.text.SpannableString)2 View (android.view.View)2 TextView (android.widget.TextView)2 CacheException (im.tny.segvault.disturbances.exception.CacheException)2 Path (im.tny.segvault.s2ls.Path)2 BSSID (im.tny.segvault.s2ls.wifi.BSSID)2 Date (java.util.Date)2 PendingIntent (android.app.PendingIntent)1 ServiceConnection (android.content.ServiceConnection)1 GradientDrawable (android.graphics.drawable.GradientDrawable)1 NotificationCompat (android.support.v4.app.NotificationCompat)1