SHARE
TWEET

Untitled

a guest Apr 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.raywenderlich.where2go;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.location.Location;
  5. import android.os.Bundle;
  6. import android.support.annotation.Nullable;
  7. import android.support.constraint.ConstraintLayout;
  8. import android.support.constraint.ConstraintSet;
  9. import android.support.design.widget.FloatingActionButton;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.util.TypedValue;
  12. import android.view.View;
  13. import android.widget.ProgressBar;
  14. import android.widget.RelativeLayout;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import com.mapbox.api.directions.v5.DirectionsCriteria;
  19. import com.mapbox.api.directions.v5.models.BannerInstructions;
  20. import com.mapbox.api.directions.v5.models.DirectionsResponse;
  21. import com.mapbox.api.directions.v5.models.DirectionsRoute;
  22. import com.mapbox.geojson.Point;
  23. import com.mapbox.mapboxsdk.annotations.Icon;
  24. import com.mapbox.mapboxsdk.annotations.IconFactory;
  25. import com.mapbox.mapboxsdk.annotations.Marker;
  26. import com.mapbox.mapboxsdk.annotations.MarkerOptions;
  27. import com.mapbox.mapboxsdk.camera.CameraPosition;
  28. import com.mapbox.mapboxsdk.geometry.LatLng;
  29. import com.mapbox.services.android.navigation.ui.v5.NavigationView;
  30. import com.mapbox.services.android.navigation.ui.v5.NavigationViewOptions;
  31. import com.mapbox.services.android.navigation.ui.v5.OnNavigationReadyCallback;
  32. import com.mapbox.services.android.navigation.ui.v5.listeners.BannerInstructionsListener;
  33. import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
  34. import com.mapbox.services.android.navigation.ui.v5.listeners.SpeechAnnouncementListener;
  35. import com.mapbox.services.android.navigation.ui.v5.voice.SpeechAnnouncement;
  36. import com.mapbox.services.android.navigation.v5.milestone.Milestone;
  37. import com.mapbox.services.android.navigation.v5.milestone.MilestoneEventListener;
  38. import com.mapbox.services.android.navigation.v5.milestone.RouteMilestone;
  39. import com.mapbox.services.android.navigation.v5.milestone.Trigger;
  40. import com.mapbox.services.android.navigation.v5.milestone.TriggerProperty;
  41. import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;
  42. import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
  43. import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
  44.  
  45. import java.util.ArrayList;
  46. import java.util.List;
  47.  
  48. import retrofit2.Call;
  49. import retrofit2.Callback;
  50. import retrofit2.Response;
  51.  
  52. import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.STEP_MANEUVER_TYPE_ARRIVE;
  53.  
  54. public class EndNavigationActivity extends AppCompatActivity implements OnNavigationReadyCallback, NavigationListener,
  55.         Callback<DirectionsResponse>, ProgressChangeListener, MilestoneEventListener, BannerInstructionsListener, SpeechAnnouncementListener {
  56.  
  57.     private NavigationView navigationView;
  58.     private ProgressBar loading;
  59.     private TextView message;
  60.     private FloatingActionButton launchNavigationFab;
  61.     private Point origin = Point.fromLngLat(21.4390307, 41.983695);
  62.     private Point pickup = Point.fromLngLat(21.4385734, 41.9842733);
  63.     //  private Point middlePickup = Point.fromLngLat(-122.428604, 37.763559);
  64.     private Point destination = Point.fromLngLat(21.4379148, 41.9853789);
  65.     private DirectionsRoute route;
  66.     private boolean paellaPickedUp = false;
  67.     private Marker paella;
  68.     private boolean isNavigationRunning;
  69.     private int ace = 0;
  70.     private RelativeLayout endNavigationLayout;
  71.  
  72.     @Override
  73.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  74.         setTheme(R.style.Theme_AppCompat_NoActionBar);
  75.         super.onCreate(savedInstanceState);
  76.         CameraPosition initialPosition = new CameraPosition.Builder()
  77.                 .target(new LatLng(41.983695, 21.4390307))
  78.                 .zoom(15)
  79.                 .build();
  80.         initializeViews(savedInstanceState);
  81. //    navigationView.initialize(this,initialPosition);
  82.         navigationView.initialize(this);
  83.         launchNavigationFab.setOnClickListener(v -> launchNavigation());
  84.         updateLoadingTo(true);
  85.     }
  86.  
  87.     @Override
  88.     public void onNavigationReady(boolean isRunning) {
  89.         isNavigationRunning = isRunning;
  90.         // navigationView.addMarker(MarkerOptions().setTitle("I'm a marker :]").setSnippet("This is a snippet about this marker that will show up here").position(point))
  91. //    paella =  navigationView.retrieveNavigationMapboxMap().retrieveMap().addMarker(new MarkerOptions()
  92. //                    .position(new LatLng(41.983695, 21.4390307)));
  93.         fetchRoute();
  94.         updateLoadingTo(false);
  95.     }
  96.  
  97.     @SuppressLint("RestrictedApi")
  98.     @Override
  99.     public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
  100.         if (validRouteResponse(response)) {
  101.             updateLoadingTo(false);
  102.             message.setText("Launch Navigation");
  103.             launchNavigationFab.setVisibility(View.VISIBLE);
  104.             launchNavigationFab.show();
  105. //      route = response.body().routes().get(0);
  106.             if (isNavigationRunning) {
  107.                 launchNavigation();
  108.             }
  109. //      route = Engine.INSTANCE.getRoute();
  110. //      for (int i=0 ;i< route.legs().size();i++){
  111. //          if(route.legs().get(i).duration()==0.0){
  112. //              route.legs().remove(i);
  113. //          }
  114. //      }
  115.             launchNavigation();
  116.         }
  117.     }
  118.  
  119.     @Override
  120.     public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
  121.     }
  122.  
  123.     @Override
  124.     public void onProgressChange(Location location, RouteProgress routeProgress) {
  125.         boolean isCurrentStepArrival = routeProgress.currentLegProgress().currentStep().maneuver().type()
  126.                 .contains(STEP_MANEUVER_TYPE_ARRIVE);
  127.  
  128.         if (isCurrentStepArrival && !paellaPickedUp) {
  129.             updateUiDelivering();
  130.         } else if (isCurrentStepArrival && paellaPickedUp) {
  131.             updateUiDelivered();
  132.         }
  133.     }
  134.  
  135.     @Override
  136.     public void onCancelNavigation() {
  137.         navigationView.stopNavigation();
  138.         updateUiNavigationFinished();
  139.     }
  140.  
  141.     @Override
  142.     public void onNavigationFinished() {
  143.         Toast.makeText(this, "FINISHED", Toast.LENGTH_LONG).show();
  144.     }
  145.  
  146.     @Override
  147.     public void onNavigationRunning() {
  148.     }
  149.  
  150.     private void fetchRoute() {
  151. //    NavigationRoute builder = NavigationRoute.builder(this)
  152. //      .accessToken("pk.eyJ1IjoibWFuZXYxNSIsImEiOiJjaWgwazg1a2Mwd2txdnFtM3p1dnNvbjJkIn0.fvNagTH12eCx2IwmoDdgRw")
  153. //      .origin(origin)
  154. //  //  .addWaypoint(pickup)
  155. ////      .addWaypoint(middlePickup)
  156. //      .destination(destination)
  157. //     // .addWaypointIndices(0, 2, 3)
  158. //      .alternatives(true)
  159. //            .voiceUnits(DirectionsCriteria.METRIC)
  160. //      .build();
  161. //    builder.getRoute(this);
  162.         route = Engine.INSTANCE.getRoute();
  163.         for (int i = 0; i < route.legs().size(); i++) {
  164.  
  165.  
  166.             if (route.legs().get(i).duration() == 0.0) {
  167.                 route.legs().remove(i);
  168.             }
  169. //            if (i < 10) {
  170. ////                route.legs().get(i).steps().get(0).maneuver().instruction().contentEquals("");
  171. ////                route.legs().get(i).steps().get(1).maneuver().instruction().contentEquals("");
  172. //            }
  173.  
  174. //            if(i>2 && i< 20)
  175. //            {
  176. //                route.legs().remove(i);
  177. //            }
  178.         }
  179.         for (int i = 0; i < route.legs().size(); i++) {
  180.             if (i > 0 && i!=route.legs().size()-1) {
  181.                 route.legs().get(0).steps().addAll(route.legs().get(i).steps());
  182. //                route.legs().remove(i);
  183.             }
  184.         }
  185.  
  186.         for (int i = route.legs().size()-1; i > 0; i--) {
  187.             if (i > 0 && i!=route.legs().size()-1) {
  188.          //       route.legs().get(0).steps().addAll(route.legs().get(i).steps());
  189.                 route.legs().remove(i);
  190.             }
  191.         }
  192.         //route.legs().get(1).steps().remove(1);
  193.  
  194. //        for (int i = 0; i < route.routeOptions().coordinates().size(); i++) {
  195. //            if (route.routeOptions().coordinates().get(i).latitude() == route.routeOptions().coordinates().get(i + 1).latitude()) {
  196. //                route.routeOptions().coordinates().remove(i);
  197. //            }
  198. //        }
  199.         launchNavigation();
  200.         updateLoadingTo(false);
  201.     }
  202.  
  203.     private void launchNavigation() {
  204.         launchNavigationFab.hide();
  205.         // drawPaella();
  206.         navigationView.setVisibility(View.VISIBLE);
  207. //    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
  208. //    message.getLayoutParams().height = height;
  209. //    ConstraintSet constraintSet = new ConstraintSet();
  210. //    constraintSet.clone(endNavigationLayout);
  211. //    constraintSet.connect(R.id.message, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
  212. //    constraintSet.connect(R.id.message, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
  213. //    constraintSet.connect(R.id.message, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
  214. //    constraintSet.applyTo(endNavigationLayout);
  215.         List<Milestone> list = new ArrayList();
  216.         list.add(new RouteMilestone.Builder()
  217.                 .setIdentifier(1001)
  218.                 .setTrigger(
  219.                         Trigger.all(
  220.                                 Trigger.lt(TriggerProperty.LAST_LEG, 3), Trigger.gt(TriggerProperty.STEP_DISTANCE_TOTAL_METERS, 200))).build()
  221.         );
  222.         NavigationViewOptions.Builder options = NavigationViewOptions.builder()
  223.                 .navigationListener(this)
  224.                 .progressChangeListener(this)
  225.                 //  .milestoneEventListener(this)
  226.                 .directionsRoute(route)
  227.                 .speechAnnouncementListener(this)
  228.                 .bannerInstructionsListener(this)
  229.                 .milestones(list)
  230.                 .directionsProfile(DirectionsCriteria.PROFILE_CYCLING)
  231.                 .shouldSimulateRoute(true);
  232.  
  233.  
  234.         navigationView.startNavigation(options.build());
  235.         updateUiPickingUp();
  236.     }
  237.  
  238.     private void drawPaella() {
  239.         Icon paellaIcon = IconFactory.getInstance(this).fromResource(R.drawable.paella_icon);
  240.         paella = navigationView.retrieveNavigationMapboxMap().retrieveMap().addMarker(new MarkerOptions()
  241.                 .position(new LatLng(41.9842733, 21.4385734))
  242.                 .icon(paellaIcon)
  243.         );
  244.     }
  245.  
  246.     private void initializeViews(@Nullable Bundle savedInstanceState) {
  247.         setContentView(R.layout.activity_end_navigation);
  248.         endNavigationLayout = findViewById(R.id.endNavigationLayout);
  249.         navigationView = findViewById(R.id.navigationView);
  250.         loading = findViewById(R.id.loading);
  251.         message = findViewById(R.id.message);
  252.         launchNavigationFab = findViewById(R.id.launchNavigation);
  253.         navigationView.onCreate(savedInstanceState);
  254.     }
  255.  
  256.     private void updateLoadingTo(boolean isVisible) {
  257.         if (isVisible) {
  258.             loading.setVisibility(View.VISIBLE);
  259.         } else {
  260.             loading.setVisibility(View.INVISIBLE);
  261.         }
  262.     }
  263.  
  264.     private boolean validRouteResponse(Response<DirectionsResponse> response) {
  265.         return response.body() != null && !response.body().routes().isEmpty();
  266.     }
  267.  
  268.     private void updateUiPickingUp() {
  269.         message.setText("Picking the paella up...");
  270.     }
  271.  
  272.     private void updateUiDelivering() {
  273.         paellaPickedUp = true;
  274.         message.setText("Delivering...");
  275.     }
  276.  
  277.     private void updateUiDelivered() {
  278.         message.setText("Delicious paella delivered!");
  279.     }
  280.  
  281.     private void updateUiNavigationFinished() {
  282.         //  navigationView.retrieveNavigationMapboxMap().retrieveMap().removeMarker(paella);
  283.         navigationView.setVisibility(View.GONE);
  284.         message.setText("Launch Navigation");
  285. //    message.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT,
  286. //      ConstraintLayout.LayoutParams.MATCH_PARENT));
  287.         launchNavigationFab.show();
  288.     }
  289.  
  290.     @Override
  291.     public void onStart() {
  292.         super.onStart();
  293.         navigationView.onStart();
  294.     }
  295.  
  296.     @Override
  297.     public void onResume() {
  298.         super.onResume();
  299.         navigationView.onResume();
  300.     }
  301.  
  302.     @Override
  303.     public void onLowMemory() {
  304.         super.onLowMemory();
  305.         navigationView.onLowMemory();
  306.     }
  307.  
  308.     @Override
  309.     public void onBackPressed() {
  310.         if (!navigationView.onBackPressed()) {
  311.             super.onBackPressed();
  312.         }
  313.     }
  314.  
  315.     @Override
  316.     protected void onSaveInstanceState(Bundle outState) {
  317.         navigationView.onSaveInstanceState(outState);
  318.         super.onSaveInstanceState(outState);
  319.     }
  320.  
  321.     @Override
  322.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
  323.         super.onRestoreInstanceState(savedInstanceState);
  324.         navigationView.onRestoreInstanceState(savedInstanceState);
  325.     }
  326.  
  327.     @Override
  328.     public void onPause() {
  329.         super.onPause();
  330.         navigationView.onPause();
  331.     }
  332.  
  333.     @Override
  334.     public void onStop() {
  335.         super.onStop();
  336.         navigationView.onStop();
  337.     }
  338.  
  339.     @Override
  340.     protected void onDestroy() {
  341.         super.onDestroy();
  342.         navigationView.onDestroy();
  343.     }
  344.  
  345.  
  346.     @Override
  347.     public void onMilestoneEvent(RouteProgress routeProgress, String instruction, Milestone milestone) {
  348.  
  349.     }
  350.  
  351.     @Override
  352.     public BannerInstructions willDisplay(BannerInstructions instructions) {
  353.         ace++;
  354.         // instructions.primary().text().contentEquals(" ");
  355.         return instructions;
  356.     }
  357.  
  358.     @Override
  359.     public String willVoice(SpeechAnnouncement announcement) {
  360.       //  if (ace == 10)
  361.             return announcement.announcement();
  362.      //   else return "";
  363.     }
  364. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top