Skip to content

Commit

Permalink
Merge branch 'master' into dan-snap-check
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder authored Jan 12, 2018
2 parents 6091b16 + 08e2ebc commit 4676037
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 41 deletions.
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>

<activity
android:name="com.mapbox.services.android.navigation.ui.v5.WaypointNavigationActivity"
android:label="@string/title_waypoint_navigation">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.mapbox.services.android.navigation.testapp.activity.RerouteActivity;
import com.mapbox.services.android.navigation.testapp.activity.navigationui.NavigationMapRouteActivity;
import com.mapbox.services.android.navigation.testapp.activity.navigationui.NavigationViewActivity;
import com.mapbox.services.android.navigation.ui.v5.WaypointNavigationActivity;
import com.mapbox.services.android.telemetry.permissions.PermissionsListener;
import com.mapbox.services.android.telemetry.permissions.PermissionsManager;

Expand Down Expand Up @@ -57,6 +58,11 @@ protected void onCreate(Bundle savedInstanceState) {
getString(R.string.title_navigation_route_ui),
getString(R.string.description_navigation_route_ui),
NavigationMapRouteActivity.class
),
new SampleItem(
getString(R.string.title_waypoint_navigation),
getString(R.string.description_waypoint_navigation),
WaypointNavigationActivity.class
)
));

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@

<string name="title_navigation_view_ui">Navigation Views</string>
<string name="description_navigation_view_ui">Drop-in UI experience</string>

<string name="title_waypoint_navigation">Waypoint Navigation</string>
<string name="description_waypoint_navigation">Navigation with waypoints between destinations</string>
</resources>
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ext {
]

version = [
mapboxMapSdk : '5.3.0',
mapboxMapSdk : '5.3.1',
mapboxServices : '2.2.9',
mapboxSdkServices : '3.0.0-beta.1',
locationLayerPlugin: '0.2.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
Expand All @@ -42,6 +43,9 @@
import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation;
import com.mapbox.services.android.navigation.v5.navigation.NavigationRoute;

import java.util.ArrayList;
import java.util.List;

/**
* View that creates the drop-in UI.
* <p>
Expand Down Expand Up @@ -85,6 +89,8 @@ public class NavigationView extends CoordinatorLayout implements LifecycleObserv
private LocationLayerPlugin locationLayer;
private OnNavigationReadyCallback onNavigationReadyCallback;
private boolean resumeState;
private boolean isInitialized;
private List<Marker> markers = new ArrayList<>();

public NavigationView(Context context) {
this(context, null);
Expand Down Expand Up @@ -173,6 +179,7 @@ public void onRestoreInstanceState(Bundle savedInstanceState) {
*/
public void onDestroy() {
mapView.onDestroy();
navigationViewModel.onDestroy();
}

/**
Expand Down Expand Up @@ -264,9 +271,9 @@ public void drawRoute(DirectionsRoute directionsRoute) {
public void addMarker(Point position) {
LatLng markerPosition = new LatLng(position.latitude(),
position.longitude());
map.addMarker(new MarkerOptions()
markers.add(map.addMarker(new MarkerOptions()
.position(markerPosition)
.icon(ThemeSwitcher.retrieveMapMarker(getContext())));
.icon(ThemeSwitcher.retrieveMapMarker(getContext()))));
}

/**
Expand Down Expand Up @@ -341,18 +348,23 @@ public void updateLocationLayer(Location location) {
* @param options with containing route / coordinate data
*/
public void startNavigation(NavigationViewOptions options) {
// Initialize navigation with options from NavigationViewOptions
navigationViewModel.initializeNavigationOptions(getContext().getApplicationContext(),
options.navigationOptions().toBuilder().isFromNavigationUi(true).build());
// Initialize the camera (listens to MapboxNavigation)
initCamera();

setupListeners(options);
clearMarkers();

// Initialize navigation with options from NavigationViewOptions
if (!isInitialized) {
navigationViewModel.initializeNavigationOptions(getContext().getApplicationContext(),
options.navigationOptions().toBuilder().isFromNavigationUi(true).build());
// Initialize the camera (listens to MapboxNavigation)
initCamera();
setupListeners(options);
// Everything is setup, subscribe to the view models
subscribeViewModels();
// Initialized and navigating at this point
isInitialized = true;
}
// Update the view models
locationViewModel.updateShouldSimulateRoute(options.shouldSimulateRoute());
routeViewModel.extractRouteOptions(options);
// Everything is setup, subscribe to the view models
subscribeViewModels();
}

/**
Expand Down Expand Up @@ -476,7 +488,6 @@ private void initLifecycleObservers() {
try {
((LifecycleOwner) getContext()).getLifecycle().addObserver(locationLayer);
((LifecycleOwner) getContext()).getLifecycle().addObserver(locationViewModel);
((LifecycleOwner) getContext()).getLifecycle().addObserver(navigationViewModel);
} catch (ClassCastException exception) {
throw new ClassCastException("Please ensure that the provided Context is a valid LifecycleOwner");
}
Expand Down Expand Up @@ -555,6 +566,15 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
});
}

/**
* Removes any markers on the map that were added using addMarker()
*/
private void clearMarkers() {
for (int i = 0; i < markers.size(); i++) {
map.removeMarker(markers.remove(i));
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
mapView.onStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
Expand Down Expand Up @@ -37,7 +34,7 @@

import java.text.DecimalFormat;

public class NavigationViewModel extends AndroidViewModel implements LifecycleObserver, ProgressChangeListener,
public class NavigationViewModel extends AndroidViewModel implements ProgressChangeListener,
OffRouteListener, MilestoneEventListener, NavigationEventListener {

public final MutableLiveData<InstructionModel> instructionModel = new MutableLiveData<>();
Expand Down Expand Up @@ -67,7 +64,6 @@ public NavigationViewModel(Application application) {
initDecimalFormat();
}

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
endNavigation();
deactivateInstructionPlayer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package com.mapbox.services.android.navigation.ui.v5;

import android.content.DialogInterface;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.mapbox.geojson.Point;
import com.mapbox.services.android.navigation.ui.v5.listeners.NavigationListener;
import com.mapbox.services.android.navigation.v5.routeprogress.ProgressChangeListener;
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.utils.RouteUtils;

import java.util.ArrayList;
import java.util.List;

public class WaypointNavigationActivity extends AppCompatActivity implements OnNavigationReadyCallback,
NavigationListener, ProgressChangeListener {

private NavigationView navigationView;
private boolean dropoffDialogShown;
private Location lastKnownLocation;

private List<Point> points = new ArrayList<>();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_NoActionBar);
super.onCreate(savedInstanceState);
points.add(Point.fromLngLat(-77.04012393951416, 38.9111117447887));
points.add(Point.fromLngLat(-77.03847169876099, 38.91113678979344));
points.add(Point.fromLngLat(-77.03848242759705, 38.91040213277608));
points.add(Point.fromLngLat(-77.03850388526917, 38.909650771013034));
points.add(Point.fromLngLat(-77.03651905059814, 38.90894949285854));
setContentView(R.layout.activity_navigation);
navigationView = findViewById(R.id.navigationView);
navigationView.onCreate(savedInstanceState);
navigationView.getNavigationAsync(this);
}

@Override
public void onLowMemory() {
super.onLowMemory();
navigationView.onLowMemory();
}

@Override
public void onBackPressed() {
// If the navigation view didn't need to do anything, call super
if (!navigationView.onBackPressed()) {
super.onBackPressed();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
navigationView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
navigationView.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
navigationView.onRestoreInstanceState(savedInstanceState);
}

@Override
public void onNavigationReady() {
navigationView.startNavigation(setupOptions(points.remove(0)));
}

private NavigationViewOptions setupOptions(Point origin) {
dropoffDialogShown = false;

NavigationViewOptions.Builder options = NavigationViewOptions.builder();
options.navigationListener(this);
options.progressChangeListener(this);
options.origin(origin);
options.destination(points.remove(0));
options.shouldSimulateRoute(true);
return options.build();
}

@Override
public void onCancelNavigation() {
// Navigation canceled, finish the activity
finish();
}

@Override
public void onNavigationFinished() {
// Intentionally empty
}

@Override
public void onNavigationRunning() {
// Intentionally empty
}

@Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
if (RouteUtils.isArrivalEvent(routeProgress)) {
lastKnownLocation = location; // Accounts for driver moving after dialog was triggered
if (!dropoffDialogShown && !points.isEmpty()) {
showDropoffDialog();
dropoffDialogShown = true; // Accounts for multiple arrival events
}
}
}

private void showDropoffDialog() {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage(getString(R.string.dropoff_dialog_text));
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.dropoff_dialog_positive_text),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int in) {
navigationView.startNavigation(
setupOptions(Point.fromLngLat(lastKnownLocation.getLongitude(), lastKnownLocation.getLatitude())));
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.dropoff_dialog_negative_text),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int in) {
// Do nothing
}
});

alertDialog.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public void updateShouldSimulateRoute(boolean shouldSimulateRoute) {
* @param route to be mocked
*/
public void updateRoute(DirectionsRoute route) {
// MockLocationEngine is deactivated first to avoid weird behavior with subsequent navigation sessions
if (shouldSimulateRoute) {
deactivateLocationEngine();
activateMockLocationEngine(route);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class RouteViewModel extends AndroidViewModel implements Callback<Directi
public final MutableLiveData<String> requestErrorMessage = new MutableLiveData<>();
private Point origin;
private Location rawLocation;
private boolean extractRouteOptions = true;
private String routeProfile;
private String unitType;

Expand Down Expand Up @@ -73,12 +72,10 @@ public void updateRawLocation(@NonNull Location rawLocation) {
*/
public void extractRouteOptions(NavigationViewOptions options) {
updateUnitType(options.navigationOptions().unitType());
if (extractRouteOptions) {
if (launchWithRoute(options)) {
extractRouteFromOptions(options);
} else {
extractCoordinatesFromOptions(options);
}
if (launchWithRoute(options)) {
extractRouteFromOptions(options);
} else {
extractCoordinatesFromOptions(options);
}
}

Expand Down Expand Up @@ -135,10 +132,7 @@ private void fetchRoute(Point origin, Point destination) {
}

private void fetchRouteFromCoordinates() {
if (extractRouteOptions) {
fetchRoute(origin, destination.getValue());
extractRouteOptions = false;
}
fetchRoute(origin, destination.getValue());
}

/**
Expand Down Expand Up @@ -166,7 +160,6 @@ private void extractRouteFromOptions(NavigationViewOptions options) {
LegStep lastStep = lastLeg.steps().get(lastLeg.steps().size() - 1);
destination.setValue(lastStep.maneuver().location());
this.route.setValue(route);
extractRouteOptions = false;
}
}

Expand Down
3 changes: 3 additions & 0 deletions libandroid-navigation-ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
translatable="false">recenter_btn_visible</string>
<string name="bottom_sheet_state"
translatable="false">bottom_sheet_state</string>
<string name="dropoff_dialog_text">Do you want to navigate to next destination?</string>
<string name="dropoff_dialog_positive_text">Start next</string>
<string name="dropoff_dialog_negative_text">Cancel</string>
</resources>
Loading

0 comments on commit 4676037

Please sign in to comment.