Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…android into fix-build

* 'master' of https://github.com/mapbox/mapbox-navigation-android:
  Adjust API Milestone to handle new routes (mapbox#425)
  Cancel notification when the service is destroyed, not just when notification button is clicked (mapbox#409)
  adds validation utils class (mapbox#424)
  Wait for map style to load before initializing run time styling (mapbox#423)
  Changed up NavigationRoute (mapbox#413)

# Conflicts:
#	libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/NavigationRoute.java
  • Loading branch information
Adrien Grsmto committed Oct 27, 2017
2 parents 556e771 + c8b3877 commit 9338368
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mapbox.services.Constants;
import com.mapbox.services.android.navigation.testapp.R;
import com.mapbox.services.android.navigation.v5.location.MockLocationEngine;
import com.mapbox.services.android.navigation.v5.milestone.MilestoneEventListener;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigationOptions;
import com.mapbox.services.android.navigation.v5.navigation.NavigationEventListener;
Expand All @@ -45,21 +46,19 @@

public class RerouteActivity extends AppCompatActivity implements OnMapReadyCallback, LocationEngineListener,
Callback<DirectionsResponse>, MapboxMap.OnMapClickListener, NavigationEventListener, OffRouteListener,
ProgressChangeListener {
ProgressChangeListener, MilestoneEventListener {

@BindView(R.id.mapView)
MapView mapView;

Point origin = Point.fromLngLat(-87.6900, 41.8529);
Point destination = Point.fromLngLat(-87.8921, 41.9794);
private LocationLayerPlugin locationLayerPlugin;
private LocationEngine locationEngine;
private MapboxNavigation navigation;
private MapboxMap mapboxMap;
private boolean running;
private Polyline polyline;

Point origin = Point.fromLngLat(-87.6900, 41.8529);
Point destination = Point.fromLngLat(-87.8921, 41.9794);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -89,6 +88,7 @@ public void onMapReady(MapboxMap mapboxMap) {

mapboxMap.setLocationSource(locationEngine);
mapboxMap.setMyLocationEnabled(true);
navigation.addMilestoneEventListener(this);
navigation.setLocationEngine(locationEngine);

// Acquire the navigation's route
Expand Down Expand Up @@ -132,6 +132,11 @@ public void onProgressChange(Location location, RouteProgress routeProgress) {
Timber.d("onRouteProgressChange: %s", routeProgress.currentLegProgress().stepIndex());
}

@Override
public void onMilestoneEvent(RouteProgress routeProgress, String instruction, int identifier) {
Timber.d("onMilestoneEvent - Current Instruction: " + instruction);
}

@Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
if (response.body() != null) {
Expand All @@ -151,13 +156,13 @@ public void onResponse(Call<DirectionsResponse> call, Response<DirectionsRespons

private void getRoute(Point origin, Point destination, Float bearing) {
NavigationRoute.Builder navigationRouteBuilder = NavigationRoute.builder()
.origin(origin)
.destination(destination)
.accessToken(Mapbox.getAccessToken());

if (bearing != null) {
navigationRouteBuilder.addBearing(Float.valueOf(bearing).doubleValue(), 90d);
navigationRouteBuilder.addBearing(null, null);
navigationRouteBuilder.origin(origin, Float.valueOf(bearing).doubleValue(), 90d);
} else {
navigationRouteBuilder.origin(origin);
}
navigationRouteBuilder.build().getRoute(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public NavigationView(Context context, @Nullable AttributeSet attrs, int defStyl

public void onCreate(@Nullable Bundle savedInstanceState) {
resumeState = savedInstanceState != null;
initMap(savedInstanceState);
mapView.onCreate(savedInstanceState);
}

public void onStart() {
Expand Down Expand Up @@ -158,13 +158,18 @@ public void onMapReady(MapboxMap mapboxMap) {
map = mapboxMap;
map.setOnScrollListener(this);
setMapPadding(0, 0, 0, summaryBehavior.getPeekHeight());
initRoute();
initCamera();
initLocationLayer();
initLifecycleObservers();
initNavigationPresenter();
subscribeViews();
navigationListener.onNavigationReady();
ThemeSwitcher.setMapStyle(getContext(), map, new MapboxMap.OnStyleLoadedListener() {
@Override
public void onStyleLoaded(String style) {
initRoute();
initCamera();
initLocationLayer();
initLifecycleObservers();
initNavigationPresenter();
subscribeViews();
navigationListener.onNavigationReady();
}
});
}

/**
Expand Down Expand Up @@ -414,16 +419,6 @@ public void onClick(View view) {
});
}

/**
* Sets up the {@link MapboxMap}.
*
* @param savedInstanceState from onCreate()
*/
private void initMap(Bundle savedInstanceState) {
mapView.onCreate(savedInstanceState);
ThemeSwitcher.setMapStyle(getContext(), mapView);
}

/**
* Initializes the {@link BottomSheetBehavior} for {@link SummaryBottomSheet}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;

/**
* This class is used to switch theme colors in {@link NavigationView}.
Expand Down Expand Up @@ -52,14 +53,14 @@ static void toggleTheme(Activity activity) {
* Sets the {@link MapView} style based on the current theme setting.
*
* @param context to retrieve {@link SharedPreferences}
* @param mapView the style will be set on
* @param map the style will be set on
*/
static void setMapStyle(Context context, MapView mapView) {
static void setMapStyle(Context context, MapboxMap map, MapboxMap.OnStyleLoadedListener listener) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false);
String nightThemeUrl = context.getString(R.string.navigation_guidance_night_v2);
String dayThemeUrl = context.getString(R.string.navigation_guidance_day_v2);
mapView.setStyleUrl(darkThemeEnabled ? nightThemeUrl : dayThemeUrl);
map.setStyleUrl(darkThemeEnabled ? nightThemeUrl : dayThemeUrl, listener);
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.mapbox.services.android.navigation.v5.milestone;

import android.text.TextUtils;

import com.mapbox.directions.v5.models.VoiceInstructions;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;

import java.util.List;

public class VoiceInstructionMilestone extends Milestone {

private String announcement;
private List<VoiceInstructions> stepVoiceInstructions;

public VoiceInstructionMilestone(Builder builder) {
super(builder);
}

@Override
public boolean isOccurring(RouteProgress previousRouteProgress, RouteProgress routeProgress) {
if (shouldAddInstructions(previousRouteProgress, routeProgress)) {
clearInstructionList();
stepVoiceInstructions = routeProgress.currentLegProgress().currentStep().voiceInstructions();
}
for (VoiceInstructions voice : stepVoiceInstructions) {
if (shouldBeVoiced(routeProgress, voice)) {
announcement = voice.announcement();
stepVoiceInstructions.remove(voice);
return true;
}
}
return false;
}

public String announcement() {
return announcement;
}

/**
* Check if a new set of step instructions should be set.
*
* @param previousRouteProgress most recent progress before the current progress
* @param routeProgress the current route progress
* @return true if new instructions should be added to the list, false if not
*/
private boolean shouldAddInstructions(RouteProgress previousRouteProgress, RouteProgress routeProgress) {
return newStep(previousRouteProgress, routeProgress)
|| newRoute(previousRouteProgress, routeProgress)
|| stepVoiceInstructions == null;
}

/**
* Called when adding new instructions to the list.
* <p>
* Make sure old announcements are not called (can happen in reroute scenarios).
*/
private void clearInstructionList() {
if (stepVoiceInstructions != null && !stepVoiceInstructions.isEmpty()) {
stepVoiceInstructions.clear();
}
}

/**
* Used to check for a new route. Route geometries will be the same only on the first
* update. This is because the previousRouteProgress is reset and the current routeProgress is generated
* from the previous.
*
* @param previousRouteProgress most recent progress before the current progress
* @param routeProgress the current route progress
* @return true if there's a new route, false if not
*/
private boolean newRoute(RouteProgress previousRouteProgress, RouteProgress routeProgress) {
return TextUtils.equals(previousRouteProgress.directionsRoute().geometry(),
routeProgress.directionsRoute().geometry());
}

/**
* @param previousRouteProgress most recent progress before the current progress
* @param routeProgress the current route progress
* @return true if on a new step, false if not
*/
private boolean newStep(RouteProgress previousRouteProgress, RouteProgress routeProgress) {
return previousRouteProgress.currentLegProgress().stepIndex()
< routeProgress.currentLegProgress().stepIndex();
}

/**
* Uses the current step distance remaining to check against voice instruction distance.
*
* @param routeProgress the current route progress
* @param voice a given voice instruction from the list of step instructions
* @return true if time to voice the announcement, false if not
*/
private boolean shouldBeVoiced(RouteProgress routeProgress, VoiceInstructions voice) {
return voice.distanceAlongGeometry()
>= routeProgress.currentLegProgress().currentStepProgress().distanceRemaining();
}

public static final class Builder extends Milestone.Builder {

private Trigger.Statement trigger;

public Builder() {
super();
}

@Override
Trigger.Statement getTrigger() {
return trigger;
}

@Override
public Builder setTrigger(Trigger.Statement trigger) {
this.trigger = trigger;
return this;
}

@Override
public VoiceInstructionMilestone build() {
return new VoiceInstructionMilestone(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.mapbox.services.android.location.LostLocationEngine;
import com.mapbox.services.android.navigation.BuildConfig;
import com.mapbox.services.android.navigation.v5.exception.NavigationException;
import com.mapbox.services.android.navigation.v5.milestone.ApiMilestone;
import com.mapbox.services.android.navigation.v5.milestone.VoiceInstructionMilestone;
import com.mapbox.services.android.navigation.v5.milestone.Milestone;
import com.mapbox.services.android.navigation.v5.milestone.MilestoneEventListener;
import com.mapbox.services.android.navigation.v5.location.MockLocationEngine;
Expand All @@ -25,6 +25,7 @@
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.snap.Snap;
import com.mapbox.services.android.navigation.v5.snap.SnapToRoute;
import com.mapbox.services.android.navigation.v5.utils.ValidationUtils;
import com.mapbox.services.android.telemetry.MapboxEvent;
import com.mapbox.services.android.telemetry.MapboxTelemetry;
import com.mapbox.services.android.telemetry.location.LocationEngine;
Expand All @@ -41,7 +42,7 @@
import retrofit2.Callback;
import timber.log.Timber;

import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.DEFAULT_MILESTONE_IDENTIFIER;
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.VOICE_INSTRUCTION_MILESTONE_ID;

/**
* A MapboxNavigation class for interacting with and customizing a navigation session.
Expand Down Expand Up @@ -132,7 +133,7 @@ private void initialize(boolean debugLoggingEnabled) {
// Create and add default milestones if enabled.
milestones = new ArrayList<>();
if (options.defaultMilestonesEnabled()) {
addMilestone(new ApiMilestone.Builder().setIdentifier(DEFAULT_MILESTONE_IDENTIFIER).build());
addMilestone(new VoiceInstructionMilestone.Builder().setIdentifier(VOICE_INSTRUCTION_MILESTONE_ID).build());
}

initializeDefaultLocationEngine();
Expand Down Expand Up @@ -353,6 +354,7 @@ public LocationEngine getLocationEngine() {
* @since 0.1.0
*/
public void startNavigation(@NonNull DirectionsRoute directionsRoute) {
ValidationUtils.validDirectionsRoute(directionsRoute, options.defaultMilestonesEnabled());
this.directionsRoute = directionsRoute;
Timber.d("MapboxNavigation startNavigation called.");
if (!isBound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private NavigationConstants() {
*
* @since 0.7.0
*/
public static final int DEFAULT_MILESTONE_IDENTIFIER = 1;
public static final int VOICE_INSTRUCTION_MILESTONE_ID = 1;

/**
* Random integer value used for identifying the navigation notification.
Expand Down
Loading

0 comments on commit 9338368

Please sign in to comment.