Search in sources :

Example 11 with Station

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

the class ExtraContentCache method cacheAllExtras.

public static void cacheAllExtras(final Context context, final OnCacheAllListener listener, Network network) {
    String[] types = new String[5];
    types[0] = Station.CONNECTION_TYPE_BOAT;
    types[1] = Station.CONNECTION_TYPE_BUS;
    types[2] = Station.CONNECTION_TYPE_TRAIN;
    types[3] = Station.CONNECTION_TYPE_PARK;
    types[4] = Station.CONNECTION_TYPE_BIKE;
    Locale l = Util.getCurrentLocale(context);
    int total = 0;
    Collection<Station> stations = network.getStations();
    for (Station station : stations) {
        for (String type : types) {
            String lang = l.getLanguage();
            String url = station.getConnectionURLforLocale(type, lang);
            if (url == null) {
                lang = "en";
                url = station.getConnectionURLforLocale(type, lang);
                if (url != null) {
                    total++;
                }
            } else {
                total++;
            }
        }
        // for trivia
        total++;
    }
    final int finalTotal = total;
    listener.onProgress(0, finalTotal);
    final Station[] stationsArray = stations.toArray(new Station[stations.size()]);
    new RetrieveConnectionInfoTask(context, new OnConnectionInfoReadyListener() {

        private int lastCurrent = 0;

        @Override
        public void onSuccess(List<String> connectionInfo) {
            new RetrieveTriviaTask(context, new OnTriviaReadyListener() {

                @Override
                public void onSuccess(List<String> trivia) {
                    listener.onSuccess();
                }

                @Override
                public void onProgress(int current) {
                    listener.onProgress(lastCurrent + current, finalTotal);
                }

                @Override
                public void onFailure() {
                    listener.onFailure();
                }
            }).execute(stationsArray);
        }

        @Override
        public void onProgress(int current) {
            listener.onProgress(current, finalTotal);
            lastCurrent = current;
        }

        @Override
        public void onFailure() {
            listener.onFailure();
        }
    }, types).execute(stationsArray);
}
Also used : Locale(java.util.Locale) Station(im.tny.segvault.subway.Station) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with Station

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

the class StationPickerView method initializeViews.

/**
 * Inflates the views in the layout.
 *
 * @param context the current context for the view.
 */
private void initializeViews(Context context) {
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.station_picker_view, this);
    textView = (InstantAutoComplete) findViewById(R.id.text_station);
    clearButton = (ImageButton) findViewById(R.id.button_clear);
    myLocationButton = (ImageButton) findViewById(R.id.button_my_location);
    textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
            selection = (Station) parent.getItemAtPosition(pos);
            weakSelection = false;
            if (onStationSelectedListener != null) {
                onStationSelectedListener.onStationSelected(selection);
            }
        }
    });
    textView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            textView.showDropDown();
        }
    });
    clearButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            textView.setText("");
            focusOnEntry();
        }
    });
    textView.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            selection = null;
            if (onSelectionLostListener != null) {
                onSelectionLostListener.onSelectionLost();
            }
            weakSelection = false;
            showHideClearButton();
        }
    });
    textView.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (weakSelection && hasFocus) {
                textView.setText("");
            }
            showHideClearButton();
        }
    });
    if (!this.allowMyLocation) {
        myLocationButton.setVisibility(GONE);
    } else if (clearButton.getVisibility() == GONE) {
        myLocationButton.setVisibility(VISIBLE);
    }
    myLocationButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - TimeUnit.MINUTES.toMillis(1)) {
                    // use the recent location fix
                    selectNearestStation(location);
                } else {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, StationPickerView.this);
                }
            }
        }
    });
}
Also used : ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) Station(im.tny.segvault.subway.Station) LayoutInflater(android.view.LayoutInflater) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) AdapterView(android.widget.AdapterView) Location(android.location.Location)

Example 13 with Station

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

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

the class StationPickerView method selectNearestStation.

public void selectNearestStation(Location location) {
    float minDistance = Float.MAX_VALUE;
    Station minStation = null;
    if (adapter == null) {
        return;
    }
    for (Station station : adapter.stations) {
        if (station.isAlwaysClosed()) {
            continue;
        }
        for (Lobby lobby : station.getLobbies()) {
            for (Lobby.Exit exit : lobby.getExits()) {
                Location exitLocation = new Location("");
                exitLocation.setLatitude(exit.worldCoord[0]);
                exitLocation.setLongitude(exit.worldCoord[1]);
                float distance = location.distanceTo(exitLocation);
                if (distance < minDistance || minStation == null) {
                    minDistance = distance;
                    minStation = station;
                }
            }
        }
    }
    setSelection(minStation);
}
Also used : Station(im.tny.segvault.subway.Station) Lobby(im.tny.segvault.subway.Lobby) Location(android.location.Location)

Example 15 with Station

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

the class StationPOIFragment method update.

private void update() {
    if (mListener == null)
        return;
    MainService service = mListener.getMainService();
    if (service == null)
        return;
    Network net = service.getNetwork(networkId);
    if (net == null)
        return;
    final Station station = net.getStation(stationId);
    if (station == null)
        return;
    Locale l = Util.getCurrentLocale(getContext());
    final String lang = l.getLanguage();
    // POIs
    pois = station.getPOIs();
    Collections.sort(pois, new Comparator<POI>() {

        @Override
        public int compare(POI o1, POI o2) {
            return o1.getNames(lang)[0].compareTo(o2.getNames(lang)[0]);
        }
    });
    for (POI poi : pois) {
        POIView v = new POIView(getContext(), poi);
        v.setInteractionListener(new POIView.OnViewInteractionListener() {

            @Override
            public void onPOIClicked(POI poi) {
                if (googleMap == null || !mapLayoutReady) {
                    return;
                }
                Marker marker = markers.get(poi);
                if (marker != null) {
                    marker.showInfoWindow();
                }
                poiScrollView.fullScroll(NestedScrollView.FOCUS_UP);
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(CameraPosition.fromLatLngZoom(marker.getPosition(), googleMap.getCameraPosition().zoom)));
            }
        });
        poisLayout.addView(v);
    }
    // Lobbies
    final int[] lobbyColors = Util.lobbyColors.clone();
    if (station.getLobbies().size() == 1) {
        lobbyColors[0] = Color.BLACK;
    }
    final ViewTreeObserver vto = mapView.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            public void onGlobalLayout() {
                mapLayoutReady = true;
                trySetupMap(station, pois, lobbyColors, lang);
                // remove the listener... or we'll be doing this a lot.
                ViewTreeObserver obs = mapView.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
            }
        });
    }
    mapView.getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;
            trySetupMap(station, pois, lobbyColors, lang);
        }
    });
}
Also used : Locale(java.util.Locale) POIView(im.tny.segvault.disturbances.ui.widget.POIView) OnMapReadyCallback(com.google.android.gms.maps.OnMapReadyCallback) POI(im.tny.segvault.subway.POI) Marker(com.google.android.gms.maps.model.Marker) MainService(im.tny.segvault.disturbances.MainService) Station(im.tny.segvault.subway.Station) GoogleMap(com.google.android.gms.maps.GoogleMap) Network(im.tny.segvault.subway.Network) ViewTreeObserver(android.view.ViewTreeObserver)

Aggregations

Station (im.tny.segvault.subway.Station)19 View (android.view.View)7 Network (im.tny.segvault.subway.Network)7 TextView (android.widget.TextView)6 MainService (im.tny.segvault.disturbances.MainService)5 Stop (im.tny.segvault.subway.Stop)5 ArrayList (java.util.ArrayList)5 Intent (android.content.Intent)4 ImageView (android.widget.ImageView)4 Line (im.tny.segvault.subway.Line)4 Lobby (im.tny.segvault.subway.Lobby)4 Realm (io.realm.Realm)4 Connection (im.tny.segvault.subway.Connection)3 POI (im.tny.segvault.subway.POI)3 Location (android.location.Location)2 ViewTreeObserver (android.view.ViewTreeObserver)2 FrameLayout (android.widget.FrameLayout)2 GoogleMap (com.google.android.gms.maps.GoogleMap)2 OnMapReadyCallback (com.google.android.gms.maps.OnMapReadyCallback)2 Marker (com.google.android.gms.maps.model.Marker)2