Search in sources :

Example 11 with Line

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

the class MainService method handleDisturbanceNotification.

private void handleDisturbanceNotification(String network, String line, String id, String status, boolean downtime, long msgtime) {
    Log.d("MainService", "handleDisturbanceNotification");
    SharedPreferences sharedPref = getSharedPreferences("notifsettings", MODE_PRIVATE);
    Set<String> linePref = sharedPref.getStringSet(PreferenceNames.NotifsLines, null);
    Network snetwork;
    synchronized (lock) {
        if (!networks.containsKey(network)) {
            return;
        }
        snetwork = networks.get(network);
    }
    Line sline = snetwork.getLine(line);
    if (sline == null) {
        return;
    }
    if (downtime) {
        lineStatusCache.markLineAsDown(sline, new Date(msgtime));
    } else {
        lineStatusCache.markLineAsUp(sline);
    }
    if (linePref != null && !linePref.contains(line)) {
        // notifications disabled for this line
        return;
    }
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (!downtime && !sharedPref.getBoolean(PreferenceNames.NotifsServiceResumed, true)) {
        // notifications for normal service resumed disabled
        notificationManager.cancel(id.hashCode());
        return;
    }
    Realm realm = Application.getDefaultRealmInstance(this);
    for (NotificationRule rule : realm.where(NotificationRule.class).findAll()) {
        if (rule.isEnabled() && rule.applies(new Date(msgtime))) {
            realm.close();
            return;
        }
    }
    realm.close();
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(MainActivity.EXTRA_INITIAL_FRAGMENT, "nav_disturbances");
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    String title = String.format(getString(R.string.notif_disturbance_title), Util.getLineNames(this, sline)[0]);
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(title);
    bigTextStyle.bigText(status);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setStyle(bigTextStyle).setSmallIcon(R.drawable.ic_disturbance_notif).setColor(sline.getColor()).setContentTitle(title).setContentText(status).setAutoCancel(true).setWhen(msgtime).setSound(Uri.parse(sharedPref.getString(downtime ? PreferenceNames.NotifsRingtone : PreferenceNames.NotifsRegularizationRingtone, "content://settings/system/notification_sound"))).setVisibility(Notification.VISIBILITY_PUBLIC).setContentIntent(pendingIntent);
    if (sharedPref.getBoolean(downtime ? PreferenceNames.NotifsVibrate : PreferenceNames.NotifsRegularizationVibrate, false)) {
        notificationBuilder.setVibrate(new long[] { 0, 100, 100, 150, 150, 200 });
    } else {
        notificationBuilder.setVibrate(new long[] { 0l });
    }
    notificationManager.notify(id.hashCode(), notificationBuilder.build());
}
Also used : NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) SpannableString(android.text.SpannableString) Date(java.util.Date) Line(im.tny.segvault.subway.Line) NotificationRule(im.tny.segvault.disturbances.model.NotificationRule) Network(im.tny.segvault.subway.Network) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Realm(io.realm.Realm)

Example 12 with Line

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

the class SearchContentProvider method query.

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    if (!serviceBound) {
        return null;
    }
    String query = uri.getLastPathSegment();
    final String normalizedQuery = Normalizer.normalize(query.toString().toLowerCase().trim(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
    final List<ResultRow> results = new ArrayList<>();
    final String locale = Util.getCurrentLanguage(getContext());
    for (Station station : mainService.getAllStations()) {
        double distance = getDistance(station.getName(), normalizedQuery);
        if (station.getId().equals(query)) {
            // push to top of results
            distance = -5000;
        }
        for (String altName : station.getAltNames()) {
            double altDistance = getDistance(altName, normalizedQuery);
            if (altDistance < distance) {
                distance = altDistance;
            }
        }
        if (distance < Double.MAX_VALUE) {
            ResultRow row = new ResultRow();
            row.title = station.getName();
            row.subtitle = String.format(getContext().getString(R.string.search_station_subtitle), Util.getNetworkNames(getContext(), station.getNetwork())[0]);
            row.drawable = R.drawable.network_pt_ml;
            row.intentData = "station:" + station.getId();
            row.distance = distance;
            results.add(row);
        }
        for (Lobby lobby : station.getLobbies()) {
            if (lobby.getId().equals(query)) {
                for (Lobby.Exit exit : lobby.getExits()) {
                    results.add(buildResultRowForExit(station, lobby, exit, -5000, null));
                }
                break;
            }
            for (Lobby.Exit exit : lobby.getExits()) {
                boolean added = false;
                for (String street : exit.streets) {
                    distance = getDistance(street, normalizedQuery);
                    if (distance < Double.MAX_VALUE) {
                        results.add(buildResultRowForExit(station, lobby, exit, distance, street));
                        added = true;
                        break;
                    }
                }
                // do not add the same exit twice
                if (added)
                    break;
            }
        }
    }
    for (Line line : mainService.getAllLines()) {
        double distance = Double.MAX_VALUE;
        if (line.getId().equals(query)) {
            // push to top of results
            distance = -5000;
        }
        for (String name : Util.getLineNames(getContext(), line)) {
            double thisDistance = getDistance(name, normalizedQuery);
            if (thisDistance < distance) {
                distance = thisDistance;
            }
        }
        if (distance < Double.MAX_VALUE) {
            ResultRow row = new ResultRow();
            row.title = Util.getLineNames(getContext(), line)[0];
            row.subtitle = String.format(getContext().getString(R.string.search_line_subtitle), Util.getNetworkNames(getContext(), line.getNetwork())[0]);
            row.drawable = Util.getDrawableResourceIdForLineId(line.getId());
            row.intentData = "line:" + line.getId();
            row.distance = distance;
            results.add(row);
        }
    }
    for (POI poi : mainService.getAllPOIs()) {
        double distance = Double.MAX_VALUE;
        // it's unlikely anyone will search by POI ID, but let's support it anyway
        if (poi.getId().equals(query)) {
            // push to top of results
            distance = -5000;
        }
        for (String name : poi.getNames(locale)) {
            double thisDistance = getDistance(name, normalizedQuery);
            if (thisDistance < distance) {
                distance = thisDistance;
            }
        }
        if (distance < Double.MAX_VALUE) {
            ResultRow row = new ResultRow();
            row.title = poi.getNames(locale)[0];
            row.subtitle = String.format("%s \u2022 %s", getContext().getString(R.string.search_poi_subtitle), getContext().getString(Util.getStringResourceIdForPOIType(poi.getType())));
            row.drawable = R.drawable.ic_place_black_24dp;
            row.drawable2 = Util.getDrawableResourceIdForPOIType(poi.getType());
            row.intentData = "poi:" + poi.getId();
            row.distance = distance;
            results.add(row);
        }
    }
    // TODO search train services, bus services, trivia, etc.
    Collections.sort(results, new Comparator<ResultRow>() {

        @Override
        public int compare(ResultRow row, ResultRow t1) {
            return Double.compare(row.distance, t1.distance);
        }
    });
    if (results.size() == 0) {
        ResultRow row = new ResultRow();
        row.title = getContext().getString(R.string.search_no_results);
        row.drawable = R.drawable.ic_sentiment_dissatisfied_black_24dp;
        row.intentData = "no-results";
        row.distance = 0;
        results.add(row);
    }
    MatrixCursor cursor = new MatrixCursor(columns);
    int i = 0;
    for (ResultRow row : results) {
        Object[] cursorRow = { i++, row.title, row.subtitle, row.drawable, row.drawable2, row.intentData };
        cursor.addRow(cursorRow);
    }
    return cursor;
}
Also used : ArrayList(java.util.ArrayList) POI(im.tny.segvault.subway.POI) MatrixCursor(android.database.MatrixCursor) Station(im.tny.segvault.subway.Station) Line(im.tny.segvault.subway.Line) Lobby(im.tny.segvault.subway.Lobby)

Example 13 with Line

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

the class TopologyCache method loadNetwork.

public static Network loadNetwork(Context context, String id, String apiEndpoint) throws CacheException {
    String filename = "net-" + id;
    Topology t = null;
    try {
        FileInputStream fis = context.openFileInput(filename);
        ObjectInputStream is = new ObjectInputStream(fis);
        t = (Topology) is.readObject();
        is.close();
        fis.close();
    } catch (FileNotFoundException e) {
        throw new CacheException(e).addInfo("File " + filename + " not found");
    } catch (IOException e) {
        throw new CacheException(e).addInfo("IO exception");
    } catch (ClassNotFoundException e) {
        throw new CacheException(e).addInfo("Class not found");
    } catch (Exception e) {
        e.printStackTrace();
        throw new CacheException(e).addInfo("Unforeseen exception");
    }
    Network net = new Network(t.network.id, t.network.mainLocale, t.network.names, t.network.typCars, t.network.holidays, t.network.timezone, t.network.newsURL);
    for (API.POI poi : t.pois) {
        net.addPOI(new POI(poi.id, poi.type, poi.worldCoord, poi.webURL, poi.mainLocale, poi.names));
    }
    for (String lineid : t.network.lines) {
        API.Line l = t.lines.get(lineid);
        Line line = new Line(net, l.mainLocale, l.names, new HashSet<Stop>(), l.id, l.typCars, l.order);
        line.setColor(Color.parseColor("#" + l.color));
        boolean isFirstStationInLine = true;
        for (String sid : l.stations) {
            API.Station s = t.stations.get(sid);
            Station station = net.getStation(s.id);
            if (station == null) {
                Map<String, String> triviaURLs = new HashMap<>();
                for (Map.Entry<String, String> entry : s.triviaURLs.entrySet()) {
                    triviaURLs.put(entry.getKey(), apiEndpoint + entry.getValue());
                }
                Map<String, Map<String, String>> connURLs = new HashMap<>();
                for (Map.Entry<String, Map<String, String>> entry : s.connURLs.entrySet()) {
                    Map<String, String> urls = new HashMap<>();
                    for (Map.Entry<String, String> localeEntry : entry.getValue().entrySet()) {
                        urls.put(localeEntry.getKey(), apiEndpoint + localeEntry.getValue());
                    }
                    connURLs.put(entry.getKey(), urls);
                }
                station = new Station(net, s.id, s.name, s.altNames, new Station.Features(s.features.lift, s.features.bus, s.features.boat, s.features.train, s.features.airport), triviaURLs);
                station.setConnectionURLs(connURLs);
                // Lobbies
                for (String lid : s.lobbies) {
                    API.Lobby alobby = t.lobbies.get(lid);
                    Lobby lobby = new Lobby(alobby.id, alobby.name);
                    for (API.Exit aexit : alobby.exits) {
                        Lobby.Exit exit = new Lobby.Exit(aexit.id, aexit.worldCoord, aexit.streets, aexit.type);
                        lobby.addExit(exit);
                    }
                    for (API.Schedule asched : alobby.schedule) {
                        Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
                        lobby.addSchedule(sched);
                    }
                    station.addLobby(lobby);
                }
                // POIs
                for (String poiId : s.pois) {
                    POI poi = net.getPOI(poiId);
                    if (poi != null) {
                        station.addPOI(poi);
                    }
                }
            }
            Stop stop = new Stop(station, line);
            line.addVertex(stop);
            station.addVertex(stop);
            if (isFirstStationInLine) {
                line.setFirstStop(stop);
                isFirstStationInLine = false;
            }
            // WiFi APs
            for (API.WiFiAP w : s.wiFiAPs) {
                // take line affinity into account
                if (w.line.equals(line.getId())) {
                    WiFiLocator.addBSSIDforStop(stop, new BSSID(w.bssid));
                }
            }
        }
        for (API.Schedule asched : l.schedule) {
            Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
            line.addSchedule(sched);
        }
        for (API.WorldPath apath : l.worldPaths) {
            WorldPath path = new WorldPath(apath.id, apath.path);
            line.addPath(path);
        }
        net.addLine(line);
    }
    // Connections are within stations in the same line
    for (API.Connection c : t.connections) {
        Set<Stop> sFrom = net.getStation(c.from).vertexSet();
        Set<Stop> sTo = net.getStation(c.to).vertexSet();
        Stop from = null, to = null;
        for (Stop s : sFrom) {
            for (Stop s2 : sTo) {
                if (s.getLine().getId().equals(s2.getLine().getId())) {
                    from = s;
                    to = s2;
                }
            }
        }
        if (from != null && to != null) {
            Connection newConnection = net.addEdge(from, to);
            from.getLine().addEdge(from, to);
            newConnection.setTimes(new Connection.Times(c.typWaitS, c.typStopS, c.typS));
            newConnection.setWorldLength(c.worldLength);
            net.setEdgeWeight(newConnection, c.typS);
        }
    }
    for (API.Transfer tr : t.transfers) {
        Transfer newTransfer = new Transfer();
        // find stations with the right IDs for each line
        Station station = net.getStation(tr.station);
        for (Stop from : station.vertexSet()) {
            for (Stop to : station.vertexSet()) {
                if (from.getLine().getId().equals(tr.from) && to.getLine().getId().equals(tr.to)) {
                    net.addEdge(from, to, newTransfer);
                    net.setEdgeWeight(newTransfer, tr.typS);
                    newTransfer.setTimes(new Connection.Times(0, 0, tr.typS));
                }
            }
        }
    }
    for (API.Schedule asched : t.network.schedule) {
        Schedule sched = new Schedule(asched.holiday, asched.day, asched.open, asched.openTime * 1000, asched.duration * 1000);
        net.addSchedule(sched);
    }
    net.setDatasetAuthors(t.info.authors);
    net.setDatasetVersion(t.info.version);
    return net;
}
Also used : CacheException(im.tny.segvault.disturbances.exception.CacheException) Stop(im.tny.segvault.subway.Stop) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) Lobby(im.tny.segvault.subway.Lobby) Network(im.tny.segvault.subway.Network) Connection(im.tny.segvault.subway.Connection) IOException(java.io.IOException) POI(im.tny.segvault.subway.POI) WorldPath(im.tny.segvault.subway.WorldPath) FileInputStream(java.io.FileInputStream) CacheException(im.tny.segvault.disturbances.exception.CacheException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Line(im.tny.segvault.subway.Line) Station(im.tny.segvault.subway.Station) BSSID(im.tny.segvault.s2ls.wifi.BSSID) Schedule(im.tny.segvault.subway.Schedule) Transfer(im.tny.segvault.subway.Transfer) HashMap(java.util.HashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream)

Example 14 with Line

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

the class Route method getNextStep.

public Step getNextStep(Path currentPath) {
    if (currentPath == null) {
        return get(0);
    }
    List<Connection> cur = currentPath.getEdgeList();
    List<Connection> tar = path.getEdgeList();
    if (cur.size() == 0) {
        return get(0);
    }
    // iterate over the target path until the last edge of the current path is found
    Connection lastCur = cur.get(cur.size() - 1);
    int tarIdx;
    for (tarIdx = 0; tarIdx < tar.size(); tarIdx++) {
        if (tar.get(tarIdx) == lastCur) {
            break;
        }
    }
    if (tarIdx >= tar.size()) {
        // so at the moment the user is not following the instructions
        if (get(0) instanceof EnterStep) {
            // if user is already on a path on the right direction, do not tell him to "catch a train" he is already in
            Stop direction = null;
            if (path.getGraph() instanceof Network) {
                direction = ((Network) path.getGraph()).getDirectionForConnection(lastCur);
            } else if (path.getGraph() instanceof Line) {
                direction = ((Line) path.getGraph()).getDirectionForConnection(lastCur);
            }
            if (direction != null && ((EnterStep) get(0)).getDirection() == direction.getStation()) {
                return get(1);
            }
        }
        return get(0);
    }
    // find next step
    for (; tarIdx < tar.size(); tarIdx++) {
        if (pathIndexToStep.get(tarIdx) != null) {
            return pathIndexToStep.get(tarIdx);
        }
    }
    return null;
}
Also used : Line(im.tny.segvault.subway.Line) Stop(im.tny.segvault.subway.Stop) Network(im.tny.segvault.subway.Network) Connection(im.tny.segvault.subway.Connection)

Example 15 with Line

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

the class MainActivity method onLineLinkClicked.

public void onLineLinkClicked(String destination) {
    if (locService != null) {
        for (Network network : locService.getNetworks()) {
            Line line;
            if ((line = network.getLine(destination)) != null) {
                Intent intent = new Intent(this, LineActivity.class);
                intent.putExtra(LineActivity.EXTRA_LINE_ID, line.getId());
                intent.putExtra(LineActivity.EXTRA_NETWORK_ID, network.getId());
                startActivity(intent);
                return;
            }
        }
    }
}
Also used : Line(im.tny.segvault.subway.Line) Network(im.tny.segvault.subway.Network) Intent(android.content.Intent)

Aggregations

Line (im.tny.segvault.subway.Line)18 Network (im.tny.segvault.subway.Network)8 Intent (android.content.Intent)5 Connection (im.tny.segvault.subway.Connection)5 SharedPreferences (android.content.SharedPreferences)4 Lobby (im.tny.segvault.subway.Lobby)4 Station (im.tny.segvault.subway.Station)4 Stop (im.tny.segvault.subway.Stop)4 ArrayList (java.util.ArrayList)4 View (android.view.View)3 FrameLayout (android.widget.FrameLayout)3 LinearLayout (android.widget.LinearLayout)3 TextView (android.widget.TextView)3 POI (im.tny.segvault.subway.POI)3 WorldPath (im.tny.segvault.subway.WorldPath)3 FileInputStream (java.io.FileInputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3