Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.malmo.lighthouse.siberianhusky;
- import android.Manifest;
- import android.app.FragmentManager;
- import android.content.DialogInterface;
- import android.content.pm.PackageManager;
- import android.graphics.Color;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.widget.CardView;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.directions.route.AbstractRouting;
- import com.directions.route.Route;
- import com.directions.route.RouteException;
- import com.directions.route.Routing;
- import com.directions.route.RoutingListener;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.Status;
- import com.google.android.gms.location.LocationListener;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.location.places.Place;
- import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
- import com.google.android.gms.location.places.ui.PlaceSelectionListener;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.BitmapDescriptorFactory;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.Marker;
- import com.google.android.gms.maps.model.MarkerOptions;
- import com.google.android.gms.maps.model.Polyline;
- import com.google.android.gms.maps.model.PolylineOptions;
- import com.google.maps.android.SphericalUtil;
- import java.util.ArrayList;
- import java.util.List;
- import se.malmo.lighthouse.siberianhusky.models.CustomDialogInterface;
- import se.malmo.lighthouse.siberianhusky.models.MapLocationDrawer;
- import se.malmo.lighthouse.siberianhusky.models.RouteCoordinate;
- import se.malmo.lighthouse.siberianhusky.models.RouteNameFragment;
- import se.malmo.lighthouse.siberianhusky.models.TravelMethodsFragment;
- import se.malmo.lighthouse.siberianhusky.repositories.DataBaseFetcher;
- public class CreateNewRouteActivity extends ToolbarMenu implements OnMapReadyCallback,
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- LocationListener,
- RoutingListener,
- GoogleMap.OnInfoWindowClickListener,
- CustomDialogInterface{
- private GoogleMap mMap;
- private GoogleApiClient googleApiClient;
- private DataBaseFetcher dataFetcher;
- private Object selectedSpinnerItem;
- private Location currentLocation;
- private MapLocationDrawer mapLocationDrawer;
- private Marker myLocationMarker;
- private Marker originMarker;
- private Marker destinationMarker;
- private final int REQUEST_LOCATION_PERMISSION = 1;
- private LatLng origin;
- private LatLng destination;
- private CardView travelLocationsCard;
- private Spinner selectionSpinner;
- private List<LatLng> coordinates;
- private List<Polyline> polylines;
- private List<LatLng> markerPositions;
- private String travelMethod;
- private List<LatLng> routeCoordinates;
- private String from = "";
- private String to = "";
- private TravelMethodsFragment travelMethodsFragment;
- private RouteNameFragment routeNameFragment;
- private AlertDialog.Builder builder;
- private se.malmo.lighthouse.siberianhusky.models.Route route;
- private PlaceAutocompleteFragment autoCompleteStartFragment;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.create_new_route_activity);
- /**
- * Realm
- */
- dataFetcher = new DataBaseFetcher(this);
- /**
- * Create a route object which we manipulate and later save when user is done
- */
- route = new se.malmo.lighthouse.siberianhusky.models.Route();
- int checkNrOfRoutes = dataFetcher.getAllSavedRoutes().size();
- route.setRouteID(checkNrOfRoutes + 1);
- /**
- * Origin search location
- */
- autoCompleteStartFragment = (PlaceAutocompleteFragment)
- getFragmentManager().findFragmentById(R.id.create_new_route_startDestination_autoComplete);
- autoCompleteStartFragment.setHint("Origin");
- /**
- * Destination search location
- */
- final PlaceAutocompleteFragment autocompleteEndFragment = (PlaceAutocompleteFragment)
- getFragmentManager().findFragmentById(R.id.create_new_route_endDestination_autoComplete);
- autocompleteEndFragment.setHint("Destination");
- /**
- * Map
- */
- final SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.create_new_route_map);
- mapFragment.getMapAsync(this);
- /**
- * CardViews
- */
- travelLocationsCard = (CardView) findViewById(R.id.select_travel_locations_card);
- travelLocationsCard.setVisibility(View.GONE);
- /**
- * Spinner and Adapter
- */
- final List<String> selections = new ArrayList<>();
- selections.add("No selection");
- selections.add("Change Travel Method");
- selections.add("Change Locations");
- selectionSpinner = (Spinner) findViewById(R.id.create_new_route_switch_selections);
- /**
- * Since spinners are not designed to perform a bunch of logic when you actually use them, I had to override the view of my adapter, hiding the first value since it's empty because of the spinners initial
- * load selection function.
- */
- ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, selections) {
- @Override
- public View getDropDownView(int position, View convertView, ViewGroup parent) {
- View view;
- if (position == 0) {
- TextView textView = new TextView(getContext());
- textView.setHeight(0);
- textView.setVisibility(View.GONE);
- view = textView;
- } else {
- view = super.getDropDownView(position, null, parent);
- }
- return view;
- }
- };
- spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- selectionSpinner.setAdapter(spinnerAdapter);
- /**
- * DialogFragment in which we select travel methods
- */
- travelMethodsFragment = new TravelMethodsFragment(this);
- /**
- * Route name fragment
- */
- routeNameFragment = new RouteNameFragment();
- /**
- * Lists for travelmethods and coordinates
- */
- coordinates = new ArrayList<>();
- routeCoordinates = new ArrayList<>();
- markerPositions = new ArrayList<>();
- /**
- * Our very loved MapLocationDrawer
- */
- mapLocationDrawer = new MapLocationDrawer(this);
- /**
- * Get current location
- */
- LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new android.location.LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- currentLocation = location;
- if (myLocationMarker != null) {
- myLocationMarker.remove();
- }
- myLocationMarker = mMap.addMarker(new MarkerOptions()
- .icon(BitmapDescriptorFactory.fromResource(R.drawable.my_location_icon))
- .position(new LatLng(location.getLatitude(), location.getLongitude()))
- .title("My Position"));
- myLocationMarker.showInfoWindow();
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onProviderDisabled(String provider) {
- }
- });
- }
- FragmentManager manager = getFragmentManager();
- travelMethodsFragment.show(manager, "Dialog");
- /**
- * Get origin coordinates from place object when user finishes search
- */
- autoCompleteStartFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
- @Override
- public void onPlaceSelected(Place place) {
- origin = place.getLatLng();
- coordinates.add(origin);
- from = place.getAddress().toString();
- if (originMarker != null) {
- originMarker.remove();
- }
- originMarker = mMap.addMarker(new MarkerOptions()
- .position(origin)
- .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
- markerPositions.add(originMarker.getPosition());
- }
- /**
- * Something must've gotten terribly wrong
- */
- @Override
- public void onError(Status status) {
- Toast.makeText(getBaseContext(),
- "Error fetching location: " + status.getStatusMessage(),
- Toast.LENGTH_SHORT)
- .show();
- }
- });
- /**
- * Get destination coordinates when user finishes search
- */
- autocompleteEndFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
- @Override
- public void onPlaceSelected(Place place) {
- destination = place.getLatLng();
- coordinates.add(destination);
- to = place.getAddress().toString();
- if (destinationMarker != null) {
- destinationMarker.remove();
- }
- destinationMarker = mMap.addMarker(new MarkerOptions()
- .position(destination)
- .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
- markerPositions.add(destinationMarker.getPosition());
- selectedSpinnerItem = null;
- if (!routeCoordinates.isEmpty()) {
- routeCoordinates.clear();
- }
- else {
- mapLocationDrawer.zoomWithinCurrentMarkers(mMap, markerPositions);
- requestRoute();
- }
- }
- /**
- * Something must've gotten terribly wrong
- */
- @Override
- public void onError(Status status) {
- Toast.makeText(getBaseContext(),
- "Error fetching location: " + status.getStatusMessage(),
- Toast.LENGTH_SHORT)
- .show();
- }
- });
- /**
- * Checks what card that is visible and perform logic based on it, code is pretty self explanatory.
- */
- selectionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- selectedSpinnerItem = parent.getItemAtPosition(position);
- if (selectedSpinnerItem.equals("Change Travel Method")) {
- selectionSpinner.setSelection(selections.indexOf(String.valueOf(selections.get(0))));
- travelMethod = "";
- if (travelLocationsCard.getVisibility() == View.VISIBLE) {
- travelLocationsCard.setVisibility(View.GONE);
- }
- } else if (selectedSpinnerItem.equals("Change Locations")) {
- if (travelLocationsCard.getVisibility() == View.GONE) {
- selectionSpinner.setSelection(selections.indexOf(String.valueOf(selections.get(0))));
- origin = null;
- destination = null;
- autoCompleteStartFragment.setText("");
- autocompleteEndFragment.setText("");
- if (polylines != null) {
- for (Polyline polyline : polylines) {
- polyline.remove();
- }
- }
- if (originMarker != null || destinationMarker != null) {
- if (originMarker != null) {
- originMarker.remove();
- }
- if (destinationMarker != null) {
- destinationMarker.remove();
- }
- }
- travelLocationsCard.setVisibility(View.VISIBLE);
- }
- }
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- }
- public void requestRoute() {
- /**
- * Check if either one of the search fields are empty
- */
- if (origin == null || destination == null) {
- if (origin == null) {
- builder = new AlertDialog.Builder(getBaseContext());
- builder.setMessage("You must enter a start location");
- builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.dismiss();
- }
- });
- }
- if (destination == null) {
- builder = new AlertDialog.Builder(getBaseContext());
- builder.setMessage("You must enter an end destination");
- builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.dismiss();
- }
- });
- }
- } else {
- /**
- * We only have to check the first index in the list since the user only has 2 choices.
- */
- if (travelMethod != null && travelMethod.equals("Walking")) {
- Routing routing = new Routing.Builder()
- .travelMode(AbstractRouting.TravelMode.WALKING)
- .withListener(this)
- .alternativeRoutes(false)
- .waypoints(origin, destination)
- .build();
- routing.execute();
- }
- /**
- * Same as above, checking the first index and set the travel method accordingly.
- */
- else if (travelMethod != null && travelMethod.equals("Biking")) {
- Routing routing = new Routing.Builder()
- .travelMode(AbstractRouting.TravelMode.BIKING)
- .withListener(this)
- .alternativeRoutes(false)
- .waypoints(origin, destination)
- .build();
- routing.execute();
- }
- }
- }
- @Override
- public void onRoutingFailure(RouteException e) {
- if (e != null) {
- Toast.makeText(getBaseContext(),
- "You are not able to travel, by this method, to this destination.",
- Toast.LENGTH_SHORT)
- .show();
- } else {
- Toast.makeText(getBaseContext(),
- "Could not find location, please try again",
- Toast.LENGTH_SHORT)
- .show();
- }
- }
- @Override
- public void onRoutingStart() {
- }
- /**
- * Override callback from request route
- *
- * @param arrayList
- * @param i
- */
- @Override
- public void onRoutingSuccess(ArrayList<Route> arrayList, int i) {
- polylines = new ArrayList<>();
- /**
- * Check if there already are Routes displayed on the map, remove if so.
- */
- if (polylines.size() > 0) {
- for (Polyline existingLine : polylines) {
- existingLine.remove();
- }
- }
- for (int n = 0; n < arrayList.size(); n++) {
- PolylineOptions polyOptions = new PolylineOptions();
- polyOptions.color(Color.rgb(26, 136, 153));
- polyOptions.width(15);
- /**
- * Fetch and add all the points contained in the polyline for route on pos n
- */
- polyOptions.addAll(arrayList.get(n).getPoints());
- Polyline line = mMap.addPolyline(polyOptions);
- polylines.add(line);
- }
- routeCoordinates = polylines.get(0).getPoints();
- if(!routeCoordinates.isEmpty()){
- double distance = calculateRouteDistance(routeCoordinates);
- route.setRouteDistance(distance);
- }
- for (int j = 0; j < routeCoordinates.size(); j++) {
- RouteCoordinate coord = new RouteCoordinate(routeCoordinates.get(j).latitude, routeCoordinates.get(j).longitude);
- coord.setParentID(route.getRouteID());
- route.getRouteCoordinates().add(coord);
- }
- /**
- * Get the fragmentManager, send the route to realm, pass the route id to the fragment.
- */
- FragmentManager manager = getFragmentManager();
- Bundle bundle = new Bundle();
- bundle.putInt("routeId", route.getRouteID());
- travelLocationsCard.setVisibility(View.GONE);
- dataFetcher.createNewRoute(route, route.getRouteName(), route.getRouteCoordinates(), route.getRouteDistance());
- routeNameFragment.setArguments(bundle);
- routeNameFragment.show(manager, "RouteNameDialog");
- }
- @Override
- public void onRoutingCancelled() {
- }
- private double calculateRouteDistance(List<LatLng> list){
- return SphericalUtil.computeLength(list);
- }
- /**
- * Build google api client to work with
- */
- protected synchronized void buildGoogleApiClient() {
- if (googleApiClient == null) {
- googleApiClient = new GoogleApiClient.Builder(this)
- .addApi(LocationServices.API)
- .build();
- googleApiClient.connect();
- }
- }
- /**
- * Override method and give us our current location.
- */
- @Override
- public void onConnected(Bundle bundle) {
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
- mMap.setOnInfoWindowClickListener(this);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- } else {
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- }
- @Override
- public void onInfoWindowClick(Marker marker){
- if(travelLocationsCard.getVisibility() == View.VISIBLE) {
- myLocationMarker = marker;
- origin = myLocationMarker.getPosition();
- markerPositions.add(origin);
- autoCompleteStartFragment.setText("My Position");
- if(originMarker != null){
- originMarker.remove();
- }
- originMarker = mMap.addMarker(new MarkerOptions()
- .position(origin)
- .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
- from = "My Position";
- }
- }
- @Override
- public void onConnectionFailed(ConnectionResult result) {
- }
- @Override
- public void onConnectionSuspended(int cause) {
- }
- @Override
- public void onLocationChanged(Location location) {
- }
- // TODO: Is never called. Should it be called in onCreate?
- public boolean checkLocationPermission() {
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_PERMISSION);
- return true;
- }
- return false;
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case REQUEST_LOCATION_PERMISSION: {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
- if (googleApiClient == null) {
- buildGoogleApiClient();
- }
- mMap.setMyLocationEnabled(true);
- }
- } else {
- Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
- @Override
- public void onStop() {
- super.onStop();
- dataFetcher.closeDb();
- }
- @Override
- public void onPause() {
- super.onPause();
- dataFetcher.closeDb();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- dataFetcher.closeDb();
- // check if routeNamefragent is not null close it.
- if (routeNameFragment.getDialog() != null) {
- routeNameFragment.getDialog().dismiss();
- }
- }
- @Override
- public void okButtonClicked(String value) {
- travelMethod = travelMethodsFragment.getSelectedMethod();
- travelLocationsCard.setVisibility(View.VISIBLE);
- }
- }
- // TODO
- /**
- * Task: Does the user want to see distance on the route?
- * Task: Coordinate travel method with existing lib to display correct routes
- * Task: Fix camera zoom and animation when requesting route result
- * Task: Display information about where you currently are such as street name?
- * Task: Implement visibility method - Check which card that is visible and make it invisible (is user wants to change travel method and locations)
- * Task: Origin can be current location
- * Task: User is able to select, by clicking on it, a polyline(Route) when creating a new route.
- * Task: When user clicks on a polyline, save the other polylines in a list which we can later draw again.
- */
Add Comment
Please, Sign In to add comment