/** * Returns a HashMap between station names and latitude-longitude coordinates. * * @return A HashMap<String, LatLng> station mapping */ public HashMap<String, LatLng> getStationLatLngMap() { HashMap<String, LatLng> stationMap = new HashMap<>(); for (Station s : mBService.getStations()) { Double sLat = Double.parseDouble(s.getLatitude()); Double sLng = Double.parseDouble(s.getLongitude()); stationMap.put(s.getName(), new LatLng(sLat, sLng)); } return stationMap; }
/** * Returns a String containing trip fares to be displayed as the snippet for the map maker of the * specified destination station. * * @param dest The name of the destination station. * @return The String to display. */ public String generateSnippet(String dest) { Station destStation = mBService.lookupStationByName(dest); Station origStation; if (originStation != null) { origStation = originStation; } else { origStation = mBService.lookupStationByAbbreviation("DBRK"); } DateFormat df = new SimpleDateFormat("hh:mma", Locale.US); Date now = Calendar.getInstance(TimeZone.getDefault()).getTime(); Trip mTrip = mBService.generateTrip(origStation, destStation, df.format(now)); float fare = mTrip.getFare(); DecimalFormat decim = new DecimalFormat("0.00"); String fareOneWay = decim.format(fare); String fareRoundTrip = decim.format(2 * fare); String mSnippet = "$" + fareOneWay + " | $" + fareRoundTrip; return mSnippet; }
/** Generates station list with which to populate the "All Stations" tab's Spinner. */ public void createAllStationsHash() { int len = stationLatLngMap.size(); allStationsList = new ArrayList<>(len); for (Station s : mBService.getStations()) { allStationsList.add(s.getName()); } Collections.sort(allStationsList); int count = 0; for (String station : allStationsList) { allStationsHash.put(count, station); fullHash.put(station, count); count++; } }
/** * Sets the global Station object, originStation, to the station nearest to the specified * latitude, longitude pair. * * @param latitude Latitude, as a double. * @param longitude Longitude, as a double. */ public void setOrigin(double latitude, double longitude) { Location userLoc = new Location("User"); userLoc.setLatitude(latitude); userLoc.setLongitude(longitude); Set<Map.Entry<String, LatLng>> entries = stationLatLngMap.entrySet(); Iterator<Map.Entry<String, LatLng>> iter = entries.iterator(); Double bestDist = Double.MAX_VALUE; while (iter.hasNext()) { Map.Entry<String, LatLng> entry = iter.next(); LatLng val = entry.getValue(); String stationName = entry.getKey(); Location stationLoc = new Location("Station"); stationLoc.setLatitude(val.latitude); stationLoc.setLongitude(val.longitude); Double currDist = (double) userLoc.distanceTo(stationLoc); if (currDist < bestDist) { bestDist = currDist; originStation = mBService.lookupStationByName(entry.getKey()); } } }
//////////////////////////////////////////////////////////////////////////////// // OVERRIDDEN METHODS (GENERAL) //////////////////////////////////////////////////////////////////////////////// @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Setting menu bar properties this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); Window window = this.getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.parseColor("#1e2a37")); mApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addApi(LocationServices.API) .addConnectionCallbacks( new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(Bundle connectionHint) { Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mApiClient); if (mLastLocation != null) { setOrigin(mLastLocation.getLatitude(), mLastLocation.getLongitude()); } } @Override public void onConnectionSuspended(int cause) {} }) .build(); mApiClient.connect(); // Query for navigation if toggle switch was checked. // Else, notice that the navInstructions ArrayList remains null. // As such, a null check on it can also be used to determine whether or not // navigation has been requested. // Capturing UI Views final ListView listView = (ListView) findViewById(R.id.listView); final FrameLayout mapFrame = (FrameLayout) findViewById(R.id.mapFrame); final Button allStationsButton = (Button) findViewById(R.id.allStations); final Button favoritesButton = (Button) findViewById(R.id.favoritesButton); final Button mapButton = (Button) findViewById(R.id.mapButton); final TextView underlineFavorites = (TextView) findViewById(R.id.underlineFavorites); final TextView underlineAll = (TextView) findViewById(R.id.underlineAll); final TextView underlineMap = (TextView) findViewById(R.id.underlineMap); underlineAll.setBackgroundColor(Color.parseColor(blue)); underlineMap.setBackgroundColor(Color.parseColor(blue)); allStationsButton.setTextColor(Color.parseColor(grey)); mapButton.setTextColor(Color.parseColor(grey)); // Bart service initialization mBService = new BartService(); stationList = mBService.getStations(); // Station Latitude-Longitude data structure stationLatLngMap = getStationLatLngMap(); // UI data structures createAllStationsHash(); createFavoritesHash(); listView.setAdapter(setFavoriteStations()); // Generate mapFragment for Map tab MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFrag); mapFragment.getMapAsync(this); // Generate UI Spinners and OnClickListeners listView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String dest = ""; if (MainActivity.currList == "All") { dest = allStationsHash.get(position); } else { dest = favoritesHash.get(position); } // Prepare destination/origin extras from data (ie. the selected station) LatLng destLatLng = stationLatLngMap.get(dest); Double destLat = destLatLng.latitude; Double destLng = destLatLng.longitude; Double origLat = Double.parseDouble(originStation.getLatitude()); Double origLng = Double.parseDouble(originStation.getLongitude()); // Create post-selection intent and put extras Intent postSelection = new Intent(); postSelection.setClass(view.getContext(), postSelection.class); postSelection.putExtra("destName", dest); postSelection.putExtra("destLat", destLat); postSelection.putExtra("destLng", destLng); postSelection.putExtra("origLat", origLat); postSelection.putExtra("origLng", origLng); postSelection.putExtra("origStation", originStation.getAbbreviation()); startActivityForResult(postSelection, 1); } }); allStationsButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { currList = "All"; listView.setAdapter(setAllStations()); mapFrame.setVisibility(View.INVISIBLE); underlineAll.setBackgroundColor(Color.parseColor(orange)); // orange underlineMap.setBackgroundColor(Color.parseColor(blue)); // blue underlineFavorites.setBackgroundColor(Color.parseColor(blue)); // blue allStationsButton.setTextColor(Color.parseColor(orange)); // orange favoritesButton.setTextColor(Color.parseColor(grey)); mapButton.setTextColor(Color.parseColor(grey)); } }); favoritesButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { currList = "Favorites"; listView.setAdapter(setFavoriteStations()); mapFrame.setVisibility(View.INVISIBLE); underlineFavorites.setBackgroundColor(Color.parseColor(orange)); underlineAll.setBackgroundColor(Color.parseColor(blue)); underlineMap.setBackgroundColor(Color.parseColor(blue)); favoritesButton.setTextColor(Color.parseColor(orange)); allStationsButton.setTextColor(Color.parseColor(grey)); mapButton.setTextColor(Color.parseColor(grey)); } }); mapButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { listView.setAdapter(clearStations()); mapFrame.setVisibility(View.VISIBLE); underlineMap.setBackgroundColor(Color.parseColor(orange)); underlineAll.setBackgroundColor(Color.parseColor(blue)); underlineFavorites.setBackgroundColor(Color.parseColor(blue)); mapButton.setTextColor(Color.parseColor(orange)); favoritesButton.setTextColor(Color.parseColor(grey)); allStationsButton.setTextColor(Color.parseColor(grey)); } }); }